minmath 0.4.1

A lightweight math library
Documentation

minmath

GitHub stars GitHub license

Crates.io Crates.io downloads Docs.rs

Rust Version

Quick start example

Matrix

use minmath::Matrix;

fn main() {
    let mat1 = Matrix::new([[1, 2], [3, 4]]);
    let mat2 = Matrix::new([[5, 6], [7, 8]]);
    let mat3 = mat1 + mat2;
    println!("{}", mat3);
}

Vector

use minmath::Vector;

fn main() {
    let vec1 = Vector::new([4, -3, 0]);
    let vec2 = Vector::new([0, 3, 2]);
    let vec3 = vec1 + vec2;
    prinln!("{}", vec3);
}

Contributing

Contributions, issues, and feature requests are welcome! Feel free to check issues page.

Adding minmath to dependencies

There are two ways to add the crate to your dependencies.

Manual

Add the following to your Cargo.toml file.

[dependencies]
minmath = "*"
# Check https://crates.io/crates/minmath for the latest version

Command Line

Run the following in your terminal.

cargo add minmath

Structures

Matrix

Features

  • Generic matrix type with const generics for size
  • Operator overloading for arithmetic
  • Matrix multiplication for square and non-square matrices
  • Determinant calculation (2x2 only for now)
  • Debug and Display formatting

The following are derived for the Matrix structure.

Clone
Copy
PartialEq
Eq

When a matrix is declared, its type and size (rows and columns) are specified as generic parameters. The type should implement the following traits.

Add<Output = T>
AddAssign
Sub<Output = T>
SubAssign
Mul<Output = T>
MulAssign
Div<Output = T>
DivAssign
Copy
Debug
Display
Default

Functions

fn new(data: [[T; COLUMNS]; ROWS]) -> Matrix<T, ROWS, COLUMNS>
  • Creates a new matrix of type T and size (ROWS, COLUMNS) from the provided 2D array.

e.g

let matrix: Matrix<i32, 3, 3> = Matrix::new([
    [4, 3, 0],
    [8, 3, 9],
    [-2, 4, 2],
]);
fn size(&self) -> (usize, usize)
  • Returns the size of the matrix as (rows, columns).

e.g

let matrix: Matrix<i32, 2, 3> = Matrix::new([
    [4, 3, 0],
    [8, 3, 9]
]);
let size: (usize, usize) = matrix.size();
fn determinant(&self) -> T
  • Returns the determinant of the matrix (only works for 2x2 at the moment).

e.g

let matrix: Matrix<i32, 2, 2> = Matrix::new([
    [4, -3],
    [8, 3],
]);
let determinant: i32 = matrix.determinant();

Converting from Matrix to Vector

let matrix = Matrix::new([1, 2, 3]);
let vector = matrix.to_vector();

Operators

All matrix sizes are supported by the operators (square and non-square).

Operation With Scalar With Matrix
Add ✓ (+) ✓ (+)
Add Assign ✓ (+=) ✓ (+=)
Subtract ✓ (-) ✓ (-)
Subtract Assign ✓ (-=) ✓ (-=)
Multiply ✓ (*) ✓ (*)
Multiply Assign ✓ (*=) ✓ (*=)
Divide ✓ (/)
Divide Assign ✓ (/=)

Matrix multiplication

let a = Matrix::new([[1, 2, 3], [4, 5, 6]]);
let b = Matrix::new([[7, 8], [9, 10], [11, 12]]);
let c = a * b; // c is Matrix<i32, 2, 2>

Vector

Features

  • Generic Vector type with const generics for size
  • Operator overloading for arithmetic
  • Debug and Display formatting

The following are derived for the Vector structure.

Clone
Copy
PartialEq
Eq

When a vector is declared, its type and size are specified as generic parameters. The type should implement the following traits.

Add<Output = T>
AddAssign
Sub<Output = T>
SubAssign
Mul<Output = T>
MulAssign
Div<Output = T>
DivAssign
Copy
Debug
Display
Default

Functions

pub fn new(data: [T; SIZE]) -> Self
  • Creates a new vector of type T and size from the provided array.

e.g

let vec = Vector::new([4, -3, 2]);
pub fn size(&self) -> usize
  • Returns the size of the vector.

e.g

let vec = Vector::new([4]);
        
let size: usize = vec.size();

Converting from Vector to Matrix

let vector = Vector::new([1, 2, 3]);
let matrix = vector.to_matrix();

Operators

All matrix sizes are supported by the operators (square and non-square).

Operation With Scalar With Vector
Add ✓ (+) ✓ (+)
Add Assign ✓ (+=) ✓ (+=)
Subtract ✓ (-) ✓ (-)
Subtract Assign ✓ (-=) ✓ (-=)
Multiply ✓ (*)
Multiply Assign ✓ (*=)
Divide ✓ (/)
Divide Assign ✓ (/=)

Vector multiplication

Dot procuct
let vec1 = Vector::new([1, 2, 3]);
let vec2 = Vector::new([4, 5, 6]);

let dot_product = vec1.dot(vec2);
Cross procuct

The cross product is only implemented for 3D vectors.

let vec1 = Vector::new([1, 2, 3]);
let vec2 = Vector::new([4, 5, 6]);

let cross_product = vec1.cross(vec2);

License

This project is licensed under the MIT License. See LICENSE for details.

Links