Matrix

Struct Matrix 

Source
pub struct Matrix<T: Send + Sync> { /* private fields */ }
Expand description

A lightweight Matrix abstraction that does almost nothing.

Implementations§

Source§

impl<T: Send + Sync> Matrix<T>

Source

pub fn from_raw_parts(values: Vec<T>, n_rows: usize) -> Self

Source

pub fn from_vecs(vecs: Vec<Vec<T>>) -> Self

Create a new Matrix from a vector of vectors

Source

pub fn nelem(&self) -> usize

Source

pub fn raw_values(&self) -> &Vec<T>

Source

pub fn raw_values_mut(&mut self) -> &mut Vec<T>

Source

pub fn rows_mut(&mut self) -> ChunksExactMut<'_, T>

Create a mutable iterator through rows

§Example
let vecs: Vec<Vec<u8>> = vec![
    vec![0, 1, 2],
    vec![3, 4, 5],
];

let mut mat = Matrix::from_vecs(vecs);

mat.rows_mut().for_each(|mut row| {
    row.iter_mut().for_each(|mut x| *x += 1 );
});

assert_eq!(mat.raw_values(), &vec![1, 2, 3, 4, 5, 6])
Source

pub fn par_rows_mut(&mut self) -> ChunksExactMut<'_, T>

Parallel version of rows_mut

Source

pub fn rows(&self) -> ChunksExact<'_, T>

Create an iterator through rows

§Example
let vecs: Vec<Vec<u8>> = vec![
    vec![0, 1, 2],
    vec![3, 4, 5],
];

let mut mat = Matrix::from_vecs(vecs);

let rowsum: Vec<u8> = mat.rows().map(|row| {
    row.iter().sum::<u8>()
})
.collect();

assert_eq!(rowsum, vec![3_u8, 12_u8])
Source

pub fn par_rows(&self) -> ChunksExact<'_, T>

Parallel version of rows

Source

pub fn implicit_transpose(self) -> ImplicitlyTransposedMatrix<T>

Does an implicit transpose by inverting coordinates.

§Notes

The matrix is not rebuild, so if you are attempting to access the transposed matrix row-wise many times, there will be a lot of cache misses. This method is best used if you plan to traverse the transposed matrix a small number of times.

§Example
use lace_utils::Shape;

let vecs: Vec<Vec<u32>> = vec![
    vec![0, 1, 2],
    vec![3, 4, 5],
    vec![6, 7, 8],
    vec![9, 10, 11],
];

let mat = Matrix::from_vecs(vecs);
let mat_t = mat.clone().implicit_transpose();

assert_eq!(mat.n_rows(), mat_t.n_cols());
assert_eq!(mat.n_cols(), mat_t.n_rows());

for i in 0..3 {
    for j in 0..3 {
        assert_eq!(mat[(i, j)], mat_t[(j, i)]);
    }
}
Source§

impl<T: Send + Sync + Clone> Matrix<T>

Source

pub fn vtile(col: Vec<T>, n_cols: usize) -> Self

Treat the input vector, col like a column vector and replicate it n_cols times.

§Example
let col: Vec<u32> = vec![0, 1, 2];

let mat = Matrix::vtile(col, 12);

assert_eq!(mat[(0, 0)], 0);
assert_eq!(mat[(0, 11)], 0);

assert_eq!(mat[(1, 0)], 1);
assert_eq!(mat[(1, 11)], 1);

assert_eq!(mat[(2, 0)], 2);
assert_eq!(mat[(2, 11)], 2);
Source

pub unsafe fn get_unchecked(&self, (i, j): (usize, usize)) -> &T

Get a reference to the item at (i, j) without bounds checks

§Safety

Behavior is undefined if (i, j) is out of bounds

Source

pub unsafe fn get_unchecked_mut(&mut self, (i, j): (usize, usize)) -> &mut T

Get a mutable reference to the item at (i, j) without bounds checks

§Safety

Behavior is undefined if (i, j) is out of bounds

Trait Implementations§

Source§

impl<T: Clone + Send + Sync> Clone for Matrix<T>

Source§

fn clone(&self) -> Matrix<T>

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<T: Debug + Send + Sync> Debug for Matrix<T>

Source§

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

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

impl<T> Index<(usize, usize)> for &Matrix<T>
where T: Send + Sync,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, (i, j): (usize, usize)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<(usize, usize)> for Matrix<T>
where T: Send + Sync,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, (i, j): (usize, usize)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: Send + Sync> Shape for &Matrix<T>

Source§

fn n_rows(&self) -> usize

Source§

fn n_cols(&self) -> usize

Source§

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

Source§

impl<T: Send + Sync> Shape for Matrix<T>

Source§

fn n_rows(&self) -> usize

Source§

fn n_cols(&self) -> usize

Source§

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

Auto Trait Implementations§

§

impl<T> Freeze for Matrix<T>

§

impl<T> RefUnwindSafe for Matrix<T>
where T: RefUnwindSafe,

§

impl<T> Send for Matrix<T>

§

impl<T> Sync for Matrix<T>

§

impl<T> Unpin for Matrix<T>
where T: Unpin,

§

impl<T> UnwindSafe for Matrix<T>
where T: UnwindSafe,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V