#vector #matrix #math

vecmat

Lightweight low-dimensional vector and matrix library

8 releases

✓ Uses Rust 2018 edition

0.4.0 Feb 14, 2019
0.3.1 Oct 8, 2018
0.3.0 Sep 5, 2018
0.2.4 Sep 1, 2018
0.2.1 May 18, 2017

#43 in Math

42 downloads per month
Used in 1 crate

MIT/Apache

69KB
2.5K SLoC

vecmat

Crates.io Docs.rs Travis CI Appveyor Codecov.io License

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

Documentation

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 
    va[0] += 3.0; // add-assign
    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

Complex analysis

  • Complex numbers
  • Quaternions

Transformations

  • Affine transformations for vector (with inverse)
  • Moebius transformation

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~175KB