Trait matrixable::MatrixExt
source · pub trait MatrixExt {
type Element;
Show 52 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> { ... }
fn first(&self) -> Option<&Self::Element> { ... }
fn last(&self) -> Option<&Self::Element> { ... }
unsafe fn get_nth_unchecked(&self, n: usize) -> &Self::Element { ... }
fn size(&self) -> usize { ... }
fn shape(&self) -> (usize, usize) { ... }
fn dimensions(&self) -> (usize, usize) { ... }
fn num_diags(&self) -> usize { ... }
fn row_len(&self) -> usize { ... }
fn col_len(&self) -> usize { ... }
fn diag_len(&self, n: usize) -> usize { ... }
fn check(&self, i: usize, j: usize) -> bool { ... }
fn check_nth(&self, n: usize) -> bool { ... }
fn index_from(&self, subscripts: (usize, usize)) -> usize { ... }
fn subscripts_from(&self, n: usize) -> (usize, usize) { ... }
fn checked_index_from(&self, subscripts: (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 { ... }
unsafe fn row_unchecked(&self, i: usize) -> Row<'_, Self> ⓘ
where Self: Sized { ... }
fn col(&self, j: usize) -> Option<Column<'_, Self>>
where Self: Sized { ... }
unsafe fn col_unchecked(&self, j: usize) -> Column<'_, Self> ⓘ
where Self: Sized { ... }
fn diag(&self, n: usize) -> Option<Diag<'_, Self>>
where Self: Sized { ... }
unsafe fn diag_unchecked(&self, n: usize) -> 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 neighbours(&self, i: usize, j: usize) -> [Option<&Self::Element>; 8] { ... }
fn access<S: AccessStrategy<Self>>(
&self,
strategy: S,
) -> Access<'_, Self, S>
where Self: Sized { ... }
fn into_rows(self) -> IntoAxes<Self::Element> ⓘ
where Self: Sized + IntoIterator,
<Self as IntoIterator>::Item: IntoIterator<Item = Self::Element> { ... }
fn into_cols(self) -> IntoAxes<Self::Element> ⓘ
where Self: Sized + IntoIterator,
<Self as IntoIterator>::Item: IntoIterator<Item = Self::Element> { ... }
fn into_diags(self) -> IntoAxes<Self::Element> ⓘ
where Self: Sized + IntoIterator,
<Self as IntoIterator>::Item: IntoIterator<Item = Self::Element> { ... }
fn transform<S: TransformStrategy<Self>>(self, strategy: &S) -> S::Output
where Self: Sized { ... }
fn position2(
&self,
f: impl Fn(&Self::Element) -> bool,
) -> Option<(usize, usize)> { ... }
fn is_empty(&self) -> bool { ... }
fn is_square(&self) -> bool { ... }
fn is_vector(&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§
Required Methods§
sourcefn num_rows(&self) -> usize
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);
sourcefn num_cols(&self) -> usize
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);
sourcefn get(&self, row: usize, column: usize) -> Option<&Self::Element>
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§
sourceunsafe fn get_unchecked(&self, row: usize, column: usize) -> &Self::Element
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);
}
sourcefn get_nth(&self, n: usize) -> Option<&Self::Element>
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));
sourcefn first(&self) -> Option<&Self::Element>
fn first(&self) -> Option<&Self::Element>
Returns the first element of the matrix, or None
if it is empty.
sourcefn last(&self) -> Option<&Self::Element>
fn last(&self) -> Option<&Self::Element>
Returns the last element of the matrix, or None
if it is empty.
sourceunsafe fn get_nth_unchecked(&self, n: usize) -> &Self::Element
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);
}
sourcefn size(&self) -> usize
fn size(&self) -> usize
Returns the number of elements 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());
sourcefn shape(&self) -> (usize, usize)
fn shape(&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.shape());
sourcefn dimensions(&self) -> (usize, usize)
👎Deprecated since 0.6.0: please use shape
instead
fn dimensions(&self) -> (usize, usize)
shape
insteadReturns the dimensions of the matrix. It is an alias of the shape
method.
sourcefn num_diags(&self) -> usize
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());
sourcefn diag_len(&self, n: usize) -> usize
fn diag_len(&self, n: usize) -> usize
Gives the length of a diagonal.
Returns 0 if the matrix is empty or if the diagonal
indexed by n
does not exist.
§Example
use matrixable::MatrixExt;
let m = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
];
assert_eq!(1, m.diag_len(0));
assert_eq!(2, m.diag_len(1));
assert_eq!(3, m.diag_len(2));
assert_eq!(3, m.diag_len(3));
assert_eq!(2, m.diag_len(4));
assert_eq!(1, m.diag_len(5));
assert_eq!(0, m.diag_len(6));
let empty: [[u8; 0]; 1] = [[]];
assert_eq!(0, empty.diag_len(0));
sourcefn check(&self, i: usize, j: usize) -> bool
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));
sourcefn check_nth(&self, n: usize) -> bool
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));
sourcefn index_from(&self, subscripts: (usize, usize)) -> usize
fn index_from(&self, subscripts: (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)));
sourcefn subscripts_from(&self, n: usize) -> (usize, usize)
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));
sourcefn checked_index_from(&self, subscripts: (usize, usize)) -> Option<usize>
fn checked_index_from(&self, subscripts: (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)));
sourcefn checked_subscripts_from(&self, n: usize) -> Option<(usize, usize)>
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));
sourcefn iter(&self) -> Iter<'_, Self> ⓘwhere
Self: Sized,
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);
sourcefn row(&self, i: usize) -> Option<Row<'_, Self>>where
Self: Sized,
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());
sourceunsafe fn row_unchecked(&self, i: usize) -> Row<'_, Self> ⓘwhere
Self: Sized,
unsafe fn row_unchecked(&self, i: usize) -> Row<'_, Self> ⓘwhere
Self: Sized,
Returns an iterator over the elements of the i
-th row, without doing bound checking.
sourcefn col(&self, j: usize) -> Option<Column<'_, Self>>where
Self: Sized,
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());
sourceunsafe fn col_unchecked(&self, j: usize) -> Column<'_, Self> ⓘwhere
Self: Sized,
unsafe fn col_unchecked(&self, j: usize) -> Column<'_, Self> ⓘwhere
Self: Sized,
Returns an iterator over the elements of the j
-th column, without doing bound checking.
sourcefn diag(&self, n: usize) -> Option<Diag<'_, Self>>where
Self: Sized,
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());
assert!(m.diag(5).is_none());
sourceunsafe fn diag_unchecked(&self, n: usize) -> Diag<'_, Self> ⓘwhere
Self: Sized,
unsafe fn diag_unchecked(&self, n: usize) -> Diag<'_, Self> ⓘwhere
Self: Sized,
Returns an iterator over the elements of the n
-th diagonal, without doing bound checking.
sourcefn main_diag(&self) -> Diag<'_, Self> ⓘwhere
Self: Sized,
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());
sourcefn enumerate(&self) -> Enumerator<Iter<'_, Self>> ⓘwhere
Self: Sized,
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());
sourcefn rows(&self) -> Rows<'_, Self> ⓘwhere
Self: Sized,
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());
sourcefn cols(&self) -> Columns<'_, Self> ⓘwhere
Self: Sized,
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());
sourcefn diags(&self) -> Diags<'_, Self> ⓘwhere
Self: Sized,
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]
];
let mut diags = m.diags();
{
let mut first_diag = diags.next().unwrap();
assert_eq!(Some(&6), first_diag.next());
assert_eq!(None, first_diag.next());
}
{
let mut diag = diags.next().unwrap();
assert_eq!(Some(&3), diag.next());
assert_eq!(Some(&7), diag.next());
assert_eq!(None, diag.next());
}
{
let mut diag = diags.next().unwrap();
assert_eq!(Some(&0), diag.next());
assert_eq!(Some(&4), diag.next());
assert_eq!(Some(&8), diag.next());
assert_eq!(None, diag.next());
}
{
let mut diag = diags.next().unwrap();
assert_eq!(Some(&1), diag.next());
assert_eq!(Some(&5), diag.next());
assert_eq!(None, diag.next());
}
{
let mut last_diag = diags.next().unwrap();
assert_eq!(Some(&2), last_diag.next());
assert_eq!(None, last_diag.next());
}
assert!(diags.next().is_none());
sourcefn neighbours(&self, i: usize, j: usize) -> [Option<&Self::Element>; 8]
fn neighbours(&self, i: usize, j: usize) -> [Option<&Self::Element>; 8]
Returns an array of elements that are one-cell-adjacent to the hypothetic element located
at i
-th row and j
-th column.
§Examples
use matrixable::MatrixExt;
let m = [
[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
];
assert_eq!([Some(&1), Some(&2), Some(&3), Some(&6), Some(&8), Some(&11), Some(&12), Some(&13)], m.neighbours(1, 2));
assert_eq!([None, Some(&0), Some(&1), None, Some(&6), None, Some(&10), Some(&11)], m.neighbours(1, 0));
assert_eq!([Some(&8), Some(&9), None, Some(&13), None, None, None, None], m.neighbours(2, 4));
Neighbouring elements are returned from top left to bottom right as shown is the following examples:
* `@` refers to the element at (i, j)
* `-` refers to other elements of the matrix that are not be returned by the function
* Numbers followed by a `?` are out of the matrix. They will be mapped to `None`.
+---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
| - | - | - | - | - | | - | - | - | 0 | 1 | 2? | - | - | - | - | - |
+---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
| - | 0 | 1 | 2 | - | | - | - | - | 3 | @ | 4? | - | - | - | - | - |
+---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
| - | 3 | @ | 4 | - | | - | - | - | 5 | 6 | 7? | - | - | - | - | - |
+---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
| - | 5 | 6 | 7 | - | | - | - | - | - | - | | - | - | - | 0 | 1 | 2?
+---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
| - | - | - | - | - | | - | - | - | - | - | | - | - | - | 3 | @ | 4?
+---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
5? 6? 7?
§Note
It is not mandatory for @ to be at a correct position of the matrix. This allows some fancy ways of collecting elements at bottom or right.
+---+---+---+---+---+ +---+---+---+---+---+
| - | - | - | - | - | | - | - | - | - | 0 | 1? 2?
+---+---+---+---+---+ +---+---+---+---+---+
| - | - | - | - | - | | - | - | - | - | 3 | @ 4?
+---+---+---+---+---+ +---+---+---+---+---+
| - | - | - | - | - | | - | - | - | - | 5 | 6? 7?
+---+---+---+---+---+ +---+---+---+---+---+
| - | 0 | 1 | 2 | - | | - | - | - | - | - |
+---+---+---+---+---+ +---+---+---+---+---+
3? @ 4?
5? 6? 7?
sourcefn access<S: AccessStrategy<Self>>(&self, strategy: S) -> Access<'_, Self, S>where
Self: Sized,
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 AccessStrategy
s to chain.
sourcefn into_rows(self) -> IntoAxes<Self::Element> ⓘ
fn into_rows(self) -> IntoAxes<Self::Element> ⓘ
Converts a matrix into an iterator over rows of the matrix.
§Important
Struct using this method must ensure that IntoIterator
implementation is an iteration over
rows, each of which implements IntoIterator
over its elements.
This requirement is indispensable for a correct use of this method.
§Example
use matrixable::MatrixExt;
let m = [[1, 2, 3], [4, 5, 6]];
let mut rows = m.into_rows();
assert_eq!(Some(vec![1, 2, 3]), rows.next());
assert_eq!(Some(vec![4, 5, 6]), rows.next());
assert!(rows.next().is_none());
sourcefn into_cols(self) -> IntoAxes<Self::Element> ⓘ
fn into_cols(self) -> IntoAxes<Self::Element> ⓘ
Converts a matrix into an iterator over columns of the matrix.
§Important
Struct using this method must ensure that IntoIterator
implementation is an iteration over
rows, each of which implements IntoIterator
over its elements.
This requirement is indispensable for a correct use of this method.
§Example
use matrixable::MatrixExt;
let m = [[1, 2, 3], [4, 5, 6]];
let mut cols = m.into_cols();
assert_eq!(Some(vec![1, 4]), cols.next());
assert_eq!(Some(vec![2, 5]), cols.next());
assert_eq!(Some(vec![3, 6]), cols.next());
assert!(cols.next().is_none());
sourcefn into_diags(self) -> IntoAxes<Self::Element> ⓘ
fn into_diags(self) -> IntoAxes<Self::Element> ⓘ
Converts a matrix into an iterator over diagonals of the matrix.
§Important
Struct using this method must ensure that IntoIterator
implementation is an iteration over
rows, each of which implements IntoIterator
over its elements.
This requirement is indispensable for a correct use of this method.
§Example
use matrixable::MatrixExt;
let m = [
[1, 2, 3],
[4, 5, 6]
];
let mut diags = m.into_diags();
assert_eq!(Some(vec![4]), diags.next());
assert_eq!(Some(vec![1, 5]), diags.next());
assert_eq!(Some(vec![2, 6]), diags.next());
assert_eq!(Some(vec![3]), diags.next());
assert!(diags.next().is_none());
sourcefn transform<S: TransformStrategy<Self>>(self, strategy: &S) -> S::Outputwhere
Self: Sized,
fn transform<S: TransformStrategy<Self>>(self, strategy: &S) -> S::Outputwhere
Self: Sized,
Consumes the matrix an returns an output defined by a TransformStrategy
.
sourcefn position2(
&self,
f: impl Fn(&Self::Element) -> bool,
) -> Option<(usize, usize)>
fn position2( &self, f: impl Fn(&Self::Element) -> bool, ) -> Option<(usize, usize)>
Returns the subscripts of the first element of the matrix that matches the condition.
sourcefn is_empty(&self) -> bool
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());
sourcefn is_square(&self) -> bool
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());
sourcefn is_vector(&self) -> bool
fn is_vector(&self) -> bool
Checks if the matrix is a vector (number of columns is 1
or number of rows is 1
)
use matrixable::MatrixExt;
assert_eq!(true, [[0]].is_vector());
assert_eq!(true, [[0, 0]].is_vector());
assert_eq!(true, [[0], [0]].is_vector());
assert_eq!(false, [[0, 0], [0, 0]].is_vector());
let empty: [[u8; 0]; 0] = [];
assert_eq!(false, empty.is_vector());
let empty2: [[u8; 0]; 1] = [[]];
assert_eq!(false, empty2.is_vector());
let empty3: [[u8; 0]; 2] = [[], []];
assert_eq!(false, empty3.is_vector());
fn is_one_dimension(&self) -> bool
is_vector
insteadsourcefn is_symmetric(&self) -> bool
fn is_symmetric(&self) -> bool
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());
sourcefn is_skew_symmetric(&self) -> bool
fn is_skew_symmetric(&self) -> bool
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());
sourcefn is_singleton(&self) -> bool
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());
sourcefn is_horizontal(&self) -> bool
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());
sourcefn is_vertical(&self) -> bool
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());
sourcefn is_diagonal(&self) -> (bool, Option<&Self::Element>)
fn is_diagonal(&self) -> (bool, Option<&Self::Element>)
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());
sourcefn is_scalar(&self) -> (bool, Option<&Self::Element>, Option<&Self::Element>)
fn is_scalar(&self) -> (bool, Option<&Self::Element>, Option<&Self::Element>)
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)));
sourcefn is_constant(&self) -> (bool, Option<&Self::Element>)
fn is_constant(&self) -> (bool, Option<&Self::Element>)
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)));