Skip to main content

Module contract

Module contract 

Source
Expand description

Tensor contraction and matrix multiplication

use mdarray::tensor;
use mdarray_linalg::prelude::*;
use mdarray_linalg::Naive;

let a = tensor![[1., 2.], [3., 4.]];
let b = tensor![[5., 6.], [7., 8.]];

// Standard matrix multiplication
let expected_matmul = tensor![[19., 22.], [43., 50.]];
let result = Naive.matmul(&a, &b).eval();
assert_eq!(result, expected_matmul);

// Matrix multiplication with scalar factor
let result_scaled = Naive.matmul(&a, &b).scale(2.0).eval();
assert_eq!(result_scaled, expected_matmul.map(|x| x * 2.0));

// Full contraction
let expected_all = 70.0;
let result_all = Naive.contract_all(&a, &b);
assert_eq!(result_all, expected_all);

// Contract last n axes of a with first n axes of b
let expected_n = tensor![[19., 22.], [43., 50.]].into_dyn();
let result_contract_n = Naive.contract_n(&a, &b, 1).eval();
assert_eq!(result_contract_n, expected_n);

// Contract specific axes (equivalent to matmul: contract axis 1 of a with axis 0 of b)
let expected_pairs = tensor![[19., 22.], [43., 50.]].into_dyn();
let result_specific = Naive
    .contract_pairs(&a, &b, &[1], &[0])
    .eval();
assert_eq!(result_specific, expected_pairs);

Traitsยง

Contract
Tensor contraction and related operations
ContractBuilder
Builder interface for configuring tensor contraction operations
MatmulBuilder
Builder interface for configuring matrix-matrix operations