## vecmat

Lightweight low-dimensional vector and matrix library

### 9 releases

✓ Uses Rust 2018 edition

 0.4.1 Nov 4, 2019 Feb 14, 2019 Oct 8, 2018 Sep 5, 2018 May 18, 2017

#26 in Math

Used in physsol

MIT/Apache

69KB
2.5K SLoC

# vecmat

Low-dimensional vector and matrix structures and common operations for them

## Usage

``````extern crate vecmat;

use vecmat::vec::*;
use vecmat::mat::*;

fn main() {
// Create vectors
let mut va = Vec3::<f64>::new(); // filled with zeros
let vb = Vec3::<f64>::from(1.0, 2.0, 3.0); // from values
println!("{}, {}", va, vb);

// Vector access
va[1] = vb[0]; // read and write
println!("{}", va);

// Vector products
println!("{}", 2.0*vb); // scalar-by-vector
println!("{}", va*vb); // component-wise
println!("{}", va.dot(vb)); // dot
println!("{}", va.cross(vb)); // cross

// Distance between two vectors
println!("{}", (va - vb).length());

// Create matrices
let mut ma = Mat3::<f64>::one(); // identity 3x3 matrix
let mb = Mat3x2::<f64>::from( // 3x2 matrix from values
1.0, 2.0, 0.0,
0.0,-1.0, 1.0,
);
println!("{},\n{}", ma, mb);

// Access matrix components
ma[(1,1)] = 2.0; // access by (i,j) indices
ma[(0,2)] = -3.0;
ma[(2,0)] = -1.0;
println!("{}", ma);

// Transpose matrix
println!("{}", mb.transpose());

// Matrix-vector product
println!("{}", mb.dot(vb));
println!("{}", va.dot(ma));

// Matrix-matrix product
println!("{}", mb.dot(ma));

// Outer product of vectors
println!("{}", va.outer(vb));

// Determinant and inverse matrix
println!("{}", ma.det());
println!("{}", ma.inverse());
}
``````

## Structs:

• Vectors: `vec::VecN<T>` where `N` is 2, 3 or 4
• Matrices: `mat::MatNxM<T>` (or `mat::MatN<T>` for square matrices) where `N` and `M` are 2, 3 or 4
• Transformations: `map::AffineN<T>` - affine transformation for vectors

## Features:

### Linear algebra

• Vector and matrix arithmetcs (component-wise `+`, `-`, `*`, `/`)
• Integer vector arithmetics (`div_floor` and `mod_floor` for integer vectors)
• Boolean vectors (vector comparison, `all`, `any`)
• `dot`, `cross` and `outer` products for vectors
• Matrix-matrix and matrix-vector multiplication
• Square matrix `det` and `inverse`

### Transformations

• Affine transformations for vector (with `inverse`)