Skip to content

Commit

Permalink
Better size identifiers (#48)
Browse files Browse the repository at this point in the history
* Experimenting with size identifiers

* WIP: Proc macros for fixed size matrices.

* WIP: static proc macros

* Some clean-up

* Static matrix definition working

* Updated resolver and fixed clippy warnings

* Fixed doc issue.

* Fixed doc
  • Loading branch information
tbetcke authored Sep 13, 2023
1 parent 0032641 commit b471004
Show file tree
Hide file tree
Showing 47 changed files with 1,006 additions and 1,504 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"

members = [
"dense",
Expand All @@ -14,4 +15,5 @@ members = [
"netlib-lapack-src",
"blis",
"rlst",
"proc-macro",
]
4 changes: 2 additions & 2 deletions algorithms/src/dense/norm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions algorithms/src/lapack/cholesky_decomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]];
Expand All @@ -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();
Expand All @@ -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]];
Expand All @@ -138,9 +138,9 @@ where
result
}

fn solve<MatImpl: MatrixImplTrait<Self::T, RS, CS>, RS: SizeIdentifier, CS: SizeIdentifier>(
fn solve<MatImpl: MatrixImplTrait<Self::T, S>, S: SizeIdentifier>(
&self,
rhs: &Matrix<Self::T, MatImpl, RS, CS>,
rhs: &Matrix<Self::T, MatImpl, S>,
) -> RlstResult<Self::Sol>
// where
// Matrix<Self::T, MatImpl, RS, CS>: TriangularSolve<T = Self::T>,
Expand Down Expand Up @@ -196,7 +196,7 @@ mod test {
paste! {
#[test]
fn [<test_cholesky_$scalar>]() {
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();
Expand All @@ -223,7 +223,7 @@ mod test {

#[test]
fn [<test_cholesky_solve_$scalar>]() {
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);
Expand Down
40 changes: 22 additions & 18 deletions algorithms/src/lapack/evd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ 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;
vl_data = vl_dummy.as_mut_slice();
}
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;
Expand Down Expand Up @@ -141,8 +141,10 @@ macro_rules! implement_evd_real {
.map(|(&re, &im)| <<$scalar as Scalar>::Complex>::new(re, im))
.collect::<Vec<<$scalar as Scalar>::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,
Expand All @@ -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,
Expand Down Expand Up @@ -210,23 +214,23 @@ 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;
vl_data = vl_dummy.as_mut_slice();
}
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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -357,15 +361,15 @@ mod test {
paste! {
#[test]
pub fn [<test_ev_$scalar>]() {
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) =
rlst_mat.linalg().evd(EigenvectorMode::All).unwrap();
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();
Expand All @@ -388,15 +392,15 @@ mod test {
}
#[test]
pub fn [<test_sym_ev_$scalar>]() {
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();

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
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/lapack/inverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod test {
paste! {
#[test]
fn [<test_inverse_$scalar>]() {
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);

Expand Down
14 changes: 7 additions & 7 deletions algorithms/src/lapack/lu_decomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -72,7 +72,7 @@ macro_rules! lu_decomp_impl {
fn get_l(&self) -> MatrixD<Self::T> {
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 {
Expand Down Expand Up @@ -100,7 +100,7 @@ macro_rules! lu_decomp_impl {
fn get_u(&self) -> MatrixD<Self::T> {
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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
20 changes: 10 additions & 10 deletions algorithms/src/lapack/qr_decomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -215,7 +215,7 @@ macro_rules! qr_decomp_impl {
fn r(&self) -> RlstResult<MatrixD<Self::T>> {
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 {
Expand Down Expand Up @@ -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) => {
Expand All @@ -261,7 +261,7 @@ mod test {
fn [<test_thick_qr_ $ScalarType _ $PivotMode>]() {
// 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;
Expand Down Expand Up @@ -308,7 +308,7 @@ mod test {
fn [<test_thin_qr_ $ScalarType _ $PivotMode>]() {
// 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;
Expand Down Expand Up @@ -364,10 +364,10 @@ mod test {
fn [<test_least_squares_solve_thin_ $ScalarType _ $PivotMode>]() {
// 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);
Expand Down Expand Up @@ -401,7 +401,7 @@ mod test {

#[test]
fn [<test_least_squares_solve_thick_no_conj_trans_ $ScalarType _ $PivotMode>]() {
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];
Expand Down
Loading

0 comments on commit b471004

Please sign in to comment.