Crate hoomd_linear_algebra

Crate hoomd_linear_algebra 

Source
Expand description

Linear algebra optimized for small matrices.

This crate places an emphasis on generality and simplicity, with optimization efforts targeted at small matrices. Some complex routines (SVD, matrix inversion, etc.) will only be implemented for certain shapes, and generally consist of specialized algorithms optimal for those inputs.

This crate should not be considered a replacement for a dedicated linear algebra library like faer-rs or nalgebra. Instead, it can be used as a simple and lightweight dependency for matrix methods common to molecular simulation and analysis.

§Matrices

Construct a small rectangular Matrix on the stack:

use hoomd_linear_algebra::matrix::Matrix;

let m = Matrix {
    rows: [[1.0, 2.0, 3.0], [-6.0, 3.0, 2.0]],
};

Diagonal matrices (DiagonalMatrix) store only the diagonal elements:

use hoomd_linear_algebra::matrix::DiagonalMatrix;

let m = DiagonalMatrix {
    elements: [-2.0, 4.0, -5.0],
};

Matrix22, Matrix33, and Matrix44 are type aliases for commonly used matrix sizes. Construct a 2x2 matrix with every element set to 4:

use hoomd_linear_algebra::{Full, matrix::Matrix22};

let m = Matrix22::full(4.0);

Construct a 3x3 identity matrix $\mathbf{I}$:

use hoomd_linear_algebra::{SquareMatrix, matrix::Matrix44};

let m = Matrix44::identity();

Index matrix entries by (row, column):

use hoomd_linear_algebra::matrix::Matrix;

let m = Matrix {
    rows: [[1.0, 2.0, 3.0], [-6.0, 3.0, 2.0]],
};

let element = m[(1, 2)];

§Matrix Operations

Matrices can be added:

use hoomd_linear_algebra::{Full, GeneralMatrix, matrix::Matrix};
let a = Matrix {
    rows: [[1.0, -3.0], [-2.0, 4.0]],
};
let b = Matrix {
    rows: [[4.0, -8.0], [6.0, 7.0]],
};

let mut c = a + b;
c += a;

subtracted:

use hoomd_linear_algebra::{Full, GeneralMatrix, matrix::Matrix};
let a = Matrix {
    rows: [[1.0, -3.0], [-2.0, 4.0]],
};
let b = Matrix {
    rows: [[4.0, -8.0], [6.0, 7.0]],
};

let mut c = a - b;
c += a;

multiplied by a scalar:

use hoomd_linear_algebra::{Full, GeneralMatrix, matrix::Matrix};
let a = Matrix {
    rows: [[4.0, -8.0], [6.0, 7.0]],
};
let b = -2.0;

let mut c = a * b;
c *= b;

negated:

use hoomd_linear_algebra::{Full, GeneralMatrix, matrix::Matrix};
let a = Matrix {
    rows: [[1.0, -3.0], [-2.0, 4.0]],
};

let b = -a;

and indexed (in row,column ordering):

use hoomd_linear_algebra::{Full, GeneralMatrix, matrix::Matrix};
let a = Matrix {
    rows: [[1.0, -3.0], [-2.0, 4.0]],
};

let element = a[(1, 0)];

You can also perform matrix-matrix multiplication:

use hoomd_linear_algebra::{MatMul, matrix::Matrix};

let a = Matrix {
    rows: [[1.0, 2.0], [3.0, 4.0]],
};
let b = Matrix {
    rows: [[4.0, 3.0], [2.0, 1.0]],
};

let c = a.matmul(&b);

and invert matrices:

use hoomd_linear_algebra::{Invertible, matrix::Matrix};

let a = Matrix {
    rows: [[1.0, 2.0], [3.0, 4.0]],
};

let b = a.inverse();

§Numerical Algorithms

hoomd-linear-algebra implements a number of numerical algorithms on matrices:

§Complete documentation

hoomd-linear-algebra is is a part of hoomd-rs. Read the complete documentation for more information.

Modules§

matrix
Structs implementing a large subset of Matrix traits.

Traits§

Full
Initialize matrices with identical elements.
GeneralMatrix
Common operations for all matrices.
Invertible
Compute the inverse of a matrix.
MatMul
Matrix multiplication.
QuadraticForm
Solve the quadratic form.
SquareMatrix
Matrices that have the same number of rows and columns.