qudit_core/array/
mod.rs

1//! Implements the tensor struct and associated methods for the Openqudit library.
2
3mod symsq;
4mod tensor;
5
6pub use tensor::Tensor;
7pub use tensor::TensorMut;
8pub use tensor::TensorRef;
9
10pub use symsq::SymSqTensor;
11pub use symsq::SymSqTensorMut;
12pub use symsq::SymSqTensorRef;
13
14/// Helper for bounds checking
15#[allow(dead_code)]
16fn is_in_bounds<const D: usize>(indices: &[usize; D], dimensions: &[usize; D]) -> bool {
17    for i in 0..D {
18        if indices[i] >= dimensions[i] {
19            return false;
20        }
21    }
22    true
23}
24
25/// Helper for panic on out of bounds
26fn check_bounds<const D: usize>(indices: &[usize; D], dimensions: &[usize; D]) {
27    for i in 0..D {
28        if indices[i] >= dimensions[i] {
29            panic!(
30                "Index out of bounds: index {} is {} but dimension {} has size {}",
31                i, indices[i], i, dimensions[i]
32            );
33        }
34    }
35}
36
37/// Calculates the strides for a tensor with the given dims in a contiguous, row-major, block.
38fn calc_continuous_strides<const D: usize>(dims: &[usize; D]) -> [usize; D] {
39    if D == 0 {
40        [0; D]
41    } else if D == 1 {
42        [1; D]
43    } else {
44        let mut stride_acm = 1;
45        let mut strides = [0; D];
46        for (i, d) in dims.iter().enumerate().rev() {
47            strides[i] = stride_acm;
48            stride_acm *= d;
49        }
50        strides
51    }
52}