Trait matrixable::MatrixExt

source ·
pub trait MatrixExt {
    type Element;

Show 39 methods // Required methods fn num_rows(&self) -> usize; fn num_cols(&self) -> usize; fn get(&self, row: usize, column: usize) -> Option<&Self::Element>; // Provided methods unsafe fn get_unchecked(&self, row: usize, column: usize) -> &Self::Element { ... } fn get_nth(&self, n: usize) -> Option<&Self::Element> { ... } unsafe fn get_nth_unchecked(&self, n: usize) -> &Self::Element { ... } fn size(&self) -> usize { ... } fn dimensions(&self) -> (usize, usize) { ... } fn num_diags(&self) -> usize { ... } fn row_len(&self) -> usize { ... } fn col_len(&self) -> usize { ... } fn check(&self, i: usize, j: usize) -> bool { ... } fn check_nth(&self, n: usize) -> bool { ... } fn index_from(&self, (i, j): (usize, usize)) -> usize { ... } fn subscripts_from(&self, n: usize) -> (usize, usize) { ... } fn checked_index_from(&self, (i, j): (usize, usize)) -> Option<usize> { ... } fn checked_subscripts_from(&self, n: usize) -> Option<(usize, usize)> { ... } fn iter(&self) -> Iter<'_, Self> where Self: Sized { ... } fn row(&self, i: usize) -> Option<Row<'_, Self>> where Self: Sized { ... } fn col(&self, j: usize) -> Option<Column<'_, Self>> where Self: Sized { ... } fn diag(&self, n: usize) -> Option<Diag<'_, Self>> where Self: Sized { ... } fn main_diag(&self) -> Diag<'_, Self> where Self: Sized { ... } fn enumerate(&self) -> Enumerator<Iter<'_, Self>> where Self: Sized { ... } fn rows(&self) -> Rows<'_, Self> where Self: Sized { ... } fn cols(&self) -> Columns<'_, Self> where Self: Sized { ... } fn diags(&self) -> Diags<'_, Self> where Self: Sized { ... } fn access<S: AccessStrategy<Self>>( &self, strategy: S ) -> Access<'_, Self, S> where Self: Sized { ... } fn transform<S: TransformStrategy<Self>>(self, strategy: &S) -> S::Output where Self: Sized { ... } fn is_empty(&self) -> bool { ... } fn is_square(&self) -> bool { ... } fn is_one_dimension(&self) -> bool { ... } fn is_symmetric(&self) -> bool where Self::Element: PartialEq { ... } fn is_skew_symmetric(&self) -> bool where Self: Sized, for<'a> &'a Self::Element: Neg, for<'a> Self::Element: PartialEq<<&'a Self::Element as Neg>::Output> { ... } fn is_singleton(&self) -> bool { ... } fn is_horizontal(&self) -> bool { ... } fn is_vertical(&self) -> bool { ... } fn is_diagonal(&self) -> (bool, Option<&Self::Element>) where Self: Sized, for<'a> &'a Self::Element: PartialEq { ... } fn is_scalar( &self ) -> (bool, Option<&Self::Element>, Option<&Self::Element>) where Self: Sized, for<'a> &'a Self::Element: PartialEq { ... } fn is_constant(&self) -> (bool, Option<&Self::Element>) where Self::Element: PartialEq { ... }
}
Expand description

This trait provides methods and tools for accessing data in matrix-like structures.

This trait allows only immutable access to elements of a matrix. For a mutable implementation see MatrixMutExt

Required Associated Types§

source

type Element

The type of the elements of the matrix.

Required Methods§

source

fn num_rows(&self) -> usize

Gets the number of rows of the matrix.

§Example
use matrixable::MatrixExt;
 
let a = [[1, 2, 3]];
assert_eq!(a.num_rows(), 1);
 
let empty: [[(); 0]; 0]  = []; 
assert_eq!(empty.num_rows(), 0);
 
let empty1: [[(); 0]; 1]  = [[]]; 
assert_eq!(empty1.num_rows(), 0);
 
let empty2: [[(); 0]; 2]  = [[], []]; 
assert_eq!(empty2.num_rows(), 0);
source

fn num_cols(&self) -> usize

Gets the number of columns of the matrix.

§Example
use matrixable::MatrixExt;
 
let a = [[1, 2, 3]];
assert_eq!(a.num_cols(), 3);
 
let empty: [[(); 0]; 0]  = []; 
assert_eq!(empty.num_cols(), 0);
 
let empty1: [[(); 0]; 1]  = [[]]; 
assert_eq!(empty1.num_cols(), 0);
 
let empty2: [[(); 0]; 2]  = [[], []]; 
assert_eq!(empty2.num_cols(), 0);
source

fn get(&self, row: usize, column: usize) -> Option<&Self::Element>

Returns a reference to an element inside the matrix, at the intersection of the i-th row and the j-th column.

§Example
use matrixable::MatrixExt;
 
let v = [[10, 40, 30]];

assert_eq!(Some(&40), v.get(0, 1));
assert_eq!(None, v.get(0, 3));

Provided Methods§

source

unsafe fn get_unchecked(&self, row: usize, column: usize) -> &Self::Element

Returns a reference to an element, without doing bounds checking.

For a safe alternative see get.

§Safety

Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.

You can think of this like .get(row_index, column_index).unwrap_unchecked().

§Example
use matrixable::MatrixExt;

let x = &[[1, 2, 4]];

unsafe {
    assert_eq!(x.get_unchecked(0, 1), &2);
}
source

fn get_nth(&self, n: usize) -> Option<&Self::Element>

Gets a reference to an element inside a matrix, given its order of disposition in Row Major Order.

§Example
use matrixable::MatrixExt;

let v = [[10, 40, 30]];

assert_eq!(Some(&40), v.get_nth(1));
assert_eq!(None, v.get_nth(3));
source

unsafe fn get_nth_unchecked(&self, n: usize) -> &Self::Element

Returns a reference to an element given its linear order, without doing bound checking.

For a safe alternative see get_nth.

§Safety

Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.

You can think of this like .get_nth(index).unwrap_unchecked().

§Example
use matrixable::MatrixExt;

let x = &[[1, 2, 4]];

unsafe {
    assert_eq!(x.get_nth_unchecked(1), &2);
}
source

fn size(&self) -> usize

Returns the size of the matrix

§Example
use matrixable::MatrixExt;
 
assert_eq!(5, [[1, 2, 3, 4, 5]].size());
assert_eq!(6, [[1, 2], [3, 4], [5, 6]].size());
source

fn dimensions(&self) -> (usize, usize)

Returns the dimensions of the matrix

§Example
use matrixable::MatrixExt;

let m = [[1, 1, 1], [2, 2, 2]];

assert_eq!((2, 3), m.dimensions());
source

fn num_diags(&self) -> usize

Returns the number of diagonals.

§Example
use matrixable::MatrixExt;

let m = [
    [3, 4, 5],
    [2, 3, 4],
    [1, 2, 3]
];

assert_eq!(5, m.num_diags());
source

fn row_len(&self) -> usize

Returns the length of a row.

source

fn col_len(&self) -> usize

Returns the length of a column.

source

fn check(&self, i: usize, j: usize) -> bool

Checks if the provided subscripts point to an element inside the matrix.

§Example
use matrixable::MatrixExt;
 
let m = [ 
    [(0,0), (0,1)],
    [(1,0), (1,1)]
];

assert!(m.check(0,0));
assert!(m.check(0,1));
assert!(m.check(1,0));
assert!(m.check(1,1));
assert!(!m.check(2,0));
source

fn check_nth(&self, n: usize) -> bool

Checks if the provided linear index point to an element inside the matrix.

§Example
use matrixable::MatrixExt;

let m = [ 
    [0, 1],
    [2, 3]
];

assert!(m.check_nth(0));
assert!(m.check_nth(1));
assert!(m.check_nth(2));
assert!(m.check_nth(3));
assert!(!m.check_nth(4));
source

fn index_from(&self, (i, j): (usize, usize)) -> usize

Use matrix as a subscripts-to-index converter.

Index provided follows the Row Major Order.

This does not check if either the provided subscripts or the given index are out of bounds. For a safe alternative see checked_index_from.

§Example
use matrixable::MatrixExt;

let m = [ 
    [0, 1],
    [2, 3],
];

assert_eq!(0, m.index_from((0, 0)));
assert_eq!(1, m.index_from((0, 1)));
assert_eq!(2, m.index_from((1, 0)));
assert_eq!(3, m.index_from((1, 1)));

// If passing out-of-bound subscripts to the method.

assert_eq!(4, m.index_from((2, 0)));
assert_eq!(7, m.index_from((2, 3)));    
assert_eq!(14, m.index_from((2, 10)));
source

fn subscripts_from(&self, n: usize) -> (usize, usize)

Use matrix as a index-to-subscripts converter.

Indexes(subscripts) are obtained from index by Row Major Order.

This does not check if either the provided subscripts or the given index are out of bounds. For a safe alternative see checked_subscripts_from.

§Example
use matrixable::MatrixExt;

let m = [ 
    [0, 1],
    [2, 3],
];

// This method visualizes an array of indexes from `0` to `n`
// (= 4 for this explanation): `[0, 1, 2, 3, 4]`.
// Then this array is divided into array of `m.row_len()` length
// (= 2 in our case): [ 0, 1 | 2, 3 ] => 0:[0, 1], 1:[0, 1], 2:[0, ]
// Finally the last subscript `(2, 0)` is returned

assert_eq!((0, 0), m.subscripts_from(0));
assert_eq!((0, 1), m.subscripts_from(1));
assert_eq!((1, 0), m.subscripts_from(2));
assert_eq!((1, 1), m.subscripts_from(3));

// If passing out-of-bound index to the method.

assert_eq!((2, 0), m.subscripts_from(4));
assert_eq!((3, 1), m.subscripts_from(7));    
assert_eq!((7, 0), m.subscripts_from(14));
source

fn checked_index_from(&self, (i, j): (usize, usize)) -> Option<usize>

Checked index calculation.

Returns None if indexes are out of bounds of the matrix.

§Example
use matrixable::MatrixExt;

let m = [ 
    [0, 1],
    [2, 3],
];

assert_eq!(Some(0), m.checked_index_from((0, 0)));
assert_eq!(Some(1), m.checked_index_from((0, 1)));
assert_eq!(Some(2), m.checked_index_from((1, 0)));
assert_eq!(Some(3), m.checked_index_from((1, 1)));

assert_eq!(None, m.checked_index_from((2, 0)));
source

fn checked_subscripts_from(&self, n: usize) -> Option<(usize, usize)>

Checked indexes calculation.

Returns None if index is out of bound of the vector representation.

§Example
use matrixable::MatrixExt;

let m = [ 
    [0, 1],
    [2, 3],
];

assert_eq!(Some((0, 0)), m.checked_subscripts_from(0));
assert_eq!(Some((0, 1)), m.checked_subscripts_from(1));
assert_eq!(Some((1, 0)), m.checked_subscripts_from(2));
assert_eq!(Some((1, 1)), m.checked_subscripts_from(3));

assert_eq!(None, m.checked_subscripts_from(4));
source

fn iter(&self) -> Iter<'_, Self>
where Self: Sized,

Returns an iterator over the elements of the matrix.

Iteration follows the Row Major Order.

§Example
use matrixable::MatrixExt;

let x = &[
     [1, 2],
     [3, 4]
];
let mut iterator = x.iter();
 
assert_eq!(iterator.next(), Some(&1));
assert_eq!(iterator.next(), Some(&2));
assert_eq!(iterator.next(), Some(&3));
assert_eq!(iterator.next(), Some(&4));
assert_eq!(iterator.next(), None);
source

fn row(&self, i: usize) -> Option<Row<'_, Self>>
where Self: Sized,

Returns an iterator over the elements of the i-th row.

None is returned if i >= number of rows.

§Example
use matrixable::MatrixExt;

let m = &[[1, 2], [3, 4], [5, 6]];

let mut row = m.row(2).unwrap();

assert_eq!(Some(&5), row.next());
assert_eq!(Some(&6), row.next());
assert_eq!(None, row.next());
 
assert!(m.row(3).is_none());
source

fn col(&self, j: usize) -> Option<Column<'_, Self>>
where Self: Sized,

Returns an iterator over elements of the j-th column.

None is returned if j >= number of columns.

§Example
use matrixable::MatrixExt;

let m = &[[1, 2], [3, 4], [5, 6]];

let mut col = m.col(1).unwrap();

assert_eq!(Some(&2), col.next());
assert_eq!(Some(&4), col.next());
assert_eq!(Some(&6), col.next());
assert_eq!(None, col.next());

assert!(m.col(2).is_none());    
source

fn diag(&self, n: usize) -> Option<Diag<'_, Self>>
where Self: Sized,

Returns an iterator over element of the n-th diagonal of the matrix, starting from bottom-left to top-right.

§Example
use matrixable::MatrixExt;

let mut m = &[
    [1, 4, 6],
    [7, 2, 5],
    [9, 8, 3]
];
 
let mut diag = m.diag(3).unwrap();
assert_eq!(Some(&4), diag.next());
assert_eq!(Some(&5), diag.next());
assert_eq!(None, diag.next());
source

fn main_diag(&self) -> Diag<'_, Self>
where Self: Sized,

Returns the main diagonal i.e. all elements at position (i, i).

§Example
use matrixable::MatrixExt;

let mut m = &[
    [1, 4, 6],
    [7, 2, 5],
    [9, 8, 3]
];

let mut diag = m.main_diag();
 
assert_eq!(Some(&1), diag.next());
assert_eq!(Some(&2), diag.next());
assert_eq!(Some(&3), diag.next());
assert_eq!(None, diag.next());
source

fn enumerate(&self) -> Enumerator<Iter<'_, Self>>
where Self: Sized,

Returns an iterator which gives the current subscripts of the current element as well as its value.

use matrixable::MatrixExt;

let m = &[[1, 2], [3, 4], [5, 6]];
let mut en = m.enumerate();

assert_eq!(Some((0, 0, &1)), en.next());
assert_eq!(Some((0, 1, &2)), en.next());
assert_eq!(Some((1, 0, &3)), en.next());
assert_eq!(Some((1, 1, &4)), en.next());
assert_eq!(Some((2, 0, &5)), en.next());
assert_eq!(Some((2, 1, &6)), en.next());
assert_eq!(None, en.next());
source

fn rows(&self) -> Rows<'_, Self>
where Self: Sized,

Returns an iterator over the rows with immutable access to elements.

 use matrixable::MatrixExt;

 let mut m = [[1, 2], [3, 4], [5, 6]];
 
 let mut rows = m.rows();
 
 assert_eq!(vec![&1, &2], rows.next().unwrap().collect::<Vec<_>>());
 assert_eq!(vec![&3, &4], rows.next().unwrap().collect::<Vec<_>>());
 assert_eq!(vec![&5, &6], rows.next().unwrap().collect::<Vec<_>>());
 assert!(rows.next().is_none());
source

fn cols(&self) -> Columns<'_, Self>
where Self: Sized,

Returns an iterator over the columns with immutable access to elements.

use matrixable::MatrixExt;

let mut m = [[1, 2], [3, 4], [5, 6]];

let mut cols = m.cols();

assert_eq!(vec![&1, &3, &5], cols.next().unwrap().collect::<Vec<_>>());
assert_eq!(vec![&2, &4, &6], cols.next().unwrap().collect::<Vec<_>>());
assert!(cols.next().is_none());
source

fn diags(&self) -> Diags<'_, Self>
where Self: Sized,

Returns an iterator over the diagonals with immutable access to elements. Examples

use matrixable::MatrixExt;

let m = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]
];
 
// After the main diagonal.
{
    let mut diag = m.diag(3).unwrap();
    assert_eq!(Some(&1), diag.next());
    assert_eq!(Some(&5), diag.next());
    assert_eq!(None, diag.next());
}
 
// At the main diagonal
{
    let mut diag = m.diag(2).unwrap();
    assert_eq!(Some(&0), diag.next());
    assert_eq!(Some(&4), diag.next());
    assert_eq!(Some(&8), diag.next());
    assert_eq!(None, diag.next());
}
 
// Before the main diagonal.
{
    let mut diag = m.diag(1).unwrap();
    assert_eq!(Some(&3), diag.next());
    assert_eq!(Some(&7), diag.next());
    assert_eq!(None, diag.next());
}
 
// At edges.
{
    let mut first_diag = m.diag(0).unwrap();
    assert_eq!(Some(&6), first_diag.next());
    assert_eq!(None, first_diag.next());
  
    let mut last_diag = m.diag(4).unwrap();
    assert_eq!(Some(&2), last_diag.next());
    assert_eq!(None, last_diag.next());
}

assert!(m.diag(5).is_none());
source

fn access<S: AccessStrategy<Self>>(&self, strategy: S) -> Access<'_, Self, S>
where Self: Sized,

Creates a matrix to access elements of this matrix following an AccessStrategy.

§Example
use matrixable::MatrixExt;
use matrixable::strategies::ShiftFront;

let m = [[0, 1], [2, 3]];
let access = m.access(ShiftFront(3));

assert_eq!(Some(&1), access.get(0, 0));
assert_eq!(Some(&2), access.get(0, 1));
assert_eq!(Some(&3), access.get(1, 0));
assert_eq!(Some(&0), access.get(1, 1));

This method returns an Access struct that implements MatrixExt. So by repeating this method on that struct you can chain access and obtain a more complex access.

use matrixable::MatrixExt;
use matrixable::strategies::{ ShiftFront, FlipH, Transpose};

let m = [[0, 1], [2, 3]]; 

let shift = m.access(ShiftFront(3)); // [[1, 2], [3, 0]]
let trans_shift = shift.access(Transpose); // [[1, 3], [2, 0]]
let flip_trans_shift = trans_shift.access(FlipH); // [[3, 1], [0, 2]]

assert_eq!(Some(&3), flip_trans_shift.get(0, 0));
assert_eq!(Some(&1), flip_trans_shift.get(0, 1));
assert_eq!(Some(&0), flip_trans_shift.get(1, 0));
assert_eq!(Some(&2), flip_trans_shift.get(1, 1));

However, prefer using AccessStrategySet method if you have a considerable number of AccessStrategys to chain.

source

fn transform<S: TransformStrategy<Self>>(self, strategy: &S) -> S::Output
where Self: Sized,

Consumes the matrix an returns an output defined by a TransformStrategy.

source

fn is_empty(&self) -> bool

Checks if the matrix is empty.

use matrixable::MatrixExt;

assert!(![[0]].is_empty());
assert!(![[0], [0]].is_empty());

let empty: [[u8; 0]; 0] = [];
assert!(empty.is_empty());

let empty2: [[u8; 0]; 1] = [[]];
assert!(empty2.is_empty());

let empty3: [[u8; 0]; 2] = [[], []];
assert!(empty3.is_empty());
source

fn is_square(&self) -> bool

Checks if the matrix is a square matrix (a matrix with equal number of rows and columns).

use matrixable::MatrixExt;

// singleton
assert!([[1]].is_square());
 
// row
assert!(![[1, 2, 3]].is_square());
 
// column
assert!(![[0], [1], [3]].is_square());
 
// square
assert!([[0; 4]; 4].is_square());

// All those three are valid because they are all empty matrices.
let empty: [[u8; 0]; 0] = [];
assert!(empty.is_square());

let empty2: [[u8; 0]; 1] = [[]];
assert!(empty2.is_square());

let empty3: [[u8; 0]; 2] = [[], []];
assert!(empty3.is_square());
 
// any other
assert!(![[0; 2]; 4].is_square());
source

fn is_one_dimension(&self) -> bool

Checks if the matrix has one dimension (number of columns is 1 or number of rows is 1)

use matrixable::MatrixExt;

assert_eq!(true, [[0]].is_one_dimension());
assert_eq!(true, [[0, 0]].is_one_dimension());
assert_eq!(true, [[0], [0]].is_one_dimension());
assert_eq!(false, [[0, 0], [0, 0]].is_one_dimension());

let empty: [[u8; 0]; 0] = [];
assert_eq!(false, empty.is_one_dimension());

let empty2: [[u8; 0]; 1] = [[]];
assert_eq!(false, empty2.is_one_dimension());

let empty3: [[u8; 0]; 2] = [[], []];
assert_eq!(false, empty3.is_one_dimension());
source

fn is_symmetric(&self) -> bool
where Self::Element: PartialEq,

Checks if the matrix is symmetric i.e. it does not change when transposed.

use matrixable::MatrixExt;

assert!([[0]].is_symmetric());
assert!([[1, 0, 0], [0, 1, 0], [0, 0, 1]].is_symmetric());
assert!([[1], [2], [3]].is_symmetric());
assert!(![[1, 2], [2, 3], [3, 4]].is_symmetric());

let empty: [[u8; 0]; 0] = [];
assert!(!empty.is_symmetric());

let empty2: [[u8; 0]; 1] = [[]];
assert!(!empty2.is_symmetric());

let empty3: [[u8; 0]; 2] = [[], []];
assert!(!empty3.is_symmetric());
source

fn is_skew_symmetric(&self) -> bool
where Self: Sized, for<'a> &'a Self::Element: Neg, for<'a> Self::Element: PartialEq<<&'a Self::Element as Neg>::Output>,

Checks if the matrix is skew-symmetric (antisymmetric).

§Example
use matrixable::MatrixExt;

let m1: [[i8; 3]; 3] = [
    [ 0, -1, -2 ],
    [ 1,  0,  5 ],
    [ 2, -5,  0 ]
];
   
assert!(!m1.is_symmetric());
assert!(m1.is_skew_symmetric());

let m2: [[i8; 3]; 3] = [
    [ 0,  1,  2 ],
    [ 1,  0,  1 ],
    [ 2,  1,  0 ]
];
   
assert!(m2.is_symmetric());
assert!(!m2.is_skew_symmetric());
source

fn is_singleton(&self) -> bool

Checks if the matrix is a singleton i.e. dimensions are equal to (1, 1).

§Examples
use matrixable::MatrixExt;

assert!([[0]].is_singleton());
assert!(![[0],[0]].is_singleton());
assert!(![[0,0]].is_singleton());

let empty: [[u8; 0]; 0] = [];
assert!(!empty.is_singleton());

let empty2: [[u8; 0]; 1] = [[]];
assert!(!empty2.is_singleton());

let empty3: [[u8; 0]; 2] = [[], []];
assert!(!empty3.is_singleton());
source

fn is_horizontal(&self) -> bool

Checks if the matrix is horizontal (number of rows of the matrix is lower than number of columns).

§Examples
use matrixable::MatrixExt;

assert!([[0]].is_horizontal());
assert!([[0,0]].is_horizontal());
assert!(![[0],[0]].is_horizontal());

let empty: [[u8; 0]; 0] = [];
assert!(empty.is_horizontal());

let empty2: [[u8; 0]; 1] = [[]];
assert!(empty2.is_horizontal());

let empty3: [[u8; 0]; 2] = [[], []];
assert!(empty3.is_horizontal());
source

fn is_vertical(&self) -> bool

Checks if the matrix is vertical (number of rows of the matrix is greater than number of columns).

§Examples
use matrixable::MatrixExt;

assert!([[0]].is_vertical());
assert!([[0],[0]].is_vertical());
assert!(![[0,0]].is_vertical());

let empty: [[u8; 0]; 0] = [];
assert!(empty.is_vertical());

let empty2: [[u8; 0]; 1] = [[]];
assert!(empty2.is_vertical());

let empty3: [[u8; 0]; 2] = [[], []];
assert!(empty3.is_vertical());
source

fn is_diagonal(&self) -> (bool, Option<&Self::Element>)
where Self: Sized, for<'a> &'a Self::Element: PartialEq,

Returns a boolean indicating if the matrix looks like a diagonal matrix (a matrix which entries outside the main diagonal are all zero), along with the reference to the element that may serve as zero in that matrix if the check was correct.

§Examples
use matrixable::MatrixExt;

let m = [
    [1, 0, 0],
    [0, 2, 0],
    [0, 0, 3]
];
assert_eq!((true, Some(&0)), m.is_diagonal());

assert_eq!((true, None), [[1]].is_diagonal());

assert_eq!((false, None), [[1],[0],[2]].is_diagonal());    
source

fn is_scalar(&self) -> (bool, Option<&Self::Element>, Option<&Self::Element>)
where Self: Sized, for<'a> &'a Self::Element: PartialEq,

Returns a boolean indicating if matrix is a square diagonal matrix having the same elements on its diagonal (assumed to be the first element of the matrix, at (0, 0)), along with that element and the element considered as zero (that is the second element of matrix, at index 0, 1).

§Examples
use matrixable::MatrixExt;

let m1 = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]
];

let mut m2 = [
    [1, 0, 0],
    [0, 2, 0],
    [0, 0, 3]
];

// rectangular matrix is not scalar...
assert_eq!([
        [1, 0, 0],
        [0, 2, 0]
    ].is_scalar(),
   (false, None, None)
);

assert_eq!(m1.is_scalar(), (false, Some(&0), Some(&0)));
assert_eq!(m2.is_scalar(), (false, Some(&1), Some(&0)));

m2[1][1] = 1;
m2[2][2] = 1;

assert_eq!(m2.is_scalar(), (true, Some(&1), Some(&0)));
source

fn is_constant(&self) -> (bool, Option<&Self::Element>)
where Self::Element: PartialEq,

Returns a boolean indicating if all elements of the matrix are equal, and that element if it the check value is true.

§Examples
use matrixable::MatrixExt;
 
let mut m = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]
];
 
assert_eq!(m.is_constant(), (true, Some(&0)));
m[0][2] = 5;

assert_eq!(m.is_constant(), (false, None));

// All elements are now equal to five.
m.iter_mut().flatten().for_each(|x| *x = 5);

assert_eq!(m.is_constant(), (true, Some(&5)));

Implementations on Foreign Types§

source§

impl<T, const N: usize, const M: usize> MatrixExt for [[T; N]; M]

§

type Element = T

source§

fn num_rows(&self) -> usize

source§

fn num_cols(&self) -> usize

source§

fn get(&self, i: usize, j: usize) -> Option<&Self::Element>

Implementors§

source§

impl MatrixExt for Observer

§

type Element = ()

source§

impl<'a, M: MatrixExt, S: AccessStrategy<M>> MatrixExt for Access<'a, M, S>

§

type Element = <M as MatrixExt>::Element

source§

impl<'a, M: MatrixMutExt, S: AccessStrategy<M>> MatrixExt for AccessMut<'a, M, S>

§

type Element = <M as MatrixExt>::Element