mtrs/lib.rs
1//! A library for creating, using, and printing matrices.
2//! Matrices can be defined using the public struct, `mtrs::Matrix`, or the macro `matrix!`.
3//! Currently the `Matrix` struct does not internally support floats. This may change in the future.
4//! ```
5//! #[macro_use] extern crate mtrs;
6//! use mtrs::Matrix;
7//!
8//! fn main() {
9//! let matrix = matrix![f32; (2, 2); 1, 2; 3, 4.1];
10//! println!("{}", matrix.scalar_add(4.1));
11//! }
12//! ```
13//! The `Matrix` struct supports addition, subtraction, and multiplication with eachother,
14//! along with implementations for basic operations between a scalar value and a `Matrix`
15
16extern crate num_traits;
17
18mod impls;
19mod macros;
20mod math;
21mod matrix;
22mod size;
23
24use num_traits::Num;
25
26/// The main Matrix struct. Can be created in a variety of different ways.
27/// ```
28/// #[macro_use] extern crate mtrs;
29/// use mtrs::Matrix;
30///
31/// // All of these create a 2 x 2 matrix.
32///
33/// // From a 2D vector
34/// let matrix = Matrix::from_vec(2, vec![1, 2, 3, 4]);
35/// // Identity matrix of i32
36/// let matrix: Matrix<i32> = Matrix::identity(2);
37/// // Matrix of ones
38/// let matrix: Matrix<i32> = Matrix::ones(2);
39/// // Matrix of zeros
40/// let matrix: Matrix<i32> = Matrix::zeros(2);
41/// // Matrix of `i32`s
42/// let matrix = matrix![(2, 2); 1, 2, 3, 4];
43/// // Matrix of `f64`s
44/// let matrix = matrix![f64; (2, 2); 1, 2; 3, 4];
45/// ```
46#[derive(Clone, PartialEq, Debug)]
47pub struct Matrix<T: Num> {
48 /// The height of the matrix
49 height: usize,
50
51 /// The width of the matrix
52 width: usize,
53
54 /// The main body of the matrix, stored as a 2d array
55 data: Vec<T>,
56}
57
58#[cfg(test)]
59mod main_tests {
60 use super::Matrix;
61
62 #[test]
63 fn test_values() {
64 let matrix = Matrix::from_slice((2, 2), &[1, 2, 3, 4]);
65
66 assert_eq!(matrix.height, 2);
67 assert_eq!(matrix.width, 2);
68 assert_eq!(matrix.data, vec![1, 2, 3, 4]);
69 }
70}