NxN

Struct NxN 

Source
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

Source

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);
Source

pub fn from_cols<T>( cols: Vec<Vec<T>>, col_vars: Option<Vec<&str>>, ) -> Result<NxN, &'static str>
where T: Into<f64> + Copy,

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);
Source

pub fn scale_row<T>(&mut self, row: usize, scalar: T)
where T: Into<f64> + Copy,

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);
Source

pub fn add_to_row<T>(&mut self, row: usize, vec: &Vec<T>)
where T: Into<f64> + Copy,

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);
Source

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);
Source

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);
Source

pub fn to_vec(self) -> Vec<Vec<f64>>

Returns the matrix as Vec<Vec<f64>>, consuming the self value in the process

Trait Implementations§

Source§

impl Clone for NxN

Source§

fn clone(&self) -> NxN

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NxN

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for NxN

Source§

fn eq(&self, other: &NxN) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.