minmath
Quick start example
Matrix
use Matrix;
Vector
use Vector;
Contributing
Contributions, issues, and feature requests are welcome! Feel free to check issues page.
Adding minmath
to dependencies
minmath
currently has zero dependencies and I plan to keep it that way.
There are two ways to add the crate to your dependencies.
Manual
Add the following to your Cargo.toml
file.
[]
= "*"
# Check https://crates.io/crates/minmath for the latest version
Command Line
Run the following in your terminal.
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.
Copy
Debug
Display
Default
AddAssign
SubAssign
MulAssign
DivAssign
Functions
- Creates a new matrix of type T and size (ROWS, COLUMNS) from the provided 2D array.
e.g
let matrix: = new;
- Returns the size of the matrix as (rows, columns).
e.g
let matrix: = new;
let size: = matrix.size;
- Returns the determinant of the matrix (only works for 2x2 at the moment).
e.g
let matrix: = new;
let determinant: i32 = matrix.determinant;
Converting from Matrix to Vector
let matrix = new;
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 = new;
let b = new;
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.
Copy
Debug
Display
Default
AddAssign
SubAssign
MulAssign
DivAssign
Functions
- Creates a new vector of type T and size from the provided array.
e.g
let vec = new;
- Returns the size of the vector.
e.g
let vec = new;
let size: usize = vec.size;
Converting from Vector to Matrix
let vector = new;
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 | ✓ (/= ) |
Dot procuct
let vec1 = new;
let vec2 = new;
let dot_product = vec1.dot;
Cross procuct
The cross product is only implemented for 3D vectors.
let vec1 = new;
let vec2 = new;
let cross_product = vec1.cross;
License
This project is licensed under the GNU General Public License. See LICENSE for details.