#matrix #algebra #scalar #matrix-operations #arithmetic #func #div

bin+lib matrix_algebra

A crate supporting matrix algebra

1 unstable release

0.1.0 Mar 27, 2022

#1821 in Math

MIT license

27KB
764 lines

matrix-algebra


lib.rs:

This crate intends to support basic matrix operations and arithmetic. It is quite open in the choice of the underlying scalar type.

For instance, the scalar type can be chosen to be anything that implements common arithmetic traits like Add, Sub, Div, ...

The core types of this crate are Matrix, Scalar and Func. A matrix can created like this:

use matrix_algebra::Matrix;

let m = Matrix::new(vec![vec![0, 1], vec![2, 3]]);


Common arithmetic operators are implemented on the type `Matrix`:
use matrix_algebra::Matrix;

let m1 = Matrix::new(vec![vec![0, 1], vec![2, 3]]);
let m2 = Matrix::new(vec![vec![0, 1], vec![2, 3]]);

assert_eq!(m1 + m2, Matrix::new(vec![vec![0, 2], vec![4, 6]]));


Also, it is possible to apply functions wrapped by `Func` element-wise on a matrix:
use matrix_algebra::{Matrix, Func};

let sin = Func::new(|x| f64::sin(x));
let m = Matrix::new(vec![vec![0.0, 1.0], vec![2.0, 3.0]]);

assert_eq!(
  sin%m,
  Matrix::new(
   vec![vec![f64::sin(0.0), f64::sin(1.0)], vec![f64::sin(2.0), f64::sin(3.0)]]
  )
);


Scalars can by multiplied from left:
use matrix_algebra::{Matrix, Scalar};

let m = &Matrix::new(vec![vec![0, 1], vec![2, 3]]);
let s = Scalar(2);

assert_eq!(s*m, m*2);

No runtime deps