pub struct NxN {
pub size: usize,
pub vars: Option<Vec<String>>,
/* private fields */
}Expand description
An n x n matrix with a given size n and a Vec containing the variables in each column if applicable.
Fields§
§size: usize§vars: Option<Vec<String>>Implementations§
Source§impl NxN
impl NxN
Sourcepub fn identity(size: usize) -> NxN
pub fn identity(size: usize) -> NxN
Initializes an NxN identity matrix of the specified size
§Example
use nexsys_math::NxN;
let my_matrix = NxN::identity(3);
let check = vec![
vec![1.0, 0.0, 0.0],
vec![0.0, 1.0, 0.0],
vec![0.0, 0.0, 1.0]
];
assert_eq!(my_matrix.to_vec(), check);Sourcepub fn from_cols<T>(
cols: Vec<Vec<T>>,
col_vars: Option<Vec<&str>>,
) -> Result<NxN, &'static str>
pub fn from_cols<T>( cols: Vec<Vec<T>>, col_vars: Option<Vec<&str>>, ) -> Result<NxN, &'static str>
Initializes an NxN matrix of given values from a Vec<Vec<f64>>
§Example
use nexsys_math::NxN;
let my_vars = vec!["x", "y", "z"];
let my_cols = vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 5.0, 6.0],
vec![7.0, 8.0, 9.0]
];
let my_matrix = NxN::from_cols(
my_cols.clone(),
Some(my_vars)
).unwrap();
assert_eq!(my_matrix.to_vec(), my_cols);Sourcepub fn scale_row<T>(&mut self, row: usize, scalar: T)
pub fn scale_row<T>(&mut self, row: usize, scalar: T)
Mutates a row, scaling it by the given value
§Example
use nexsys_math::NxN;
let mut my_matrix = NxN::identity(3);
let check = vec![
vec![1.0, 0.0, 0.0],
vec![0.0, 2.0, 0.0],
vec![0.0, 0.0, 1.0]
];
my_matrix.scale_row(1, 2);
assert_eq!(my_matrix.to_vec(), check);Sourcepub fn add_to_row<T>(&mut self, row: usize, vec: &Vec<T>)
pub fn add_to_row<T>(&mut self, row: usize, vec: &Vec<T>)
Adds a given row vector to a row in the matrix
§Example
use nexsys_math::NxN;
let mut my_matrix = NxN::identity(3);
let check = vec![
vec![1.0, 2.0, 0.0],
vec![0.0, 3.0, 0.0],
vec![0.0, 2.0, 1.0]
];
my_matrix.add_to_row(1, &vec![2, 2, 2]);
assert_eq!(my_matrix.to_vec(), check);Sourcepub fn get_row(&self, row: usize) -> Vec<f64>
pub fn get_row(&self, row: usize) -> Vec<f64>
Returns a row from the matrix
§Example
use nexsys_math::NxN;
let mut my_matrix = NxN::identity(3);
let check = vec![0.0, 0.0, 1.0];
assert_eq!(my_matrix.get_row(2), check);Sourcepub fn invert(&mut self) -> Result<(), &'static str>
pub fn invert(&mut self) -> Result<(), &'static str>
inverts the matrix, if possible. This method returns a result that indicates whether the inversion was successful or not.
§Example
use nexsys_math::NxN;
let mut my_matrix = NxN::from_cols(vec![
vec![-1.0, 1.0],
vec![ 1.5,-1.0]
], None).unwrap();
my_matrix.invert().unwrap();
let inverse = vec![
vec![2.0, 2.0],
vec![3.0, 2.0]
];
assert_eq!(my_matrix.to_vec(), inverse);Trait Implementations§
impl StructuralPartialEq for NxN
Auto Trait Implementations§
impl Freeze for NxN
impl RefUnwindSafe for NxN
impl Send for NxN
impl Sync for NxN
impl Unpin for NxN
impl UnwindSafe for NxN
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more