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>
impl<T: Scalar> MatrixACA<T>
Sourcepub fn new(nr: usize, nc: usize) -> Self
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());Sourcepub fn from_matrix_with_pivot(
a: &Matrix<T>,
first_pivot: (usize, usize),
) -> Result<Self>
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]);Sourcepub fn npivots(&self) -> usize
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);Sourcepub fn u(&self) -> &Matrix<T>
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 pivotSourcepub fn v(&self) -> &Matrix<T>
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 matrixSourcepub fn alpha(&self) -> &[T]
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);Sourcepub fn add_pivot_col(&mut self, a: &Matrix<T>, col_index: usize) -> Result<()>
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);Sourcepub fn add_pivot_row(&mut self, a: &Matrix<T>, row_index: usize) -> Result<()>
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);Sourcepub fn add_pivot(&mut self, a: &Matrix<T>, pivot: (usize, usize)) -> Result<()>
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);
}
}Sourcepub fn add_best_pivot(&mut self, a: &Matrix<T>) -> Result<(usize, usize)>
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);Sourcepub fn set_cols(&mut self, new_pivot_rows: &Matrix<T>, permutation: &[usize])
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]);Sourcepub fn set_rows(&mut self, new_pivot_cols: &Matrix<T>, permutation: &[usize])
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>
impl<T: Scalar> AbstractMatrixCI<T> for MatrixACA<T>
Source§fn row_indices(&self) -> &[usize]
fn row_indices(&self) -> &[usize]
Source§fn col_indices(&self) -> &[usize]
fn col_indices(&self) -> &[usize]
Source§fn evaluate(&self, i: usize, j: usize) -> T
fn evaluate(&self, i: usize, j: usize) -> T
Source§fn submatrix(&self, rows: &[usize], cols: &[usize]) -> Matrix<T>
fn submatrix(&self, rows: &[usize], cols: &[usize]) -> Matrix<T>
Source§fn available_rows(&self) -> Vec<usize>
fn available_rows(&self) -> Vec<usize>
Source§fn available_cols(&self) -> Vec<usize>
fn available_cols(&self) -> Vec<usize>
Source§fn local_error(
&self,
a: &Matrix<T>,
rows: &[usize],
cols: &[usize],
) -> Matrix<T>where
T: Sub<Output = T>,
fn local_error(
&self,
a: &Matrix<T>,
rows: &[usize],
cols: &[usize],
) -> Matrix<T>where
T: Sub<Output = T>,
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§
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
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 moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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