From 40e74e01866ea4e5f5967a0ef215afa5ab6d7fc0 Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Sat, 19 May 2018 18:05:56 +0200 Subject: [PATCH] Fix compilation with and without #![no_std]. --- CHANGELOG.md | 3 ++ Cargo.toml | 2 +- src/base/blas.rs | 2 +- src/base/matrix.rs | 6 +++ src/base/matrix_alga.rs | 65 +++++++++++++++++------------- src/linalg/permutation_sequence.rs | 1 + 6 files changed, 50 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57063ccd..7ed2fddd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ Some feature are no longer available when libstd is not used: * Support for dynamically-sized matrices. * Support for the `::new_random()` matrix constructor. * Support for the `.resize(...)` method since it returns a dynamically-sized matrix. + * Entries of matrices displayed using `println!("{}", matrix)` will not be correctly aligned vertically. + * The computation of the orthogonormal subspace basis of a vector is limited to vector with dimension up + to 3 (we will attempt to lift this restriction in a future release). All other feature, including matrix factorizations, will still work on statically-sized matrices! ### Modified * Rename the `core` module to `base` to avoid conflicts with the `core` crate implicitly imported when diff --git a/Cargo.toml b/Cargo.toml index c7444cf6..3cf46f4d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ path = "src/lib.rs" [features] default = [ "std" ] -std = [ "matrixmultiply" ] +std = [ "matrixmultiply", "rand/std" ] arbitrary = [ "quickcheck" ] serde-serialize = [ "serde", "serde_derive", "num-complex/serde" ] abomonation-serialize = [ "abomonation" ] diff --git a/src/base/blas.rs b/src/base/blas.rs index 71d1e1f5..b73367a5 100644 --- a/src/base/blas.rs +++ b/src/base/blas.rs @@ -459,7 +459,7 @@ where + SameNumberOfColumns + AreMultipliable, { - let ncols1 = self.shape(); + let ncols1 = self.ncols(); #[cfg(feature = "std")] { diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 9be0cc32..308e61a8 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -919,6 +919,7 @@ where DefaultAllocator: Allocator, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + #[cfg(feature = "std")] fn val_width(val: N, f: &mut fmt::Formatter) -> usize { match f.precision() { Some(precision) => format!("{:.1$}", val, precision).chars().count(), @@ -926,6 +927,11 @@ where } } + #[cfg(not(feature = "std"))] + fn val_width(val: N, f: &mut fmt::Formatter) -> usize { + 4 + } + let (nrows, ncols) = self.data.shape(); if nrows.value() == 0 || ncols.value() == 0 { diff --git a/src/base/matrix_alga.rs b/src/base/matrix_alga.rs index 2630eaf9..68341366 100644 --- a/src/base/matrix_alga.rs +++ b/src/base/matrix_alga.rs @@ -1,16 +1,19 @@ use num::{One, Zero}; -use alga::general::{AbstractGroup, AbstractGroupAbelian, AbstractLoop, AbstractMagma, - AbstractModule, AbstractMonoid, AbstractQuasigroup, AbstractSemigroup, - Additive, ClosedAdd, ClosedMul, ClosedNeg, Field, Identity, Inverse, - JoinSemilattice, Lattice, MeetSemilattice, Module, Multiplicative, Real, - RingCommutative}; -use alga::linear::{FiniteDimInnerSpace, FiniteDimVectorSpace, InnerSpace, NormedSpace, VectorSpace}; +use alga::general::{ + AbstractGroup, AbstractGroupAbelian, AbstractLoop, AbstractMagma, AbstractModule, + AbstractMonoid, AbstractQuasigroup, AbstractSemigroup, Additive, ClosedAdd, ClosedMul, + ClosedNeg, Field, Identity, Inverse, JoinSemilattice, Lattice, MeetSemilattice, Module, + Multiplicative, Real, RingCommutative, +}; +use alga::linear::{ + FiniteDimInnerSpace, FiniteDimVectorSpace, InnerSpace, NormedSpace, VectorSpace, +}; -use base::{DefaultAllocator, MatrixMN, MatrixN, Scalar}; +use base::allocator::Allocator; use base::dimension::{Dim, DimName}; use base::storage::{Storage, StorageMut}; -use base::allocator::Allocator; +use base::{DefaultAllocator, MatrixMN, MatrixN, Scalar}; /* * @@ -283,27 +286,35 @@ where } } _ => { - // XXX: use a GenericArray instead. - let mut known_basis = Vec::new(); + #[cfg(any(feature = "std", feature = "alloc"))] + { + // XXX: use a GenericArray instead. + let mut known_basis = Vec::new(); - for v in vs.iter() { - known_basis.push(v.normalize()) + for v in vs.iter() { + known_basis.push(v.normalize()) + } + + for i in 0..Self::dimension() - vs.len() { + let mut elt = Self::canonical_basis_element(i); + + for v in &known_basis { + elt -= v * elt.dot(v) + } + + if let Some(subsp_elt) = elt.try_normalize(N::zero()) { + if !f(&subsp_elt) { + return; + }; + + known_basis.push(subsp_elt); + } + } } - - for i in 0..Self::dimension() - vs.len() { - let mut elt = Self::canonical_basis_element(i); - - for v in &known_basis { - elt -= v * elt.dot(v) - } - - if let Some(subsp_elt) = elt.try_normalize(N::zero()) { - if !f(&subsp_elt) { - return; - }; - - known_basis.push(subsp_elt); - } + #[cfg(all(not(feature = "std"), not(feature = "alloc")))] + { + panic!("Cannot compute the orthogonal subspace basis of a vector with a dimension greater than 3 \ + if #![no_std] is enabled and the 'alloc' feature is not enabled.") } } } diff --git a/src/linalg/permutation_sequence.rs b/src/linalg/permutation_sequence.rs index 34f451c8..bb9576a2 100644 --- a/src/linalg/permutation_sequence.rs +++ b/src/linalg/permutation_sequence.rs @@ -56,6 +56,7 @@ where } } +#[cfg(any(feature = "std", feature = "alloc"))] impl PermutationSequence where DefaultAllocator: Allocator<(usize, usize), Dynamic>,