diff --git a/Cargo.toml b/Cargo.toml index 8be5b269..522db1f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] +resolver = "2" members = [ "dense", @@ -14,4 +15,5 @@ members = [ "netlib-lapack-src", "blis", "rlst", + "proc-macro", ] diff --git a/algorithms/src/dense/norm2.rs b/algorithms/src/dense/norm2.rs index 1252211c..7a374988 100644 --- a/algorithms/src/dense/norm2.rs +++ b/algorithms/src/dense/norm2.rs @@ -41,7 +41,7 @@ mod test { use rand::prelude::*; use rand_chacha::ChaCha8Rng; use rlst_dense::rlst_col_vec; - use rlst_dense::rlst_mat; + use rlst_dense::rlst_dynamic_mat; #[test] fn test_vector_norm() { @@ -57,7 +57,7 @@ mod test { #[test] fn test_matrix_norm() { - let mut mat = rlst_mat![f64, (2, 2)]; + let mut mat = rlst_dynamic_mat![f64, (2, 2)]; mat[[0, 0]] = -1.0; mat[[1, 1]] = 0.5; diff --git a/algorithms/src/lapack/cholesky_decomp.rs b/algorithms/src/lapack/cholesky_decomp.rs index 3c169703..9146f444 100644 --- a/algorithms/src/lapack/cholesky_decomp.rs +++ b/algorithms/src/lapack/cholesky_decomp.rs @@ -90,7 +90,7 @@ where TriangularType::Upper => { // We need to translate to lower triangular let n = self.mat.shape().0; - result = rlst_dense::rlst_mat![Self::T, (n, n)]; + result = rlst_dense::rlst_dynamic_mat![Self::T, (n, n)]; for row in 0..n { for col in row..n { result[[col, row]] = self.mat[[row, col]].conj(); @@ -100,7 +100,7 @@ where TriangularType::Lower => { // Just copy over the result let n = self.mat.shape().0; - result = rlst_dense::rlst_mat![Self::T, (n, n)]; + result = rlst_dense::rlst_dynamic_mat![Self::T, (n, n)]; for col in 0..n { for row in col..n { result[[row, col]] = self.mat[[row, col]]; @@ -117,7 +117,7 @@ where TriangularType::Lower => { // We need to translate to upper triangular let n = self.mat.shape().0; - result = rlst_dense::rlst_mat![Self::T, (n, n)]; + result = rlst_dense::rlst_dynamic_mat![Self::T, (n, n)]; for col in 0..n { for row in col..n { result[[col, row]] = self.mat[[row, col]].conj(); @@ -127,7 +127,7 @@ where TriangularType::Upper => { // Just copy over the result let n = self.mat.shape().0; - result = rlst_dense::rlst_mat![Self::T, (n, n)]; + result = rlst_dense::rlst_dynamic_mat![Self::T, (n, n)]; for row in 0..n { for col in row..n { result[[row, col]] = self.mat[[row, col]]; @@ -138,9 +138,9 @@ where result } - fn solve, RS: SizeIdentifier, CS: SizeIdentifier>( + fn solve, S: SizeIdentifier>( &self, - rhs: &Matrix, + rhs: &Matrix, ) -> RlstResult // where // Matrix: TriangularSolve, @@ -196,7 +196,7 @@ mod test { paste! { #[test] fn []() { - let mut rlst_mat = rlst_dense::rlst_mat![$scalar, (2, 2)]; + let mut rlst_mat = rlst_dense::rlst_dynamic_mat![$scalar, (2, 2)]; rlst_mat.fill_from_seed_equally_distributed(0); rlst_mat[[1, 0]] = rlst_mat[[0, 1]].conj(); @@ -223,7 +223,7 @@ mod test { #[test] fn []() { - let mut rlst_mat = rlst_dense::rlst_mat![$scalar, (2, 2)]; + let mut rlst_mat = rlst_dense::rlst_dynamic_mat![$scalar, (2, 2)]; let mut rlst_vec = rlst_dense::rlst_col_vec![$scalar, 2]; rlst_mat.fill_from_seed_equally_distributed(0); diff --git a/algorithms/src/lapack/evd.rs b/algorithms/src/lapack/evd.rs index 1b2df6c0..3fd40346 100644 --- a/algorithms/src/lapack/evd.rs +++ b/algorithms/src/lapack/evd.rs @@ -82,15 +82,15 @@ macro_rules! implement_evd_real { match mode { EigenvectorMode::All => { jobvr = b'V'; - vr_matrix = Some(rlst_dense::rlst_mat![$scalar, (m, m)]); + vr_matrix = Some(rlst_dense::rlst_dynamic_mat![$scalar, (m, m)]); vr_data = vr_matrix.as_mut().unwrap().data_mut(); jobvl = b'V'; - vl_matrix = Some(rlst_dense::rlst_mat![$scalar, (m, m)]); + vl_matrix = Some(rlst_dense::rlst_dynamic_mat![$scalar, (m, m)]); vl_data = vl_matrix.as_mut().unwrap().data_mut(); } EigenvectorMode::Right => { jobvr = b'V'; - vr_matrix = Some(rlst_dense::rlst_mat![$scalar, (m, m)]); + vr_matrix = Some(rlst_dense::rlst_dynamic_mat![$scalar, (m, m)]); vr_data = vr_matrix.as_mut().unwrap().data_mut(); jobvl = b'N'; vl_matrix = None; @@ -98,7 +98,7 @@ macro_rules! implement_evd_real { } EigenvectorMode::Left => { jobvl = b'V'; - vl_matrix = Some(rlst_dense::rlst_mat![$scalar, (m, m)]); + vl_matrix = Some(rlst_dense::rlst_dynamic_mat![$scalar, (m, m)]); vl_data = vl_matrix.as_mut().unwrap().data_mut(); jobvr = b'N'; vr_matrix = None; @@ -141,8 +141,10 @@ macro_rules! implement_evd_real { .map(|(&re, &im)| <<$scalar as Scalar>::Complex>::new(re, im)) .collect::::Complex>>(); if jobvl == b'V' { - vl_complex = - Some(rlst_dense::rlst_mat![<$scalar as Scalar>::Complex, (m, m)]); + vl_complex = Some(rlst_dense::rlst_dynamic_mat![ + <$scalar as Scalar>::Complex, + (m, m) + ]); convert_eigvecs_to_complex( n, &eigenvalues, @@ -151,8 +153,10 @@ macro_rules! implement_evd_real { ); } if jobvr == b'V' { - vr_complex = - Some(rlst_dense::rlst_mat![<$scalar as Scalar>::Complex, (m, m)]); + vr_complex = Some(rlst_dense::rlst_dynamic_mat![ + <$scalar as Scalar>::Complex, + (m, m) + ]); convert_eigvecs_to_complex( n, &eigenvalues, @@ -210,15 +214,15 @@ macro_rules! implement_evd_complex { match mode { EigenvectorMode::All => { jobvr = b'V'; - vr_matrix = Some(rlst_dense::rlst_mat![$scalar, (m, m)]); + vr_matrix = Some(rlst_dense::rlst_dynamic_mat![$scalar, (m, m)]); vr_data = vr_matrix.as_mut().unwrap().data_mut(); jobvl = b'V'; - vl_matrix = Some(rlst_dense::rlst_mat![$scalar, (m, m)]); + vl_matrix = Some(rlst_dense::rlst_dynamic_mat![$scalar, (m, m)]); vl_data = vl_matrix.as_mut().unwrap().data_mut(); } EigenvectorMode::Right => { jobvr = b'V'; - vr_matrix = Some(rlst_dense::rlst_mat![$scalar, (m, m)]); + vr_matrix = Some(rlst_dense::rlst_dynamic_mat![$scalar, (m, m)]); vr_data = vr_matrix.as_mut().unwrap().data_mut(); jobvl = b'N'; vl_matrix = None; @@ -226,7 +230,7 @@ macro_rules! implement_evd_complex { } EigenvectorMode::Left => { jobvl = b'V'; - vl_matrix = Some(rlst_dense::rlst_mat![$scalar, (m, m)]); + vl_matrix = Some(rlst_dense::rlst_dynamic_mat![$scalar, (m, m)]); vl_data = vl_matrix.as_mut().unwrap().data_mut(); jobvr = b'N'; vr_matrix = None; @@ -313,7 +317,7 @@ macro_rules! implement_sym_evd { 0 => { let eigvec_opt; if jobv == b'V' { - let mut eigvecs = rlst_dense::rlst_mat![$scalar, (m, m)]; + let mut eigvecs = rlst_dense::rlst_dynamic_mat![$scalar, (m, m)]; for col in 0..n { for row in 0..n { eigvecs[[row, col]] = mat.get_value(row, col).unwrap(); @@ -357,7 +361,7 @@ mod test { paste! { #[test] pub fn []() { - let mut rlst_mat = rlst_dense::rlst_mat![$scalar, (2, 2)]; + let mut rlst_mat = rlst_dense::rlst_dynamic_mat![$scalar, (2, 2)]; rlst_mat.fill_from_seed_equally_distributed(0); let (eigvals, right, left) = @@ -365,7 +369,7 @@ mod test { let left = left.unwrap().conj().transpose().eval(); let right = right.unwrap(); - let mut diag = rlst_dense::rlst_mat![<$scalar as Scalar>::Complex, (2, 2)]; + let mut diag = rlst_dense::rlst_dynamic_mat![<$scalar as Scalar>::Complex, (2, 2)]; diag.set_diag_from_slice(eigvals.as_slice()); let rlst_mat = rlst_mat.to_complex().eval(); @@ -388,7 +392,7 @@ mod test { } #[test] pub fn []() { - let mut mat = rlst_dense::rlst_mat![$scalar, (3, 3)]; + let mut mat = rlst_dense::rlst_dynamic_mat![$scalar, (3, 3)]; mat.fill_from_seed_equally_distributed(0); let mat = (mat.view() + mat.view().conj().transpose()).eval(); @@ -396,7 +400,7 @@ mod test { let (eigvals, eigvecs) = mat.linalg().sym_evd(EigenvectorMode::All).unwrap(); let eigvecs = eigvecs.unwrap(); - let mut diag = rlst_dense::rlst_mat![<$scalar as Scalar>::Real, (3, 3)]; + let mut diag = rlst_dense::rlst_dynamic_mat![<$scalar as Scalar>::Real, (3, 3)]; diag.set_diag_from_iter(eigvals.iter().copied()); // Convert all to complex as otherwise @@ -417,7 +421,7 @@ mod test { #[test] fn test_complex_conjugate_pair() { - let mut mat = rlst_dense::rlst_mat![f64, (3, 3)]; + let mut mat = rlst_dense::rlst_dynamic_mat![f64, (3, 3)]; mat[[0, 0]] = 1.0; mat[[0, 1]] = 1.0; diff --git a/algorithms/src/lapack/inverse.rs b/algorithms/src/lapack/inverse.rs index 08316760..aed90f18 100644 --- a/algorithms/src/lapack/inverse.rs +++ b/algorithms/src/lapack/inverse.rs @@ -76,7 +76,7 @@ mod test { paste! { #[test] fn []() { - let mut rlst_mat = rlst_dense::rlst_mat![$scalar, (2, 2)]; + let mut rlst_mat = rlst_dense::rlst_dynamic_mat![$scalar, (2, 2)]; rlst_mat.fill_from_seed_equally_distributed(0); diff --git a/algorithms/src/lapack/lu_decomp.rs b/algorithms/src/lapack/lu_decomp.rs index 529e9960..de0feb39 100644 --- a/algorithms/src/lapack/lu_decomp.rs +++ b/algorithms/src/lapack/lu_decomp.rs @@ -7,7 +7,7 @@ use crate::traits::lu_decomp::{LUDecomp, LU}; use lapacke; use num::traits::One; use rlst_common::types::{c32, c64, RlstError, RlstResult, Scalar}; -use rlst_dense::{rlst_mat, traits::*, MatrixD}; +use rlst_dense::{rlst_dynamic_mat, traits::*, MatrixD}; use crate::linalg::DenseMatrixLinAlgBuilder; use crate::traits::types::*; @@ -72,7 +72,7 @@ macro_rules! lu_decomp_impl { fn get_l(&self) -> MatrixD { let shape = self.shape(); let dim = std::cmp::min(shape.0, shape.1); - let mut mat = rlst_mat!(Self::T, (shape.0, dim)); + let mut mat = rlst_dynamic_mat!(Self::T, (shape.0, dim)); for col in 0..dim { for row in (1 + col)..shape.0 { @@ -100,7 +100,7 @@ macro_rules! lu_decomp_impl { fn get_u(&self) -> MatrixD { let shape = self.shape(); let dim = std::cmp::min(shape.0, shape.1); - let mut mat = rlst_mat!(Self::T, (dim, shape.1)); + let mut mat = rlst_dynamic_mat!(Self::T, (dim, shape.1)); for row in 0..dim { for col in row..shape.1 { @@ -136,7 +136,7 @@ macro_rules! lu_decomp_impl { return Err(RlstError::MatrixIsEmpty(rhs.shape())); } - let mut sol = rlst_mat![$scalar, rhs.shape()]; + let mut sol = rlst_dynamic_mat![$scalar, rhs.shape()]; for col_index in 0..rhs.shape().1 { for row_index in 0..rhs.shape().0 { @@ -190,7 +190,7 @@ mod test { #[test] fn test_lu_solve_f64() { - let mut rlst_mat = rlst_dense::rlst_mat![f64, (2, 2)]; + let mut rlst_mat = rlst_dense::rlst_dynamic_mat![f64, (2, 2)]; let mut rlst_vec = rlst_dense::rlst_col_vec![f64, 2]; rlst_mat[[0, 0]] = 1.0; @@ -218,7 +218,7 @@ mod test { fn test_thin_lu_decomp() { let mut rng = ChaCha8Rng::seed_from_u64(0); - let mut mat = rlst_mat!(f64, (10, 6)); + let mut mat = rlst_dynamic_mat!(f64, (10, 6)); mat.fill_from_standard_normal(&mut rng); @@ -243,7 +243,7 @@ mod test { fn test_thick_lu_decomp() { let mut rng = ChaCha8Rng::seed_from_u64(0); - let mut mat = rlst_mat!(f64, (6, 10)); + let mut mat = rlst_dynamic_mat!(f64, (6, 10)); mat.fill_from_standard_normal(&mut rng); diff --git a/algorithms/src/lapack/qr_decomp.rs b/algorithms/src/lapack/qr_decomp.rs index a6d0d4d3..35e166cb 100644 --- a/algorithms/src/lapack/qr_decomp.rs +++ b/algorithms/src/lapack/qr_decomp.rs @@ -5,7 +5,7 @@ use num::Zero; #[allow(unused_imports)] use rlst_common::traits::{Copy, Identity, PermuteColumns}; use rlst_common::types::{c32, c64, RlstError, RlstResult, Scalar}; -use rlst_dense::{rlst_mat, MatrixD}; +use rlst_dense::{rlst_dynamic_mat, MatrixD}; use rlst_dense::{RandomAccessByValue, RawAccess, RawAccessMut, Shape, Stride}; use super::DenseMatrixLinAlgBuilder; @@ -118,7 +118,7 @@ macro_rules! qr_decomp_impl { let nrhs = rhs.shape().1; // Create the rhs with the right dimension for any case. - let mut work_rhs = rlst_mat![Self::T, (ldb, nrhs)]; + let mut work_rhs = rlst_dynamic_mat![Self::T, (ldb, nrhs)]; // Copy rhs into work_rhs for col_index in 0..nrhs { @@ -153,7 +153,7 @@ macro_rules! qr_decomp_impl { TransposeMode::ConjugateTrans => m, } as usize; - let mut sol = rlst_mat![Self::T, (x_rows, nrhs)]; + let mut sol = rlst_dynamic_mat![Self::T, (x_rows, nrhs)]; // Copy solution back @@ -215,7 +215,7 @@ macro_rules! qr_decomp_impl { fn r(&self) -> RlstResult> { let shape = self.mat.shape(); let dim = std::cmp::min(shape.0, shape.1); - let mut mat = rlst_mat!(Self::T, (dim, shape.1)); + let mut mat = rlst_dynamic_mat!(Self::T, (dim, shape.1)); for row in 0..dim { for col in row..shape.1 { @@ -252,7 +252,7 @@ mod test { use super::*; use crate::traits::lu_decomp::LUDecomp; - use rlst_dense::{rlst_col_vec, rlst_mat, rlst_rand_col_vec, rlst_rand_mat, Dot}; + use rlst_dense::{rlst_col_vec, rlst_dynamic_mat, rlst_rand_col_vec, rlst_rand_mat, Dot}; macro_rules! qr_tests { ($ScalarType:ident, $PivotMode:expr, $trans:expr, $tol:expr) => { @@ -261,7 +261,7 @@ mod test { fn []() { // QR Decomposition of a thick matrix. - let mut mat = rlst_mat![$ScalarType, (3, 5)]; + let mut mat = rlst_dynamic_mat![$ScalarType, (3, 5)]; mat.fill_from_seed_equally_distributed(0); let pivot_mode; @@ -308,7 +308,7 @@ mod test { fn []() { // QR Decomposition of a thin matrix. - let mut mat = rlst_mat![$ScalarType, (5, 3)]; + let mut mat = rlst_dynamic_mat![$ScalarType, (5, 3)]; mat.fill_from_seed_equally_distributed(0); let pivot_mode; @@ -364,10 +364,10 @@ mod test { fn []() { // Test notrans - let mut mat = rlst_mat![$ScalarType, (5, 3)]; + let mut mat = rlst_dynamic_mat![$ScalarType, (5, 3)]; mat.fill_from_seed_equally_distributed(0); - let mut rhs = rlst_mat![$ScalarType, (5, 2)]; + let mut rhs = rlst_dynamic_mat![$ScalarType, (5, 2)]; rhs.fill_from_seed_equally_distributed(2); let normal_lhs = mat.view().conj().transpose().eval().dot(&mat); @@ -401,7 +401,7 @@ mod test { #[test] fn []() { - let mut mat = rlst_mat![$ScalarType, (3, 5)]; + let mut mat = rlst_dynamic_mat![$ScalarType, (3, 5)]; mat.fill_from_seed_equally_distributed(0); let mut rhs = rlst_col_vec![$ScalarType, 3]; diff --git a/algorithms/src/lapack/svd.rs b/algorithms/src/lapack/svd.rs index 3a7674a5..e925d40b 100644 --- a/algorithms/src/lapack/svd.rs +++ b/algorithms/src/lapack/svd.rs @@ -6,7 +6,7 @@ use lapacke; use num::traits::Zero; use rlst_common::traits::*; use rlst_common::types::{c32, c64, RlstError, RlstResult, Scalar}; -use rlst_dense::{rlst_mat, MatrixD}; +use rlst_dense::{rlst_dynamic_mat, MatrixD}; macro_rules! implement_svd { ($scalar:ty, $lapack_gesvd:ident) => { @@ -49,13 +49,13 @@ macro_rules! implement_svd { match u_mode { Mode::All => { jobu = b'A'; - u_matrix = Some(rlst_mat![$scalar, (m as usize, m as usize)]); + u_matrix = Some(rlst_dynamic_mat![$scalar, (m as usize, m as usize)]); u_data = u_matrix.as_mut().unwrap().data_mut(); ldu = m as i32; } Mode::Slim => { jobu = b'S'; - u_matrix = Some(rlst_mat![$scalar, (m as usize, k as usize)]); + u_matrix = Some(rlst_dynamic_mat![$scalar, (m as usize, k as usize)]); u_data = u_matrix.as_mut().unwrap().data_mut(); ldu = m as i32; } @@ -70,13 +70,13 @@ macro_rules! implement_svd { match vt_mode { Mode::All => { jobvt = b'A'; - vt_matrix = Some(rlst_mat![$scalar, (n as usize, n as usize)]); + vt_matrix = Some(rlst_dynamic_mat![$scalar, (n as usize, n as usize)]); vt_data = vt_matrix.as_mut().unwrap().data_mut(); ldvt = n as i32; } Mode::Slim => { jobvt = b'S'; - vt_matrix = Some(rlst_mat![$scalar, (k as usize, n as usize)]); + vt_matrix = Some(rlst_dynamic_mat![$scalar, (k as usize, n as usize)]); vt_data = vt_matrix.as_mut().unwrap().data_mut(); ldvt = k as i32; } @@ -137,7 +137,7 @@ mod test { let m = 3; let n = 4; let k = std::cmp::min(m, n); - let mut mat = rlst_mat!(f64, (m, n)); + let mut mat = rlst_dynamic_mat!(f64, (m, n)); mat.fill_from_equally_distributed(&mut rng); let expected = mat.copy(); @@ -148,7 +148,7 @@ mod test { let u_matrix = u_matrix.unwrap(); let vt_matrix = vt_matrix.unwrap(); - let mut sigma = rlst_mat!(f64, (k, k)); + let mut sigma = rlst_dynamic_mat!(f64, (k, k)); for index in 0..k { sigma[[index, index]] = singular_values[index]; } @@ -169,7 +169,7 @@ mod test { let m = 4; let n = 3; let k = std::cmp::min(m, n); - let mut mat = rlst_mat!(f64, (m, n)); + let mut mat = rlst_dynamic_mat!(f64, (m, n)); mat.fill_from_equally_distributed(&mut rng); let expected = mat.copy(); @@ -180,7 +180,7 @@ mod test { let u_matrix = u_matrix.unwrap(); let vt_matrix = vt_matrix.unwrap(); - let mut sigma = rlst_mat!(f64, (k, k)); + let mut sigma = rlst_dynamic_mat!(f64, (k, k)); for index in 0..k { sigma[[index, index]] = singular_values[index]; } @@ -201,7 +201,7 @@ mod test { let m = 5; let n = 5; let k = std::cmp::min(m, n); - let mut mat = rlst_mat!(f64, (m, n)); + let mut mat = rlst_dynamic_mat!(f64, (m, n)); mat.fill_from_equally_distributed(&mut rng); let expected = mat.copy(); @@ -212,7 +212,7 @@ mod test { let u_matrix = u_matrix.unwrap(); let vt_matrix = vt_matrix.unwrap(); - let mut sigma = rlst_mat!(f64, (k, k)); + let mut sigma = rlst_dynamic_mat!(f64, (k, k)); for index in 0..k { sigma[[index, index]] = singular_values[index]; } diff --git a/algorithms/src/lapack/triangular_solve.rs b/algorithms/src/lapack/triangular_solve.rs index 65602ea8..408d0fa9 100644 --- a/algorithms/src/lapack/triangular_solve.rs +++ b/algorithms/src/lapack/triangular_solve.rs @@ -18,13 +18,9 @@ macro_rules! trisolve_real_impl { ($scalar:ty, $lapack_trisolve:ident) => { impl TriangularSolve for DenseMatrixLinAlgBuilder<$scalar> { type T = $scalar; - fn triangular_solve< - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait, - >( + fn triangular_solve>( &self, - rhs: &Matrix, + rhs: &Matrix, tritype: TriangularType, tridiag: TriangularDiagonal, trans: TransposeMode, @@ -89,13 +85,9 @@ macro_rules! trisolve_complex_impl { ($scalar:ty, $lapack_trisolve:ident) => { impl TriangularSolve for DenseMatrixLinAlgBuilder<$scalar> { type T = $scalar; - fn triangular_solve< - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait, - >( + fn triangular_solve>( &self, - rhs: &Matrix, + rhs: &Matrix, tritype: TriangularType, tridiag: TriangularDiagonal, trans: TransposeMode, @@ -161,7 +153,7 @@ trisolve_complex_impl!(c64, ztrtrs); #[cfg(test)] mod test { use num::Zero; - use rlst_dense::{rlst_col_vec, rlst_mat, Dot}; + use rlst_dense::{rlst_col_vec, rlst_dynamic_mat, Dot}; use super::*; use crate::linalg::LinAlg; @@ -171,7 +163,7 @@ mod test { ($scalar:ty, $name:ident, $tol:expr) => { #[test] fn $name() { - let mut mat_a = rlst_mat![$scalar, (4, 4)]; + let mut mat_a = rlst_dynamic_mat![$scalar, (4, 4)]; mat_a.fill_from_seed_equally_distributed(0); for row in 0..mat_a.shape().0 { for col in 0..row { diff --git a/algorithms/src/linalg.rs b/algorithms/src/linalg.rs index 981385d2..5061e6eb 100644 --- a/algorithms/src/linalg.rs +++ b/algorithms/src/linalg.rs @@ -24,8 +24,8 @@ pub struct SparseMatrixLinalgBuilder<'a, T: Scalar, Mat> { _marker: std::marker::PhantomData, } -impl, RS: SizeIdentifier, CS: SizeIdentifier> LinAlg - for Matrix +impl, S: SizeIdentifier> LinAlg + for Matrix { type T = T; type Out<'a> = DenseMatrixLinAlgBuilder where Self: 'a; diff --git a/algorithms/src/sparse/lu_umfpack.rs b/algorithms/src/sparse/lu_umfpack.rs index f0f2e464..da0ea87c 100644 --- a/algorithms/src/sparse/lu_umfpack.rs +++ b/algorithms/src/sparse/lu_umfpack.rs @@ -8,7 +8,7 @@ use crate::{ }, }; use rlst_common::types::{c64, RlstError, Scalar}; -use rlst_dense::{rlst_mat, MatrixD, RawAccessMut, Shape}; +use rlst_dense::{rlst_dynamic_mat, MatrixD, RawAccessMut, Shape}; use rlst_sparse::sparse::{csc_mat::CscMatrix, csr_mat::CsrMatrix}; use rlst_umfpack as umfpack; use std::ffi::c_void; @@ -221,7 +221,7 @@ impl LUDecomp for UmfpackLu { let n = self.shape().0; - let mut sol = rlst_mat![f64, rhs.shape()]; + let mut sol = rlst_dynamic_mat![f64, rhs.shape()]; let mut b = vec![0.0; n]; for col_index in 0..sol.shape().1 { @@ -297,7 +297,7 @@ impl LUDecomp for UmfpackLu { let n = self.shape().0; - let mut sol = rlst_mat![c64, rhs.shape()]; + let mut sol = rlst_dynamic_mat![c64, rhs.shape()]; let mut b = vec![c64::new(0.0, 0.0); n]; for col_index in 0..sol.shape().1 { diff --git a/algorithms/src/traits/cholesky_decomp.rs b/algorithms/src/traits/cholesky_decomp.rs index f2d56d3b..22db1613 100644 --- a/algorithms/src/traits/cholesky_decomp.rs +++ b/algorithms/src/traits/cholesky_decomp.rs @@ -25,9 +25,9 @@ pub trait CholeskyDecomp { fn shape(&self) -> (usize, usize); /// Solve for a right-hand side. - fn solve, RS: SizeIdentifier, CS: SizeIdentifier>( + fn solve, S: SizeIdentifier>( &self, - rhs: &Matrix, + rhs: &Matrix, ) -> RlstResult; } diff --git a/algorithms/src/traits/triangular_solve.rs b/algorithms/src/traits/triangular_solve.rs index f6dd720c..d32147bb 100644 --- a/algorithms/src/traits/triangular_solve.rs +++ b/algorithms/src/traits/triangular_solve.rs @@ -5,13 +5,9 @@ use rlst_dense::{Matrix, MatrixD, MatrixImplTrait, SizeIdentifier}; pub trait TriangularSolve { type T: Scalar; - fn triangular_solve< - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait, - >( + fn triangular_solve>( &self, - rhs: &Matrix, + rhs: &Matrix, tritype: TriangularType, tridiag: TriangularDiagonal, trans: TransposeMode, diff --git a/dense/Cargo.toml b/dense/Cargo.toml index f30e897a..f1b91bce 100644 --- a/dense/Cargo.toml +++ b/dense/Cargo.toml @@ -30,8 +30,6 @@ rlst-common = {path = "../common"} paste = "1" rand_chacha = "0.3" - - [dev-dependencies] criterion = { version = "0.3", features = ["html_reports"] } diff --git a/dense/src/base_matrix.rs b/dense/src/base_matrix.rs index b005e551..85da02df 100644 --- a/dense/src/base_matrix.rs +++ b/dense/src/base_matrix.rs @@ -13,21 +13,13 @@ use crate::types::Scalar; use crate::{traits::*, DefaultLayout}; use std::marker::PhantomData; -pub struct BaseMatrix< - Item: Scalar, - Data: DataContainer, - RS: SizeIdentifier, - CS: SizeIdentifier, -> { +pub struct BaseMatrix, S: SizeIdentifier> { data: Data, layout: DefaultLayout, - phantom_r: PhantomData, - phantom_c: PhantomData, + phantom_s: PhantomData, } -impl, RS: SizeIdentifier, CS: SizeIdentifier> - BaseMatrix -{ +impl, S: SizeIdentifier> BaseMatrix { pub fn new(data: Data, layout: DefaultLayout) -> Self { assert!( layout.number_of_elements() <= data.number_of_elements(), @@ -35,17 +27,16 @@ impl, RS: SizeIdentifier, CS: Siz data.number_of_elements(), layout.number_of_elements(), ); - BaseMatrix:: { + BaseMatrix:: { data, layout, - phantom_r: PhantomData, - phantom_c: PhantomData, + phantom_s: PhantomData, } } } -impl, RS: SizeIdentifier, CS: SizeIdentifier> - RawAccess for BaseMatrix +impl, S: SizeIdentifier> RawAccess + for BaseMatrix { type T = Item; @@ -65,8 +56,8 @@ impl, RS: SizeIdentifier, CS: Siz } } -impl, RS: SizeIdentifier, CS: SizeIdentifier> - RawAccessMut for BaseMatrix +impl, S: SizeIdentifier> RawAccessMut + for BaseMatrix { fn get_pointer_mut(&mut self) -> *mut Item { self.data.get_pointer_mut() @@ -81,8 +72,8 @@ impl, RS: SizeIdentifier, CS: } } -impl, RS: SizeIdentifier, CS: SizeIdentifier> Layout - for BaseMatrix +impl, S: SizeIdentifier> Layout + for BaseMatrix { type Impl = DefaultLayout; @@ -92,21 +83,20 @@ impl, RS: SizeIdentifier, CS: Siz } } -impl, RS: SizeIdentifier, CS: SizeIdentifier> - MatrixImplIdentifier for BaseMatrix +impl, S: SizeIdentifier> MatrixImplIdentifier + for BaseMatrix { const MAT_IMPL: MatrixImplType = MatrixImplType::Base; } -impl, RS: SizeIdentifier, CS: SizeIdentifier> - SizeType for BaseMatrix +impl, S: SizeIdentifier> Size + for BaseMatrix { - type R = RS; - type C = CS; + type S = S; } -impl, RS: SizeIdentifier, CS: SizeIdentifier> - UnsafeRandomAccessByValue for BaseMatrix +impl, S: SizeIdentifier> UnsafeRandomAccessByValue + for BaseMatrix { type Item = Item; @@ -123,8 +113,8 @@ impl, RS: SizeIdentifier, CS: Siz } } -impl, RS: SizeIdentifier, CS: SizeIdentifier> - UnsafeRandomAccessByRef for BaseMatrix +impl, S: SizeIdentifier> UnsafeRandomAccessByRef + for BaseMatrix { type Item = Item; @@ -140,8 +130,8 @@ impl, RS: SizeIdentifier, CS: Siz } } -impl, RS: SizeIdentifier, CS: SizeIdentifier> - UnsafeRandomAccessMut for BaseMatrix +impl, S: SizeIdentifier> UnsafeRandomAccessMut + for BaseMatrix { type Item = Item; diff --git a/dense/src/examples.rs b/dense/src/examples.rs index 6d3e4b15..49670816 100644 --- a/dense/src/examples.rs +++ b/dense/src/examples.rs @@ -7,7 +7,7 @@ //! //! ``` //! # use rlst_dense::*; -//! let mut mat = rlst_mat![f64, (3, 5)]; +//! let mut mat = rlst_dynamic_mat![f64, (3, 5)]; //! ``` //! The result is a column-major matrix. //! A new column vector can be created by @@ -31,7 +31,7 @@ //! ``` //! # use rlst_dense::*; //! let mut rng = rand::thread_rng(); -//! let mut mat = rlst_mat![f64, (3, 5)]; +//! let mut mat = rlst_dynamic_mat![f64, (3, 5)]; //! mat.fill_from_standard_normal(&mut rng); //! ``` //! # Accessing entries @@ -40,7 +40,7 @@ //! with bounds checks in the following way. //! ``` //! # use rlst_dense::*; -//! let mut mat = rlst_mat![f64, (3, 5)]; +//! let mut mat = rlst_dynamic_mat![f64, (3, 5)]; //! mat[[2, 3]] = 4.0; //! assert_eq!(mat[[2, 3]], 4.0); //! ``` @@ -50,20 +50,20 @@ //! an exception. //! ```should_panic //! # use rlst_dense::*; -//! let mut mat = rlst_mat![f64, (3, 5)]; +//! let mut mat = rlst_dynamic_mat![f64, (3, 5)]; //! mat[[3, 5]] = 4.0; //! ``` //! //! ```should_panic //! # use rlst_dense::*; -//! let mut mat = rlst_mat![f64, (3, 5)]; +//! let mut mat = rlst_dynamic_mat![f64, (3, 5)]; //! println!("Print out of bounds entry: {}",mat[[3, 5]]); //! ``` //! Bounds checks are not always desired. We therefore also provide //! unsafe access routines. //! ``` //! # use rlst_dense::*; -//! let mut mat = rlst_mat![f64, (3, 5)]; +//! let mut mat = rlst_dynamic_mat![f64, (3, 5)]; //! unsafe { //! *mat.get_unchecked_mut(2, 3) = 4.0; //! assert_eq!(*mat.get_unchecked(2, 3), 4.0); diff --git a/dense/src/macros.rs b/dense/src/macros.rs index 0ce100eb..cf2ef61a 100644 --- a/dense/src/macros.rs +++ b/dense/src/macros.rs @@ -8,21 +8,24 @@ pub use rand; /// ``` /// # use rlst_dense::*; /// // Creates a (3, 5) matrix with `f64` entries. -/// let mat = rlst_mat![f64, (3, 5)]; +/// let mat = rlst_dynamic_mat![f64, (3, 5)]; /// ``` #[macro_export] -macro_rules! rlst_mat { +macro_rules! rlst_dynamic_mat { ($ScalarType:ty, $dim:expr) => {{ - use $crate::LayoutType; - $crate::GenericBaseMatrix::< - $ScalarType, - $crate::VectorContainer<$ScalarType>, - $crate::Dynamic, - $crate::Dynamic, - >::from_data( - $crate::VectorContainer::<$ScalarType>::new($dim.0 * $dim.1), - $crate::DefaultLayout::from_dimension(($dim.0, $dim.1)), - ) + use $crate::{Dynamic, MatrixBuilder}; + >::new_matrix($dim) + }}; +} +#[macro_export] +macro_rules! rlst_static_mat { + ($ScalarType:ty, $SizeIdentifier:ty) => {{ + use $crate::{MatrixBuilder}; + use $crate::SizeIdentifierValue; + match <$SizeIdentifier>::SIZE { + SizeIdentifierValue::Static(m, n) => <$SizeIdentifier as MatrixBuilder<$ScalarType>>::new_matrix((m, n)), + _ => panic!("Macro `rlst_static_mat` can only be called with size identifiers representic static sizes."), + } }}; } @@ -46,7 +49,7 @@ macro_rules! rlst_mut_pointer_mat { let slice = std::slice::from_raw_parts_mut($ptr, nindices); let data = $crate::SliceContainerMut::<$a, $ScalarType>::new(slice); - $crate::SliceMatrixMut::<$a, $ScalarType, Dynamic, Dynamic>::from_data(data, new_layout) + $crate::SliceMatrixMut::<$a, $ScalarType, Dynamic>::from_data(data, new_layout) }}; } @@ -71,7 +74,7 @@ macro_rules! rlst_pointer_mat { let slice = std::slice::from_raw_parts($ptr, nindices); let data = $crate::SliceContainer::<$a, $ScalarType>::new(slice); - $crate::SliceMatrix::<$a, $ScalarType, Dynamic, Dynamic>::from_data(data, new_layout) + $crate::SliceMatrix::<$a, $ScalarType, Dynamic>::from_data(data, new_layout) }}; } @@ -88,66 +91,66 @@ macro_rules! rlst_pointer_mat { macro_rules! rlst_rand_mat { ($ScalarType:ty, $dim:expr) => {{ let mut rng = $crate::macros::rand::thread_rng(); - let mut mat = $crate::rlst_mat![$ScalarType, $dim]; + let mut mat = $crate::rlst_dynamic_mat![$ScalarType, $dim]; mat.fill_from_standard_normal(&mut rng); mat }}; } -/// Create a new matrix with compile-time known dimension parameters and stack allocated storage. -/// -/// Currently only dimensions 1, 2, 3 are supported as -/// dimension parameters. -/// -/// Example: -/// ``` -/// # use rlst_dense::*; -/// # use rlst_common::traits::*; -/// // Creates a (2, 3) . -/// let mat = rlst_fixed_mat![f64, 2, 3]; -/// # assert_eq!(mat.shape(), (2, 3)) -/// ``` -#[macro_export] -macro_rules! rlst_fixed_mat { - ($ScalarType:ty, $dim1:literal, $dim2:literal) => {{ - use paste::paste; - use $crate::LayoutType; - #[allow(unused_imports)] - use $crate::{Fixed1, Fixed2, Fixed3}; - $crate::GenericBaseMatrix::< - $ScalarType, - $crate::ArrayContainer<$ScalarType, { $dim1 * $dim2 }>, - paste! {[]}, - paste! {[]}, - >::from_data( - $crate::ArrayContainer::<$ScalarType, { $dim1 * $dim2 }>::new(), - $crate::DefaultLayout::from_dimension(($dim1, $dim2)), - ) - }}; -} +// / Create a new matrix with compile-time known dimension parameters and stack allocated storage. +// / +// / Currently only dimensions 1, 2, 3 are supported as +// / dimension parameters. +// / +// / Example: +// / ``` +// / # use rlst_dense::*; +// / # use rlst_common::traits::*; +// / // Creates a (2, 3) . +// / let mat = rlst_fixed_mat![f64, 2, 3]; +// / # assert_eq!(mat.shape(), (2, 3)) +// / ``` +// #[macro_export] +// macro_rules! rlst_fixed_mat { +// ($ScalarType:ty, $dim1:literal, $dim2:literal) => {{ +// use paste::paste; +// use $crate::LayoutType; +// #[allow(unused_imports)] +// use $crate::{Fixed2, Fixed3}; +// $crate::GenericBaseMatrix::< +// $ScalarType, +// $crate::ArrayContainer<$ScalarType, { $dim1 * $dim2 }>, +// paste! {[]}, +// paste! {[]}, +// >::from_data( +// $crate::ArrayContainer::<$ScalarType, { $dim1 * $dim2 }>::new(), +// $crate::DefaultLayout::from_dimension(($dim1, $dim2)), +// ) +// }}; +// } -/// Create a new random matrix with compile-time known dimension parameters and stack allocated storage. -/// -/// Currently only dimensions 1, 2, 3 are supported as -/// dimension parameters. -/// -/// Example: -/// ``` -/// # use rlst_dense::*; -/// # use rlst_common::traits::*; -/// // Creates a (2, 3) . -/// let mat = rlst_fixed_rand_mat![f64, 2, 3]; -/// # assert_eq!(mat.shape(), (2, 3)) -/// ``` -#[macro_export] -macro_rules! rlst_fixed_rand_mat { - ($ScalarType:ty, $dim1:literal, $dim2:literal) => {{ - let mut rng = $crate::macros::rand::thread_rng(); - let mut mat = $crate::rlst_fixed_mat![$ScalarType, $dim1, $dim2]; - mat.fill_from_standard_normal(&mut rng); - mat - }}; -} +// /// Create a new random matrix with compile-time known dimension parameters and stack allocated storage. +// /// +// /// Currently only dimensions 1, 2, 3 are supported as +// /// dimension parameters. +// /// +// /// Example: +// /// ``` +// /// # use rlst_dense::*; +// /// # use rlst_common::traits::*; +// /// // Creates a (2, 3) . +// /// let mat = rlst_static_rand_mat![f64, 2, 3]; +// /// # assert_eq!(mat.shape(), (2, 3)) +// /// ``` +// #[macro_export] +// macro_rules! rlst_static_rand_mat { +// ($ScalarType:ty, $dim1:literal, $dim2:literal) => {{ +// let mut rng = $crate::macros::rand::thread_rng(); +// let mut mat = $crate::rlst_fixed_mat![$ScalarType, $dim1, $dim2]; +// mat.fill_from_standard_normal(&mut rng); +// mat +// }}; +// } /// Create a new column vector. /// @@ -163,7 +166,7 @@ macro_rules! rlst_fixed_rand_mat { #[macro_export] macro_rules! rlst_col_vec { ($ScalarType:ty, $len:expr) => {{ - $crate::rlst_mat![$ScalarType, ($len, 1)] + $crate::rlst_dynamic_mat![$ScalarType, ($len, 1)] }}; } @@ -181,7 +184,7 @@ macro_rules! rlst_col_vec { #[macro_export] macro_rules! rlst_row_vec { ($ScalarType:ty, $len:expr) => {{ - $crate::rlst_mat![$ScalarType, (1, $len)] + $crate::rlst_dynamic_mat![$ScalarType, (1, $len)] }}; } @@ -233,17 +236,17 @@ mod test { #[test] fn create_matrix() { let dim = (2, 3); - let mat = rlst_mat![f64, dim]; + let mat = rlst_dynamic_mat![f64, dim]; assert_eq!(mat.shape(), (2, 3)); } - #[test] - fn create_fixed_matrix() { - let mat = rlst_fixed_mat![f64, 2, 3]; + // #[test] + // fn create_fixed_matrix() { + // let mat = rlst_fixed_mat![f64, 2, 3]; - assert_eq!(mat.shape(), (2, 3)); - } + // assert_eq!(mat.shape(), (2, 3)); + // } #[test] fn create_random_matrix() { diff --git a/dense/src/matrix.rs b/dense/src/matrix.rs index d4c070f2..058f88b6 100644 --- a/dense/src/matrix.rs +++ b/dense/src/matrix.rs @@ -12,12 +12,8 @@ //! - `MatImpl`. The actual implementation of the matrix. It must itself implement the //! trait [MatrixImplTrait] or [MatrixImplTraitMut] depending on whether mutable access //! is required. -//! - `L`. A given type that implements the [LayoutType] trait and specifies the memory layout -//! of the matrix. -//! - `RS`. A type that implements [SizeType] and specifies whether the row dimension is known -//! at compile time or dynamically at runtime. -//! - `CS`. A type that implements [SizeType] and specifies whether the column dimension is -//! known at compile time or dynamically at runtime. +//! - `S`. A type that implements [SizeIdentifier] and specifies whether the dimensions are not at +//! compile time or dynamically at runtime. pub mod common_impl; pub mod constructors; @@ -37,75 +33,44 @@ use std::marker::PhantomData; /// A [RefMat] is a matrix whose implementation is a reference to another matrix. /// This is used to convert a reference to a matrix to an owned matrix whose implementation /// is a reference to matrix. -pub type RefMat<'a, Item, MatImpl, RS, CS> = - Matrix, RS, CS>; +pub type RefMat<'a, Item, MatImpl, S> = Matrix, S>; /// A [RefMatMut] is a matrix whose implementation is a reference to another matrix. /// This is used to convert a reference to a matrix to an owned matrix whose implementation /// is a reference to matrix. -pub type RefMatMut<'a, Item, MatImpl, RS, CS> = - Matrix, RS, CS>; +pub type RefMatMut<'a, Item, MatImpl, S> = Matrix, S>; /// This type represents a generic matrix that depends on a [BaseMatrix] type. -pub type GenericBaseMatrix = - Matrix, RS, CS>; +pub type GenericBaseMatrix = Matrix, S>; /// A [SliceMatrix] is defined by a [BaseMatrix] whose container stores a memory slice. -pub type SliceMatrix<'a, Item, RS, CS> = - Matrix, RS, CS>, RS, CS>; +pub type SliceMatrix<'a, Item, S> = Matrix, S>, S>; /// Like [SliceMatrix] but with mutable access. -pub type SliceMatrixMut<'a, Item, RS, CS> = - Matrix, RS, CS>, RS, CS>; +pub type SliceMatrixMut<'a, Item, S> = + Matrix, S>, S>; /// A dynamic matrix generic over the item type and the underlying layout. -pub type MatrixD = - Matrix, Dynamic, Dynamic>, Dynamic, Dynamic>; +pub type MatrixD = Matrix, Dynamic>, Dynamic>; /// A matrix that provides a view onto part of another matrix. -pub type ViewMatrix<'a, Item, MatImpl, RS, CS> = - Matrix, RS, CS>; +pub type ViewMatrix<'a, Item, MatImpl, S> = Matrix, S>; /// A matrix that provides a mutable view onto part of another matrix. -pub type ViewMatrixMut<'a, Item, MatImpl, RS, CS> = - Matrix, RS, CS>; - -/// A dynamic column vector. This means that the row dimension is dynamic and the column -/// dimension is [Fixed1]. -pub type ColumnVectorD = GenericBaseMatrix, Dynamic, Fixed1>; - -/// A dynamic row vector. This means that the column dimension is dynamic and the row dimension -/// is [Fixed1]. -pub type RowVectorD = GenericBaseMatrix, Fixed1, Dynamic>; +pub type ViewMatrixMut<'a, Item, MatImpl, S> = Matrix, S>; /// A fixed 2x2 matrix. -pub type Matrix22 = - Matrix, Fixed2, Fixed2>, Fixed2, Fixed2>; +pub type Matrix22 = Matrix, Fixed2>, Fixed2>; /// A fixed 3x3 matrix. -pub type Matrix33 = - Matrix, Fixed3, Fixed3>, Fixed3, Fixed3>; - -/// A fixed 3x2 matrix. -pub type Matrix32 = - Matrix, Fixed3, Fixed2>, Fixed3, Fixed2>; - -/// A fixed 2x3 matrix. -pub type Matrix23 = - Matrix, Fixed2, Fixed3>, Fixed2, Fixed3>; +pub type Matrix33 = Matrix, Fixed3>, Fixed3>; /// The basic tuple type defining a matrix. It is given as `(MatImpl, _, _, _, _)`. /// The only relevant member is the first one `MatImpl`, an implementation type to which /// all calls are forwarded. The other members are of type [PhantomData] and are necessary /// to make the type dependent on the corresponding generic type parameter. -pub struct Matrix( - MatImpl, - PhantomData, - PhantomData, - PhantomData, -) +pub struct Matrix(MatImpl, PhantomData, PhantomData) where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait; + S: SizeIdentifier, + MatImpl: MatrixImplTrait; diff --git a/dense/src/matrix/common_impl.rs b/dense/src/matrix/common_impl.rs index 8ca1c166..59b2c69e 100644 --- a/dense/src/matrix/common_impl.rs +++ b/dense/src/matrix/common_impl.rs @@ -9,12 +9,8 @@ use crate::{RefMatMut, ViewMatrix}; use num::traits::Zero; use rlst_common::traits::*; -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for Matrix +impl, S: SizeIdentifier> Layout + for Matrix { type Impl = DefaultLayout; fn layout(&self) -> &Self::Impl { @@ -22,59 +18,38 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Shape for Matrix +impl, S: SizeIdentifier> Shape + for Matrix { fn shape(&self) -> (usize, usize) { self.layout().dim() } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Stride for Matrix +impl, S: SizeIdentifier> Stride + for Matrix { fn stride(&self) -> (usize, usize) { self.layout().stride() } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > NumberOfElements for Matrix +impl, S: SizeIdentifier> NumberOfElements + for Matrix { fn number_of_elements(&self) -> usize { self.layout().number_of_elements() } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for Matrix +impl, S: SizeIdentifier> Size + for Matrix { - type R = RS; - type C = CS; + type S = S; } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for Matrix +impl, S: SizeIdentifier> UnsafeRandomAccessByValue + for Matrix { type Item = Item; @@ -89,12 +64,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessMut for Matrix +impl, S: SizeIdentifier> UnsafeRandomAccessMut + for Matrix { type Item = Item; @@ -109,12 +80,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTraitAccessByRef, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByRef for Matrix +impl, S: SizeIdentifier> + UnsafeRandomAccessByRef for Matrix { type Item = Item; @@ -129,12 +96,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTraitAccessByRef, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Index<[usize; 2]> for Matrix +impl, S: SizeIdentifier> + std::ops::Index<[usize; 2]> for Matrix { type Output = Item; @@ -145,22 +108,17 @@ impl< impl< Item: Scalar, - MatImpl: MatrixImplTraitMut + MatrixImplTraitAccessByRef, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::IndexMut<[usize; 2]> for Matrix + MatImpl: MatrixImplTraitMut + MatrixImplTraitAccessByRef, + S: SizeIdentifier, + > std::ops::IndexMut<[usize; 2]> for Matrix { fn index_mut(&mut self, index: [usize; 2]) -> &mut Self::Output { self.get_mut(index[0], index[1]).unwrap() } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Eval for Matrix +impl, S: SizeIdentifier + MatrixBuilder> Eval + for Matrix where Self: NewLikeSelf, ::Out: ColumnMajorIteratorMut, @@ -176,12 +134,8 @@ where } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Copy for Matrix +impl, S: SizeIdentifier> Copy + for Matrix where Self: Eval, { @@ -192,12 +146,8 @@ where } } -impl< - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > ForEach for Matrix +impl, S: SizeIdentifier> ForEach + for Matrix { type T = Item; fn for_each(&mut self, mut f: F) { @@ -207,12 +157,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait + RawAccess, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccess for Matrix +impl + RawAccess, S: SizeIdentifier> + RawAccess for Matrix { type T = Item; @@ -234,10 +180,9 @@ impl< impl< Item: Scalar, - MatImpl: MatrixImplTraitMut + RawAccessMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccessMut for Matrix + MatImpl: MatrixImplTraitMut + RawAccessMut, + S: SizeIdentifier, + > RawAccessMut for Matrix { fn get_pointer_mut(&mut self) -> *mut Item { self.0.get_pointer_mut() @@ -252,12 +197,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > ScaleInPlace for Matrix +impl, S: SizeIdentifier> ScaleInPlace + for Matrix { type T = Item; @@ -268,11 +209,10 @@ impl< impl< Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, + MatImpl: MatrixImplTraitMut, + S: SizeIdentifier, Other: Shape + ColumnMajorIterator, - > FillFrom for Matrix + > FillFrom for Matrix { fn fill_from(&mut self, other: &Other) { assert_eq!( @@ -291,11 +231,10 @@ impl< impl< Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, + MatImpl: MatrixImplTraitMut, + S: SizeIdentifier, Other: Shape + ColumnMajorIterator, - > SumInto for Matrix + > SumInto for Matrix { type T = Item; @@ -314,12 +253,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SetDiag for Matrix +impl, S: SizeIdentifier> SetDiag + for Matrix { type T = Item; @@ -344,12 +279,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SquareSum for Matrix +impl, S: SizeIdentifier> SquareSum + for Matrix { type T = Item; fn square_sum(&self) -> ::Real { @@ -362,15 +293,9 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Matrix -{ +impl, S: SizeIdentifier> Matrix { /// Return a view to the whole matrix. - pub fn view(&self) -> RefMat { + pub fn view(&self) -> RefMat { Matrix::from_ref(self) } @@ -379,30 +304,26 @@ impl< &self, offset: (usize, usize), block_size: (usize, usize), - ) -> ViewMatrix { + ) -> ViewMatrix { Matrix::new(MatrixView::new(self, offset, block_size)) } /// Return a single column of a matrix. - pub fn col(&self, col_index: usize) -> ViewMatrix { + pub fn col(&self, col_index: usize) -> ViewMatrix { self.subview((0, col_index), (self.shape().0, 1)) } /// Return a single row of a matrix. - pub fn row(&self, row_index: usize) -> ViewMatrix { + pub fn row(&self, row_index: usize) -> ViewMatrix { self.subview((row_index, 0), (1, self.shape().1)) } } -impl< - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Matrix +impl, S: SizeIdentifier> + Matrix { /// Return a mutable view to the whole matrix. - pub fn view_mut(&mut self) -> RefMatMut { + pub fn view_mut(&mut self) -> RefMatMut { Matrix::from_ref_mut(self) } @@ -411,27 +332,23 @@ impl< &mut self, offset: (usize, usize), block_size: (usize, usize), - ) -> ViewMatrixMut { + ) -> ViewMatrixMut { Matrix::new(MatrixViewMut::new(self, offset, block_size)) } /// Return a mutable single column of a matrix. - pub fn col_mut(&mut self, col_index: usize) -> ViewMatrixMut { + pub fn col_mut(&mut self, col_index: usize) -> ViewMatrixMut { self.subview_mut((0, col_index), (self.shape().0, 1)) } /// Return a mutable single row of a matrix. - pub fn row_mut(&mut self, row_index: usize) -> ViewMatrixMut { + pub fn row_mut(&mut self, row_index: usize) -> ViewMatrixMut { self.subview_mut((row_index, 0), (1, self.shape().1)) } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > IsHermitian for Matrix +impl, S: SizeIdentifier> IsHermitian + for Matrix { fn is_hermitian(&self) -> bool { if self.shape().0 != self.shape().1 { @@ -452,12 +369,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > IsSymmetric for Matrix +impl, S: SizeIdentifier> IsSymmetric + for Matrix { fn is_symmetric(&self) -> bool { if self.shape().0 != self.shape().1 { @@ -478,19 +391,13 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Matrix -{ +impl, S: SizeIdentifier> Matrix { pub fn get_mat_impl_type(&self) -> MatrixImplType { MatImpl::MAT_IMPL } pub fn to_dyn_matrix(&self) -> MatrixD { - let mut mat = crate::rlst_mat![Item, self.shape()]; + let mut mat = crate::rlst_dynamic_mat![Item, self.shape()]; for (item, other) in mat.iter_col_major_mut().zip(self.iter_col_major()) { *item = other; @@ -499,12 +406,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Matrix +impl, S: SizeIdentifier> + Matrix { pub fn sum_into_block< Other: RandomAccessByValue + ColumnMajorIterator + Shape, @@ -519,14 +422,12 @@ impl< } } -impl> - Matrix -{ +impl> Matrix { /// Pad with `ncolumns` columns at the right of the matrix. pub fn pad_right(&self, ncolumns: usize) -> MatrixD { let new_dim = (self.shape().0, self.shape().1 + ncolumns); - let mut new_mat = crate::rlst_mat![Item, new_dim]; + let mut new_mat = crate::rlst_dynamic_mat![Item, new_dim]; new_mat.subview_mut((0, 0), self.shape()).fill_from(self); new_mat } @@ -535,7 +436,7 @@ impl> pub fn pad_left(&self, ncolumns: usize) -> MatrixD { let new_dim = (self.shape().0, self.shape().1 + ncolumns); - let mut new_mat = crate::rlst_mat![Item, new_dim]; + let mut new_mat = crate::rlst_dynamic_mat![Item, new_dim]; new_mat .subview_mut((0, ncolumns), self.shape()) .fill_from(self); @@ -546,7 +447,7 @@ impl> pub fn pad_above(&self, nrows: usize) -> MatrixD { let new_dim = (self.shape().0 + nrows, self.shape().1); - let mut new_mat = crate::rlst_mat![Item, new_dim]; + let mut new_mat = crate::rlst_dynamic_mat![Item, new_dim]; new_mat .subview_mut((nrows, 0), self.shape()) .fill_from(self); @@ -557,15 +458,15 @@ impl> pub fn pad_below(&self, nrows: usize) -> MatrixD { let new_dim = (self.shape().0 + nrows, self.shape().1); - let mut new_mat = crate::rlst_mat![Item, new_dim]; + let mut new_mat = crate::rlst_dynamic_mat![Item, new_dim]; new_mat.subview_mut((0, 0), self.shape()).fill_from(self); new_mat } /// Append `other` to the right of the current matrix. - pub fn append_right>( + pub fn append_right>( &self, - other: &Matrix, + other: &Matrix, ) -> MatrixD { let mut new_mat = self.pad_right(other.shape().1); new_mat @@ -575,9 +476,9 @@ impl> } /// Append `other` to the bottom of the current matrix. - pub fn append_below>( + pub fn append_below>( &self, - other: &Matrix, + other: &Matrix, ) -> MatrixD { let mut new_mat = self.pad_below(other.shape().0); new_mat diff --git a/dense/src/matrix/constructors.rs b/dense/src/matrix/constructors.rs index 3e53e8a9..619960d1 100644 --- a/dense/src/matrix/constructors.rs +++ b/dense/src/matrix/constructors.rs @@ -1,7 +1,6 @@ //! A collection of routines to construct matrix objects from scratch or existing data. use crate::base_matrix::BaseMatrix; -use crate::data_container::ArrayContainer; use crate::matrix::Matrix; use crate::matrix_ref::{MatrixRef, MatrixRefMut}; use crate::traits::*; @@ -11,47 +10,35 @@ use rlst_common::traits::constructors::{NewLikeSelf, NewLikeTranspose}; use rlst_common::traits::Identity; use std::marker::PhantomData; -impl< - Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait, - > Matrix -{ +impl> Matrix { /// Create a new matrix from a given implementation. pub fn new(mat: MatImpl) -> Self { - Self(mat, PhantomData, PhantomData, PhantomData) + Self(mat, PhantomData, PhantomData) } /// Create a new matrix from a reference to an existing matrix. - pub fn from_ref( - mat: &Matrix, - ) -> crate::RefMat<'_, Item, MatImpl, RS, CS> { + pub fn from_ref(mat: &Matrix) -> crate::RefMat<'_, Item, MatImpl, S> { crate::RefMat::new(MatrixRef::new(mat)) } } -impl< - Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTraitMut, - > Matrix +impl> + Matrix { /// Create a new matrix from a reference to an existing matrix. pub fn from_ref_mut( - mat: &mut Matrix, - ) -> crate::RefMatMut<'_, Item, MatImpl, RS, CS> { + mat: &mut Matrix, + ) -> crate::RefMatMut<'_, Item, MatImpl, S> { crate::RefMatMut::new(MatrixRefMut::new(mat)) } } -impl> - Matrix, RS, CS> +impl> + Matrix, S> { /// Create a new matrix from a data container and a layout structure. pub fn from_data(data: Data, layout: DefaultLayout) -> Self { - Self::new(BaseMatrix::::new(data, layout)) + Self::new(BaseMatrix::::new(data, layout)) } } @@ -60,7 +47,7 @@ impl MatrixD { /// provides [RandomAccessByValue](rlst_common::traits::RandomAccessByValue) and /// [Shape](rlst_common::traits::Shape) traits. pub fn from_other + Shape>(other: &Other) -> Self { - let mut mat = crate::rlst_mat![Item, other.shape()]; + let mut mat = crate::rlst_dynamic_mat![Item, other.shape()]; for col_index in 0..other.shape().1 { for row_index in 0..other.shape().0 { @@ -76,7 +63,7 @@ impl Identity for MatrixD { type Out = Self; fn identity(shape: (usize, usize)) -> Self::Out { - let mut ident = crate::rlst_mat![Item, shape]; + let mut ident = crate::rlst_dynamic_mat![Item, shape]; for index in 0..std::cmp::min(shape.0, shape.1) { ident[[index, index]] = ::one(); @@ -86,73 +73,83 @@ impl Identity for MatrixD { } } -impl> NewLikeSelf - for Matrix +impl, S: SizeIdentifier + MatrixBuilder> + NewLikeSelf for Matrix { - type Out = crate::MatrixD; + type Out = >::Out; fn new_like_self(&self) -> Self::Out { - crate::rlst_mat![Item, self.layout().dim()] + >::new_matrix(self.layout().dim()) } } -impl> NewLikeTranspose - for Matrix +// impl, S: StaticMatrixBuilder> +// NewLikeSelf for Matrix +// { +// type Out = crate::MatrixD; + +// fn new_like_self(&self) -> Self::Out { +// crate::rlst_mat![Item, self.layout().dim()] +// } +// } + +impl> NewLikeTranspose + for Matrix { type Out = crate::MatrixD; fn new_like_transpose(&self) -> Self::Out { let dim = self.layout().dim(); - crate::rlst_mat![Item, (dim.1, dim.0)] + crate::rlst_dynamic_mat![Item, (dim.1, dim.0)] } } -macro_rules! implement_new_from_self_fixed { - ($RS:ty, $CS:ty) => { - impl> NewLikeSelf - for Matrix - { - type Out = Matrix< - Item, - BaseMatrix::N * <$CS>::N }>, $RS, $CS>, - $RS, - $CS, - >; - fn new_like_self(&self) -> Self::Out { - ::from_data( - ArrayContainer::::N * <$CS>::N }>::new(), - DefaultLayout::from_dimension((<$RS>::N, <$CS>::N)), - ) - } - } - - impl> NewLikeTranspose - for Matrix - { - type Out = Matrix< - Item, - BaseMatrix::N * <$RS>::N }>, $CS, $RS>, - $CS, - $RS, - >; - fn new_like_transpose(&self) -> Self::Out { - ::from_data( - ArrayContainer::::N * <$RS>::N }>::new(), - DefaultLayout::from_dimension((<$CS>::N, <$RS>::N)), - ) - } - } - }; -} - -implement_new_from_self_fixed!(Fixed2, Fixed2); -implement_new_from_self_fixed!(Fixed1, Fixed2); - -implement_new_from_self_fixed!(Fixed3, Fixed3); -implement_new_from_self_fixed!(Fixed1, Fixed3); - -implement_new_from_self_fixed!(Fixed2, Fixed3); -implement_new_from_self_fixed!(Fixed3, Fixed2); - -implement_new_from_self_fixed!(Fixed2, Fixed1); -implement_new_from_self_fixed!(Fixed3, Fixed1); +// macro_rules! implement_new_from_self_fixed { +// ($S:ty) => { +// impl> NewLikeSelf +// for Matrix +// { +// type Out = Matrix< +// Item, +// BaseMatrix::N * <$S>::N }>, $S, $S>, +// $S, +// $S, +// >; +// fn new_like_self(&self) -> Self::Out { +// ::from_data( +// ArrayContainer::::N * <$S>::N }>::new(), +// DefaultLayout::from_dimension((<$S>::N, <$S>::N)), +// ) +// } +// } + +// impl> NewLikeTranspose +// for Matrix +// { +// type Out = Matrix< +// Item, +// BaseMatrix::N * <$RS>::N }>, $CS, $RS>, +// $CS, +// $RS, +// >; +// fn new_like_transpose(&self) -> Self::Out { +// ::from_data( +// ArrayContainer::::N * <$RS>::N }>::new(), +// DefaultLayout::from_dimension((<$CS>::N, <$RS>::N)), +// ) +// } +// } +// }; +// } + +// implement_new_from_self_fixed!(Fixed2, Fixed2); +// implement_new_from_self_fixed!(Fixed1, Fixed2); + +// implement_new_from_self_fixed!(Fixed3, Fixed3); +// implement_new_from_self_fixed!(Fixed1, Fixed3); + +// implement_new_from_self_fixed!(Fixed2, Fixed3); +// implement_new_from_self_fixed!(Fixed3, Fixed2); + +// implement_new_from_self_fixed!(Fixed2, Fixed1); +// implement_new_from_self_fixed!(Fixed3, Fixed1); diff --git a/dense/src/matrix/iterators.rs b/dense/src/matrix/iterators.rs index fa122e8d..18b1f969 100644 --- a/dense/src/matrix/iterators.rs +++ b/dense/src/matrix/iterators.rs @@ -7,106 +7,73 @@ use rlst_common::types::Scalar; pub struct MatrixColumnMajorIterator< 'a, Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, + MatImpl: MatrixImplTrait, + S: SizeIdentifier, > { - mat: &'a Matrix, + mat: &'a Matrix, pos: usize, } pub struct MatrixColumnMajorIteratorMut< 'a, Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, + MatImpl: MatrixImplTraitMut, + S: SizeIdentifier, > { - mat: &'a mut Matrix, + mat: &'a mut Matrix, pos: usize, } -pub struct DiagonalIterator< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, -> { - mat: &'a Matrix, +pub struct DiagonalIterator<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> +{ + mat: &'a Matrix, pos: usize, } pub struct DiagonalIteratorMut< 'a, Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, + MatImpl: MatrixImplTraitMut, + S: SizeIdentifier, > { - mat: &'a mut Matrix, + mat: &'a mut Matrix, pos: usize, } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixColumnMajorIterator<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> + MatrixColumnMajorIterator<'a, Item, MatImpl, S> { - pub fn new(mat: &'a Matrix) -> Self { + pub fn new(mat: &'a Matrix) -> Self { Self { mat, pos: 0 } } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixColumnMajorIteratorMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> + MatrixColumnMajorIteratorMut<'a, Item, MatImpl, S> { - pub fn new(mat: &'a mut Matrix) -> Self { + pub fn new(mat: &'a mut Matrix) -> Self { Self { mat, pos: 0 } } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > DiagonalIterator<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> + DiagonalIterator<'a, Item, MatImpl, S> { - pub fn new(mat: &'a Matrix) -> Self { + pub fn new(mat: &'a Matrix) -> Self { Self { mat, pos: 0 } } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > DiagonalIteratorMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> + DiagonalIteratorMut<'a, Item, MatImpl, S> { - pub fn new(mat: &'a mut Matrix) -> Self { + pub fn new(mat: &'a mut Matrix) -> Self { Self { mat, pos: 0 } } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::iter::Iterator for MatrixColumnMajorIterator<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> std::iter::Iterator + for MatrixColumnMajorIterator<'a, Item, MatImpl, S> { type Item = Item; fn next(&mut self) -> Option { @@ -116,13 +83,8 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::iter::Iterator for DiagonalIterator<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> std::iter::Iterator + for DiagonalIterator<'a, Item, MatImpl, S> { type Item = Item; fn next(&mut self) -> Option { @@ -139,13 +101,8 @@ impl< // See also: https://stackoverflow.com/questions/62361624/lifetime-parameter-problem-in-custom-iterator-over-mutable-references // And also: https://users.rust-lang.org/t/when-is-transmuting-lifetimes-useful/56140 -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::iter::Iterator for MatrixColumnMajorIteratorMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> std::iter::Iterator + for MatrixColumnMajorIteratorMut<'a, Item, MatImpl, S> { type Item = &'a mut Item; fn next(&mut self) -> Option { @@ -158,13 +115,8 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::iter::Iterator for DiagonalIteratorMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> std::iter::Iterator + for DiagonalIteratorMut<'a, Item, MatImpl, S> { type Item = &'a mut Item; fn next(&mut self) -> Option { @@ -177,60 +129,44 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > rlst_common::traits::iterators::ColumnMajorIterator for Matrix +impl, S: SizeIdentifier> + rlst_common::traits::iterators::ColumnMajorIterator for Matrix { type T = Item; - type Iter<'a> = MatrixColumnMajorIterator<'a, Item, MatImpl, RS, CS> where Self: 'a; + type Iter<'a> = MatrixColumnMajorIterator<'a, Item, MatImpl, S> where Self: 'a; fn iter_col_major(&self) -> Self::Iter<'_> { MatrixColumnMajorIterator::new(self) } } -impl< - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > rlst_common::traits::iterators::ColumnMajorIteratorMut for Matrix +impl, S: SizeIdentifier> + rlst_common::traits::iterators::ColumnMajorIteratorMut for Matrix { type T = Item; - type IterMut<'a> = MatrixColumnMajorIteratorMut<'a, Item, MatImpl, RS, CS> where Self: 'a; + type IterMut<'a> = MatrixColumnMajorIteratorMut<'a, Item, MatImpl, S> where Self: 'a; fn iter_col_major_mut(&mut self) -> Self::IterMut<'_> { MatrixColumnMajorIteratorMut::new(self) } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > rlst_common::traits::iterators::DiagonalIterator for Matrix +impl, S: SizeIdentifier> + rlst_common::traits::iterators::DiagonalIterator for Matrix { type T = Item; - type Iter<'a> = DiagonalIterator<'a, Item, MatImpl, RS, CS> where Self: 'a; + type Iter<'a> = DiagonalIterator<'a, Item, MatImpl, S> where Self: 'a; fn iter_diag(&self) -> Self::Iter<'_> { DiagonalIterator::new(self) } } -impl< - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > rlst_common::traits::iterators::DiagonalIteratorMut for Matrix +impl, S: SizeIdentifier> + rlst_common::traits::iterators::DiagonalIteratorMut for Matrix { type T = Item; - type IterMut<'a> = DiagonalIteratorMut<'a, Item, MatImpl, RS, CS> where Self: 'a; + type IterMut<'a> = DiagonalIteratorMut<'a, Item, MatImpl, S> where Self: 'a; fn iter_diag_mut(&mut self) -> Self::IterMut<'_> { DiagonalIteratorMut::new(self) @@ -244,7 +180,7 @@ mod test { #[test] fn test_col_major_mut() { - let mut mat = crate::rlst_mat![f64, (2, 2)]; + let mut mat = crate::rlst_dynamic_mat![f64, (2, 2)]; for (index, item) in mat.iter_col_major_mut().enumerate() { *item = index as f64; diff --git a/dense/src/matrix/matrix_slices.rs b/dense/src/matrix/matrix_slices.rs index b1981f9d..94f79bbb 100644 --- a/dense/src/matrix/matrix_slices.rs +++ b/dense/src/matrix/matrix_slices.rs @@ -7,7 +7,7 @@ use crate::traits::*; use crate::types::Scalar; impl> - Matrix, Dynamic, Dynamic> + Matrix, Dynamic> { /// Return a new matrix that is a subblock of another matrix. /// @@ -17,7 +17,7 @@ impl> &'a self, top_left: (usize, usize), dim: (usize, usize), - ) -> SliceMatrix<'a, Item, Dynamic, Dynamic> { + ) -> SliceMatrix<'a, Item, Dynamic> { assert!( (top_left.0 + dim.0 <= self.layout().dim().0) & (top_left.1 + dim.1 <= self.layout().dim().1), @@ -32,7 +32,7 @@ impl> } } impl> - Matrix, Dynamic, Dynamic> + Matrix, Dynamic> { /// Return a new matrix that is a mutable subblock of another matrix. /// @@ -42,7 +42,7 @@ impl> &'a mut self, top_left: (usize, usize), dim: (usize, usize), - ) -> SliceMatrixMut<'a, Item, Dynamic, Dynamic> { + ) -> SliceMatrixMut<'a, Item, Dynamic> { assert!( (top_left.0 + dim.0 <= self.layout().dim().0) & (top_left.1 + dim.1 <= self.layout().dim().1), @@ -58,9 +58,7 @@ impl> } } -impl> - GenericBaseMatrix -{ +impl> GenericBaseMatrix { #[allow(clippy::type_complexity)] /// Split a mutable matrix into four mutable subblocks. /// @@ -76,10 +74,10 @@ impl> &'a mut self, split_at: (usize, usize), ) -> ( - SliceMatrixMut<'a, Item, Dynamic, Dynamic>, - SliceMatrixMut<'a, Item, Dynamic, Dynamic>, - SliceMatrixMut<'a, Item, Dynamic, Dynamic>, - SliceMatrixMut<'a, Item, Dynamic, Dynamic>, + SliceMatrixMut<'a, Item, Dynamic>, + SliceMatrixMut<'a, Item, Dynamic>, + SliceMatrixMut<'a, Item, Dynamic>, + SliceMatrixMut<'a, Item, Dynamic>, ) { let dim = self.layout().dim(); let stride = self.layout().stride(); @@ -118,7 +116,7 @@ mod test { #[test] fn test_simple_slice() { - let mut mat = crate::rlst_mat![f64, (3, 4)]; + let mut mat = crate::rlst_dynamic_mat![f64, (3, 4)]; *mat.get_mut(1, 2).unwrap() = 1.0; let slice = mat.block((0, 1), (2, 2)); @@ -129,7 +127,7 @@ mod test { #[test] fn test_double_slice() { - let mut mat = crate::rlst_mat![f64, (3, 4)]; + let mut mat = crate::rlst_dynamic_mat![f64, (3, 4)]; *mat.get_mut(1, 2).unwrap() = 1.0; let slice1 = mat.block((0, 1), (3, 3)); diff --git a/dense/src/matrix/operations/cmp_wise_product.rs b/dense/src/matrix/operations/cmp_wise_product.rs index 6282e837..6820a935 100644 --- a/dense/src/matrix/operations/cmp_wise_product.rs +++ b/dense/src/matrix/operations/cmp_wise_product.rs @@ -10,30 +10,28 @@ pub use rlst_common::types::Scalar; impl< 'a, Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > CmpWiseProduct<&'a Matrix> for Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > CmpWiseProduct<&'a Matrix> for Matrix { - type Out = CmpWiseProductMat, RS, CS>; + type Out = CmpWiseProductMat, S>; - fn cmp_wise_product(self, other: &'a Matrix) -> Self::Out { + fn cmp_wise_product(self, other: &'a Matrix) -> Self::Out { Matrix::new(CmpWiseProductContainer::new(self, other.view())) } } impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > CmpWiseProduct> for Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > CmpWiseProduct> for Matrix { - type Out = CmpWiseProductMat; + type Out = CmpWiseProductMat; - fn cmp_wise_product(self, other: Matrix) -> Self::Out { + fn cmp_wise_product(self, other: Matrix) -> Self::Out { Matrix::new(CmpWiseProductContainer::new(self, other)) } } @@ -54,13 +52,13 @@ impl< mod test { use rlst_common::traits::CmpWiseProduct; - use crate::rlst_mat; + use crate::rlst_dynamic_mat; use rlst_common::traits::*; #[test] fn test_cmp_wise_prod() { - let mut mat1 = rlst_mat![f64, (4, 5)]; - let mut mat2 = rlst_mat![f64, (4, 5)]; + let mut mat1 = rlst_dynamic_mat![f64, (4, 5)]; + let mut mat2 = rlst_dynamic_mat![f64, (4, 5)]; mat1.fill_from_seed_equally_distributed(0); mat2.fill_from_seed_equally_distributed(1); diff --git a/dense/src/matrix/operations/conjugate.rs b/dense/src/matrix/operations/conjugate.rs index 97ad4438..8605fe01 100644 --- a/dense/src/matrix/operations/conjugate.rs +++ b/dense/src/matrix/operations/conjugate.rs @@ -7,14 +7,10 @@ use rlst_common::traits::Conjugate; pub use rlst_common::types::Scalar; -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Conjugate for Matrix +impl, S: SizeIdentifier> Conjugate + for Matrix { - type Out = ConjugateMat; + type Out = ConjugateMat; fn conj(self) -> Self::Out { Matrix::new(ConjugateContainer::new(self)) diff --git a/dense/src/matrix/operations/permutations.rs b/dense/src/matrix/operations/permutations.rs index e5271a55..b93094d1 100644 --- a/dense/src/matrix/operations/permutations.rs +++ b/dense/src/matrix/operations/permutations.rs @@ -6,12 +6,8 @@ use rlst_common::traits::{ }; pub use rlst_common::types::Scalar; -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > PermuteColumns for Matrix +impl, S: SizeIdentifier> PermuteColumns + for Matrix where Self: NewLikeSelf + RandomAccessByValue, ::Out: RandomAccessMut, @@ -40,12 +36,8 @@ where } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > PermuteRows for Matrix +impl, S: SizeIdentifier> PermuteRows + for Matrix where Self: NewLikeSelf + RandomAccessByValue, ::Out: RandomAccessMut, diff --git a/dense/src/matrix/operations/to_complex.rs b/dense/src/matrix/operations/to_complex.rs index a98631ab..44c4a8fb 100644 --- a/dense/src/matrix/operations/to_complex.rs +++ b/dense/src/matrix/operations/to_complex.rs @@ -7,29 +7,23 @@ use rlst_common::traits::ToComplex; pub use rlst_common::types::{c32, c64, Scalar}; -impl, RS: SizeIdentifier, CS: SizeIdentifier> ToComplex - for Matrix -{ - type Out = ComplexMat; +impl, S: SizeIdentifier> ToComplex for Matrix { + type Out = ComplexMat; fn to_complex(self) -> Self::Out { - Matrix::new(ComplexContainer::::new(self)) + Matrix::new(ComplexContainer::::new(self)) } } -impl, RS: SizeIdentifier, CS: SizeIdentifier> ToComplex - for Matrix -{ - type Out = ComplexMat; +impl, S: SizeIdentifier> ToComplex for Matrix { + type Out = ComplexMat; fn to_complex(self) -> Self::Out { - Matrix::new(ComplexContainer::::new(self)) + Matrix::new(ComplexContainer::::new(self)) } } -impl, RS: SizeIdentifier, CS: SizeIdentifier> ToComplex - for Matrix -{ +impl, S: SizeIdentifier> ToComplex for Matrix { type Out = Self; fn to_complex(self) -> Self::Out { @@ -37,9 +31,7 @@ impl, RS: SizeIdentifier, CS: SizeIdentifi } } -impl, RS: SizeIdentifier, CS: SizeIdentifier> ToComplex - for Matrix -{ +impl, S: SizeIdentifier> ToComplex for Matrix { type Out = Self; fn to_complex(self) -> Self::Out { diff --git a/dense/src/matrix/operations/transpose.rs b/dense/src/matrix/operations/transpose.rs index 6c5389db..c09c0acb 100644 --- a/dense/src/matrix/operations/transpose.rs +++ b/dense/src/matrix/operations/transpose.rs @@ -7,14 +7,10 @@ use rlst_common::traits::Transpose; pub use rlst_common::types::Scalar; -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Transpose for Matrix +impl, S: SizeIdentifier> Transpose + for Matrix { - type Out = TransposeMat; + type Out = TransposeMat; fn transpose(self) -> Self::Out { Matrix::new(TransposeContainer::new(self)) diff --git a/dense/src/matrix/random.rs b/dense/src/matrix/random.rs index d8fb3c07..6647381c 100644 --- a/dense/src/matrix/random.rs +++ b/dense/src/matrix/random.rs @@ -11,12 +11,8 @@ use rlst_common::traits::*; use super::GenericBaseMatrix; -impl< - Item: Scalar + RandScalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - Data: DataContainerMut, - > GenericBaseMatrix +impl> + GenericBaseMatrix where StandardNormal: Distribution<::Real>, Standard: Distribution<::Real>, diff --git a/dense/src/matrix_multiply.rs b/dense/src/matrix_multiply.rs index 00b23231..39e5a9c5 100644 --- a/dense/src/matrix_multiply.rs +++ b/dense/src/matrix_multiply.rs @@ -36,34 +36,29 @@ pub trait MultiplyAdd< Data1: DataContainer, Data2: DataContainer, Data3: DataContainerMut, - RS1: SizeIdentifier, - CS1: SizeIdentifier, - RS2: SizeIdentifier, - CS2: SizeIdentifier, - RS3: SizeIdentifier, - CS3: SizeIdentifier, + S1: SizeIdentifier, + S2: SizeIdentifier, + S3: SizeIdentifier, > { /// Perform the operation `mat_c = alpha * mat_a * mat_b + beta * mat_c`. fn multiply_add( alpha: Item, - mat_a: &GenericBaseMatrix, - mat_b: &GenericBaseMatrix, + mat_a: &GenericBaseMatrix, + mat_b: &GenericBaseMatrix, beta: Item, - mat_c: &mut GenericBaseMatrix, + mat_c: &mut GenericBaseMatrix, ); } // Matrix x Matrix = Matrix impl< T: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS1: SizeIdentifier, - CS1: SizeIdentifier, - RS2: SizeIdentifier, - CS2: SizeIdentifier, - > Dot> for Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S1: SizeIdentifier, + S2: SizeIdentifier, + > Dot> for Matrix where T: MultiplyAdd< T, @@ -73,22 +68,19 @@ where Dynamic, Dynamic, Dynamic, - Dynamic, - Dynamic, - Dynamic, >, { type Output = MatrixD; - fn dot(&self, rhs: &Matrix) -> Self::Output { + fn dot(&self, rhs: &Matrix) -> Self::Output { // We evaluate self and the other matrix and then perform the multiplication. - let mut left = crate::rlst_mat![T, self.shape()]; - let mut right = crate::rlst_mat![T, rhs.shape()]; + let mut left = crate::rlst_dynamic_mat![T, self.shape()]; + let mut right = crate::rlst_dynamic_mat![T, rhs.shape()]; left.fill_from(self); right.fill_from(rhs); - let mut res = crate::rlst_mat!(T, (self.shape().0, rhs.shape().1)); + let mut res = crate::rlst_dynamic_mat!(T, (self.shape().0, rhs.shape().1)); T::multiply_add( num::cast::(1.0).unwrap(), @@ -106,17 +98,16 @@ impl< Data1: DataContainer, Data2: DataContainer, Data3: DataContainerMut, - > MultiplyAdd - for T + > MultiplyAdd for T where T: Gemm, { fn multiply_add( alpha: T, - mat_a: &GenericBaseMatrix, - mat_b: &GenericBaseMatrix, + mat_a: &GenericBaseMatrix, + mat_b: &GenericBaseMatrix, beta: T, - mat_c: &mut GenericBaseMatrix, + mat_c: &mut GenericBaseMatrix, ) { let dim1 = mat_a.layout().dim(); let dim2 = mat_b.layout().dim(); @@ -170,18 +161,15 @@ mod test { Data1: DataContainer, Data2: DataContainer, Data3: DataContainerMut, - RS1: SizeIdentifier, - CS1: SizeIdentifier, - RS2: SizeIdentifier, - CS2: SizeIdentifier, - RS3: SizeIdentifier, - CS3: SizeIdentifier, + S1: SizeIdentifier, + S2: SizeIdentifier, + S3: SizeIdentifier, >( alpha: Item, - mat_a: &GenericBaseMatrix, - mat_b: &GenericBaseMatrix, + mat_a: &GenericBaseMatrix, + mat_b: &GenericBaseMatrix, beta: Item, - mat_c: &mut GenericBaseMatrix, + mat_c: &mut GenericBaseMatrix, ) { let m = mat_a.layout().dim().0; let k = mat_a.layout().dim().1; @@ -203,10 +191,10 @@ mod test { ($Scalar:ty, $fname:ident) => { #[test] fn $fname() { - let mut mat_a = crate::rlst_mat!($Scalar, (4, 6)); - let mut mat_b = crate::rlst_mat!($Scalar, (6, 5)); - let mut mat_c_actual = crate::rlst_mat!($Scalar, (4, 5)); - let mut mat_c_expect = crate::rlst_mat!($Scalar, (4, 5)); + let mut mat_a = crate::rlst_dynamic_mat!($Scalar, (4, 6)); + let mut mat_b = crate::rlst_dynamic_mat!($Scalar, (6, 5)); + let mut mat_c_actual = crate::rlst_dynamic_mat!($Scalar, (4, 5)); + let mut mat_c_expect = crate::rlst_dynamic_mat!($Scalar, (4, 5)); let dist = StandardNormal; @@ -240,7 +228,7 @@ mod test { ($Scalar:ty, $fname:ident) => { #[test] fn $fname() { - let mut mat_a = crate::rlst_mat![$Scalar, (4, 6)]; + let mut mat_a = crate::rlst_dynamic_mat![$Scalar, (4, 6)]; let mut mat_b = crate::rlst_col_vec![$Scalar, 6]; let mut mat_c_actual = crate::rlst_col_vec![$Scalar, 4]; let mut mat_c_expect = crate::rlst_col_vec![$Scalar, 4]; @@ -278,7 +266,7 @@ mod test { #[test] fn $fname() { let mut mat_a = crate::rlst_row_vec![$Scalar, 4]; - let mut mat_b = crate::rlst_mat![$Scalar, (4, 6)]; + let mut mat_b = crate::rlst_dynamic_mat![$Scalar, (4, 6)]; let mut mat_c_actual = crate::rlst_row_vec![$Scalar, 6]; let mut mat_c_expect = crate::rlst_row_vec![$Scalar, 6]; @@ -327,18 +315,18 @@ mod test { #[test] fn test_dot_matvec() { - let mut mat1 = crate::rlst_mat![f64, (2, 2)]; - let mut mat2 = crate::rlst_mat![f64, (2, 2)]; + let mut mat1 = crate::rlst_dynamic_mat![f64, (2, 2)]; + let mut mat2 = crate::rlst_dynamic_mat![f64, (2, 2)]; mat1.fill_from_seed_equally_distributed(0); mat2.fill_from_seed_equally_distributed(1); - let mut mat3 = crate::rlst_mat![f64, (2, 3)]; + let mut mat3 = crate::rlst_dynamic_mat![f64, (2, 3)]; mat3.fill_from_seed_equally_distributed(2); let actual = (mat1.view() + mat2.view()).dot(&mat3); - let mut expect = crate::rlst_mat![f64, (2, 3)]; + let mut expect = crate::rlst_dynamic_mat![f64, (2, 3)]; let mat_sum = (mat1.view() + mat2.view()).eval(); diff --git a/dense/src/matrix_ref.rs b/dense/src/matrix_ref.rs index 8d44a9c9..7c650198 100644 --- a/dense/src/matrix_ref.rs +++ b/dense/src/matrix_ref.rs @@ -22,63 +22,44 @@ use std::marker::PhantomData; /// A struct that implements [MatrixImplTrait] by holding a reference /// to a matrix and forwarding all matrix operations to the held reference. -pub struct MatrixRef<'a, Item, MatImpl, RS, CS>( - &'a Matrix, +pub struct MatrixRef<'a, Item, MatImpl, S>( + &'a Matrix, PhantomData, - PhantomData, - PhantomData, + PhantomData, ) where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait; + S: SizeIdentifier, + MatImpl: MatrixImplTrait; -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixRef<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> + MatrixRef<'a, Item, MatImpl, S> { - pub fn new(mat: &'a Matrix) -> Self { - Self(mat, PhantomData, PhantomData, PhantomData) + pub fn new(mat: &'a Matrix) -> Self { + Self(mat, PhantomData, PhantomData) } } -pub struct MatrixRefMut<'a, Item, MatImpl, RS, CS>( - &'a mut Matrix, +pub struct MatrixRefMut<'a, Item, MatImpl, S>( + &'a mut Matrix, PhantomData, - PhantomData, - PhantomData, + PhantomData, ) where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTraitMut; + S: SizeIdentifier, + MatImpl: MatrixImplTraitMut; -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixRefMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> + MatrixRefMut<'a, Item, MatImpl, S> { - pub fn new(mat: &'a mut Matrix) -> Self { - Self(mat, PhantomData, PhantomData, PhantomData) + pub fn new(mat: &'a mut Matrix) -> Self { + Self(mat, PhantomData, PhantomData) } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for MatrixRef<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> Layout + for MatrixRef<'a, Item, MatImpl, S> { type Impl = DefaultLayout; @@ -88,36 +69,20 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for MatrixRef<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> Size + for MatrixRef<'a, Item, MatImpl, S> { - type R = RS; - type C = CS; + type S = S; } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for MatrixRef<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> MatrixImplIdentifier + for MatrixRef<'a, Item, MatImpl, S> { const MAT_IMPL: MatrixImplType = MatImpl::MAT_IMPL; } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for MatrixRef<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> + UnsafeRandomAccessByValue for MatrixRef<'a, Item, MatImpl, S> { type Item = Item; @@ -132,13 +97,8 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for MatrixRefMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> Layout + for MatrixRefMut<'a, Item, MatImpl, S> { type Impl = DefaultLayout; @@ -148,36 +108,20 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for MatrixRefMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> MatrixImplIdentifier + for MatrixRefMut<'a, Item, MatImpl, S> { const MAT_IMPL: MatrixImplType = MatImpl::MAT_IMPL; } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for MatrixRefMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> Size + for MatrixRefMut<'a, Item, MatImpl, S> { - type R = RS; - type C = CS; + type S = S; } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for MatrixRefMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> + UnsafeRandomAccessByValue for MatrixRefMut<'a, Item, MatImpl, S> { type Item = Item; @@ -195,10 +139,9 @@ impl< impl< 'a, Item: Scalar, - MatImpl: MatrixImplTraitMut + MatrixImplTraitAccessByRef, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByRef for MatrixRefMut<'a, Item, MatImpl, RS, CS> + MatImpl: MatrixImplTraitMut + MatrixImplTraitAccessByRef, + S: SizeIdentifier, + > UnsafeRandomAccessByRef for MatrixRefMut<'a, Item, MatImpl, S> { type Item = Item; @@ -213,13 +156,8 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitAccessByRef, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByRef for MatrixRef<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitAccessByRef, S: SizeIdentifier> + UnsafeRandomAccessByRef for MatrixRef<'a, Item, MatImpl, S> { type Item = Item; @@ -234,13 +172,8 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessMut for MatrixRefMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> + UnsafeRandomAccessMut for MatrixRefMut<'a, Item, MatImpl, S> { type Item = Item; @@ -255,15 +188,10 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccess for MatrixRefMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> RawAccess + for MatrixRefMut<'a, Item, MatImpl, S> where - Matrix: RawAccess, + Matrix: RawAccess, { type T = Item; @@ -283,15 +211,10 @@ where } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccess for MatrixRef<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> RawAccess + for MatrixRef<'a, Item, MatImpl, S> where - Matrix: RawAccess, + Matrix: RawAccess, { type T = Item; @@ -311,15 +234,10 @@ where } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccessMut for MatrixRefMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> RawAccessMut + for MatrixRefMut<'a, Item, MatImpl, S> where - Matrix: RawAccessMut, + Matrix: RawAccessMut, { fn get_pointer_mut(&mut self) -> *mut Item { self.0.get_pointer_mut() diff --git a/dense/src/matrix_view.rs b/dense/src/matrix_view.rs index 0ec5ff28..502694b6 100644 --- a/dense/src/matrix_view.rs +++ b/dense/src/matrix_view.rs @@ -8,40 +8,33 @@ use crate::types::Scalar; use crate::{traits::*, RefMat}; use crate::{DefaultLayout, RefMatMut}; -pub struct MatrixView<'a, Item, MatImpl, RS, CS> +pub struct MatrixView<'a, Item, MatImpl, S> where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait, + S: SizeIdentifier, + MatImpl: MatrixImplTrait, { - mat: RefMat<'a, Item, MatImpl, RS, CS>, + mat: RefMat<'a, Item, MatImpl, S>, offset: (usize, usize), layout: DefaultLayout, } -pub struct MatrixViewMut<'a, Item, MatImpl, RS, CS> +pub struct MatrixViewMut<'a, Item, MatImpl, S> where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTraitMut, + S: SizeIdentifier, + MatImpl: MatrixImplTraitMut, { - mat: RefMatMut<'a, Item, MatImpl, RS, CS>, + mat: RefMatMut<'a, Item, MatImpl, S>, offset: (usize, usize), layout: DefaultLayout, } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixView<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> + MatrixView<'a, Item, MatImpl, S> { pub fn new( - mat: &'a Matrix, + mat: &'a Matrix, offset: (usize, usize), block_size: (usize, usize), ) -> Self { @@ -62,16 +55,11 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixViewMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> + MatrixViewMut<'a, Item, MatImpl, S> { pub fn new( - mat: &'a mut Matrix, + mat: &'a mut Matrix, offset: (usize, usize), block_size: (usize, usize), ) -> Self { @@ -92,13 +80,8 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for MatrixView<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> Layout + for MatrixView<'a, Item, MatImpl, S> { type Impl = DefaultLayout; @@ -108,13 +91,8 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for MatrixViewMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> Layout + for MatrixViewMut<'a, Item, MatImpl, S> { type Impl = DefaultLayout; @@ -124,59 +102,32 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for MatrixView<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> Size + for MatrixView<'a, Item, MatImpl, S> { - type R = RS; - type C = CS; + type S = S; } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for MatrixViewMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> Size + for MatrixViewMut<'a, Item, MatImpl, S> { - type R = RS; - type C = CS; + type S = S; } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for MatrixView<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> MatrixImplIdentifier + for MatrixView<'a, Item, MatImpl, S> { const MAT_IMPL: MatrixImplType = MatrixImplType::Derived; } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for MatrixViewMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> MatrixImplIdentifier + for MatrixViewMut<'a, Item, MatImpl, S> { const MAT_IMPL: MatrixImplType = MatrixImplType::Derived; } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for MatrixView<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTrait, S: SizeIdentifier> + UnsafeRandomAccessByValue for MatrixView<'a, Item, MatImpl, S> { type Item = Item; @@ -193,13 +144,8 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for MatrixViewMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> + UnsafeRandomAccessByValue for MatrixViewMut<'a, Item, MatImpl, S> { type Item = Item; @@ -216,13 +162,8 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitAccessByRef, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByRef for MatrixView<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitAccessByRef, S: SizeIdentifier> + UnsafeRandomAccessByRef for MatrixView<'a, Item, MatImpl, S> { type Item = Item; @@ -242,10 +183,9 @@ impl< impl< 'a, Item: Scalar, - MatImpl: MatrixImplTraitAccessByRef + MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByRef for MatrixViewMut<'a, Item, MatImpl, RS, CS> + MatImpl: MatrixImplTraitAccessByRef + MatrixImplTraitMut, + S: SizeIdentifier, + > UnsafeRandomAccessByRef for MatrixViewMut<'a, Item, MatImpl, S> { type Item = Item; @@ -262,15 +202,10 @@ impl< } } -impl< - 'a, - Item: Scalar, - MatImpl: MatrixImplTraitMut, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessMut for MatrixViewMut<'a, Item, MatImpl, RS, CS> +impl<'a, Item: Scalar, MatImpl: MatrixImplTraitMut, S: SizeIdentifier> + UnsafeRandomAccessMut for MatrixViewMut<'a, Item, MatImpl, S> where - Matrix: UnsafeRandomAccessMut, + Matrix: UnsafeRandomAccessMut, { type Item = Item; diff --git a/dense/src/op_containers/addition.rs b/dense/src/op_containers/addition.rs index 480d5ac7..97a2213e 100644 --- a/dense/src/op_containers/addition.rs +++ b/dense/src/op_containers/addition.rs @@ -12,32 +12,30 @@ use crate::DefaultLayout; use std::marker::PhantomData; /// A type that represents the sum of two matrices. -pub type AdditionMat = - Matrix, RS, CS>; +pub type AdditionMat = + Matrix, S>; -pub struct Addition( - Matrix, - Matrix, +pub struct Addition( + Matrix, + Matrix, DefaultLayout, - PhantomData, - PhantomData, + PhantomData, + PhantomData, ) where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait; + S: SizeIdentifier, + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait; impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Addition + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > Addition { - pub fn new(mat1: Matrix, mat2: Matrix) -> Self { + pub fn new(mat1: Matrix, mat2: Matrix) -> Self { assert_eq!( mat1.layout().dim(), mat2.layout().dim(), @@ -58,11 +56,10 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for Addition + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > Layout for Addition { type Impl = DefaultLayout; @@ -73,23 +70,20 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for Addition + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > Size for Addition { - type C = CS; - type R = RS; + type S = S; } impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for Addition + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > UnsafeRandomAccessByValue for Addition { type Item = Item; @@ -106,22 +100,20 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for Addition + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > MatrixImplIdentifier for Addition { const MAT_IMPL: MatrixImplType = MatrixImplType::Derived; } impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccess for Addition + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > RawAccess for Addition { type T = Item; @@ -143,11 +135,10 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccessMut for Addition + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > RawAccessMut for Addition { #[inline] fn data_mut(&mut self) -> &mut [Self::T] { @@ -167,15 +158,14 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Add> for Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > std::ops::Add> for Matrix { - type Output = AdditionMat; + type Output = AdditionMat; - fn add(self, rhs: Matrix) -> Self::Output { + fn add(self, rhs: Matrix) -> Self::Output { Matrix::new(Addition::new(self, rhs)) } } @@ -183,15 +173,14 @@ impl< impl< 'a, Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Add<&'a Matrix> for Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > std::ops::Add<&'a Matrix> for Matrix { - type Output = AdditionMat, RS, CS>; + type Output = AdditionMat, S>; - fn add(self, rhs: &'a Matrix) -> Self::Output { + fn add(self, rhs: &'a Matrix) -> Self::Output { Matrix::new(Addition::new(self, Matrix::from_ref(rhs))) } } @@ -199,15 +188,14 @@ impl< impl< 'a, Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Add> for &'a Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > std::ops::Add> for &'a Matrix { - type Output = AdditionMat, MatImpl2, RS, CS>; + type Output = AdditionMat, MatImpl2, S>; - fn add(self, rhs: Matrix) -> Self::Output { + fn add(self, rhs: Matrix) -> Self::Output { Matrix::new(Addition::new(Matrix::from_ref(self), rhs)) } } @@ -215,21 +203,15 @@ impl< impl< 'a, Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Add<&'a Matrix> for &'a Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > std::ops::Add<&'a Matrix> for &'a Matrix { - type Output = AdditionMat< - Item, - MatrixRef<'a, Item, MatImpl1, RS, CS>, - MatrixRef<'a, Item, MatImpl2, RS, CS>, - RS, - CS, - >; - - fn add(self, rhs: &'a Matrix) -> Self::Output { + type Output = + AdditionMat, MatrixRef<'a, Item, MatImpl2, S>, S>; + + fn add(self, rhs: &'a Matrix) -> Self::Output { Matrix::new(Addition::new(Matrix::from_ref(self), Matrix::from_ref(rhs))) } } @@ -242,8 +224,8 @@ mod test { #[test] fn scalar_mult() { - let mut mat1 = crate::rlst_mat!(f64, (2, 3)); - let mut mat2 = crate::rlst_mat!(f64, (2, 3)); + let mut mat1 = crate::rlst_dynamic_mat!(f64, (2, 3)); + let mut mat2 = crate::rlst_dynamic_mat!(f64, (2, 3)); mat1[[1, 2]] = 5.0; mat2[[1, 2]] = 6.0; diff --git a/dense/src/op_containers/cmp_wise_product.rs b/dense/src/op_containers/cmp_wise_product.rs index 62b7bf12..590daf9a 100644 --- a/dense/src/op_containers/cmp_wise_product.rs +++ b/dense/src/op_containers/cmp_wise_product.rs @@ -13,32 +13,29 @@ use crate::DefaultLayout; use std::marker::PhantomData; /// A type that represents the componentwise product of two matrices. -pub type CmpWiseProductMat = - Matrix, RS, CS>; +pub type CmpWiseProductMat = + Matrix, S>; -pub struct CmpWiseProductContainer( - Matrix, - Matrix, +pub struct CmpWiseProductContainer( + Matrix, + Matrix, DefaultLayout, - PhantomData, - PhantomData, + PhantomData, ) where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait; + S: SizeIdentifier, + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait; impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > CmpWiseProductContainer + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > CmpWiseProductContainer { - pub fn new(mat1: Matrix, mat2: Matrix) -> Self { + pub fn new(mat1: Matrix, mat2: Matrix) -> Self { assert_eq!( mat1.layout().dim(), mat2.layout().dim(), @@ -47,23 +44,16 @@ impl< mat2.layout().dim() ); let dim = mat1.layout().dim(); - Self( - mat1, - mat2, - DefaultLayout::from_dimension(dim), - PhantomData, - PhantomData, - ) + Self(mat1, mat2, DefaultLayout::from_dimension(dim), PhantomData) } } impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for CmpWiseProductContainer + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > Layout for CmpWiseProductContainer { type Impl = DefaultLayout; @@ -74,23 +64,20 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for CmpWiseProductContainer + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > Size for CmpWiseProductContainer { - type C = CS; - type R = RS; + type S = S; } impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for CmpWiseProductContainer + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > UnsafeRandomAccessByValue for CmpWiseProductContainer { type Item = Item; @@ -107,22 +94,20 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for CmpWiseProductContainer + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > MatrixImplIdentifier for CmpWiseProductContainer { const MAT_IMPL: MatrixImplType = MatrixImplType::Derived; } impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccess for CmpWiseProductContainer + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > RawAccess for CmpWiseProductContainer { type T = Item; @@ -144,11 +129,10 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccessMut for CmpWiseProductContainer + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > RawAccessMut for CmpWiseProductContainer { #[inline] fn data_mut(&mut self) -> &mut [Self::T] { diff --git a/dense/src/op_containers/complex_mat.rs b/dense/src/op_containers/complex_mat.rs index 6e3141c5..71c46d2e 100644 --- a/dense/src/op_containers/complex_mat.rs +++ b/dense/src/op_containers/complex_mat.rs @@ -6,19 +6,16 @@ use crate::{matrix::*, DefaultLayout}; use std::marker::PhantomData; /// This type represents the Complex of a matrix. -pub type ComplexMat = - Matrix, RS, CS>; +pub type ComplexMat = Matrix, S>; -pub struct ComplexContainer( - Matrix<::Real, MatImpl, RS, CS>, - PhantomData, - PhantomData, +pub struct ComplexContainer( + Matrix<::Real, MatImpl, S>, + PhantomData, ) where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait<::Real, RS, CS>; + S: SizeIdentifier, + MatImpl: MatrixImplTrait<::Real, S>; macro_rules! complex_container_impl { ($scalar:ty) => { @@ -26,41 +23,28 @@ macro_rules! complex_container_impl { /// This struct implements [MatrixImplTrait] and acts like a matrix. /// However, random access returns the corresponding complex entries. - impl< - MatImpl: MatrixImplTrait<<$scalar as Scalar>::Real, RS, CS>, - RS: SizeIdentifier, - CS: SizeIdentifier, - > ComplexContainer<$scalar, MatImpl, RS, CS> + impl::Real, S>, S: SizeIdentifier> + ComplexContainer<$scalar, MatImpl, S> { - pub fn new(mat: Matrix<<$scalar as Scalar>::Real, MatImpl, RS, CS>) -> Self { - Self(mat, PhantomData, PhantomData) + pub fn new(mat: Matrix<<$scalar as Scalar>::Real, MatImpl, S>) -> Self { + Self(mat, PhantomData) } } - impl< - MatImpl: MatrixImplTrait<<$scalar as Scalar>::Real, RS, CS>, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for ComplexContainer<$scalar, MatImpl, RS, CS> + impl::Real, S>, S: SizeIdentifier> Size + for ComplexContainer<$scalar, MatImpl, S> { - type R = RS; - type C = CS; + type S = S; } - impl< - MatImpl: MatrixImplTrait<<$scalar as Scalar>::Real, RS, CS>, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for ComplexContainer<$scalar, MatImpl, RS, CS> + impl::Real, S>, S: SizeIdentifier> + MatrixImplIdentifier for ComplexContainer<$scalar, MatImpl, S> { const MAT_IMPL: MatrixImplType = MatrixImplType::Derived; } - impl< - MatImpl: MatrixImplTrait<<$scalar as Scalar>::Real, RS, CS>, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for ComplexContainer<$scalar, MatImpl, RS, CS> + impl::Real, S>, S: SizeIdentifier> Layout + for ComplexContainer<$scalar, MatImpl, S> { type Impl = DefaultLayout; @@ -70,11 +54,8 @@ macro_rules! complex_container_impl { } } - impl< - MatImpl: MatrixImplTrait<<$scalar as Scalar>::Real, RS, CS>, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for ComplexContainer<$scalar, MatImpl, RS, CS> + impl::Real, S>, S: SizeIdentifier> + UnsafeRandomAccessByValue for ComplexContainer<$scalar, MatImpl, S> { type Item = $scalar; @@ -89,11 +70,8 @@ macro_rules! complex_container_impl { } } - impl< - MatImpl: MatrixImplTrait<<$scalar as Scalar>::Real, RS, CS>, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccess for ComplexContainer<$scalar, MatImpl, RS, CS> + impl::Real, S>, S: SizeIdentifier> RawAccess + for ComplexContainer<$scalar, MatImpl, S> { type T = $scalar; @@ -113,11 +91,8 @@ macro_rules! complex_container_impl { } } - impl< - MatImpl: MatrixImplTrait<<$scalar as Scalar>::Real, RS, CS>, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccessMut for ComplexContainer<$scalar, MatImpl, RS, CS> + impl::Real, S>, S: SizeIdentifier> RawAccessMut + for ComplexContainer<$scalar, MatImpl, S> { #[inline] fn data_mut(&mut self) -> &mut [Self::T] { diff --git a/dense/src/op_containers/conjugate.rs b/dense/src/op_containers/conjugate.rs index 01b006fb..1ca64502 100644 --- a/dense/src/op_containers/conjugate.rs +++ b/dense/src/op_containers/conjugate.rs @@ -6,53 +6,37 @@ use crate::{matrix::*, DefaultLayout}; use std::marker::PhantomData; /// This type represents the conjugate of a matrix. -pub type ConjugateMat = - Matrix, RS, CS>; +pub type ConjugateMat = Matrix, S>; /// A structure holding a reference to the matrix. /// This struct implements [MatrixImplTrait] and acts like a matrix. /// However, random access returns the corresponding conjugate entries. -pub struct ConjugateContainer( - Matrix, +pub struct ConjugateContainer( + Matrix, PhantomData, - PhantomData, - PhantomData, + PhantomData, ) where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait; - -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > ConjugateContainer + S: SizeIdentifier, + MatImpl: MatrixImplTrait; + +impl, S: SizeIdentifier> + ConjugateContainer { - pub fn new(mat: Matrix) -> Self { - Self(mat, PhantomData, PhantomData, PhantomData) + pub fn new(mat: Matrix) -> Self { + Self(mat, PhantomData, PhantomData) } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for ConjugateContainer +impl, S: SizeIdentifier> Size + for ConjugateContainer { - type R = RS; - type C = CS; + type S = S; } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for ConjugateContainer +impl, S: SizeIdentifier> Layout + for ConjugateContainer { type Impl = DefaultLayout; @@ -62,12 +46,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for ConjugateContainer +impl, S: SizeIdentifier> UnsafeRandomAccessByValue + for ConjugateContainer { type Item = Item; @@ -82,22 +62,14 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for ConjugateContainer +impl, S: SizeIdentifier> MatrixImplIdentifier + for ConjugateContainer { const MAT_IMPL: MatrixImplType = MatrixImplType::Derived; } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccess for ConjugateContainer +impl, S: SizeIdentifier> RawAccess + for ConjugateContainer { type T = Item; @@ -117,12 +89,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccessMut for ConjugateContainer +impl, S: SizeIdentifier> RawAccessMut + for ConjugateContainer { #[inline] fn data_mut(&mut self) -> &mut [Self::T] { diff --git a/dense/src/op_containers/scalar_mult.rs b/dense/src/op_containers/scalar_mult.rs index 95605d90..8f85c1e3 100644 --- a/dense/src/op_containers/scalar_mult.rs +++ b/dense/src/op_containers/scalar_mult.rs @@ -12,55 +12,39 @@ use crate::{matrix::*, DefaultLayout}; use std::marker::PhantomData; /// This type represents the multiplication of a matrix with a scalar. -pub type ScalarProdMat = - Matrix, RS, CS>; +pub type ScalarProdMat = Matrix, S>; /// A structure holding a reference to the matrix and the scalar to be multiplied /// with it. This struct implements [MatrixImplTrait] and acts like a matrix. /// However, random access returns the corresponding matrix entry multiplied with /// the scalar. -pub struct ScalarMult( - Matrix, +pub struct ScalarMult( + Matrix, Item, PhantomData, - PhantomData, - PhantomData, + PhantomData, ) where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait; - -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > ScalarMult + S: SizeIdentifier, + MatImpl: MatrixImplTrait; + +impl, S: SizeIdentifier> + ScalarMult { - pub fn new(mat: Matrix, scalar: Item) -> Self { - Self(mat, scalar, PhantomData, PhantomData, PhantomData) + pub fn new(mat: Matrix, scalar: Item) -> Self { + Self(mat, scalar, PhantomData, PhantomData) } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for ScalarMult +impl, S: SizeIdentifier> Size + for ScalarMult { - type R = RS; - type C = CS; + type S = S; } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for ScalarMult +impl, S: SizeIdentifier> Layout + for ScalarMult { type Impl = DefaultLayout; @@ -70,22 +54,14 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for ScalarMult +impl, S: SizeIdentifier> MatrixImplIdentifier + for ScalarMult { const MAT_IMPL: MatrixImplType = MatrixImplType::Derived; } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccess for ScalarMult +impl, S: SizeIdentifier> RawAccess + for ScalarMult { type T = Item; @@ -105,12 +81,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccessMut for ScalarMult +impl, S: SizeIdentifier> RawAccessMut + for ScalarMult { #[inline] fn data_mut(&mut self) -> &mut [Self::T] { @@ -128,12 +100,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for ScalarMult +impl, S: SizeIdentifier> UnsafeRandomAccessByValue + for ScalarMult { type Item = Item; @@ -150,48 +118,40 @@ impl< macro_rules! scalar_mult_impl { ($Scalar:ty) => { - impl, RS: SizeIdentifier, CS: SizeIdentifier> - std::ops::Mul> for $Scalar + impl, S: SizeIdentifier> + std::ops::Mul> for $Scalar { - type Output = ScalarProdMat<$Scalar, MatImpl, RS, CS>; + type Output = ScalarProdMat<$Scalar, MatImpl, S>; - fn mul(self, rhs: Matrix<$Scalar, MatImpl, RS, CS>) -> Self::Output { + fn mul(self, rhs: Matrix<$Scalar, MatImpl, S>) -> Self::Output { Matrix::new(ScalarMult::new(rhs, self)) } } - impl< - 'a, - MatImpl: MatrixImplTrait<$Scalar, RS, CS>, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Mul<&'a Matrix<$Scalar, MatImpl, RS, CS>> for $Scalar + impl<'a, MatImpl: MatrixImplTrait<$Scalar, S>, S: SizeIdentifier> + std::ops::Mul<&'a Matrix<$Scalar, MatImpl, S>> for $Scalar { - type Output = ScalarProdMat<$Scalar, MatrixRef<'a, $Scalar, MatImpl, RS, CS>, RS, CS>; + type Output = ScalarProdMat<$Scalar, MatrixRef<'a, $Scalar, MatImpl, S>, S>; - fn mul(self, rhs: &'a Matrix<$Scalar, MatImpl, RS, CS>) -> Self::Output { + fn mul(self, rhs: &'a Matrix<$Scalar, MatImpl, S>) -> Self::Output { ScalarProdMat::new(ScalarMult::new(Matrix::from_ref(rhs), self)) } } - impl, RS: SizeIdentifier, CS: SizeIdentifier> - std::ops::Mul<$Scalar> for Matrix<$Scalar, MatImpl, RS, CS> + impl, S: SizeIdentifier> std::ops::Mul<$Scalar> + for Matrix<$Scalar, MatImpl, S> { - type Output = ScalarProdMat<$Scalar, MatImpl, RS, CS>; + type Output = ScalarProdMat<$Scalar, MatImpl, S>; fn mul(self, rhs: $Scalar) -> Self::Output { Matrix::new(ScalarMult::new(self, rhs)) } } - impl< - 'a, - MatImpl: MatrixImplTrait<$Scalar, RS, CS>, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Mul<$Scalar> for &'a Matrix<$Scalar, MatImpl, RS, CS> + impl<'a, MatImpl: MatrixImplTrait<$Scalar, S>, S: SizeIdentifier> std::ops::Mul<$Scalar> + for &'a Matrix<$Scalar, MatImpl, S> { - type Output = ScalarProdMat<$Scalar, MatrixRef<'a, $Scalar, MatImpl, RS, CS>, RS, CS>; + type Output = ScalarProdMat<$Scalar, MatrixRef<'a, $Scalar, MatImpl, S>, S>; fn mul(self, rhs: $Scalar) -> Self::Output { ScalarProdMat::new(ScalarMult::new(Matrix::from_ref(self), rhs)) @@ -213,7 +173,7 @@ mod test { #[test] fn scalar_mult() { - let mut mat = crate::rlst_mat![f64, (2, 3)]; + let mut mat = crate::rlst_dynamic_mat![f64, (2, 3)]; *mat.get_mut(1, 2).unwrap() = 5.0; diff --git a/dense/src/op_containers/subtraction.rs b/dense/src/op_containers/subtraction.rs index f69abf1f..a3f0b5ba 100644 --- a/dense/src/op_containers/subtraction.rs +++ b/dense/src/op_containers/subtraction.rs @@ -13,32 +13,29 @@ use crate::DefaultLayout; use std::marker::PhantomData; /// A type that represents the sum of two matrices. -pub type SubtractionMat = - Matrix, RS, CS>; +pub type SubtractionMat = + Matrix, S>; -pub struct Subtraction( - Matrix, - Matrix, +pub struct Subtraction( + Matrix, + Matrix, DefaultLayout, - PhantomData, - PhantomData, + PhantomData, ) where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait; + S: SizeIdentifier, + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait; impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Subtraction + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > Subtraction { - pub fn new(mat1: Matrix, mat2: Matrix) -> Self { + pub fn new(mat1: Matrix, mat2: Matrix) -> Self { assert_eq!( mat1.layout().dim(), mat2.layout().dim(), @@ -47,23 +44,16 @@ impl< mat2.layout().dim() ); let dim = mat1.layout().dim(); - Self( - mat1, - mat2, - DefaultLayout::from_dimension(dim), - PhantomData, - PhantomData, - ) + Self(mat1, mat2, DefaultLayout::from_dimension(dim), PhantomData) } } impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for Subtraction + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > Layout for Subtraction { type Impl = DefaultLayout; @@ -74,34 +64,30 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for Subtraction + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > Size for Subtraction { - type C = CS; - type R = RS; + type S = S; } impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for Subtraction + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > MatrixImplIdentifier for Subtraction { const MAT_IMPL: MatrixImplType = MatrixImplType::Derived; } impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccess for Subtraction + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > RawAccess for Subtraction { type T = Item; @@ -123,11 +109,10 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccessMut for Subtraction + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > RawAccessMut for Subtraction { #[inline] fn data_mut(&mut self) -> &mut [Self::T] { @@ -147,11 +132,10 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for Subtraction + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > UnsafeRandomAccessByValue for Subtraction { type Item = Item; @@ -168,15 +152,14 @@ impl< impl< Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Sub> for Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > std::ops::Sub> for Matrix { - type Output = SubtractionMat; + type Output = SubtractionMat; - fn sub(self, rhs: Matrix) -> Self::Output { + fn sub(self, rhs: Matrix) -> Self::Output { Matrix::new(Subtraction::new(self, rhs)) } } @@ -184,15 +167,14 @@ impl< impl< 'a, Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Sub<&'a Matrix> for Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > std::ops::Sub<&'a Matrix> for Matrix { - type Output = SubtractionMat, RS, CS>; + type Output = SubtractionMat, S>; - fn sub(self, rhs: &'a Matrix) -> Self::Output { + fn sub(self, rhs: &'a Matrix) -> Self::Output { Matrix::new(Subtraction::new(self, Matrix::from_ref(rhs))) } } @@ -200,15 +182,14 @@ impl< impl< 'a, Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Sub> for &'a Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > std::ops::Sub> for &'a Matrix { - type Output = SubtractionMat, MatImpl2, RS, CS>; + type Output = SubtractionMat, MatImpl2, S>; - fn sub(self, rhs: Matrix) -> Self::Output { + fn sub(self, rhs: Matrix) -> Self::Output { Matrix::new(Subtraction::new(Matrix::from_ref(self), rhs)) } } @@ -216,21 +197,15 @@ impl< impl< 'a, Item: Scalar, - MatImpl1: MatrixImplTrait, - MatImpl2: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > std::ops::Sub<&'a Matrix> for &'a Matrix + MatImpl1: MatrixImplTrait, + MatImpl2: MatrixImplTrait, + S: SizeIdentifier, + > std::ops::Sub<&'a Matrix> for &'a Matrix { - type Output = SubtractionMat< - Item, - MatrixRef<'a, Item, MatImpl1, RS, CS>, - MatrixRef<'a, Item, MatImpl2, RS, CS>, - RS, - CS, - >; - - fn sub(self, rhs: &'a Matrix) -> Self::Output { + type Output = + SubtractionMat, MatrixRef<'a, Item, MatImpl2, S>, S>; + + fn sub(self, rhs: &'a Matrix) -> Self::Output { Matrix::new(Subtraction::new( Matrix::from_ref(self), Matrix::from_ref(rhs), @@ -246,8 +221,8 @@ mod test { #[test] fn scalar_mult() { - let mut mat1 = crate::rlst_mat![f64, (2, 3)]; - let mut mat2 = crate::rlst_mat![f64, (2, 3)]; + let mut mat1 = crate::rlst_dynamic_mat![f64, (2, 3)]; + let mut mat2 = crate::rlst_dynamic_mat![f64, (2, 3)]; mat1[[1, 2]] = 5.0; mat2[[1, 2]] = 6.0; diff --git a/dense/src/op_containers/transpose.rs b/dense/src/op_containers/transpose.rs index 6d04b06c..6e89e43d 100644 --- a/dense/src/op_containers/transpose.rs +++ b/dense/src/op_containers/transpose.rs @@ -6,65 +6,45 @@ use crate::{matrix::*, DefaultLayout}; use std::marker::PhantomData; /// This type represents the conjugate of a matrix. -pub type TransposeMat = - Matrix, RS, CS>; +pub type TransposeMat = Matrix, S>; /// A structure holding a reference to the matrix. /// This struct implements [MatrixImplTrait] and acts like a matrix. /// However, random access returns the corresponding conjugate entries. -pub struct TransposeContainer( - Matrix, +pub struct TransposeContainer( + Matrix, DefaultLayout, PhantomData, - PhantomData, - PhantomData, + PhantomData, ) where Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - MatImpl: MatrixImplTrait; - -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > TransposeContainer + S: SizeIdentifier, + MatImpl: MatrixImplTrait; + +impl, S: SizeIdentifier> + TransposeContainer { - pub fn new(mat: Matrix) -> Self { + pub fn new(mat: Matrix) -> Self { let layout = DefaultLayout::from_dimension((mat.shape().1, mat.shape().0)); - Self(mat, layout, PhantomData, PhantomData, PhantomData) + Self(mat, layout, PhantomData, PhantomData) } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > SizeType for TransposeContainer +impl, S: SizeIdentifier> Size + for TransposeContainer { - type R = RS; - type C = CS; + type S = S; } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > MatrixImplIdentifier for TransposeContainer +impl, S: SizeIdentifier> MatrixImplIdentifier + for TransposeContainer { const MAT_IMPL: MatrixImplType = MatrixImplType::Derived; } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccess for TransposeContainer +impl, S: SizeIdentifier> RawAccess + for TransposeContainer { type T = Item; @@ -84,12 +64,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > RawAccessMut for TransposeContainer +impl, S: SizeIdentifier> RawAccessMut + for TransposeContainer { #[inline] fn data_mut(&mut self) -> &mut [Self::T] { @@ -107,12 +83,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > Layout for TransposeContainer +impl, S: SizeIdentifier> Layout + for TransposeContainer { type Impl = DefaultLayout; @@ -122,12 +94,8 @@ impl< } } -impl< - Item: Scalar, - MatImpl: MatrixImplTrait, - RS: SizeIdentifier, - CS: SizeIdentifier, - > UnsafeRandomAccessByValue for TransposeContainer +impl, S: SizeIdentifier> UnsafeRandomAccessByValue + for TransposeContainer { type Item = Item; diff --git a/dense/src/traits/matrix.rs b/dense/src/traits/matrix.rs index 205200a5..8f7cd253 100644 --- a/dense/src/traits/matrix.rs +++ b/dense/src/traits/matrix.rs @@ -2,7 +2,7 @@ //! //! [MatrixImplTrait] and its mutable counterpart [MatrixImplTraitMut] are //! traits that define matrix implementations. The [MatrixImplTrait] is auto-implemented -//! for any implementation that supports [UnsafeRandomAccessByValue], [Layout] and [SizeType]. +//! for any implementation that supports [UnsafeRandomAccessByValue] and [Layout]. //! If the matrix elements are associated with a physical memory location one can implement //! [UnsafeRandomAccessByRef]. If also [MatrixImplTrait] is implemented the trait //! [MatrixImplTraitAccessByRef] is then auto-implemented. This marks matrix implementations @@ -11,7 +11,7 @@ //! [UnsafeRandomAccessMut] are implemented. //! use crate::traits::{ - Layout, SizeIdentifier, SizeType, UnsafeRandomAccessByRef, UnsafeRandomAccessByValue, + Layout, Size, SizeIdentifier, UnsafeRandomAccessByRef, UnsafeRandomAccessByValue, UnsafeRandomAccessMut, }; use crate::types::Scalar; @@ -33,52 +33,49 @@ pub trait MatrixImplIdentifier { /// Combined trait for basic matrix properties. See [crate::traits::matrix] /// for details. -pub trait MatrixImplTrait: +pub trait MatrixImplTrait: UnsafeRandomAccessByValue + Layout - + SizeType + MatrixImplIdentifier + + Size { } /// Extended Matrix trait if access by reference is possible. -pub trait MatrixImplTraitAccessByRef: - UnsafeRandomAccessByRef + MatrixImplTrait +pub trait MatrixImplTraitAccessByRef: + UnsafeRandomAccessByRef + MatrixImplTrait { } /// Combined trait for mutable matrices. See [crate::traits::matrix] for details. -pub trait MatrixImplTraitMut: - UnsafeRandomAccessMut + MatrixImplTrait +pub trait MatrixImplTraitMut: + UnsafeRandomAccessMut + MatrixImplTrait { } impl< Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, + S: SizeIdentifier, Mat: UnsafeRandomAccessByValue + Layout - + SizeType + + Size + MatrixImplIdentifier, - > MatrixImplTrait for Mat + > MatrixImplTrait for Mat { } impl< Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - Mat: UnsafeRandomAccessByRef + MatrixImplTrait, - > MatrixImplTraitAccessByRef for Mat + S: SizeIdentifier, + Mat: UnsafeRandomAccessByRef + MatrixImplTrait, + > MatrixImplTraitAccessByRef for Mat { } impl< Item: Scalar, - RS: SizeIdentifier, - CS: SizeIdentifier, - Mat: MatrixImplTrait + UnsafeRandomAccessMut, - > MatrixImplTraitMut for Mat + S: SizeIdentifier, + Mat: MatrixImplTrait + UnsafeRandomAccessMut, + > MatrixImplTraitMut for Mat { } diff --git a/dense/src/traits/size.rs b/dense/src/traits/size.rs index e6921531..54181544 100644 --- a/dense/src/traits/size.rs +++ b/dense/src/traits/size.rs @@ -1,23 +1,13 @@ //! Definition of Size Traits. //! -//! Each matrix in RLST has two size identifiers, one for the rows -//! and one for the columns. The size identifiers determine if a row/column -//! size is fixed at compile time or dynamically determined at runtime. -//! The [SizeType] trait implements this functionality. It has two associated types, -//! the row size [SizeType::R] and the column size [SizeType::C]. Both are expected to -//! be types that implement the [SizeIdentifier] trait. A [SizeIdentifier] has an associated -//! [usize] constant [SizeIdentifier::N] that gives compile time information on the actual -//! size. The following data types are implemented that have size types //! -//! - [Fixed1]. This type specifies a row/column of fixed dimension 1. //! - [Fixed2]. This type specifies a row/column of fixed dimension 2. //! - [Fixed3]. This type specifies a row/column of fixed dimension 3. //! - [Dynamic]. This type specifies a row/column dimension defined at runtime. -//! The corresponding constant [SizeIdentifier::N] is set to 0. //! -/// Fixed Dimension 1. -pub struct Fixed1; +use crate::{base_matrix::BaseMatrix, DefaultLayout, LayoutType, VectorContainer}; +use rlst_common::types::Scalar; /// Fixed Dimension 2. pub struct Fixed2; @@ -28,32 +18,44 @@ pub struct Fixed3; /// Dimension determined at runtime. pub struct Dynamic; -/// This trait provides a constant [SizeIdentifier::N] from -/// which returns a dimension parameter. For `N` > 0 it specifies -/// a compile time dimension. In the case `N` == 0 the dimension is -/// a runtime parameter and not known at compile time. pub trait SizeIdentifier { - const N: usize; + const SIZE: SizeIdentifierValue; } -impl SizeIdentifier for Fixed1 { - const N: usize = 1; +pub trait MatrixBuilder { + type Out; + + fn new_matrix(dim: (usize, usize)) -> Self::Out; +} + +pub trait Size { + type S: SizeIdentifier; } impl SizeIdentifier for Fixed2 { - const N: usize = 2; + const SIZE: SizeIdentifierValue = SizeIdentifierValue::Static(2, 2); } + impl SizeIdentifier for Fixed3 { - const N: usize = 3; + const SIZE: SizeIdentifierValue = SizeIdentifierValue::Static(3, 3); } + impl SizeIdentifier for Dynamic { - const N: usize = 0; + const SIZE: SizeIdentifierValue = SizeIdentifierValue::Dynamic; +} + +impl MatrixBuilder for Dynamic { + type Out = crate::MatrixD; + + fn new_matrix(dim: (usize, usize)) -> Self::Out { + >::new(BaseMatrix::new( + VectorContainer::new(dim.0 * dim.1), + DefaultLayout::from_dimension(dim), + )) + } } -/// This trait provides information about the row -/// dimension type [SizeType::R] and the column dimension -/// type [SizeType::C]. -pub trait SizeType { - type R: SizeIdentifier; - type C: SizeIdentifier; +pub enum SizeIdentifierValue { + Dynamic, + Static(usize, usize), } diff --git a/io/src/matrix_market.rs b/io/src/matrix_market.rs index daa70d71..94cd9e67 100644 --- a/io/src/matrix_market.rs +++ b/io/src/matrix_market.rs @@ -194,7 +194,7 @@ pub fn read_array_mm(fname: &str) -> RlstResult> { } } - let mut mat = rlst_dense::rlst_mat!(T, (nrows, ncols)); + let mut mat = rlst_dense::rlst_dynamic_mat!(T, (nrows, ncols)); let res = parse_array(&mut reader, mat.data_mut(), nrows * ncols); if let Err(e) = res { diff --git a/proc-macro/Cargo.toml b/proc-macro/Cargo.toml new file mode 100644 index 00000000..f154d1da --- /dev/null +++ b/proc-macro/Cargo.toml @@ -0,0 +1,26 @@ +[features] +strict = [] + +[package] +name = "rlst-proc-macro" +version = "0.0.1" +authors = ["The linalg-rs contributors"] +edition = "2021" +description = "A Rust native dense linear algebra library." +license = "MIT + Apache 2.0" +homepage = "https://github.com/linalg-rs" +repository = "https://github.com/linalg-rs/rlst" +readme = "README.md" +keywords = ["numerics"] +categories = ["mathematics", "science"] + +[lib] +proc-macro = true + +[dependencies] +syn = { version = "1.0", features = ["full"]} +quote = "1.0" + + + + diff --git a/proc-macro/src/dense.rs b/proc-macro/src/dense.rs new file mode 100644 index 00000000..bf711df3 --- /dev/null +++ b/proc-macro/src/dense.rs @@ -0,0 +1,39 @@ +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, ItemStruct}; + +pub(crate) fn rlst_static_size_impl(args: TokenStream, input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as ItemStruct); + let struct_name = input.ident.clone(); + + let args = args.to_string(); + + let dims: Vec<&str> = args.split(',').map(|elem| elem.trim()).collect(); + let m = dims[0].parse::().unwrap(); + let n = dims[1].parse::().unwrap(); + + let output = quote! { + #input + + impl rlst_dense::traits::SizeIdentifier for #struct_name { + const SIZE: rlst_dense::traits::SizeIdentifierValue = rlst_dense::traits::SizeIdentifierValue::Static(#m, #n); + } + + impl rlst_dense::traits::MatrixBuilder for #struct_name { + type Out = rlst_dense::GenericBaseMatrix::, #struct_name>; + + + fn new_matrix(dim: (usize, usize)) -> Self::Out { + + assert_eq!(dim, (#m, #n), "Expected fixed dimension ({}, {}) for static matrix.", #m, #n); + use rlst_dense::LayoutType; + ::from_data(rlst_dense::ArrayContainer::::new(), + rlst_dense::DefaultLayout::from_dimension((#m, #n)), + ) + + } + } + + }; + output.into() +} diff --git a/proc-macro/src/lib.rs b/proc-macro/src/lib.rs new file mode 100644 index 00000000..54497ec4 --- /dev/null +++ b/proc-macro/src/lib.rs @@ -0,0 +1,10 @@ +//! Proc Macros for the rlst library + +use proc_macro::TokenStream; + +mod dense; + +#[proc_macro_attribute] +pub fn rlst_static_size(args: TokenStream, input: TokenStream) -> TokenStream { + dense::rlst_static_size_impl(args, input) +} diff --git a/rlst/Cargo.toml b/rlst/Cargo.toml index 1b3be80f..965d1947 100644 --- a/rlst/Cargo.toml +++ b/rlst/Cargo.toml @@ -25,6 +25,7 @@ rlst-dense = { path = "../dense"} rlst-algorithms = { path = "../algorithms"} rlst-blis = { path = "../blis"} rlst-umfpack = { path = "../umfpack"} +rlst-proc-macro = { path = "../proc-macro"} diff --git a/rlst/examples/fixed_size_matrix.rs b/rlst/examples/fixed_size_matrix.rs new file mode 100644 index 00000000..bd9e642e --- /dev/null +++ b/rlst/examples/fixed_size_matrix.rs @@ -0,0 +1,22 @@ +//! Fixed size matrix example +//! + +use rlst_dense::Shape; +use rlst_dense::SizeIdentifier; +use rlst_proc_macro::rlst_static_size; + +#[rlst_static_size(33, 4)] +pub struct MySizeType; + +pub fn main() { + match MySizeType::SIZE { + rlst_dense::SizeIdentifierValue::Static(m, n) => println!("{:#?}", (m, n)), + rlst_dense::SizeIdentifierValue::Dynamic => println!("Dynamic"), + } + + let mat = rlst_dense::rlst_static_mat!(f64, MySizeType); + let mat_dynamic: rlst_dense::Matrix = rlst_dense::rlst_dynamic_mat!(f64, (30, 40)); + + println!("{:#?}", mat.shape()); + println!("{:#?}", mat_dynamic.shape()); +} diff --git a/umfpack/examples/umfpack_simple.rs b/umfpack/examples/umfpack_simple.rs index b5f52cea..2e23c69d 100644 --- a/umfpack/examples/umfpack_simple.rs +++ b/umfpack/examples/umfpack_simple.rs @@ -6,10 +6,10 @@ use rlst_umfpack as umfpack; pub fn main() { let n = 5; - let ai = vec![0, 1, 0, 2, 4, 1, 2, 3, 4, 2, 1, 4]; - let ap = vec![0, 2, 5, 9, 10, 12]; - let ax = vec![2.0, 3.0, 3.0, -1.0, 4.0, 4.0, -3.0, 1.0, 2.0, 2.0, 6.0, 1.0]; - let b = vec![8.0, 45.0, -3.0, 3.0, 19.0]; + let ai = [0, 1, 0, 2, 4, 1, 2, 3, 4, 2, 1, 4]; + let ap = [0, 2, 5, 9, 10, 12]; + let ax = [2.0, 3.0, 3.0, -1.0, 4.0, 4.0, -3.0, 1.0, 2.0, 2.0, 6.0, 1.0]; + let b = [8.0, 45.0, -3.0, 3.0, 19.0]; let mut symbolic = std::ptr::null_mut::(); let mut numeric = std::ptr::null_mut::(); let mut x = vec![0.0; 5];