Struct ldpc::SparseBinMat[][src]

pub struct SparseBinMat { /* fields omitted */ }
Expand description

A sparse binary matrix optimized for row operations.

Implementations

impl SparseBinMat[src]

pub fn new(
    number_of_columns: usize,
    rows: Vec<Vec<usize, Global>, Global>
) -> SparseBinMat
[src]

Creates a new matrix with the given number of columns and list of rows .

A row is a list of the positions where the elements have value 1. All rows are sorted during insertion.

Example

let matrix = SparseBinMat::new(4, vec![vec![0, 1, 2], vec![0, 2, 3]]);

assert_eq!(matrix.number_of_rows(), 2);
assert_eq!(matrix.number_of_columns(), 4);
assert_eq!(matrix.number_of_elements(), 8);

Panic

Panics if a position in a row is greater or equal the number of columns, a row is unsorted or a row contains duplicate.

pub fn try_new(
    number_of_columns: usize,
    rows: Vec<Vec<usize, Global>, Global>
) -> Result<SparseBinMat, InvalidPositions>
[src]

Creates a new matrix with the given number of columns and list of rows or returns an error if a position in a row is greater or equal the number of columns, a row is unsorted or a row contains duplicate.

A row is a list of the positions where the elements have value 1. All rows are sorted during insertion.

Example

let first_matrix = SparseBinMat::try_new(4, vec![vec![0, 1, 2], vec![0, 2, 3]]);
let second_matrix = SparseBinMat::new(4, vec![vec![0, 1, 2], vec![0, 2, 3]]);
assert_eq!(first_matrix, Ok(second_matrix));

pub fn identity(length: usize) -> SparseBinMat[src]

Creates an identity matrix of the given length.

Example

let matrix = SparseBinMat::identity(5);

let identity_rows = (0..5).map(|x| vec![x]).collect();
let identity_matrix = SparseBinMat::new(5, identity_rows);

assert_eq!(matrix, identity_matrix);

pub fn zeros(number_of_rows: usize, number_of_columns: usize) -> SparseBinMat[src]

Creates a matrix fill with zeros of the given dimensions.

Example

let matrix = SparseBinMat::zeros(2, 3);

assert_eq!(matrix.number_of_rows(), 2);
assert_eq!(matrix.number_of_columns(), 3);
assert_eq!(matrix.number_of_zeros(), 6);
assert_eq!(matrix.number_of_ones(), 0);

pub fn empty() -> SparseBinMat[src]

Creates an empty matrix.

This allocate minimally, so it is a good placeholder.

Example

let matrix = SparseBinMat::empty();

assert_eq!(matrix.number_of_rows(), 0);
assert_eq!(matrix.number_of_columns(), 0); assert_eq!(matrix.number_of_elements(), 0);
// Note that these are not equal since new preallocate some space
// to store the data.
assert_ne!(SparseBinMat::new(0, Vec::new()), SparseBinMat::empty());

// To test for emptyness, you should prefer the following.
assert!(matrix.is_empty());

pub fn number_of_columns(&self) -> usize[src]

Returns the number of columns in the matrix.

pub fn number_of_rows(&self) -> usize[src]

Returns the number of rows in the matrix

pub fn dimension(&self) -> (usize, usize)[src]

Returns the number of rows and columns in the matrix.

pub fn number_of_elements(&self) -> usize[src]

Returns the number of elements in the matrix.

pub fn number_of_zeros(&self) -> usize[src]

Returns the number of elements with value 0 in the matrix.

pub fn number_of_ones(&self) -> usize[src]

Returns the number of elements with value 1 in the matrix.

pub fn is_empty(&self) -> bool[src]

Returns true if the number of elements in the matrix is 0.

pub fn is_zero(&self) -> bool[src]

Returns true if the all elements of the matrix are 0.

pub fn get(&self, row: usize, column: usize) -> Option<u8>[src]

Returns the value at the given row and column or None if one of the index is out of bound.

Example

let rows = vec![vec![0, 1], vec![1, 2]];
let matrix = SparseBinMat::new(3, rows);

assert_eq!(matrix.get(0, 0), Some(1));
assert_eq!(matrix.get(1, 0), Some(0));
assert_eq!(matrix.get(2, 0), None);

pub fn is_zero_at(&self, row: usize, column: usize) -> Option<bool>[src]

Returns true if the value at the given row and column is 0 or None if one of the index is out of bound.

Example

let rows = vec![vec![0, 1], vec![1, 2]];
let matrix = SparseBinMat::new(3, rows);

assert_eq!(matrix.is_zero_at(0, 0), Some(false));
assert_eq!(matrix.is_zero_at(1, 0), Some(true));
assert_eq!(matrix.is_zero_at(2, 0), None);

pub fn is_one_at(&self, row: usize, column: usize) -> Option<bool>[src]

Returns true if the value at the given row and column is 1 or None if one of the index is out of bound.

Example

let rows = vec![vec![0, 1], vec![1, 2]];
let matrix = SparseBinMat::new(3, rows);

assert_eq!(matrix.is_one_at(0, 0), Some(true));
assert_eq!(matrix.is_one_at(1, 0), Some(false));
assert_eq!(matrix.is_one_at(2, 0), None);

pub fn row(&self, row: usize) -> Option<SparseBinVecBase<&[usize]>>[src]

Returns a reference to the given row of the matrix or None if the row index is out of bound.

Example

let rows = vec![vec![0, 1], vec![1, 2]];
let matrix = SparseBinMat::new(3, rows.clone());

assert_eq!(matrix.row(0), Some(SparseBinSlice::new(3, &rows[0])));
assert_eq!(matrix.row(1), Some(SparseBinSlice::new(3, &rows[1])));
assert_eq!(matrix.row(2), None);

pub fn rows(&self) -> Rows<'_>[src]

Returns an iterator yielding the rows of the matrix as slice of non zero positions.

Example

let rows = vec![vec![0, 1, 2, 5], vec![1, 3, 4], vec![2, 4, 5], vec![0, 5]];
let matrix = SparseBinMat::new(7, rows.clone());

let mut iter = matrix.rows();

assert_eq!(iter.next(), Some(SparseBinSlice::new(7, &rows[0])));
assert_eq!(iter.next(), Some(SparseBinSlice::new(7, &rows[1])));
assert_eq!(iter.next(), Some(SparseBinSlice::new(7, &rows[2])));
assert_eq!(iter.next(), Some(SparseBinSlice::new(7, &rows[3])));
assert_eq!(iter.next(), None);

pub fn non_trivial_elements(&self) -> NonTrivialElements<'_>[src]

Returns an iterator yielding the positions of the non trivial elements.

Example

let rows = vec![vec![0, 2], vec![1], vec![0, 2], vec![1]];
let matrix = SparseBinMat::new(3, rows);

let mut iter = matrix.non_trivial_elements();

assert_eq!(iter.next(), Some((0, 0)));
assert_eq!(iter.next(), Some((0, 2)));
assert_eq!(iter.next(), Some((1, 1)));
assert_eq!(iter.next(), Some((2, 0)));
assert_eq!(iter.next(), Some((2, 2)));
assert_eq!(iter.next(), Some((3, 1)));
assert_eq!(iter.next(), None);

pub fn row_weights(&'a self) -> impl Iterator<Item = usize> + 'a[src]

Returns an iterator yielding the number of non zero elements in each row of the matrix.

Example

let rows = vec![vec![0, 1, 2, 5], vec![1, 3, 4], vec![2, 4, 5], vec![0, 5]];
let matrix = SparseBinMat::new(7, rows);

assert_eq!(matrix.row_weights().collect::<Vec<usize>>(), vec![4, 3, 3, 2]);

pub fn transposed(&self) -> SparseBinMat[src]

Gets the transposed version of the matrix by swapping the columns with the rows.

Example


let rows = vec![vec![0, 1, 2], vec![1, 3], vec![0, 2, 3]];
let matrix = SparseBinMat::new(4, rows);

let transposed_matrix = matrix.transposed();

let expected_rows = vec![vec![0, 2], vec![0, 1], vec![0, 2], vec![1, 2]];
let expected_matrix = SparseBinMat::new(3, expected_rows);

assert_eq!(transposed_matrix, expected_matrix);

pub fn rank(&self) -> usize[src]

Computes the rank of the matrix. That is, the number of linearly independent rows or columns.

Example


let rows = vec![vec![0, 1], vec![1, 2], vec![0, 2]];
let matrix = SparseBinMat::new(3, rows);

assert_eq!(matrix.rank(), 2);

pub fn echelon_form(&self) -> SparseBinMat[src]

Returns an echeloned version of the matrix.

A matrix in echelon form as the property that no rows any given row have a 1 in the first non trivial position of that row. Also, all rows are linearly independent.

Example

let matrix = SparseBinMat::new(3, vec![vec![0, 1, 2], vec![0], vec![1, 2], vec![0, 2]]);
let expected = SparseBinMat::new(3, vec![vec![0, 1, 2], vec![1], vec![2]]);
assert_eq!(matrix.echelon_form(), expected);

pub fn nullspace(&self) -> SparseBinMat[src]

Returns a matrix for which the rows are the generators of the nullspace of the original matrix.

The nullspace of a matrix M is the set of vectors N such that Mx = 0 for all x in N. Therefore, if N is the nullspace matrix obtain from this function, we have that M * N^T = 0.

Example

let matrix = SparseBinMat::new(
    6,
    vec![vec![0, 1, 3, 5], vec![2, 3, 4], vec![2, 5], vec![0, 1, 3]],
);

let expected = SparseBinMat::new(6, vec![vec![0, 3, 4,], vec![0, 1]]);
let nullspace = matrix.nullspace();

assert_eq!(nullspace, expected);
assert_eq!(&matrix * &nullspace.transposed(), SparseBinMat::zeros(4, 2));

pub fn horizontal_concat_with(&self, other: &SparseBinMat) -> SparseBinMat[src]

Returns the horizontal concatenation of two matrices.

If the matrix have different number of rows, the smallest one is padded with empty rows.

Example

let left_matrix = SparseBinMat::new(3, vec![vec![0, 1], vec![1, 2]]);
let right_matrix = SparseBinMat::new(4, vec![vec![1, 2, 3], vec![0, 1], vec![2, 3]]);

let concatened = left_matrix.horizontal_concat_with(&right_matrix);

let expected = SparseBinMat::new(7, vec![vec![0, 1, 4, 5, 6], vec![1, 2, 3, 4], vec![5, 6]]);

assert_eq!(concatened, expected);

pub fn vertical_concat_with(&self, other: &SparseBinMat) -> SparseBinMat[src]

Returns the vertical concatenation of two matrices.

If the matrix have different number of columns, the smallest one is padded with empty columns.

Example

let left_matrix = SparseBinMat::new(3, vec![vec![0, 1], vec![1, 2]]);
let right_matrix = SparseBinMat::identity(3);

let concatened = left_matrix.vertical_concat_with(&right_matrix);
let expected = SparseBinMat::new(3, vec![vec![0, 1], vec![1, 2], vec![0], vec![1], vec![2]]);

assert_eq!(concatened, expected);

pub fn dot_with_vector<T>(
    &self,
    vector: &SparseBinVecBase<T>
) -> Result<SparseBinVecBase<Vec<usize, Global>>, IncompatibleDimensions<(usize, usize), usize>> where
    T: Deref<Target = [usize]>, 
[src]

Returns the dot product between a matrix and a vector or an error if the number of columns in the matrix is not equal to the length of the vector.

Use the Mul (*) operator for a version that panics instead of returning a Result.

Example

let matrix = SparseBinMat::new(3, vec![vec![0, 1], vec![1, 2]]);
let vector = SparseBinVec::new(3, vec![0, 1]);
let result = SparseBinVec::new(2, vec![1]);

assert_eq!(matrix.dot_with_vector(&vector), Ok(result));

pub fn dot_with_matrix(
    &self,
    other: &SparseBinMat
) -> Result<SparseBinMat, IncompatibleDimensions<(usize, usize), (usize, usize)>>
[src]

Returns the dot product between two matrices or an error if the number of columns in the first matrix is not equal to the number of rows in the second matrix.

Use the Mul (*) operator for a version that panics instead of returning a Result.

Example

let matrix = SparseBinMat::new(3, vec![vec![0, 1], vec![1, 2]]);
let other_matrix = SparseBinMat::new(4, vec![vec![1], vec![2], vec![3]]);
let result = SparseBinMat::new(4, vec![vec![1, 2], vec![2, 3]]);

assert_eq!(matrix.dot_with_matrix(&other_matrix), Ok(result));

pub fn bitwise_xor_with(
    &self,
    other: &SparseBinMat
) -> Result<SparseBinMat, IncompatibleDimensions<(usize, usize), (usize, usize)>>
[src]

Returns the bitwise xor sum of two matrices or an error if the matrices have different dimensions.

Use the Add (+) operator for a version that panics instead of returning a Result.

Example

let matrix = SparseBinMat::new(3, vec![vec![0, 1], vec![1, 2]]);
let other_matrix = SparseBinMat::new(3, vec![vec![0, 1, 2], vec![0, 1, 2]]);
let result = SparseBinMat::new(3, vec![vec![2], vec![0]]);

assert_eq!(matrix.bitwise_xor_with(&other_matrix), Ok(result));

pub fn keep_only_rows(
    &self,
    rows: &[usize]
) -> Result<SparseBinMat, InvalidPositions>
[src]

Returns a new matrix keeping only the given rows or an error if rows are out of bound, unsorted or not unique.

Example

use sparse_bin_mat::SparseBinMat;
let matrix = SparseBinMat::new(5, vec![
    vec![0, 1, 2],
    vec![2, 3, 4],
    vec![0, 2, 4],
    vec![1, 3],
]);

let truncated = SparseBinMat::new(5, vec![
    vec![0, 1, 2],
    vec![0, 2, 4],
]);

assert_eq!(matrix.keep_only_rows(&[0, 2]), Ok(truncated));
assert_eq!(matrix.keep_only_rows(&[0, 2, 3]).unwrap().number_of_rows(), 3);

pub fn without_rows(
    &self,
    rows: &[usize]
) -> Result<SparseBinMat, InvalidPositions>
[src]

Returns a truncated matrix where the given rows are removed or an error if rows are out of bound or unsorted.

Example

let matrix = SparseBinMat::new(5, vec![
    vec![0, 1, 2],
    vec![2, 3, 4],
    vec![0, 2, 4],
    vec![1, 3],
]);

let truncated = SparseBinMat::new(5, vec![
    vec![2, 3, 4],
    vec![1, 3],
]);

assert_eq!(matrix.without_rows(&[0, 2]), Ok(truncated));
assert_eq!(matrix.without_rows(&[1, 2, 3]).unwrap().number_of_rows(), 1);

pub fn keep_only_columns(
    &self,
    columns: &[usize]
) -> Result<SparseBinMat, InvalidPositions>
[src]

Returns a new matrix keeping only the given columns or an error if columns are out of bound, unsorted or not unique.

Columns are relabeled to the fit new number of columns.

Example

use sparse_bin_mat::SparseBinMat;
let matrix = SparseBinMat::new(5, vec![
    vec![0, 1, 2],
    vec![2, 3, 4],
    vec![0, 2, 4],
    vec![1, 3],
]);

let truncated = SparseBinMat::new(3, vec![
    vec![0, 1],
    vec![2],
    vec![0, 2],
    vec![1],
]);

assert_eq!(matrix.keep_only_columns(&[0, 1, 4]), Ok(truncated));
assert_eq!(matrix.keep_only_columns(&[1, 2]).unwrap().number_of_columns(), 2);

pub fn without_columns(
    &self,
    columns: &[usize]
) -> Result<SparseBinMat, InvalidPositions>
[src]

Returns a truncated matrix where the given columns are removed or an error if columns are out of bound or unsorted.

Columns are relabeled to fit the new number of columns.

Example

let matrix = SparseBinMat::new(5, vec![
    vec![0, 1, 2],
    vec![2, 3, 4],
    vec![0, 2, 4],
    vec![1, 3],
]);

let truncated = SparseBinMat::new(3, vec![
    vec![0],
    vec![1, 2],
    vec![2],
    vec![0, 1],
]);

assert_eq!(matrix.without_columns(&[0, 2]), Ok(truncated));

pub fn kron_with(&self, other: &SparseBinMat) -> SparseBinMat[src]

Returns the Kronecker product of two matrices.

Example

let left_matrix = SparseBinMat::new(2, vec![vec![1], vec![0]]);
let right_matrix = SparseBinMat::new(3, vec![vec![0, 1], vec![1, 2]]);

let product = left_matrix.kron_with(&right_matrix);
let expected = SparseBinMat::new(6, vec![vec![3, 4], vec![4, 5], vec![0, 1], vec![1, 2]]);

assert_eq!(product, expected);

pub fn as_json(&self) -> Result<String, Error>[src]

Returns a json string for the matrix.

Trait Implementations

impl<'_, '_> Add<&'_ SparseBinMat> for &'_ SparseBinMat[src]

type Output = SparseBinMat

The resulting type after applying the + operator.

pub fn add(self, other: &SparseBinMat) -> SparseBinMat[src]

Performs the + operation. Read more

impl Clone for SparseBinMat[src]

pub fn clone(&self) -> SparseBinMat[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for SparseBinMat[src]

pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>[src]

Formats the value using the given formatter. Read more

impl<'de> Deserialize<'de> for SparseBinMat[src]

pub fn deserialize<D>(
    deserializer: D
) -> Result<SparseBinMat, <D as Deserializer<'de>>::Error> where
    D: Deserializer<'de>, 
[src]

Deserialize this value from the given Serde deserializer. Read more

impl Display for SparseBinMat[src]

pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>[src]

Formats the value using the given formatter. Read more

impl Hash for SparseBinMat[src]

pub fn hash<__H>(&self, state: &mut __H) where
    __H: Hasher
[src]

Feeds this value into the given Hasher. Read more

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given Hasher. Read more

impl<'_, '_> Mul<&'_ SparseBinMat> for &'_ SparseBinMat[src]

type Output = SparseBinMat

The resulting type after applying the * operator.

pub fn mul(self, other: &SparseBinMat) -> SparseBinMat[src]

Performs the * operation. Read more

impl<'_, '_, T> Mul<&'_ SparseBinVecBase<T>> for &'_ SparseBinMat where
    T: Deref<Target = [usize]>, 
[src]

type Output = SparseBinVecBase<Vec<usize, Global>>

The resulting type after applying the * operator.

pub fn mul(
    self,
    other: &SparseBinVecBase<T>
) -> SparseBinVecBase<Vec<usize, Global>>
[src]

Performs the * operation. Read more

impl PartialEq<SparseBinMat> for SparseBinMat[src]

pub fn eq(&self, other: &SparseBinMat) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

pub fn ne(&self, other: &SparseBinMat) -> bool[src]

This method tests for !=.

impl Serialize for SparseBinMat[src]

pub fn serialize<S>(
    &self,
    serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
    S: Serializer
[src]

Serialize this value into the given Serde serializer. Read more

impl Eq for SparseBinMat[src]

impl StructuralEq for SparseBinMat[src]

impl StructuralPartialEq for SparseBinMat[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<Q, K> Equivalent<K> for Q where
    K: Borrow<Q> + ?Sized,
    Q: Eq + ?Sized
[src]

pub fn equivalent(&self, key: &K) -> bool[src]

Compare self to key and return true if they are equal.

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Pointable for T

pub const ALIGN: usize

The alignment of pointer.

type Init = T

The type for initializers.

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

Initializes a with the given initializer. Read more

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

Dereferences the given pointer. Read more

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

Mutably dereferences the given pointer. Read more

pub unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more

impl<SS, SP> SupersetOf<SS> for SP where
    SS: SubsetOf<SP>, 

pub fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more

pub fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).

pub unsafe fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.

pub fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T> ToString for T where
    T: Display + ?Sized
[src]

pub default fn to_string(&self) -> String[src]

Converts the given value to a String. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

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

pub fn vzip(self) -> V

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]