Skip to main content

MatrixACA

Struct MatrixACA 

Source
pub struct MatrixACA<T: Scalar> { /* private fields */ }
Expand description

Adaptive Cross Approximation representation.

Stores a low-rank approximation A ~ U * diag(alpha) * V, where alpha[k] = 1/delta[k]. Implements AbstractMatrixCI for element-wise evaluation and submatrix extraction.

§Examples

use tensor4all_tcicore::{AbstractMatrixCI, MatrixACA, from_vec2d};

let m = from_vec2d(vec![
    vec![1.0_f64, 2.0, 3.0],
    vec![4.0, 5.0, 6.0],
    vec![7.0, 8.0, 9.0],
]);

// Build ACA from a starting pivot
let mut aca = MatrixACA::from_matrix_with_pivot(&m, (1, 1)).unwrap();
assert_eq!(aca.rank(), 1);

// Add more pivots
aca.add_pivot(&m, (0, 0)).unwrap();
assert_eq!(aca.rank(), 2);

// Evaluate the approximation
let approx_11 = aca.evaluate(1, 1);
assert!((approx_11 - 5.0).abs() < 1e-10);

Implementations§

Source§

impl<T: Scalar> MatrixACA<T>

Source

pub fn new(nr: usize, nc: usize) -> Self

Create an empty MatrixACA for a matrix of given size

§Examples
use tensor4all_tcicore::{AbstractMatrixCI, MatrixACA};

let aca = MatrixACA::<f64>::new(3, 4);
assert_eq!(aca.nrows(), 3);
assert_eq!(aca.ncols(), 4);
assert_eq!(aca.rank(), 0);
assert!(aca.is_empty());
Source

pub fn from_matrix_with_pivot( a: &Matrix<T>, first_pivot: (usize, usize), ) -> Result<Self>

Create a MatrixACA from a matrix with an initial pivot

Returns an error if the pivot value is zero or near-zero.

§Examples
use tensor4all_tcicore::{AbstractMatrixCI, MatrixACA, from_vec2d};

let m = from_vec2d(vec![vec![1.0_f64, 2.0], vec![3.0, 4.0]]);
let aca = MatrixACA::from_matrix_with_pivot(&m, (0, 0)).unwrap();
assert_eq!(aca.rank(), 1);
assert_eq!(aca.row_indices(), &[0]);
assert_eq!(aca.col_indices(), &[0]);
Source

pub fn npivots(&self) -> usize

Number of pivots

§Examples
use tensor4all_tcicore::{MatrixACA, from_vec2d};

let m = from_vec2d(vec![vec![1.0_f64, 2.0], vec![3.0, 4.0]]);
let mut aca = MatrixACA::from_matrix_with_pivot(&m, (0, 0)).unwrap();
assert_eq!(aca.npivots(), 1);
aca.add_pivot(&m, (1, 1)).unwrap();
assert_eq!(aca.npivots(), 2);
Source

pub fn u(&self) -> &Matrix<T>

Get reference to U matrix

§Examples
use tensor4all_tcicore::{MatrixACA, from_vec2d};

let m = from_vec2d(vec![vec![1.0_f64, 2.0], vec![3.0, 4.0]]);
let aca = MatrixACA::from_matrix_with_pivot(&m, (0, 0)).unwrap();
let u = aca.u();
assert_eq!(u.nrows(), 2); // nrows of original matrix
assert_eq!(u.ncols(), 1); // one pivot
Source

pub fn v(&self) -> &Matrix<T>

Get reference to V matrix

§Examples
use tensor4all_tcicore::{MatrixACA, from_vec2d};

let m = from_vec2d(vec![vec![1.0_f64, 2.0], vec![3.0, 4.0]]);
let aca = MatrixACA::from_matrix_with_pivot(&m, (0, 0)).unwrap();
let v = aca.v();
assert_eq!(v.nrows(), 1); // one pivot
assert_eq!(v.ncols(), 2); // ncols of original matrix
Source

pub fn alpha(&self) -> &[T]

Get reference to alpha values

§Examples
use tensor4all_tcicore::{MatrixACA, from_vec2d};

let m = from_vec2d(vec![vec![2.0_f64, 1.0], vec![1.0, 3.0]]);
let aca = MatrixACA::from_matrix_with_pivot(&m, (0, 0)).unwrap();
let alpha = aca.alpha();
assert_eq!(alpha.len(), 1);
// alpha = 1 / pivot_value = 1 / m[0,0] = 0.5
assert!((alpha[0] - 0.5).abs() < 1e-10);
Source

pub fn add_pivot_col(&mut self, a: &Matrix<T>, col_index: usize) -> Result<()>

Add a pivot column

§Examples
use tensor4all_tcicore::{MatrixACA, from_vec2d};

let m = from_vec2d(vec![vec![1.0_f64, 2.0], vec![3.0, 4.0]]);
let mut aca = MatrixACA::<f64>::new(2, 2);
aca.add_pivot_col(&m, 0).unwrap();
assert_eq!(aca.u().ncols(), 1);
Source

pub fn add_pivot_row(&mut self, a: &Matrix<T>, row_index: usize) -> Result<()>

Add a pivot row

§Examples
use tensor4all_tcicore::{MatrixACA, from_vec2d};

let m = from_vec2d(vec![vec![1.0_f64, 2.0], vec![3.0, 4.0]]);
let mut aca = MatrixACA::<f64>::new(2, 2);
aca.add_pivot_col(&m, 0).unwrap();
aca.add_pivot_row(&m, 0).unwrap();
assert_eq!(aca.npivots(), 1);
assert_eq!(aca.v().nrows(), 1);
Source

pub fn add_pivot(&mut self, a: &Matrix<T>, pivot: (usize, usize)) -> Result<()>

Add a pivot at the given position

§Examples
use tensor4all_tcicore::{AbstractMatrixCI, MatrixACA, from_vec2d};

let m = from_vec2d(vec![vec![1.0_f64, 2.0], vec![3.0, 4.0]]);
let mut aca = MatrixACA::from_matrix_with_pivot(&m, (0, 0)).unwrap();
aca.add_pivot(&m, (1, 1)).unwrap();
assert_eq!(aca.rank(), 2);
// Exact reconstruction at all positions
for i in 0..2 {
    for j in 0..2 {
        assert!((aca.evaluate(i, j) - m[[i, j]]).abs() < 1e-10);
    }
}
Source

pub fn add_best_pivot(&mut self, a: &Matrix<T>) -> Result<(usize, usize)>

Add a pivot that maximizes the error using ACA heuristic

§Examples
use tensor4all_tcicore::{AbstractMatrixCI, MatrixACA, from_vec2d};

let m = from_vec2d(vec![
    vec![1.0_f64, 2.0, 3.0],
    vec![4.0, 5.0, 6.0],
    vec![7.0, 8.0, 10.0],
]);
let mut aca = MatrixACA::<f64>::new(3, 3);
let (r, c) = aca.add_best_pivot(&m).unwrap();
assert!(r < 3);
assert!(c < 3);
assert_eq!(aca.rank(), 1);
Source

pub fn set_cols(&mut self, new_pivot_rows: &Matrix<T>, permutation: &[usize])

Set columns with new pivot rows and permutation

Updates the column indices and V matrix according to the given permutation and new pivot row data.

§Examples
use tensor4all_tcicore::{AbstractMatrixCI, MatrixACA, from_vec2d};

let m = from_vec2d(vec![vec![1.0_f64, 2.0], vec![3.0, 4.0]]);
let mut aca = MatrixACA::from_matrix_with_pivot(&m, (0, 0)).unwrap();
// Identity permutation keeps columns the same
let pivot_rows = from_vec2d(vec![vec![1.0_f64, 2.0]]);
aca.set_cols(&pivot_rows, &[0, 1]);
assert_eq!(aca.col_indices(), &[0]);
Source

pub fn set_rows(&mut self, new_pivot_cols: &Matrix<T>, permutation: &[usize])

Set rows with new pivot columns and permutation

Updates the row indices and U matrix according to the given permutation and new pivot column data.

§Examples
use tensor4all_tcicore::{AbstractMatrixCI, MatrixACA, from_vec2d};

let m = from_vec2d(vec![vec![1.0_f64, 2.0], vec![3.0, 4.0]]);
let mut aca = MatrixACA::from_matrix_with_pivot(&m, (0, 0)).unwrap();
// Identity permutation keeps rows the same
let pivot_cols = from_vec2d(vec![vec![1.0_f64], vec![3.0]]);
aca.set_rows(&pivot_cols, &[0, 1]);
assert_eq!(aca.row_indices(), &[0]);

Trait Implementations§

Source§

impl<T: Scalar> AbstractMatrixCI<T> for MatrixACA<T>

Source§

fn nrows(&self) -> usize

Number of rows in the approximated matrix Read more
Source§

fn ncols(&self) -> usize

Number of columns in the approximated matrix Read more
Source§

fn rank(&self) -> usize

Current rank of the approximation (number of pivots) Read more
Source§

fn row_indices(&self) -> &[usize]

Row indices selected as pivots (I set) Read more
Source§

fn col_indices(&self) -> &[usize]

Column indices selected as pivots (J set) Read more
Source§

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

Evaluate the approximation at position (i, j) Read more
Source§

fn submatrix(&self, rows: &[usize], cols: &[usize]) -> Matrix<T>

Get a submatrix of the approximation Read more
Source§

fn is_empty(&self) -> bool

Check if the approximation is empty (no pivots) Read more
Source§

fn row(&self, i: usize) -> Vec<T>

Get a row of the approximation Read more
Source§

fn col(&self, j: usize) -> Vec<T>

Get a column of the approximation Read more
Source§

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

Get the full approximated matrix Read more
Source§

fn available_rows(&self) -> Vec<usize>

Get available row indices (rows without pivots) Read more
Source§

fn available_cols(&self) -> Vec<usize>

Get available column indices (columns without pivots) Read more
Source§

fn local_error( &self, a: &Matrix<T>, rows: &[usize], cols: &[usize], ) -> Matrix<T>
where T: Sub<Output = T>,

Compute local error |A - CI| for given indices Read more
Source§

fn find_new_pivot(&self, a: &Matrix<T>) -> Result<((usize, usize), T)>
where T: Sub<Output = T>,

Find a new pivot that maximizes the local error Read more
Source§

fn find_new_pivot_in( &self, a: &Matrix<T>, rows: &[usize], cols: &[usize], ) -> Result<((usize, usize), T)>
where T: Sub<Output = T>,

Find a new pivot in the given row/column subsets Read more
Source§

impl<T: Clone + Scalar> Clone for MatrixACA<T>

Source§

fn clone(&self) -> MatrixACA<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 + Scalar> Debug for MatrixACA<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for MatrixACA<T>

§

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

§

impl<T> Send for MatrixACA<T>

§

impl<T> Sync for MatrixACA<T>

§

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

§

impl<T> UnsafeUnpin for MatrixACA<T>

§

impl<T> UnwindSafe for MatrixACA<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
§

impl<U> As for U

§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. 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
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

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<T> DistributionExt for T
where T: ?Sized,

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

§

impl<T> DistributionExt for T
where T: ?Sized,

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

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<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.
§

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

§

fn vzip(self) -> V

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

§

impl<T> MaybeSend for T

§

impl<T> MaybeSendSync for T

§

impl<T> MaybeSync for T