Trait rest_tensors::matrix::MathMatrix
source · [−]pub trait MathMatrix<'a, T>where
Self: BasicMatrix<'a, T> + BasicMatrixOpt<'a, T>,
T: Copy + Clone,{
fn add<Q>(&'a self, other: &'a Q) -> Option<MatrixFull<T>>
where
T: Add<Output = T> + AddAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
{ ... }
fn scaled_add<Q>(
&'a self,
other: &'a Q,
scale_factor: T
) -> Option<MatrixFull<T>>
where
T: Add + AddAssign + Mul<Output = T>,
Q: BasicMatrix<'a, T>,
Self: Sized,
{ ... }
fn self_add<Q>(&'a mut self, bm: &'a Q)
where
T: Add + AddAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
{ ... }
fn self_scaled_add<Q>(&'a mut self, bm: &'a Q, b: T)
where
T: Add + AddAssign + Mul<Output = T>,
Q: BasicMatrix<'a, T>,
Self: Sized,
{ ... }
fn self_general_add<Q>(&'a mut self, bm: &'a Q, a: T, b: T)
where
T: Add<Output = T> + AddAssign + Mul<Output = T>,
Q: BasicMatrix<'a, T>,
Self: Sized,
{ ... }
fn sub<Q>(&'a self, other: &'a Q) -> Option<MatrixFull<T>>
where
T: Sub + SubAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
{ ... }
fn self_sub<Q>(&'a mut self, bm: &'a Q)
where
T: Sub + SubAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
{ ... }
fn self_multiple(&mut self, a: T)
where
T: Mul<Output = T> + MulAssign,
{ ... }
}
Provided Methods
fn add<Q>(&'a self, other: &'a Q) -> Option<MatrixFull<T>>where
T: Add<Output = T> + AddAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
fn scaled_add<Q>(&'a self, other: &'a Q, scale_factor: T) -> Option<MatrixFull<T>>where
T: Add + AddAssign + Mul<Output = T>,
Q: BasicMatrix<'a, T>,
Self: Sized,
sourcefn self_add<Q>(&'a mut self, bm: &'a Q)where
T: Add + AddAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
fn self_add<Q>(&'a mut self, bm: &'a Q)where
T: Add + AddAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
For A += B
sourcefn self_scaled_add<Q>(&'a mut self, bm: &'a Q, b: T)where
T: Add + AddAssign + Mul<Output = T>,
Q: BasicMatrix<'a, T>,
Self: Sized,
fn self_scaled_add<Q>(&'a mut self, bm: &'a Q, b: T)where
T: Add + AddAssign + Mul<Output = T>,
Q: BasicMatrix<'a, T>,
Self: Sized,
For A += c*B where c is a scale factor
sourcefn self_general_add<Q>(&'a mut self, bm: &'a Q, a: T, b: T)where
T: Add<Output = T> + AddAssign + Mul<Output = T>,
Q: BasicMatrix<'a, T>,
Self: Sized,
fn self_general_add<Q>(&'a mut self, bm: &'a Q, a: T, b: T)where
T: Add<Output = T> + AddAssign + Mul<Output = T>,
Q: BasicMatrix<'a, T>,
Self: Sized,
For aA + bB -> A
sourcefn sub<Q>(&'a self, other: &'a Q) -> Option<MatrixFull<T>>where
T: Sub + SubAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
fn sub<Q>(&'a self, other: &'a Q) -> Option<MatrixFull<T>>where
T: Sub + SubAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
For A - B -> C
sourcefn self_sub<Q>(&'a mut self, bm: &'a Q)where
T: Sub + SubAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
fn self_sub<Q>(&'a mut self, bm: &'a Q)where
T: Sub + SubAssign,
Q: BasicMatrix<'a, T>,
Self: Sized,
For A - B -> A
sourcefn self_multiple(&mut self, a: T)where
T: Mul<Output = T> + MulAssign,
fn self_multiple(&mut self, a: T)where
T: Mul<Output = T> + MulAssign,
For a*A -> A
Implementors
impl<'a, T> MathMatrix<'a, T> for SubMatrixFull<'a, T>where
T: Copy + Clone,
impl<'a, T> MathMatrix<'a, T> for SubMatrixFullMut<'a, T>where
T: Copy + Clone,
impl<'a, T> MathMatrix<'a, T> for MatrixFullSlice<'a, T>where
T: Copy + Clone,
impl<'a, T> MathMatrix<'a, T> for MatrixFullSliceMut<'a, T>where
T: Copy + Clone,
impl<'a, T> MathMatrix<'a, T> for MatrixFull<T>where
T: Copy + Clone,
more math operations for the rest package
These operators are provided by the trait of MathMatrix
.
Here, we illustrate the usage mainly using the MatrixFull
struct. You can perform these operations for MatrixFullSlice
,
MatrixFullSliceMut
, SubMatrixFull
, and SubMatrixFullMut
.
We also provide the rayon parallel version of these operators by the trait of ParMathMatrix
.
- For C = A + B, use
MathMatrix::add
andParMathMatrix::par_add
- For C = A + c*B, use
MathMatrix::scaled_add
andParMathMatrix::par_scaled_add
- For A += B, use
MathMatrix::self_add
andParMathMatrix::par_self_add
- For A += c*B, using
MathMatrix::self_scaled_add
andParMathMatrix::par_scaled_add
- For aA + bB -> A, using
MathMatrix::self_general_add
andParMathMatrix::par_self_general_add
- For C = A -B, use
MathMatrix::sub
andParMathMatrix::par_sub
- For A -= B, use
MathMatrix::self_sub
andParMathMatrix::par_self_sub
- For A *= a, use
MathMatrix::self_multiple
andParMathMatrix::par_self_multiple
Several examples are given as follow:
-
- add and self_sub
use rest_tensors::{MatrixFull, MathMatrix};
let vec_a = vec![
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0,
10.0, 11.0, 12.0];
let matr_a = MatrixFull::from_vec([3,4],vec_a).unwrap();
// | 1.0 | 4.0 | 7.0 | 10.0 |
//matr_a = | 2.0 | 5.0 | 8.0 | 11.0 |
// | 3.0 | 6.0 | 9.0 | 12.0 |
let vec_b = (13..25).map(|x| x as f64).collect::<Vec<f64>>();
let matr_b = MatrixFull::from_vec([3,4],vec_b).unwrap();
// | 13.0 | 16.0 | 19.0 | 22.0 |
//matr_b = | 14.0 | 17.0 | 20.0 | 23.0 |
// | 15.0 | 18.0 | 21.0 | 24.0 |
// matr_c = matr_a + matr_b;
let mut matr_c = MatrixFull::add(&matr_a, &matr_b).unwrap();
// | 14.0 | 20.0 | 26.0 | 32.0 |
//matr_c = | 16.0 | 22.0 | 28.0 | 34.0 |
// | 18.0 | 24.0 | 30.0 | 36.0 |
assert_eq!(matr_c[(..,3)], [32.0,34.0,36.0]);
// matr_c_ref: MatrixFullSliceMut<f64>
let mut matr_c_ref = matr_c.to_matrixfullslicemut();
// matr_c -= matr_b = matr_a
matr_c_ref.self_sub(&matr_b);
assert_eq!(matr_c, matr_a);
-
- scaled add and self_sub
use rest_tensors::{MatrixFull, MathMatrix, ParMathMatrix};
let vec_a = vec![
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0,
10.0, 11.0, 12.0];
let matr_a = MatrixFull::from_vec([3,4],vec_a).unwrap();
// | 1.0 | 4.0 | 7.0 | 10.0 |
//matr_a = | 2.0 | 5.0 | 8.0 | 11.0 |
// | 3.0 | 6.0 | 9.0 | 12.0 |
let vec_b = (13..25).map(|x| x as f64).collect::<Vec<f64>>();
let mut matr_b = MatrixFull::from_vec([3,4],vec_b).unwrap();
// | 13.0 | 16.0 | 19.0 | 22.0 |
//matr_b = | 14.0 | 17.0 | 20.0 | 23.0 |
// | 15.0 | 18.0 | 21.0 | 24.0 |
// matr_c = 1.0*matr_a + matr_b
let mut matr_c = matr_b.scaled_add(&matr_a, 1.0).unwrap();
// | 14.0 | 20.0 | 26.0 | 32.0 |
//matr_c = | 16.0 | 22.0 | 28.0 | 34.0 |
// | 18.0 | 24.0 | 30.0 | 36.0 |
assert_eq!(matr_c[(..,3)], [32.0,34.0,36.0]);
// matr_c_ref: MatrixFullSliceMut<f64>
let mut matr_c_ref = matr_c.to_matrixfullslicemut();
// matr_c -= matr_b = matr_a, using the rayon parallel version
matr_c_ref.par_self_sub(&matr_b);
assert_eq!(matr_c, matr_a);