Using Spenso from Rust
This example constructs two dense tensors, contracts one pair of dual indices, and checks one output component. It uses a two-dimensional Euclidean representation as a compact software exercise rather than a Lorentz-space physics example.
Create a Rust project
Use Rust 1.85 or newer:
cargo new spenso-quickstart
cd spenso-quickstart
cargo add spenso@0.6.0Replace src/main.rs with:
use spenso::{
contraction::Contract,
structure::{
Canonicalized, OrderedStructure,
representation::{Euclidean, LibraryRep, RepName},
slot::{DualSlotTo, IsAbstractSlot},
},
tensors::data::{DenseTensor, GetTensorData, SetTensorData},
};
fn main() {
let rep = Euclidean {};
let left_free = rep.new_slot(2, 0).to_lib();
let right_free = rep.new_slot(2, 2).to_lib();
let shared = rep.new_slot(2, 10).to_lib();
let left_structure: OrderedStructure<LibraryRep> =
Canonicalized::from_iter([left_free, shared]).into_canonical();
let right_structure: OrderedStructure<LibraryRep> =
Canonicalized::from_iter([right_free, shared.dual()]).into_canonical();
let mut left = DenseTensor::<i32, _>::zero(left_structure);
let mut right = DenseTensor::<i32, _>::zero(right_structure);
left.set(&[0, 1], 2).unwrap();
right.set(&[0, 1], 5).unwrap();
let result = left.contract(&right).unwrap();
assert_eq!(*result.get_ref([0, 0]).unwrap(), 10);
println!("result structure: {}", result.structure);
println!("result data: {:?}", result.data);
}Run cargo run. Success means the assertion passes and the printed result contains 10 at the component selected by the two remaining free indices. Spenso matches index identity and duality, not merely equal dimensions.
Running locally
Run this Rust program locally with
Run this Rust program locally with
cargo run; it cannot run in the browser. The contraction itself needs neither Symbolica nor Spenso’s optional symbolica feature. A cold dependency build takes longer than this small contraction.Continue with the first tensor workflow for a more deliberate explanation of structure and storage, then use the tensor-network guide when more than two tensors should be planned and executed together.