7 releases

Uses old Rust 2015

0.2.2 Jul 9, 2023
0.2.1 Jul 4, 2023
0.1.8 Jul 1, 2023
0.1.6 Jun 30, 2023

#555 in Math

25 downloads per month

MIT license

91KB
2K SLoC

mematrica Package Documentation

mematrica_logo

an easy to use library for working with matrices.

Features

  • Matrix-scalar addition, substraction, multiplication
  • Matrix-matrix addition, substraction, multiplication
  • Matrix indexing
  • Matrix determinant search
  • Inverse matrix search
  • Matrix transpose
  • Matrix from file reading
  • Matrix to file writing

Usage

[dependencies]
mematrica = "0.2.2"

Examples

extern crate mematrica;

use mematrica::*;

fn main() {
    let mut matrix_2x2 = Matrix2::new(1, 2, 3, 4); // creates a matrix 2x2 with elements 1, 2, 3, 4
    
    let double = matrix_2x2 * 2; // multiplies matrix on 2

    assert_eq!(vec![vec![2, 4], vec![6, 8]], double.get_elements()); 
}

Using from_vec()

extern crate mematrica;

use mematrica::*;

fn main() {
    let matrix_2x2 = Matrix2::from_vec_as_rows(vec![1, 2]);

    assert_eq!(vec![vec![1, 2], vec![1, 2]], matrix_2x2.get_elements()); 
}

to_file() & from_file() example

extern crate mematrica;

use mematrica::*;

fn main() {
    let m = Matrix2::new(1, 2, 3, 4);
    m.to_file(String::from("./src/bin/file.txt"), ',');

    let q = Matrix2::<i32>::from_file(String::from("./src/bin/file.txt"), ',');

    assert_eq!(vec![vec![1, 2], vec![3, 4]], q.get_elements());
}

Other ways to create matrix

  • zero: creates a matrix with zeros as its elements
  • one: creates a matrix with ones as its elements
  • identity: creates an identity matrix
  • from_file: reads matrix from file
  • from_element: creates a matrix from element
  • from_vec: creates matrix from vector as its rows or columns (from_vec_as_rows or from vec_as_columns)

Get element by index

extern crate mematrica;

use mematrica::*;

fn main() {
    let mut matrix_2x2 = Matrix2::new(1, 2, 3, 4);

    assert_eq!(1, matrix_2x2[(0, 0)]);

    // change element
    matrix[(0, 0)] = 2;
    assert_eq!(2, matrix_2x2[(0, 0)]);
}

Elementary operations

extern crate mematrica;

use mematrica::*;

fn main() {
    let m1 = CMatrix::from_element(2, 2, 4);
    let m2 = Matrix2::from_element(2);

    assert_eq!(vec![vec![16, 16], vec![16, 16]], (m1*m2).get_elements()); //also can use '+' '-'
}

You can find more examples in examples folder

Dependencies

~465KB