7.5 Linear algebra computations
Explanation
Linear algebra code must track shapes. Matrix-vector products, matrix-matrix products, transposes, and factorizations all have shape requirements. A shape mismatch is not a small detail; it usually means the mathematical expression is not well-defined.
For one-dimensional vector operations, use slices at function boundaries:
fn dot(x: &[f64], y: &[f64]) -> Option<f64> {
if x.len() != y.len() {
return None;
}
Some(x.iter().zip(y).map(|(a, b)| a * b).sum())
}For two-dimensional matrix or tensor examples, use tenferro typed tensors by default. In the current crate layout this means tenferro_tensor::TypedTensor, but tenferro-rs is under active development. Inspect the current API, crate name, import path, and resolved Git revision before writing constructor or indexing code. The scientific design should still be clear before the exact API calls are chosen:
- State the shape of each input.
- State the axis meaning.
- Check shape compatibility before computation.
- Return an explicit error for incompatible shapes.
ndarray and ndarray-linalg remain alternatives for established BLAS and LAPACK workflows, especially when a project needs routines already exposed by that ecosystem. The algorithmic cost of matrix multiplication is discussed in Matrix multiplication as a performance model.
Things to look up
- Matrix-vector product
- Matrix-matrix product
- Shape mismatch
- Dense matrix
- Sparse matrix
- BLAS
- LAPACK
Exercise
Use a small matrix-vector or matrix-matrix example with explicit shape checks.
For example, design a matrix-vector product where the matrix has shape rows x cols and the vector has length cols. Before computing, check that the vector length matches the matrix column count. In your answer:
- State the input shapes.
- State the output shape.
- State what error is returned for a mismatch.
- Explain which part would use
&[f64]and which part would use a tenferro typed tensor.
Notes for the exercise
- Do not use nested vectors as a matrix representation.
- Check shapes before computation.
- Keep one-dimensional vector inputs as slices at function boundaries.
- For two-dimensional data, verify the current
tenferroAPI before trusting generated code. - Matrix-matrix multiplication is usually
O(N^3)for the basic algorithm. - Mention whether symmetry or sparsity could change the approach.