Subspace

Struct Subspace 

Source
#[repr(transparent)]
pub struct Subspace { matrix: Matrix, }
Expand description

A subspace of a vector space.

In general, a method is defined on the Subspace if it is a meaningful property of the subspace itself. Otherwise, users can dereference the subspace to gain read-only access to the underlying matrix object.

§Fields

  • matrix - A matrix in reduced row echelon, whose number of columns is the dimension of the ambient space and each row is a basis vector of the subspace.

Fields§

§matrix: Matrix

Implementations§

Source§

impl Subspace

Source

pub fn new(p: ValidPrime, dim: usize) -> Self

Source

pub fn from_matrix(matrix: Matrix) -> Self

Create a new subspace from a matrix. The matrix does not have to be in row echelon form.

Source

pub fn update_then_row_reduce<T, F: FnOnce(&mut Matrix) -> T>( &mut self, f: F, ) -> T

Run a closure on the matrix and then ensure it is row-reduced.

Source

pub fn from_bytes(p: ValidPrime, data: &mut impl Read) -> Result<Self>

Source

pub fn to_bytes(&self, buffer: &mut impl Write) -> Result<()>

Source

pub fn entire_space(p: ValidPrime, dim: usize) -> Self

Source

pub fn add_vector(&mut self, row: FpSlice<'_>) -> usize

This adds a vector to the subspace. This function assumes that the last row of the matrix is zero, i.e. the dimension of the current subspace is strictly less than the number of rows. This can be achieved by setting the number of rows to be the dimension plus one when creating the subspace.

§Returns

The new dimension of the subspace

Source

pub fn add_vectors( &mut self, rows: impl for<'a> FnMut(FpSliceMut<'a>) -> Option<()>, )

This adds some rows to the subspace

§Arguments
  • rows: A function that writes the row to be added to the given FpSliceMut. This returns None if it runs out of rows, Some(()) otherwise.
Source

pub fn add_basis_elements(&mut self, rows: impl Iterator<Item = usize>)

Source

pub fn reduce(&self, vector: FpSliceMut<'_>)

Projects a vector to a complement of the subspace. The complement is the set of vectors that have a 0 in every column where there is a pivot in matrix

Source

pub fn contains(&self, vector: FpSlice<'_>) -> bool

Source

pub fn contains_space(&self, other: &Self) -> bool

Source

pub fn dimension(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Whether the subspace is empty. This assumes the subspace is row reduced.

Source

pub fn ambient_dimension(&self) -> usize

Source

pub fn basis(&self) -> impl Iterator<Item = FpSlice<'_>>

Returns a basis of the subspace.

Source

pub fn set_to_zero(&mut self)

Sets the subspace to be the zero subspace.

Source

pub fn set_to_entire(&mut self)

Sets the subspace to be the entire subspace.

Source

pub fn iter(&self) -> impl Iterator<Item = FpSlice<'_>>

Source

pub fn iter_all_vectors(&self) -> impl Iterator<Item = FpVector> + '_

Iterate over all vectors in the subspace.

§Example
let subspace = Subspace::from_matrix(Matrix::from_vec(
    ValidPrime::new(3),
    &[vec![1, 0, 0], vec![0, 1, 2]],
));
let mut all_vectors = subspace.iter_all_vectors().map(|v| (&v).into());

assert_eq!(all_vectors.next(), Some(vec![0, 0, 0]));
assert_eq!(all_vectors.next(), Some(vec![0, 1, 2]));
assert_eq!(all_vectors.next(), Some(vec![0, 2, 1]));
assert_eq!(all_vectors.next(), Some(vec![1, 0, 0]));
assert_eq!(all_vectors.next(), Some(vec![1, 1, 2]));
assert_eq!(all_vectors.next(), Some(vec![1, 2, 1]));
assert_eq!(all_vectors.next(), Some(vec![2, 0, 0]));
assert_eq!(all_vectors.next(), Some(vec![2, 1, 2]));
assert_eq!(all_vectors.next(), Some(vec![2, 2, 1]));
assert_eq!(all_vectors.next(), None);
Source

pub fn sum(&self, other: &Self) -> Self

Methods from Deref<Target = Matrix>§

Source

pub fn to_bytes(&self, data: &mut impl Write) -> Result<()>

Source

pub fn prime(&self) -> ValidPrime

Source

pub fn rows(&self) -> usize

Gets the number of rows in the matrix.

Source

pub(crate) fn physical_rows(&self) -> usize

Gets the physical number of rows allocated (for BLAS operations).

Source

pub fn columns(&self) -> usize

Gets the number of columns in the matrix.

Source

pub(crate) fn stride(&self) -> usize

Source

pub(crate) fn data(&self) -> &[u64]

Source

pub fn pivots(&self) -> &[isize]

Source

pub fn to_vec(&self) -> Vec<Vec<u32>>


let matrix_vec = vec![vec![1, 0], vec![0, 1]];

assert_eq!(Matrix::from_vec(TWO, &matrix_vec).to_vec(), matrix_vec);
Source

pub fn is_zero(&self) -> bool

Source

pub fn row(&self, row: usize) -> FpSlice<'_>

Source

pub fn iter(&self) -> impl Iterator<Item = FpSlice<'_>>

Source

pub fn find_first_row_in_block(&self, first_column: usize) -> usize

Given a row reduced matrix, find the first row whose pivot column is after (or at) first_column.

Source

pub fn compute_quasi_inverse( &self, last_target_col: usize, first_source_col: usize, ) -> QuasiInverse

Computes the quasi-inverse of a matrix given a rref of [A|0|I], where 0 is the zero padding as usual.

§Arguments
  • last_target_col - the last column of A
  • first_source_col - the first column of I
§Example
let p = ValidPrime::new(3);
let input = [
    vec![1, 2, 1, 1, 0],
    vec![1, 0, 2, 1, 1],
    vec![2, 2, 0, 2, 1],
];

let (padded_cols, mut m) = Matrix::augmented_from_vec(p, &input);
m.row_reduce();
let qi = m.compute_quasi_inverse(input[0].len(), padded_cols);

let preimage = [vec![0, 1, 0], vec![0, 2, 2]];
assert_eq!(qi.preimage(), &Matrix::from_vec(p, &preimage));
Source

pub fn compute_image( &self, last_target_col: usize, first_source_col: usize, ) -> Subspace

Computes the quasi-inverse of a matrix given a rref of [A|0|I], where 0 is the zero padding as usual.

§Arguments
  • last_target_col - the last column of A
  • first_source_col - the first column of I
§Example
let p = ValidPrime::new(3);
let input = [
    vec![1, 2, 1, 1, 0],
    vec![1, 0, 2, 1, 1],
    vec![2, 2, 0, 2, 1],
];

let (padded_cols, mut m) = Matrix::augmented_from_vec(p, &input);
m.row_reduce();

let computed_image = m.compute_image(input[0].len(), padded_cols);

let image = [vec![1, 0, 2, 1, 1], vec![0, 1, 1, 0, 1]];
assert_eq!(*computed_image, Matrix::from_vec(p, &image));
assert_eq!(computed_image.pivots(), &vec![0, 1, -1, -1, -1]);
Source

pub fn compute_kernel(&self, first_source_column: usize) -> Subspace

Computes the kernel from an augmented matrix in rref. To compute the kernel of a matrix A, produce an augmented matrix of the form

[A | I]

An important thing to note is that the number of columns of A should be a multiple of the number of entries per limb in an FpVector, and this is often achieved by padding columns with 0. The padded length can be obtained from FpVector::padded_dimension.

After this matrix is set up, perform row reduction with Matrix::row_reduce, and then apply compute_kernel.

§Arguments
  • column_to_pivot_row - This is the list of pivots row_reduce gave you.
  • first_source_column - The column where the I part of the augmented matrix starts.
§Example
let p = ValidPrime::new(3);
let input = [
    vec![1, 2, 1, 1, 0],
    vec![1, 0, 2, 1, 1],
    vec![2, 2, 0, 2, 1],
];

let (padded_cols, mut m) = Matrix::augmented_from_vec(p, &input);
m.row_reduce();
let ker = m.compute_kernel(padded_cols);

let mut target = vec![0; 3];
assert_eq!(ker.row(0).iter().collect::<Vec<u32>>(), vec![1, 1, 2]);
Source

pub fn apply(&self, result: FpSliceMut<'_>, coeff: u32, input: FpSlice<'_>)

Applies a matrix to a vector.

§Example
let p = ValidPrime::new(7);
let input = [vec![1, 3, 6], vec![0, 3, 4]];

let m = Matrix::from_vec(p, &input);
let v = FpVector::from_slice(p, &vec![3, 1]);
let mut result = FpVector::new(p, 3);
let desired_result = FpVector::from_slice(p, &vec![3, 5, 1]);
m.apply(result.as_slice_mut(), 1, v.as_slice());
assert_eq!(result, desired_result);
Source

pub fn as_tile(&self) -> MatrixTileSlice<'_>

Source

pub fn naive_mul(&self, rhs: &Self) -> Self

Source

pub fn fast_mul_sequential(&self, other: &Self) -> Self

Source

pub fn fast_mul_sequential_order<L: LoopOrder>(&self, other: &Self) -> Self

Source

pub fn fast_mul_concurrent(&self, other: &Self) -> Self

Source

pub fn fast_mul_concurrent_blocksize<const M: usize, const N: usize>( &self, other: &Self, ) -> Self

Source

pub fn fast_mul_concurrent_blocksize_order<const M: usize, const N: usize, L: LoopOrder>( &self, other: &Self, ) -> Self

Trait Implementations§

Source§

impl Arbitrary for Subspace

Source§

type Parameters = SubspaceArbParams

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
Source§

type Strategy = BoxedStrategy<Subspace>

The type of Strategy used to generate values of type Self.
Source§

fn arbitrary_with(args: Self::Parameters) -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
§

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
Source§

impl Clone for Subspace

Source§

fn clone(&self) -> Subspace

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 Subspace

Source§

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

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

impl Deref for Subspace

Source§

type Target = Matrix

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'de> Deserialize<'de> for Subspace

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Subspace

Source§

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

§Example
let subspace = Subspace::entire_space(TWO, 3);

expect![[r#"
    [1, 0, 0]
    [0, 1, 0]
    [0, 0, 1]
"#]]
.assert_eq(&format!("{}", subspace));

assert_eq!(
    "[1, 0, 0], [0, 1, 0], [0, 0, 1]",
    &format!("{:#}", subspace)
);
Source§

impl From<Subspace> for AffineSubspace

Source§

fn from(subspace: Subspace) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Subspace

Source§

fn eq(&self, other: &Subspace) -> 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 Serialize for Subspace

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Subspace

Source§

impl StructuralPartialEq for Subspace

Auto Trait Implementations§

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
§

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

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
§

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

§

fn vzip(self) -> V

Source§

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