From 87a6f4965bcc196454eb095e69ba10c10fe0c660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sun, 9 Jan 2022 13:43:45 +0100 Subject: [PATCH 01/81] Add support for conversion with glam 0.19 and 0.20 --- Cargo.toml | 4 ++++ src/third_party/glam/mod.rs | 4 ++++ src/third_party/glam/v019/mod.rs | 18 ++++++++++++++++++ src/third_party/glam/v020/mod.rs | 18 ++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 src/third_party/glam/v019/mod.rs create mode 100644 src/third_party/glam/v020/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 8294398e..4ecafc59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,8 @@ convert-glam015 = [ "glam015" ] convert-glam016 = [ "glam016" ] convert-glam017 = [ "glam017" ] convert-glam018 = [ "glam018" ] +convert-glam019 = [ "glam019" ] +convert-glam020 = [ "glam020" ] # Serialization ## To use serde in a #[no-std] environment, enable the @@ -94,6 +96,8 @@ glam015 = { package = "glam", version = "0.15", optional = true } glam016 = { package = "glam", version = "0.16", optional = true } glam017 = { package = "glam", version = "0.17", optional = true } glam018 = { package = "glam", version = "0.18", optional = true } +glam019 = { package = "glam", version = "0.19", optional = true } +glam020 = { package = "glam", version = "0.20", optional = true } [target.'cfg(not(target_os = "cuda"))'.dependencies] cust = { version = "0.2", optional = true } diff --git a/src/third_party/glam/mod.rs b/src/third_party/glam/mod.rs index b1006e91..d24ff7e5 100644 --- a/src/third_party/glam/mod.rs +++ b/src/third_party/glam/mod.rs @@ -10,3 +10,7 @@ mod v016; mod v017; #[cfg(feature = "glam018")] mod v018; +#[cfg(feature = "glam019")] +mod v019; +#[cfg(feature = "glam020")] +mod v020; diff --git a/src/third_party/glam/v019/mod.rs b/src/third_party/glam/v019/mod.rs new file mode 100644 index 00000000..8b155607 --- /dev/null +++ b/src/third_party/glam/v019/mod.rs @@ -0,0 +1,18 @@ +#[path = "../common/glam_isometry.rs"] +mod glam_isometry; +#[path = "../common/glam_matrix.rs"] +mod glam_matrix; +#[path = "../common/glam_point.rs"] +mod glam_point; +#[path = "../common/glam_quaternion.rs"] +mod glam_quaternion; +#[path = "../common/glam_rotation.rs"] +mod glam_rotation; +#[path = "../common/glam_similarity.rs"] +mod glam_similarity; +#[path = "../common/glam_translation.rs"] +mod glam_translation; +#[path = "../common/glam_unit_complex.rs"] +mod glam_unit_complex; + +pub(self) use glam019 as glam; diff --git a/src/third_party/glam/v020/mod.rs b/src/third_party/glam/v020/mod.rs new file mode 100644 index 00000000..99fbc602 --- /dev/null +++ b/src/third_party/glam/v020/mod.rs @@ -0,0 +1,18 @@ +#[path = "../common/glam_isometry.rs"] +mod glam_isometry; +#[path = "../common/glam_matrix.rs"] +mod glam_matrix; +#[path = "../common/glam_point.rs"] +mod glam_point; +#[path = "../common/glam_quaternion.rs"] +mod glam_quaternion; +#[path = "../common/glam_rotation.rs"] +mod glam_rotation; +#[path = "../common/glam_similarity.rs"] +mod glam_similarity; +#[path = "../common/glam_translation.rs"] +mod glam_translation; +#[path = "../common/glam_unit_complex.rs"] +mod glam_unit_complex; + +pub(self) use glam020 as glam; From 6b1e14c8a2790ce8dc6a861085df2260514ad34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sun, 9 Jan 2022 14:37:25 +0100 Subject: [PATCH 02/81] Release v0.30.1 --- CHANGELOG.md | 4 ++++ Cargo.toml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88006c2e..eacace4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ documented here. This project adheres to [Semantic Versioning](https://semver.org/). +## [0.30.1] (09 Jan. 2022) +### Added +- Add conversion from/to types of `glam` 0.19 and 0.20. + ## [0.30.0] (02 Jan. 2022) ### Breaking changes diff --git a/Cargo.toml b/Cargo.toml index 4ecafc59..13f0584c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nalgebra" -version = "0.30.0" +version = "0.30.1" authors = [ "Sébastien Crozet " ] description = "General-purpose linear algebra library with transformations and statically-sized or dynamically-sized matrices." From 6cc633474dddb91bf8da84532d3ae62b1a1b70d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sun, 9 Jan 2022 14:40:39 +0100 Subject: [PATCH 03/81] Update changelog. --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eacace4d..bf6b6a0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ This project adheres to [Semantic Versioning](https://semver.org/). - The `Dim` trait is now marked as unsafe. - The `Matrix::pow` and `Matrix::pow_mut` methods only allow positive integer exponents now. To compute negative exponents, the user is free to invert the matrix before calling `pow` with the exponent’s absolute value. +- Remove the `Bounded` requirement from `RealField`. Replace it by methods returning `Option` so that they can + still be implemented by unbounded types (by returning `None`). +- The `ComplexField` trait derives from `FromPrimitive` again. We can actually keep this because all its methods + return `Option`, meaning that it could be implemented by any type. ### Modified - Use more concise debug impls for matrices and geometric transformation types. From f9aca24b1567115e23d0968d227f0b2e7f1f97ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20L=C3=B6schner?= Date: Sun, 7 Nov 2021 21:32:11 +0100 Subject: [PATCH 04/81] Implement Serialize and Deserialize for CsrMatrix --- nalgebra-sparse/Cargo.toml | 5 ++- nalgebra-sparse/src/csr.rs | 64 ++++++++++++++++++++++++++++++++++ nalgebra-sparse/tests/serde.rs | 62 ++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 nalgebra-sparse/tests/serde.rs diff --git a/nalgebra-sparse/Cargo.toml b/nalgebra-sparse/Cargo.toml index 6f7a7b4a..eec7326d 100644 --- a/nalgebra-sparse/Cargo.toml +++ b/nalgebra-sparse/Cargo.toml @@ -15,6 +15,7 @@ license = "Apache-2.0" [features] proptest-support = ["proptest", "nalgebra/proptest-support"] compare = [ "matrixcompare-core" ] +serde-serialize = [ "serde/std" ] # Enable matrix market I/O io = [ "pest", "pest_derive" ] @@ -29,12 +30,14 @@ proptest = { version = "1.0", optional = true } matrixcompare-core = { version = "0.1.0", optional = true } pest = { version = "2", optional = true } pest_derive = { version = "2", optional = true } +serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true } [dev-dependencies] itertools = "0.10" matrixcompare = { version = "0.3.0", features = [ "proptest-support" ] } nalgebra = { version="0.30", path = "../", features = ["compare"] } +serde_json = "1.0" [package.metadata.docs.rs] # Enable certain features when building docs for docs.rs -features = [ "proptest-support", "compare" ] \ No newline at end of file +features = [ "proptest-support", "compare" ] diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 4324d18d..b98717b8 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -10,6 +10,9 @@ use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKin use nalgebra::Scalar; use num_traits::One; +#[cfg(feature = "serde-serialize")] +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + use std::iter::FromIterator; use std::slice::{Iter, IterMut}; @@ -596,6 +599,67 @@ impl CsrMatrix { } } +#[cfg_attr(feature = "serde-serialize", derive(Serialize))] +struct CsrMatrixSerializationHelper<'a, T> { + nrows: usize, + ncols: usize, + row_offsets: &'a [usize], + col_indices: &'a [usize], + values: &'a [T], +} + +#[cfg(feature = "serde-serialize")] +impl Serialize for CsrMatrix +where + T: Serialize, +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CsrMatrixSerializationHelper { + nrows: self.nrows(), + ncols: self.ncols(), + row_offsets: self.row_offsets(), + col_indices: self.col_indices(), + values: self.values(), + } + .serialize(serializer) + } +} + +#[cfg_attr(feature = "serde-serialize", derive(Deserialize))] +struct CsrMatrixDeserializationHelper { + nrows: usize, + ncols: usize, + row_offsets: Vec, + col_indices: Vec, + values: Vec, +} + +#[cfg(feature = "serde-serialize")] +impl<'de, T> Deserialize<'de> for CsrMatrix +where + T: for<'de2> Deserialize<'de2>, +{ + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let CsrMatrixDeserializationHelper { + nrows, + ncols, + row_offsets, + col_indices, + values, + } = CsrMatrixDeserializationHelper::deserialize(deserializer)?; + CsrMatrix::try_from_csr_data(nrows, ncols, row_offsets, col_indices, values) + .map(|m| m.into()) + // TODO: More specific error + .map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid CSR matrix"), &"a valid CSR matrix")) + } +} + /// Convert pattern format errors into more meaningful CSR-specific errors. /// /// This ensures that the terminology is consistent: we are talking about rows and columns, diff --git a/nalgebra-sparse/tests/serde.rs b/nalgebra-sparse/tests/serde.rs new file mode 100644 index 00000000..ecee76d1 --- /dev/null +++ b/nalgebra-sparse/tests/serde.rs @@ -0,0 +1,62 @@ +#![cfg(feature = "serde-serialize")] +//! Serialization tests +#[cfg(any(not(feature = "proptest-support"), not(feature = "compare")))] +compile_error!("Tests must be run with features `proptest-support` and `compare`"); + +#[macro_use] +pub mod common; + +use nalgebra_sparse::csr::CsrMatrix; + +use proptest::prelude::*; +use serde::{Deserialize, Serialize}; + +use crate::common::csr_strategy; + +fn json_roundtrip Deserialize<'a>>(csr: &CsrMatrix) -> CsrMatrix { + let serialized = serde_json::to_string(csr).unwrap(); + let deserialized: CsrMatrix = serde_json::from_str(&serialized).unwrap(); + deserialized +} + +#[test] +fn csr_roundtrip() { + { + // A CSR matrix with zero explicitly stored entries + let offsets = vec![0, 0, 0, 0]; + let indices = vec![]; + let values = Vec::::new(); + let matrix = CsrMatrix::try_from_csr_data(3, 2, offsets, indices, values).unwrap(); + + assert_eq!(json_roundtrip(&matrix), matrix); + } + + { + // An arbitrary CSR matrix + let offsets = vec![0, 2, 2, 5]; + let indices = vec![0, 5, 1, 2, 3]; + let values = vec![0, 1, 2, 3, 4]; + let matrix = + CsrMatrix::try_from_csr_data(3, 6, offsets.clone(), indices.clone(), values.clone()) + .unwrap(); + + assert_eq!(json_roundtrip(&matrix), matrix); + } +} + +#[test] +fn invalid_csr_deserialize() { + // Valid matrix: {"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]} + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,8,3],"values":[0,1,2,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3,1,1],"values":[0,1,2,3,4]}"#).is_err()); + // The following actually panics ('range end index 10 out of range for slice of length 5', nalgebra-sparse\src\pattern.rs:156:38) + //assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,10,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); +} + +proptest! { + #[test] + fn csr_roundtrip_proptest(csr in csr_strategy()) { + prop_assert_eq!(json_roundtrip(&csr), csr); + } +} From 18b694dad282c543cc2eb786f783f7b94afa3a0d Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Mon, 8 Nov 2021 11:10:58 +0100 Subject: [PATCH 05/81] Move serialization helper structs into trait impls --- nalgebra-sparse/src/csr.rs | 48 +++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index b98717b8..bc2a3df0 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -599,15 +599,6 @@ impl CsrMatrix { } } -#[cfg_attr(feature = "serde-serialize", derive(Serialize))] -struct CsrMatrixSerializationHelper<'a, T> { - nrows: usize, - ncols: usize, - row_offsets: &'a [usize], - col_indices: &'a [usize], - values: &'a [T], -} - #[cfg(feature = "serde-serialize")] impl Serialize for CsrMatrix where @@ -617,7 +608,16 @@ where where S: Serializer, { - CsrMatrixSerializationHelper { + #[derive(Serialize)] + struct CsrMatrixSerializationData<'a, T> { + nrows: usize, + ncols: usize, + row_offsets: &'a [usize], + col_indices: &'a [usize], + values: &'a [T], + } + + CsrMatrixSerializationData { nrows: self.nrows(), ncols: self.ncols(), row_offsets: self.row_offsets(), @@ -628,15 +628,6 @@ where } } -#[cfg_attr(feature = "serde-serialize", derive(Deserialize))] -struct CsrMatrixDeserializationHelper { - nrows: usize, - ncols: usize, - row_offsets: Vec, - col_indices: Vec, - values: Vec, -} - #[cfg(feature = "serde-serialize")] impl<'de, T> Deserialize<'de> for CsrMatrix where @@ -646,14 +637,17 @@ where where D: Deserializer<'de>, { - let CsrMatrixDeserializationHelper { - nrows, - ncols, - row_offsets, - col_indices, - values, - } = CsrMatrixDeserializationHelper::deserialize(deserializer)?; - CsrMatrix::try_from_csr_data(nrows, ncols, row_offsets, col_indices, values) + #[derive(Deserialize)] + struct CsrMatrixDeserializationData { + nrows: usize, + ncols: usize, + row_offsets: Vec, + col_indices: Vec, + values: Vec, + } + + let de = CsrMatrixDeserializationData::deserialize(deserializer)?; + CsrMatrix::try_from_csr_data(de.nrows, de.ncols, de.row_offsets, de.col_indices, de.values) .map(|m| m.into()) // TODO: More specific error .map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid CSR matrix"), &"a valid CSR matrix")) From 40d8a904a3e5c1f486adfbc40fb2562254985c64 Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 10:30:02 +0100 Subject: [PATCH 06/81] Implement Serialize, Deserialize for Csc, Coo; move helper out of impls --- nalgebra-sparse/src/coo.rs | 60 ++++++++++++++++++ nalgebra-sparse/src/csc.rs | 59 ++++++++++++++++++ nalgebra-sparse/src/csr.rs | 40 ++++++------ nalgebra-sparse/tests/serde.rs | 110 +++++++++++++++++++++++++++++++-- 4 files changed, 245 insertions(+), 24 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 34e5ceec..35a14083 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -2,6 +2,9 @@ use crate::SparseFormatError; +#[cfg(feature = "serde-serialize")] +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + /// A COO representation of a sparse matrix. /// /// A COO matrix stores entries in coordinate-form, that is triplets `(i, j, v)`, where `i` and `j` @@ -273,3 +276,60 @@ impl CooMatrix { (self.row_indices, self.col_indices, self.values) } } + +#[cfg(feature = "serde-serialize")] +#[derive(Serialize)] +struct CooMatrixSerializationData<'a, T> { + nrows: usize, + ncols: usize, + row_indices: &'a [usize], + col_indices: &'a [usize], + values: &'a [T], +} + +#[cfg(feature = "serde-serialize")] +impl Serialize for CooMatrix +where + T: Serialize, +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CooMatrixSerializationData { + nrows: self.nrows(), + ncols: self.ncols(), + row_indices: self.row_indices(), + col_indices: self.col_indices(), + values: self.values(), + } + .serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +#[derive(Deserialize)] +struct CooMatrixDeserializationData { + nrows: usize, + ncols: usize, + row_indices: Vec, + col_indices: Vec, + values: Vec, +} + +#[cfg(feature = "serde-serialize")] +impl<'de, T> Deserialize<'de> for CooMatrix +where + T: for<'de2> Deserialize<'de2>, +{ + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CooMatrixDeserializationData::deserialize(deserializer)?; + CooMatrix::try_from_triplets(de.nrows, de.ncols, de.row_indices, de.col_indices, de.values) + .map(|m| m.into()) + // TODO: More specific error + .map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid COO matrix"), &"a valid COO matrix")) + } +} diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 607cc0cf..cb7cb79b 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -10,6 +10,8 @@ use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKin use nalgebra::Scalar; use num_traits::One; +#[cfg(feature = "serde-serialize")] +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::slice::{Iter, IterMut}; /// A CSC representation of a sparse matrix. @@ -524,6 +526,63 @@ impl CscMatrix { } } +#[cfg(feature = "serde-serialize")] +#[derive(Serialize)] +struct CscMatrixSerializationData<'a, T> { + nrows: usize, + ncols: usize, + col_offsets: &'a [usize], + row_indices: &'a [usize], + values: &'a [T], +} + +#[cfg(feature = "serde-serialize")] +impl Serialize for CscMatrix +where + T: Serialize, +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CscMatrixSerializationData { + nrows: self.nrows(), + ncols: self.ncols(), + col_offsets: self.col_offsets(), + row_indices: self.row_indices(), + values: self.values(), + } + .serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +#[derive(Deserialize)] +struct CscMatrixDeserializationData { + nrows: usize, + ncols: usize, + col_offsets: Vec, + row_indices: Vec, + values: Vec, +} + +#[cfg(feature = "serde-serialize")] +impl<'de, T> Deserialize<'de> for CscMatrix +where + T: for<'de2> Deserialize<'de2>, +{ + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CscMatrixDeserializationData::deserialize(deserializer)?; + CscMatrix::try_from_csc_data(de.nrows, de.ncols, de.col_offsets, de.row_indices, de.values) + .map(|m| m.into()) + // TODO: More specific error + .map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid CSC matrix"), &"a valid CSC matrix")) + } +} + /// Convert pattern format errors into more meaningful CSC-specific errors. /// /// This ensures that the terminology is consistent: we are talking about rows and columns, diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index bc2a3df0..b36fbb2f 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -9,10 +9,8 @@ use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKin use nalgebra::Scalar; use num_traits::One; - #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - use std::iter::FromIterator; use std::slice::{Iter, IterMut}; @@ -599,6 +597,16 @@ impl CsrMatrix { } } +#[cfg(feature = "serde-serialize")] +#[derive(Serialize)] +struct CsrMatrixSerializationData<'a, T> { + nrows: usize, + ncols: usize, + row_offsets: &'a [usize], + col_indices: &'a [usize], + values: &'a [T], +} + #[cfg(feature = "serde-serialize")] impl Serialize for CsrMatrix where @@ -608,15 +616,6 @@ where where S: Serializer, { - #[derive(Serialize)] - struct CsrMatrixSerializationData<'a, T> { - nrows: usize, - ncols: usize, - row_offsets: &'a [usize], - col_indices: &'a [usize], - values: &'a [T], - } - CsrMatrixSerializationData { nrows: self.nrows(), ncols: self.ncols(), @@ -628,6 +627,16 @@ where } } +#[cfg(feature = "serde-serialize")] +#[derive(Deserialize)] +struct CsrMatrixDeserializationData { + nrows: usize, + ncols: usize, + row_offsets: Vec, + col_indices: Vec, + values: Vec, +} + #[cfg(feature = "serde-serialize")] impl<'de, T> Deserialize<'de> for CsrMatrix where @@ -637,15 +646,6 @@ where where D: Deserializer<'de>, { - #[derive(Deserialize)] - struct CsrMatrixDeserializationData { - nrows: usize, - ncols: usize, - row_offsets: Vec, - col_indices: Vec, - values: Vec, - } - let de = CsrMatrixDeserializationData::deserialize(deserializer)?; CsrMatrix::try_from_csr_data(de.nrows, de.ncols, de.row_offsets, de.col_indices, de.values) .map(|m| m.into()) diff --git a/nalgebra-sparse/tests/serde.rs b/nalgebra-sparse/tests/serde.rs index ecee76d1..5afe93c5 100644 --- a/nalgebra-sparse/tests/serde.rs +++ b/nalgebra-sparse/tests/serde.rs @@ -6,19 +6,112 @@ compile_error!("Tests must be run with features `proptest-support` and `compare` #[macro_use] pub mod common; +use nalgebra_sparse::coo::CooMatrix; +use nalgebra_sparse::csc::CscMatrix; use nalgebra_sparse::csr::CsrMatrix; use proptest::prelude::*; use serde::{Deserialize, Serialize}; -use crate::common::csr_strategy; +use crate::common::{csc_strategy, csr_strategy}; -fn json_roundtrip Deserialize<'a>>(csr: &CsrMatrix) -> CsrMatrix { +fn json_roundtrip Deserialize<'a>>(csr: &T) -> T { let serialized = serde_json::to_string(csr).unwrap(); - let deserialized: CsrMatrix = serde_json::from_str(&serialized).unwrap(); + let deserialized: T = serde_json::from_str(&serialized).unwrap(); deserialized } +#[test] +fn coo_roundtrip() { + { + // A COO matrix without entries + let matrix = + CooMatrix::::try_from_triplets(3, 2, Vec::new(), Vec::new(), Vec::new()).unwrap(); + + assert_eq!(json_roundtrip(&matrix), matrix); + } + + { + // Arbitrary COO matrix, no duplicates + let i = vec![0, 1, 0, 0, 2]; + let j = vec![0, 2, 1, 3, 3]; + let v = vec![2, 3, 7, 3, 1]; + let matrix = + CooMatrix::::try_from_triplets(3, 5, i.clone(), j.clone(), v.clone()).unwrap(); + + assert_eq!(json_roundtrip(&matrix), matrix); + } +} + +#[test] +fn coo_deserialize_invalid() { + // Valid matrix: {"nrows":3,"ncols":5,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3,1]} + assert!(serde_json::from_str::>(r#"{"nrows":0,"ncols":0,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":-3,"ncols":5,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":5,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":5,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3,4,5]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":5,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,8,3],"values":[2,3,7,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":5,"row_indices":[0,1,0,0],"col_indices":[0,2,1,8,3],"values":[2,3,7,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":5,"row_indices":[0,10,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":5,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,30,3],"values":[2,3,7,3,4]}"#).is_err()); +} + +#[test] +fn coo_deserialize_duplicates() { + assert_eq!( + serde_json::from_str::>( + r#"{"nrows":3,"ncols":5,"row_indices":[0,1,0,0,2,0,1],"col_indices":[0,2,1,3,3,0,2],"values":[2,3,7,3,1,5,6]}"# + ).unwrap(), + CooMatrix::::try_from_triplets( + 3, + 5, + vec![0, 1, 0, 0, 2, 0, 1], + vec![0, 2, 1, 3, 3, 0, 2], + vec![2, 3, 7, 3, 1, 5, 6] + ) + .unwrap() + ); +} + +#[test] +fn csc_roundtrip() { + { + // A CSC matrix with zero explicitly stored entries + let offsets = vec![0, 0, 0, 0]; + let indices = vec![]; + let values = Vec::::new(); + let matrix = CscMatrix::try_from_csc_data(2, 3, offsets, indices, values).unwrap(); + + assert_eq!(json_roundtrip(&matrix), matrix); + } + + { + // An arbitrary CSC matrix + let offsets = vec![0, 2, 2, 5]; + let indices = vec![0, 5, 1, 2, 3]; + let values = vec![0, 1, 2, 3, 4]; + let matrix = + CscMatrix::try_from_csc_data(6, 3, offsets.clone(), indices.clone(), values.clone()) + .unwrap(); + + assert_eq!(json_roundtrip(&matrix), matrix); + } +} + +#[test] +fn csc_deserialize_invalid() { + // Valid matrix: {"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]} + assert!(serde_json::from_str::>(r#"{"nrows":0,"ncols":0,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":-6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4,5]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,8,3],"values":[0,1,2,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3,1,1],"values":[0,1,2,3,4]}"#).is_err()); + // The following actually panics ('range end index 10 out of range for slice of length 5', nalgebra-sparse\src\pattern.rs:156:38) + //assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,10,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); +} + #[test] fn csr_roundtrip() { { @@ -45,16 +138,25 @@ fn csr_roundtrip() { } #[test] -fn invalid_csr_deserialize() { +fn csr_deserialize_invalid() { // Valid matrix: {"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]} + assert!(serde_json::from_str::>(r#"{"nrows":0,"ncols":0,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":-3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4,5]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,8,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3,1,1],"values":[0,1,2,3,4]}"#).is_err()); // The following actually panics ('range end index 10 out of range for slice of length 5', nalgebra-sparse\src\pattern.rs:156:38) //assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,10,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); } proptest! { + #[test] + fn csc_roundtrip_proptest(csc in csc_strategy()) { + prop_assert_eq!(json_roundtrip(&csc), csc); + } + #[test] fn csr_roundtrip_proptest(csr in csr_strategy()) { prop_assert_eq!(json_roundtrip(&csr), csr); From 2a3e657b565dadcd2af422750cd0fb5459be0f6f Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 10:31:50 +0100 Subject: [PATCH 07/81] Rename nrows/ncols args for try_from_*_data functions for consistency --- nalgebra-sparse/src/csc.rs | 14 +++++--------- nalgebra-sparse/src/csr.rs | 14 +++++--------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index cb7cb79b..512be540 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -156,19 +156,15 @@ impl CscMatrix { /// An error is returned if the data given does not conform to the CSC storage format. /// See the documentation for [CscMatrix](struct.CscMatrix.html) for more information. pub fn try_from_csc_data( - num_rows: usize, - num_cols: usize, + nrows: usize, + ncols: usize, col_offsets: Vec, row_indices: Vec, values: Vec, ) -> Result { - let pattern = SparsityPattern::try_from_offsets_and_indices( - num_cols, - num_rows, - col_offsets, - row_indices, - ) - .map_err(pattern_format_error_to_csc_error)?; + let pattern = + SparsityPattern::try_from_offsets_and_indices(ncols, nrows, col_offsets, row_indices) + .map_err(pattern_format_error_to_csc_error)?; Self::try_from_pattern_and_values(pattern, values) } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index b36fbb2f..9e31c90c 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -156,19 +156,15 @@ impl CsrMatrix { /// An error is returned if the data given does not conform to the CSR storage format. /// See the documentation for [CsrMatrix](struct.CsrMatrix.html) for more information. pub fn try_from_csr_data( - num_rows: usize, - num_cols: usize, + nrows: usize, + ncols: usize, row_offsets: Vec, col_indices: Vec, values: Vec, ) -> Result { - let pattern = SparsityPattern::try_from_offsets_and_indices( - num_rows, - num_cols, - row_offsets, - col_indices, - ) - .map_err(pattern_format_error_to_csr_error)?; + let pattern = + SparsityPattern::try_from_offsets_and_indices(nrows, ncols, row_offsets, col_indices) + .map_err(pattern_format_error_to_csr_error)?; Self::try_from_pattern_and_values(pattern, values) } From bfaf29393c546e58c0effee163fb38b0bdc0aca0 Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 10:59:24 +0100 Subject: [PATCH 08/81] Implement Serialize, Deserialize for SparsityPattern --- nalgebra-sparse/src/pattern.rs | 51 ++++++++++++++++++++++++++++++++++ nalgebra-sparse/tests/serde.rs | 42 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 85f6bc1a..4243543d 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -4,6 +4,9 @@ use crate::SparseFormatError; use std::error::Error; use std::fmt; +#[cfg(feature = "serde-serialize")] +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + /// A representation of the sparsity pattern of a CSR or CSC matrix. /// /// CSR and CSC matrices store matrices in a very similar fashion. In fact, in a certain sense, @@ -285,6 +288,54 @@ pub enum SparsityPatternFormatError { NonmonotonicMinorIndices, } +#[cfg(feature = "serde-serialize")] +#[derive(Serialize)] +struct SparsityPatternSerializationData<'a> { + major_dim: usize, + minor_dim: usize, + major_offsets: &'a [usize], + minor_indices: &'a [usize], +} + +#[cfg(feature = "serde-serialize")] +impl Serialize for SparsityPattern { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + SparsityPatternSerializationData { + major_dim: self.major_dim(), + minor_dim: self.minor_dim(), + major_offsets: self.major_offsets(), + minor_indices: self.minor_indices(), + } + .serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +#[derive(Deserialize)] +struct SparsityPatternDeserializationData { + major_dim: usize, + minor_dim: usize, + major_offsets: Vec, + minor_indices: Vec, +} + +#[cfg(feature = "serde-serialize")] +impl<'de> Deserialize<'de> for SparsityPattern { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let de = SparsityPatternDeserializationData::deserialize(deserializer)?; + SparsityPattern::try_from_offsets_and_indices(de.major_dim, de.minor_dim, de.major_offsets, de.minor_indices) + .map(|m| m.into()) + // TODO: More specific error + .map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid sparsity pattern"), &"a valid sparsity pattern")) + } +} + impl From for SparseFormatError { fn from(err: SparsityPatternFormatError) -> Self { use crate::SparseFormatErrorKind; diff --git a/nalgebra-sparse/tests/serde.rs b/nalgebra-sparse/tests/serde.rs index 5afe93c5..1ce1953f 100644 --- a/nalgebra-sparse/tests/serde.rs +++ b/nalgebra-sparse/tests/serde.rs @@ -9,6 +9,7 @@ pub mod common; use nalgebra_sparse::coo::CooMatrix; use nalgebra_sparse::csc::CscMatrix; use nalgebra_sparse::csr::CsrMatrix; +use nalgebra_sparse::pattern::SparsityPattern; use proptest::prelude::*; use serde::{Deserialize, Serialize}; @@ -21,6 +22,44 @@ fn json_roundtrip Deserialize<'a>>(csr: &T) -> T { deserialized } +#[test] +fn pattern_roundtrip() { + { + // A pattern with zero explicitly stored entries + let pattern = + SparsityPattern::try_from_offsets_and_indices(3, 2, vec![0, 0, 0, 0], Vec::new()) + .unwrap(); + + assert_eq!(json_roundtrip(&pattern), pattern); + } + + { + // Arbitrary pattern + let offsets = vec![0, 2, 2, 5]; + let indices = vec![0, 5, 1, 2, 3]; + let pattern = + SparsityPattern::try_from_offsets_and_indices(3, 6, offsets.clone(), indices.clone()) + .unwrap(); + + assert_eq!(json_roundtrip(&pattern), pattern); + } +} + +#[test] +#[rustfmt::skip] +fn pattern_deserialize_invalid() { + assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0,2,2,5],"minor_indices":[0,5,1,2,3]}"#).is_ok()); + assert!(serde_json::from_str::(r#"{"major_dim":0,"minor_dim":0,"major_offsets":[],"minor_indices":[]}"#).is_err()); + assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 3, 5],"minor_indices":[0, 1, 2, 3, 5]}"#).is_err()); + assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[1, 2, 2, 5],"minor_indices":[0, 5, 1, 2, 3]}"#).is_err()); + assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2, 4],"minor_indices":[0, 5, 1, 2, 3]}"#).is_err()); + assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2],"minor_indices":[0, 5, 1, 2, 3]}"#).is_err()); + assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 3, 2, 5],"minor_indices":[0, 1, 2, 3, 4]}"#).is_err()); + assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2, 5],"minor_indices":[0, 2, 3, 1, 4]}"#).is_err()); + assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2, 5],"minor_indices":[0, 6, 1, 2, 3]}"#).is_err()); + assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2, 5],"minor_indices":[0, 5, 2, 2, 3]}"#).is_err()); +} + #[test] fn coo_roundtrip() { { @@ -46,6 +85,7 @@ fn coo_roundtrip() { #[test] fn coo_deserialize_invalid() { // Valid matrix: {"nrows":3,"ncols":5,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3,1]} + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":5,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3,1]}"#).is_ok()); assert!(serde_json::from_str::>(r#"{"nrows":0,"ncols":0,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":-3,"ncols":5,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":5,"row_indices":[0,1,0,0,2],"col_indices":[0,2,1,3,3],"values":[2,3,7,3]}"#).is_err()); @@ -101,6 +141,7 @@ fn csc_roundtrip() { #[test] fn csc_deserialize_invalid() { // Valid matrix: {"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]} + assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_ok()); assert!(serde_json::from_str::>(r#"{"nrows":0,"ncols":0,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":-6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3]}"#).is_err()); @@ -140,6 +181,7 @@ fn csr_roundtrip() { #[test] fn csr_deserialize_invalid() { // Valid matrix: {"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]} + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_ok()); assert!(serde_json::from_str::>(r#"{"nrows":0,"ncols":0,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":-3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3]}"#).is_err()); From e2c33b48ace078fba17a91844a81fc7faaf5ba5f Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 11:09:48 +0100 Subject: [PATCH 09/81] Simplify Deserialize bound --- nalgebra-sparse/src/coo.rs | 2 +- nalgebra-sparse/src/csc.rs | 2 +- nalgebra-sparse/src/csr.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 35a14083..b84b2cc5 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -320,7 +320,7 @@ struct CooMatrixDeserializationData { #[cfg(feature = "serde-serialize")] impl<'de, T> Deserialize<'de> for CooMatrix where - T: for<'de2> Deserialize<'de2>, + T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result, D::Error> where diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 512be540..fccc6146 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -565,7 +565,7 @@ struct CscMatrixDeserializationData { #[cfg(feature = "serde-serialize")] impl<'de, T> Deserialize<'de> for CscMatrix where - T: for<'de2> Deserialize<'de2>, + T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result, D::Error> where diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 9e31c90c..31375340 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -636,7 +636,7 @@ struct CsrMatrixDeserializationData { #[cfg(feature = "serde-serialize")] impl<'de, T> Deserialize<'de> for CsrMatrix where - T: for<'de2> Deserialize<'de2>, + T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result, D::Error> where From e9b771829246794a33574ed2a44e7f15567152c6 Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 14:07:57 +0100 Subject: [PATCH 10/81] Fix panic in SparsityPattern::try_from_* if major index is out of bounds --- nalgebra-sparse/src/csc.rs | 3 +++ nalgebra-sparse/src/csr.rs | 3 +++ nalgebra-sparse/src/pattern.rs | 11 +++++++++-- nalgebra-sparse/tests/serde.rs | 7 +++---- nalgebra-sparse/tests/unit_tests/pattern.rs | 11 +++++++++++ 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index fccc6146..e6eb1589 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -606,6 +606,9 @@ fn pattern_format_error_to_csc_error(err: SparsityPatternFormatError) -> SparseF K::InvalidStructure, "Row indices are not monotonically increasing (sorted) within each column.", ), + MajorIndexOutOfBounds => { + E::from_kind_and_msg(K::IndexOutOfBounds, "Column indices are out of bounds.") + } MinorIndexOutOfBounds => { E::from_kind_and_msg(K::IndexOutOfBounds, "Row indices are out of bounds.") } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 31375340..7bd996da 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -677,6 +677,9 @@ fn pattern_format_error_to_csr_error(err: SparsityPatternFormatError) -> SparseF K::InvalidStructure, "Column indices are not monotonically increasing (sorted) within each row.", ), + MajorIndexOutOfBounds => { + E::from_kind_and_msg(K::IndexOutOfBounds, "Row indices are out of bounds.") + } MinorIndexOutOfBounds => { E::from_kind_and_msg(K::IndexOutOfBounds, "Column indices are out of bounds.") } diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 4243543d..1f9dde84 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -156,7 +156,9 @@ impl SparsityPattern { return Err(NonmonotonicOffsets); } - let minor_indices = &minor_indices[range_start..range_end]; + let minor_indices = minor_indices + .get(range_start..range_end) + .ok_or(MajorIndexOutOfBounds)?; // We test for in-bounds, uniqueness and monotonicity at the same time // to ensure that we only visit each minor index once @@ -277,6 +279,8 @@ pub enum SparsityPatternFormatError { InvalidOffsetFirstLast, /// Indicates that the major offsets are not monotonically increasing. NonmonotonicOffsets, + /// One or more major indices are out of bounds. + MajorIndexOutOfBounds, /// One or more minor indices are out of bounds. MinorIndexOutOfBounds, /// One or more duplicate entries were detected. @@ -349,7 +353,7 @@ impl From for SparseFormatError { | NonmonotonicMinorIndices => { SparseFormatError::from_kind_and_error(InvalidStructure, Box::from(err)) } - MinorIndexOutOfBounds => { + MajorIndexOutOfBounds | MinorIndexOutOfBounds => { SparseFormatError::from_kind_and_error(IndexOutOfBounds, Box::from(err)) } PatternDuplicateEntry => SparseFormatError::from_kind_and_error( @@ -373,6 +377,9 @@ impl fmt::Display for SparsityPatternFormatError { SparsityPatternFormatError::NonmonotonicOffsets => { write!(f, "Offsets are not monotonically increasing.") } + SparsityPatternFormatError::MajorIndexOutOfBounds => { + write!(f, "A major index is out of bounds.") + } SparsityPatternFormatError::MinorIndexOutOfBounds => { write!(f, "A minor index is out of bounds.") } diff --git a/nalgebra-sparse/tests/serde.rs b/nalgebra-sparse/tests/serde.rs index 1ce1953f..7842fd1e 100644 --- a/nalgebra-sparse/tests/serde.rs +++ b/nalgebra-sparse/tests/serde.rs @@ -58,6 +58,7 @@ fn pattern_deserialize_invalid() { assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2, 5],"minor_indices":[0, 2, 3, 1, 4]}"#).is_err()); assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2, 5],"minor_indices":[0, 6, 1, 2, 3]}"#).is_err()); assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2, 5],"minor_indices":[0, 5, 2, 2, 3]}"#).is_err()); + assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 10, 2, 5],"minor_indices":[0, 5, 1, 2, 3]}"#).is_err()); } #[test] @@ -148,8 +149,7 @@ fn csc_deserialize_invalid() { assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4,5]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,8,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3,1,1],"values":[0,1,2,3,4]}"#).is_err()); - // The following actually panics ('range end index 10 out of range for slice of length 5', nalgebra-sparse\src\pattern.rs:156:38) - //assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,10,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,10,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); } @@ -188,8 +188,7 @@ fn csr_deserialize_invalid() { assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4,5]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,8,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3,1,1],"values":[0,1,2,3,4]}"#).is_err()); - // The following actually panics ('range end index 10 out of range for slice of length 5', nalgebra-sparse\src\pattern.rs:156:38) - //assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,10,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); + assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,10,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); } diff --git a/nalgebra-sparse/tests/unit_tests/pattern.rs b/nalgebra-sparse/tests/unit_tests/pattern.rs index 310cffae..e2706ee5 100644 --- a/nalgebra-sparse/tests/unit_tests/pattern.rs +++ b/nalgebra-sparse/tests/unit_tests/pattern.rs @@ -133,6 +133,17 @@ fn sparsity_pattern_try_from_invalid_data() { ); } + { + // Major index out of bounds + let offsets = vec![0, 10, 2, 5]; + let indices = vec![0, 1, 2, 3, 4]; + let pattern = SparsityPattern::try_from_offsets_and_indices(3, 6, offsets, indices); + assert_eq!( + pattern, + Err(SparsityPatternFormatError::MajorIndexOutOfBounds) + ); + } + { // Minor index out of bounds let offsets = vec![0, 2, 2, 5]; From 7e67d920a7bddbbd2e6cf8ccb423cfa8f32f85cd Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 17:06:24 +0100 Subject: [PATCH 11/81] Use custom serde errors, make all sparse errs lowercase w/o punctuation --- nalgebra-sparse/src/coo.rs | 21 ++++++++++++-------- nalgebra-sparse/src/csc.rs | 29 ++++++++++++++++------------ nalgebra-sparse/src/csr.rs | 35 +++++++++++++++++++--------------- nalgebra-sparse/src/pattern.rs | 28 +++++++++++++++------------ 4 files changed, 66 insertions(+), 47 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index b84b2cc5..15b23566 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -127,12 +127,12 @@ impl CooMatrix { if row_indices.len() != col_indices.len() { return Err(SparseFormatError::from_kind_and_msg( InvalidStructure, - "Number of row and col indices must be the same.", + "number of row and col indices must be the same", )); } else if col_indices.len() != values.len() { return Err(SparseFormatError::from_kind_and_msg( InvalidStructure, - "Number of col indices and values must be the same.", + "number of col indices and values must be the same", )); } @@ -142,12 +142,12 @@ impl CooMatrix { if !row_indices_in_bounds { Err(SparseFormatError::from_kind_and_msg( IndexOutOfBounds, - "Row index out of bounds.", + "row index out of bounds", )) } else if !col_indices_in_bounds { Err(SparseFormatError::from_kind_and_msg( IndexOutOfBounds, - "Col index out of bounds.", + "col index out of bounds", )) } else { Ok(Self { @@ -327,9 +327,14 @@ where D: Deserializer<'de>, { let de = CooMatrixDeserializationData::deserialize(deserializer)?; - CooMatrix::try_from_triplets(de.nrows, de.ncols, de.row_indices, de.col_indices, de.values) - .map(|m| m.into()) - // TODO: More specific error - .map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid COO matrix"), &"a valid COO matrix")) + CooMatrix::try_from_triplets( + de.nrows, + de.ncols, + de.row_indices, + de.col_indices, + de.values, + ) + .map(|m| m.into()) + .map_err(|e| de::Error::custom(e)) } } diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index e6eb1589..54383b01 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -183,7 +183,7 @@ impl CscMatrix { } else { Err(SparseFormatError::from_kind_and_msg( SparseFormatErrorKind::InvalidStructure, - "Number of values and row indices must be the same", + "number of values and row indices must be the same", )) } } @@ -572,10 +572,15 @@ where D: Deserializer<'de>, { let de = CscMatrixDeserializationData::deserialize(deserializer)?; - CscMatrix::try_from_csc_data(de.nrows, de.ncols, de.col_offsets, de.row_indices, de.values) - .map(|m| m.into()) - // TODO: More specific error - .map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid CSC matrix"), &"a valid CSC matrix")) + CscMatrix::try_from_csc_data( + de.nrows, + de.ncols, + de.col_offsets, + de.row_indices, + de.values, + ) + .map(|m| m.into()) + .map_err(|e| de::Error::custom(e)) } } @@ -592,28 +597,28 @@ fn pattern_format_error_to_csc_error(err: SparsityPatternFormatError) -> SparseF match err { InvalidOffsetArrayLength => E::from_kind_and_msg( K::InvalidStructure, - "Length of col offset array is not equal to ncols + 1.", + "length of col offset array is not equal to ncols + 1", ), InvalidOffsetFirstLast => E::from_kind_and_msg( K::InvalidStructure, - "First or last col offset is inconsistent with format specification.", + "first or last col offset is inconsistent with format specification", ), NonmonotonicOffsets => E::from_kind_and_msg( K::InvalidStructure, - "Col offsets are not monotonically increasing.", + "col offsets are not monotonically increasing", ), NonmonotonicMinorIndices => E::from_kind_and_msg( K::InvalidStructure, - "Row indices are not monotonically increasing (sorted) within each column.", + "row indices are not monotonically increasing (sorted) within each column", ), MajorIndexOutOfBounds => { - E::from_kind_and_msg(K::IndexOutOfBounds, "Column indices are out of bounds.") + E::from_kind_and_msg(K::IndexOutOfBounds, "column indices are out of bounds") } MinorIndexOutOfBounds => { - E::from_kind_and_msg(K::IndexOutOfBounds, "Row indices are out of bounds.") + E::from_kind_and_msg(K::IndexOutOfBounds, "row indices are out of bounds") } PatternDuplicateEntry => { - E::from_kind_and_msg(K::DuplicateEntry, "Matrix data contains duplicate entries.") + E::from_kind_and_msg(K::DuplicateEntry, "matrix data contains duplicate entries") } } } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 7bd996da..804606ca 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -194,14 +194,14 @@ impl CsrMatrix { if col_indices.len() != values.len() { return Err(SparseFormatError::from_kind_and_msg( SparseFormatErrorKind::InvalidStructure, - "Number of values and column indices must be the same", + "number of values and column indices must be the same", )); } if row_offsets.len() == 0 { return Err(SparseFormatError::from_kind_and_msg( SparseFormatErrorKind::InvalidStructure, - "Number of offsets should be greater than 0", + "number of offsets should be greater than 0", )); } @@ -210,7 +210,7 @@ impl CsrMatrix { if next_offset > count { return Err(SparseFormatError::from_kind_and_msg( SparseFormatErrorKind::InvalidStructure, - "No row offset should be greater than the number of column indices", + "no row offset should be greater than the number of column indices", )); } if offset > next_offset { @@ -254,7 +254,7 @@ impl CsrMatrix { } else { Err(SparseFormatError::from_kind_and_msg( SparseFormatErrorKind::InvalidStructure, - "Number of values and column indices must be the same", + "number of values and column indices must be the same", )) } } @@ -643,10 +643,15 @@ where D: Deserializer<'de>, { let de = CsrMatrixDeserializationData::deserialize(deserializer)?; - CsrMatrix::try_from_csr_data(de.nrows, de.ncols, de.row_offsets, de.col_indices, de.values) - .map(|m| m.into()) - // TODO: More specific error - .map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid CSR matrix"), &"a valid CSR matrix")) + CsrMatrix::try_from_csr_data( + de.nrows, + de.ncols, + de.row_offsets, + de.col_indices, + de.values, + ) + .map(|m| m.into()) + .map_err(|e| de::Error::custom(e)) } } @@ -663,28 +668,28 @@ fn pattern_format_error_to_csr_error(err: SparsityPatternFormatError) -> SparseF match err { InvalidOffsetArrayLength => E::from_kind_and_msg( K::InvalidStructure, - "Length of row offset array is not equal to nrows + 1.", + "length of row offset array is not equal to nrows + 1", ), InvalidOffsetFirstLast => E::from_kind_and_msg( K::InvalidStructure, - "First or last row offset is inconsistent with format specification.", + "first or last row offset is inconsistent with format specification", ), NonmonotonicOffsets => E::from_kind_and_msg( K::InvalidStructure, - "Row offsets are not monotonically increasing.", + "row offsets are not monotonically increasing", ), NonmonotonicMinorIndices => E::from_kind_and_msg( K::InvalidStructure, - "Column indices are not monotonically increasing (sorted) within each row.", + "column indices are not monotonically increasing (sorted) within each row", ), MajorIndexOutOfBounds => { - E::from_kind_and_msg(K::IndexOutOfBounds, "Row indices are out of bounds.") + E::from_kind_and_msg(K::IndexOutOfBounds, "row indices are out of bounds") } MinorIndexOutOfBounds => { - E::from_kind_and_msg(K::IndexOutOfBounds, "Column indices are out of bounds.") + E::from_kind_and_msg(K::IndexOutOfBounds, "column indices are out of bounds") } PatternDuplicateEntry => { - E::from_kind_and_msg(K::DuplicateEntry, "Matrix data contains duplicate entries.") + E::from_kind_and_msg(K::DuplicateEntry, "matrix data contains duplicate entries") } } } diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 1f9dde84..1d92dae3 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -259,7 +259,7 @@ impl SparsityPattern { new_offsets, new_indices, ) - .expect("Internal error: Transpose should never fail.") + .expect("internal error: Transpose should never fail") } } @@ -333,10 +333,14 @@ impl<'de> Deserialize<'de> for SparsityPattern { D: Deserializer<'de>, { let de = SparsityPatternDeserializationData::deserialize(deserializer)?; - SparsityPattern::try_from_offsets_and_indices(de.major_dim, de.minor_dim, de.major_offsets, de.minor_indices) - .map(|m| m.into()) - // TODO: More specific error - .map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid sparsity pattern"), &"a valid sparsity pattern")) + SparsityPattern::try_from_offsets_and_indices( + de.major_dim, + de.minor_dim, + de.major_offsets, + de.minor_indices, + ) + .map(|m| m.into()) + .map_err(|e| de::Error::custom(e)) } } @@ -369,27 +373,27 @@ impl fmt::Display for SparsityPatternFormatError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { SparsityPatternFormatError::InvalidOffsetArrayLength => { - write!(f, "Length of offset array is not equal to (major_dim + 1).") + write!(f, "length of offset array is not equal to (major_dim + 1)") } SparsityPatternFormatError::InvalidOffsetFirstLast => { - write!(f, "First or last offset is incompatible with format.") + write!(f, "first or last offset is incompatible with format") } SparsityPatternFormatError::NonmonotonicOffsets => { - write!(f, "Offsets are not monotonically increasing.") + write!(f, "offsets are not monotonically increasing") } SparsityPatternFormatError::MajorIndexOutOfBounds => { - write!(f, "A major index is out of bounds.") + write!(f, "a major index is out of bounds") } SparsityPatternFormatError::MinorIndexOutOfBounds => { - write!(f, "A minor index is out of bounds.") + write!(f, "a minor index is out of bounds") } SparsityPatternFormatError::DuplicateEntry => { - write!(f, "Input data contains duplicate entries.") + write!(f, "input data contains duplicate entries") } SparsityPatternFormatError::NonmonotonicMinorIndices => { write!( f, - "Minor indices are not monotonically increasing within each lane." + "minor indices are not monotonically increasing within each lane" ) } } From 3be81be2e3298c882eed75a49334dfac1dd686f1 Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 17:15:40 +0100 Subject: [PATCH 12/81] Updated more error messages --- nalgebra-sparse/src/convert/serial.rs | 12 ++++++------ nalgebra-sparse/src/factorization/cholesky.rs | 6 +++--- nalgebra-sparse/src/ops/serial/cs.rs | 4 ++-- nalgebra-sparse/src/ops/serial/csc.rs | 6 +++--- nalgebra-sparse/src/ops/serial/mod.rs | 8 ++++---- nalgebra-sparse/src/ops/serial/pattern.rs | 8 ++++---- nalgebra-sparse/src/pattern.rs | 2 +- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/nalgebra-sparse/src/convert/serial.rs b/nalgebra-sparse/src/convert/serial.rs index ecbe1dab..9fc3d04e 100644 --- a/nalgebra-sparse/src/convert/serial.rs +++ b/nalgebra-sparse/src/convert/serial.rs @@ -64,7 +64,7 @@ where // TODO: Avoid "try_from" since it validates the data? (requires unsafe, should benchmark // to see if it can be justified for performance reasons) CsrMatrix::try_from_csr_data(coo.nrows(), coo.ncols(), offsets, indices, values) - .expect("Internal error: Invalid CSR data during COO->CSR conversion") + .expect("internal error: invalid CSR data during COO->CSR conversion") } /// Converts a [`CsrMatrix`] to a [`CooMatrix`]. @@ -120,7 +120,7 @@ where // TODO: Consider circumventing the data validity check here // (would require unsafe, should benchmark) CsrMatrix::try_from_csr_data(dense.nrows(), dense.ncols(), row_offsets, col_idx, values) - .expect("Internal error: Invalid CsrMatrix format during dense-> CSR conversion") + .expect("internal error: invalid CsrMatrix format during dense -> CSR conversion") } /// Converts a [`CooMatrix`] to a [`CscMatrix`]. @@ -138,7 +138,7 @@ where // TODO: Avoid "try_from" since it validates the data? (requires unsafe, should benchmark // to see if it can be justified for performance reasons) CscMatrix::try_from_csc_data(coo.nrows(), coo.ncols(), offsets, indices, values) - .expect("Internal error: Invalid CSC data during COO->CSC conversion") + .expect("internal error: invalid CSC data during COO -> CSC conversion") } /// Converts a [`CscMatrix`] to a [`CooMatrix`]. @@ -194,7 +194,7 @@ where // TODO: Consider circumventing the data validity check here // (would require unsafe, should benchmark) CscMatrix::try_from_csc_data(dense.nrows(), dense.ncols(), col_offsets, row_idx, values) - .expect("Internal error: Invalid CscMatrix format during dense-> CSC conversion") + .expect("internal error: invalid CscMatrix format during dense -> CSC conversion") } /// Converts a [`CsrMatrix`] to a [`CscMatrix`]. @@ -212,7 +212,7 @@ where // TODO: Avoid data validity check? CscMatrix::try_from_csc_data(csr.nrows(), csr.ncols(), offsets, indices, values) - .expect("Internal error: Invalid CSC data during CSR->CSC conversion") + .expect("internal error: invalid CSC data during CSR -> CSC conversion") } /// Converts a [`CscMatrix`] to a [`CsrMatrix`]. @@ -230,7 +230,7 @@ where // TODO: Avoid data validity check? CsrMatrix::try_from_csr_data(csc.nrows(), csc.ncols(), offsets, indices, values) - .expect("Internal error: Invalid CSR data during CSC->CSR conversion") + .expect("internal error: invalid CSR data during CSC -> CSR conversion") } fn convert_coo_cs( diff --git a/nalgebra-sparse/src/factorization/cholesky.rs b/nalgebra-sparse/src/factorization/cholesky.rs index 1f653278..b306f104 100644 --- a/nalgebra-sparse/src/factorization/cholesky.rs +++ b/nalgebra-sparse/src/factorization/cholesky.rs @@ -31,7 +31,7 @@ impl CscSymbolicCholesky { assert_eq!( pattern.major_dim(), pattern.minor_dim(), - "Major and minor dimensions must be the same (square matrix)." + "major and minor dimensions must be the same (square matrix)" ); let (l_pattern, u_pattern) = nonzero_pattern(&pattern); Self { @@ -82,7 +82,7 @@ pub enum CholeskyError { impl Display for CholeskyError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "Matrix is not positive definite") + write!(f, "matrix is not positive definite") } } @@ -279,7 +279,7 @@ impl CscCholesky { /// /// Panics if `b` is not square. pub fn solve_mut<'a>(&'a self, b: impl Into>) { - let expect_msg = "If the Cholesky factorization succeeded,\ + let expect_msg = "if the Cholesky factorization succeeded,\ then the triangular solve should never fail"; // Solve LY = B let mut y = b.into(); diff --git a/nalgebra-sparse/src/ops/serial/cs.rs b/nalgebra-sparse/src/ops/serial/cs.rs index 86484053..9cd46152 100644 --- a/nalgebra-sparse/src/ops/serial/cs.rs +++ b/nalgebra-sparse/src/ops/serial/cs.rs @@ -8,7 +8,7 @@ use num_traits::{One, Zero}; fn spmm_cs_unexpected_entry() -> OperationError { OperationError::from_kind_and_message( OperationErrorKind::InvalidPattern, - String::from("Found unexpected entry that is not present in `c`."), + String::from("found unexpected entry that is not present in `c`"), ) } @@ -62,7 +62,7 @@ where fn spadd_cs_unexpected_entry() -> OperationError { OperationError::from_kind_and_message( OperationErrorKind::InvalidPattern, - String::from("Found entry in `op(a)` that is not present in `c`."), + String::from("found entry in `op(a)` that is not present in `c`"), ) } diff --git a/nalgebra-sparse/src/ops/serial/csc.rs b/nalgebra-sparse/src/ops/serial/csc.rs index e5c9ae4e..db7b81ba 100644 --- a/nalgebra-sparse/src/ops/serial/csc.rs +++ b/nalgebra-sparse/src/ops/serial/csc.rs @@ -132,12 +132,12 @@ pub fn spsolve_csc_lower_triangular<'a, T: RealField>( assert_eq!( l_matrix.nrows(), l_matrix.ncols(), - "Matrix must be square for triangular solve." + "matrix must be square for triangular solve" ); assert_eq!( l_matrix.nrows(), b.nrows(), - "Dimension mismatch in sparse lower triangular solver." + "dimension mismatch in sparse lower triangular solver" ); match l { Op::NoOp(a) => spsolve_csc_lower_triangular_no_transpose(a, b), @@ -196,7 +196,7 @@ fn spsolve_csc_lower_triangular_no_transpose( } fn spsolve_encountered_zero_diagonal() -> Result<(), OperationError> { - let message = "Matrix contains at least one diagonal entry that is zero."; + let message = "matrix contains at least one diagonal entry that is zero"; Err(OperationError::from_kind_and_message( OperationErrorKind::Singular, String::from(message), diff --git a/nalgebra-sparse/src/ops/serial/mod.rs b/nalgebra-sparse/src/ops/serial/mod.rs index d8f1a343..3ce47a66 100644 --- a/nalgebra-sparse/src/ops/serial/mod.rs +++ b/nalgebra-sparse/src/ops/serial/mod.rs @@ -108,16 +108,16 @@ impl OperationError { impl fmt::Display for OperationError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "Sparse matrix operation error: ")?; + write!(f, "sparse matrix operation error: ")?; match self.kind() { OperationErrorKind::InvalidPattern => { - write!(f, "InvalidPattern")?; + write!(f, "invalid pattern")?; } OperationErrorKind::Singular => { - write!(f, "Singular")?; + write!(f, "singular")?; } } - write!(f, " Message: {}", self.message) + write!(f, " message: {}", self.message) } } diff --git a/nalgebra-sparse/src/ops/serial/pattern.rs b/nalgebra-sparse/src/ops/serial/pattern.rs index b73f3375..97137e46 100644 --- a/nalgebra-sparse/src/ops/serial/pattern.rs +++ b/nalgebra-sparse/src/ops/serial/pattern.rs @@ -16,12 +16,12 @@ pub fn spadd_pattern(a: &SparsityPattern, b: &SparsityPattern) -> SparsityPatter assert_eq!( a.major_dim(), b.major_dim(), - "Patterns must have identical major dimensions." + "patterns must have identical major dimensions" ); assert_eq!( a.minor_dim(), b.minor_dim(), - "Patterns must have identical minor dimensions." + "patterns must have identical minor dimensions" ); let mut offsets = Vec::new(); @@ -40,7 +40,7 @@ pub fn spadd_pattern(a: &SparsityPattern, b: &SparsityPattern) -> SparsityPatter // TODO: Consider circumventing format checks? (requires unsafe, should benchmark first) SparsityPattern::try_from_offsets_and_indices(a.major_dim(), a.minor_dim(), offsets, indices) - .expect("Internal error: Pattern must be valid by definition") + .expect("internal error: pattern must be valid by definition") } /// Sparse matrix multiplication pattern construction, `C <- A * B`. @@ -114,7 +114,7 @@ pub fn spmm_csr_pattern(a: &SparsityPattern, b: &SparsityPattern) -> SparsityPat } SparsityPattern::try_from_offsets_and_indices(a.major_dim(), b.minor_dim(), offsets, indices) - .expect("Internal error: Invalid pattern during matrix multiplication pattern construction") + .expect("internal error: invalid pattern during matrix multiplication pattern construction") } /// Iterate over the union of the two sets represented by sorted slices diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 1d92dae3..818cf748 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -259,7 +259,7 @@ impl SparsityPattern { new_offsets, new_indices, ) - .expect("internal error: Transpose should never fail") + .expect("internal error: transpose should never fail") } } From a8fa7f71c0cb918b3dc0cd4d0ff0881fc2248c34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20L=C3=B6schner?= Date: Thu, 9 Dec 2021 18:18:32 +0100 Subject: [PATCH 13/81] Unify separate (de)serialization helper structs by using Cow<'a, [T]> --- nalgebra-sparse/src/coo.rs | 40 +++++++++++++--------------------- nalgebra-sparse/src/csc.rs | 40 +++++++++++++--------------------- nalgebra-sparse/src/csr.rs | 40 +++++++++++++--------------------- nalgebra-sparse/src/pattern.rs | 27 ++++++++--------------- 4 files changed, 54 insertions(+), 93 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 15b23566..d3612bdc 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -1,6 +1,7 @@ //! An implementation of the COO sparse matrix format. use crate::SparseFormatError; +use std::borrow::Cow; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; @@ -278,19 +279,19 @@ impl CooMatrix { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize)] -struct CooMatrixSerializationData<'a, T> { +#[derive(Serialize, Deserialize)] +struct CooMatrixSerializationData<'a, T: Clone> { nrows: usize, ncols: usize, - row_indices: &'a [usize], - col_indices: &'a [usize], - values: &'a [T], + row_indices: Cow<'a, [usize]>, + col_indices: Cow<'a, [usize]>, + values: Cow<'a, [T]>, } #[cfg(feature = "serde-serialize")] impl Serialize for CooMatrix where - T: Serialize, + T: Serialize + Clone, { fn serialize(&self, serializer: S) -> Result where @@ -299,42 +300,31 @@ where CooMatrixSerializationData { nrows: self.nrows(), ncols: self.ncols(), - row_indices: self.row_indices(), - col_indices: self.col_indices(), - values: self.values(), + row_indices: self.row_indices().into(), + col_indices: self.col_indices().into(), + values: self.values().into(), } .serialize(serializer) } } -#[cfg(feature = "serde-serialize")] -#[derive(Deserialize)] -struct CooMatrixDeserializationData { - nrows: usize, - ncols: usize, - row_indices: Vec, - col_indices: Vec, - values: Vec, -} - #[cfg(feature = "serde-serialize")] impl<'de, T> Deserialize<'de> for CooMatrix where - T: Deserialize<'de>, + T: Deserialize<'de> + Clone, { fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { - let de = CooMatrixDeserializationData::deserialize(deserializer)?; + let de = CooMatrixSerializationData::deserialize(deserializer)?; CooMatrix::try_from_triplets( de.nrows, de.ncols, - de.row_indices, - de.col_indices, - de.values, + de.row_indices.into(), + de.col_indices.into(), + de.values.into(), ) - .map(|m| m.into()) .map_err(|e| de::Error::custom(e)) } } diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 54383b01..28af852d 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -7,6 +7,7 @@ use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; use crate::csr::CsrMatrix; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind}; +use std::borrow::Cow; use nalgebra::Scalar; use num_traits::One; @@ -523,19 +524,19 @@ impl CscMatrix { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize)] -struct CscMatrixSerializationData<'a, T> { +#[derive(Serialize, Deserialize)] +struct CscMatrixSerializationData<'a, T: Clone> { nrows: usize, ncols: usize, - col_offsets: &'a [usize], - row_indices: &'a [usize], - values: &'a [T], + col_offsets: Cow<'a, [usize]>, + row_indices: Cow<'a, [usize]>, + values: Cow<'a, [T]>, } #[cfg(feature = "serde-serialize")] impl Serialize for CscMatrix where - T: Serialize, + T: Serialize + Clone, { fn serialize(&self, serializer: S) -> Result where @@ -544,42 +545,31 @@ where CscMatrixSerializationData { nrows: self.nrows(), ncols: self.ncols(), - col_offsets: self.col_offsets(), - row_indices: self.row_indices(), - values: self.values(), + col_offsets: Cow::Borrowed(self.col_offsets()), + row_indices: Cow::Borrowed(self.row_indices()), + values: Cow::Borrowed(self.values()), } .serialize(serializer) } } -#[cfg(feature = "serde-serialize")] -#[derive(Deserialize)] -struct CscMatrixDeserializationData { - nrows: usize, - ncols: usize, - col_offsets: Vec, - row_indices: Vec, - values: Vec, -} - #[cfg(feature = "serde-serialize")] impl<'de, T> Deserialize<'de> for CscMatrix where - T: Deserialize<'de>, + T: Deserialize<'de> + Clone, { fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { - let de = CscMatrixDeserializationData::deserialize(deserializer)?; + let de = CscMatrixSerializationData::deserialize(deserializer)?; CscMatrix::try_from_csc_data( de.nrows, de.ncols, - de.col_offsets, - de.row_indices, - de.values, + de.col_offsets.into(), + de.row_indices.into(), + de.values.into(), ) - .map(|m| m.into()) .map_err(|e| de::Error::custom(e)) } } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 804606ca..43fb2118 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -6,6 +6,7 @@ use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; use crate::csc::CscMatrix; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind}; +use std::borrow::Cow; use nalgebra::Scalar; use num_traits::One; @@ -594,19 +595,19 @@ impl CsrMatrix { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize)] -struct CsrMatrixSerializationData<'a, T> { +#[derive(Serialize, Deserialize)] +struct CsrMatrixSerializationData<'a, T: Clone> { nrows: usize, ncols: usize, - row_offsets: &'a [usize], - col_indices: &'a [usize], - values: &'a [T], + row_offsets: Cow<'a, [usize]>, + col_indices: Cow<'a, [usize]>, + values: Cow<'a, [T]>, } #[cfg(feature = "serde-serialize")] impl Serialize for CsrMatrix where - T: Serialize, + T: Serialize + Clone, { fn serialize(&self, serializer: S) -> Result where @@ -615,42 +616,31 @@ where CsrMatrixSerializationData { nrows: self.nrows(), ncols: self.ncols(), - row_offsets: self.row_offsets(), - col_indices: self.col_indices(), - values: self.values(), + row_offsets: Cow::Borrowed(self.row_offsets()), + col_indices: Cow::Borrowed(self.col_indices()), + values: Cow::Borrowed(self.values()), } .serialize(serializer) } } -#[cfg(feature = "serde-serialize")] -#[derive(Deserialize)] -struct CsrMatrixDeserializationData { - nrows: usize, - ncols: usize, - row_offsets: Vec, - col_indices: Vec, - values: Vec, -} - #[cfg(feature = "serde-serialize")] impl<'de, T> Deserialize<'de> for CsrMatrix where - T: Deserialize<'de>, + T: Deserialize<'de> + Clone, { fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { - let de = CsrMatrixDeserializationData::deserialize(deserializer)?; + let de = CsrMatrixSerializationData::deserialize(deserializer)?; CsrMatrix::try_from_csr_data( de.nrows, de.ncols, - de.row_offsets, - de.col_indices, - de.values, + de.row_offsets.into(), + de.col_indices.into(), + de.values.into(), ) - .map(|m| m.into()) .map_err(|e| de::Error::custom(e)) } } diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 818cf748..5f01ea10 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -1,6 +1,7 @@ //! Sparsity patterns for CSR and CSC matrices. use crate::cs::transpose_cs; use crate::SparseFormatError; +use std::borrow::Cow; use std::error::Error; use std::fmt; @@ -293,12 +294,12 @@ pub enum SparsityPatternFormatError { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize)] +#[derive(Serialize, Deserialize)] struct SparsityPatternSerializationData<'a> { major_dim: usize, minor_dim: usize, - major_offsets: &'a [usize], - minor_indices: &'a [usize], + major_offsets: Cow<'a, [usize]>, + minor_indices: Cow<'a, [usize]>, } #[cfg(feature = "serde-serialize")] @@ -310,36 +311,26 @@ impl Serialize for SparsityPattern { SparsityPatternSerializationData { major_dim: self.major_dim(), minor_dim: self.minor_dim(), - major_offsets: self.major_offsets(), - minor_indices: self.minor_indices(), + major_offsets: Cow::Borrowed(self.major_offsets()), + minor_indices: Cow::Borrowed(self.minor_indices()), } .serialize(serializer) } } -#[cfg(feature = "serde-serialize")] -#[derive(Deserialize)] -struct SparsityPatternDeserializationData { - major_dim: usize, - minor_dim: usize, - major_offsets: Vec, - minor_indices: Vec, -} - #[cfg(feature = "serde-serialize")] impl<'de> Deserialize<'de> for SparsityPattern { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { - let de = SparsityPatternDeserializationData::deserialize(deserializer)?; + let de = SparsityPatternSerializationData::deserialize(deserializer)?; SparsityPattern::try_from_offsets_and_indices( de.major_dim, de.minor_dim, - de.major_offsets, - de.minor_indices, + de.major_offsets.into(), + de.minor_indices.into(), ) - .map(|m| m.into()) .map_err(|e| de::Error::custom(e)) } } From 9b87fa4ffa7909b0a72d0e6e1bde6772568def0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20L=C3=B6schner?= Date: Thu, 9 Dec 2021 18:29:45 +0100 Subject: [PATCH 14/81] Add cfg attribute to Cow imports --- nalgebra-sparse/src/coo.rs | 3 ++- nalgebra-sparse/src/csc.rs | 3 ++- nalgebra-sparse/src/csr.rs | 3 ++- nalgebra-sparse/src/pattern.rs | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index d3612bdc..0cebb725 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -1,10 +1,11 @@ //! An implementation of the COO sparse matrix format. use crate::SparseFormatError; -use std::borrow::Cow; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(feature = "serde-serialize")] +use std::borrow::Cow; /// A COO representation of a sparse matrix. /// diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 28af852d..bd70d4c8 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -7,12 +7,13 @@ use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; use crate::csr::CsrMatrix; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind}; -use std::borrow::Cow; use nalgebra::Scalar; use num_traits::One; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(feature = "serde-serialize")] +use std::borrow::Cow; use std::slice::{Iter, IterMut}; /// A CSC representation of a sparse matrix. diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 43fb2118..bd43927d 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -6,12 +6,13 @@ use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; use crate::csc::CscMatrix; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind}; -use std::borrow::Cow; use nalgebra::Scalar; use num_traits::One; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(feature = "serde-serialize")] +use std::borrow::Cow; use std::iter::FromIterator; use std::slice::{Iter, IterMut}; diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 5f01ea10..a833250c 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -1,12 +1,13 @@ //! Sparsity patterns for CSR and CSC matrices. use crate::cs::transpose_cs; use crate::SparseFormatError; -use std::borrow::Cow; use std::error::Error; use std::fmt; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(feature = "serde-serialize")] +use std::borrow::Cow; /// A representation of the sparsity pattern of a CSR or CSC matrix. /// From 49eb1bd77846a59c9d13dfffe3e890645850973a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20L=C3=B6schner?= Date: Thu, 9 Dec 2021 21:47:59 +0100 Subject: [PATCH 15/81] CI: Run nalgebra-sparse builds with different feature sets, serde tests --- .github/workflows/nalgebra-ci-build.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/nalgebra-ci-build.yml b/.github/workflows/nalgebra-ci-build.yml index 3a56df13..b9df85da 100644 --- a/.github/workflows/nalgebra-ci-build.yml +++ b/.github/workflows/nalgebra-ci-build.yml @@ -38,8 +38,12 @@ jobs: run: cargo build --features serde-serialize - name: Build nalgebra-lapack run: cd nalgebra-lapack; cargo build; - - name: Build nalgebra-sparse + - name: Build nalgebra-sparse --no-default-features + run: cd nalgebra-sparse; cargo build --no-default-features; + - name: Build nalgebra-sparse (default features) run: cd nalgebra-sparse; cargo build; + - name: Build nalgebra-sparse --all-features + run: cd nalgebra-sparse; cargo build --all-features; # Run this on it’s own job because it alone takes a lot of time. # So it’s best to let it run in parallel to the other jobs. build-nalgebra-all-features: @@ -71,10 +75,10 @@ jobs: - name: test nalgebra-sparse # Manifest-path is necessary because cargo otherwise won't correctly forward features # We increase number of proptest cases to hopefully catch more potential bugs - run: PROPTEST_CASES=10000 cargo test --manifest-path=nalgebra-sparse/Cargo.toml --features compare,proptest-support,io + run: PROPTEST_CASES=10000 cargo test --manifest-path=nalgebra-sparse/Cargo.toml --features compare,proptest-support,io,serde-serialize - name: test nalgebra-sparse (slow tests) # Unfortunately, the "slow-tests" take so much time that we need to run them with --release - run: PROPTEST_CASES=10000 cargo test --release --manifest-path=nalgebra-sparse/Cargo.toml --features compare,proptest-support,io,slow-tests slow + run: PROPTEST_CASES=10000 cargo test --release --manifest-path=nalgebra-sparse/Cargo.toml --features compare,proptest-support,io,serde-serialize,slow-tests slow test-nalgebra-macros: runs-on: ubuntu-latest steps: From 837ded932e8e2724cb2c29317a90566793e56256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20L=C3=B6schner?= Date: Wed, 15 Dec 2021 11:43:35 +0100 Subject: [PATCH 16/81] Replace usage of Cow with generic type --- nalgebra-sparse/src/coo.rs | 26 ++++++++++++-------------- nalgebra-sparse/src/csc.rs | 26 ++++++++++++-------------- nalgebra-sparse/src/csr.rs | 26 ++++++++++++-------------- nalgebra-sparse/src/pattern.rs | 20 +++++++++----------- 4 files changed, 45 insertions(+), 53 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 0cebb725..3e3c7b4c 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -4,8 +4,6 @@ use crate::SparseFormatError; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -use std::borrow::Cow; /// A COO representation of a sparse matrix. /// @@ -281,12 +279,12 @@ impl CooMatrix { #[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)] -struct CooMatrixSerializationData<'a, T: Clone> { +struct CooMatrixSerializationData { nrows: usize, ncols: usize, - row_indices: Cow<'a, [usize]>, - col_indices: Cow<'a, [usize]>, - values: Cow<'a, [T]>, + row_indices: Indices, + col_indices: Indices, + values: Values, } #[cfg(feature = "serde-serialize")] @@ -298,12 +296,12 @@ where where S: Serializer, { - CooMatrixSerializationData { + CooMatrixSerializationData::<&[usize], &[T]> { nrows: self.nrows(), ncols: self.ncols(), - row_indices: self.row_indices().into(), - col_indices: self.col_indices().into(), - values: self.values().into(), + row_indices: self.row_indices(), + col_indices: self.col_indices(), + values: self.values(), } .serialize(serializer) } @@ -318,13 +316,13 @@ where where D: Deserializer<'de>, { - let de = CooMatrixSerializationData::deserialize(deserializer)?; + let de = CooMatrixSerializationData::, Vec>::deserialize(deserializer)?; CooMatrix::try_from_triplets( de.nrows, de.ncols, - de.row_indices.into(), - de.col_indices.into(), - de.values.into(), + de.row_indices, + de.col_indices, + de.values, ) .map_err(|e| de::Error::custom(e)) } diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index bd70d4c8..7c7832b5 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -12,8 +12,6 @@ use nalgebra::Scalar; use num_traits::One; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -use std::borrow::Cow; use std::slice::{Iter, IterMut}; /// A CSC representation of a sparse matrix. @@ -526,12 +524,12 @@ impl CscMatrix { #[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)] -struct CscMatrixSerializationData<'a, T: Clone> { +struct CscMatrixSerializationData { nrows: usize, ncols: usize, - col_offsets: Cow<'a, [usize]>, - row_indices: Cow<'a, [usize]>, - values: Cow<'a, [T]>, + col_offsets: Indices, + row_indices: Indices, + values: Values, } #[cfg(feature = "serde-serialize")] @@ -543,12 +541,12 @@ where where S: Serializer, { - CscMatrixSerializationData { + CscMatrixSerializationData::<&[usize], &[T]> { nrows: self.nrows(), ncols: self.ncols(), - col_offsets: Cow::Borrowed(self.col_offsets()), - row_indices: Cow::Borrowed(self.row_indices()), - values: Cow::Borrowed(self.values()), + col_offsets: self.col_offsets(), + row_indices: self.row_indices(), + values: self.values(), } .serialize(serializer) } @@ -563,13 +561,13 @@ where where D: Deserializer<'de>, { - let de = CscMatrixSerializationData::deserialize(deserializer)?; + let de = CscMatrixSerializationData::, Vec>::deserialize(deserializer)?; CscMatrix::try_from_csc_data( de.nrows, de.ncols, - de.col_offsets.into(), - de.row_indices.into(), - de.values.into(), + de.col_offsets, + de.row_indices, + de.values, ) .map_err(|e| de::Error::custom(e)) } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index bd43927d..786736ff 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -11,8 +11,6 @@ use nalgebra::Scalar; use num_traits::One; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -use std::borrow::Cow; use std::iter::FromIterator; use std::slice::{Iter, IterMut}; @@ -597,12 +595,12 @@ impl CsrMatrix { #[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)] -struct CsrMatrixSerializationData<'a, T: Clone> { +struct CsrMatrixSerializationData { nrows: usize, ncols: usize, - row_offsets: Cow<'a, [usize]>, - col_indices: Cow<'a, [usize]>, - values: Cow<'a, [T]>, + row_offsets: Indices, + col_indices: Indices, + values: Values, } #[cfg(feature = "serde-serialize")] @@ -614,12 +612,12 @@ where where S: Serializer, { - CsrMatrixSerializationData { + CsrMatrixSerializationData::<&[usize], &[T]> { nrows: self.nrows(), ncols: self.ncols(), - row_offsets: Cow::Borrowed(self.row_offsets()), - col_indices: Cow::Borrowed(self.col_indices()), - values: Cow::Borrowed(self.values()), + row_offsets: self.row_offsets(), + col_indices: self.col_indices(), + values: self.values(), } .serialize(serializer) } @@ -634,13 +632,13 @@ where where D: Deserializer<'de>, { - let de = CsrMatrixSerializationData::deserialize(deserializer)?; + let de = CsrMatrixSerializationData::, Vec>::deserialize(deserializer)?; CsrMatrix::try_from_csr_data( de.nrows, de.ncols, - de.row_offsets.into(), - de.col_indices.into(), - de.values.into(), + de.row_offsets, + de.col_indices, + de.values, ) .map_err(|e| de::Error::custom(e)) } diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index a833250c..f7289438 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -6,8 +6,6 @@ use std::fmt; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -use std::borrow::Cow; /// A representation of the sparsity pattern of a CSR or CSC matrix. /// @@ -296,11 +294,11 @@ pub enum SparsityPatternFormatError { #[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)] -struct SparsityPatternSerializationData<'a> { +struct SparsityPatternSerializationData { major_dim: usize, minor_dim: usize, - major_offsets: Cow<'a, [usize]>, - minor_indices: Cow<'a, [usize]>, + major_offsets: Indices, + minor_indices: Indices, } #[cfg(feature = "serde-serialize")] @@ -309,11 +307,11 @@ impl Serialize for SparsityPattern { where S: Serializer, { - SparsityPatternSerializationData { + SparsityPatternSerializationData::<&[usize]> { major_dim: self.major_dim(), minor_dim: self.minor_dim(), - major_offsets: Cow::Borrowed(self.major_offsets()), - minor_indices: Cow::Borrowed(self.minor_indices()), + major_offsets: self.major_offsets(), + minor_indices: self.minor_indices(), } .serialize(serializer) } @@ -325,12 +323,12 @@ impl<'de> Deserialize<'de> for SparsityPattern { where D: Deserializer<'de>, { - let de = SparsityPatternSerializationData::deserialize(deserializer)?; + let de = SparsityPatternSerializationData::>::deserialize(deserializer)?; SparsityPattern::try_from_offsets_and_indices( de.major_dim, de.minor_dim, - de.major_offsets.into(), - de.minor_indices.into(), + de.major_offsets, + de.minor_indices, ) .map_err(|e| de::Error::custom(e)) } From 647455daddd4fdc3830c17a97282bcd43803a36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20L=C3=B6schner?= Date: Wed, 15 Dec 2021 11:48:51 +0100 Subject: [PATCH 17/81] Move serialization code to submodules --- nalgebra-sparse/src/coo.rs | 84 ++++++++++++++++---------------- nalgebra-sparse/src/csc.rs | 89 +++++++++++++++++----------------- nalgebra-sparse/src/csr.rs | 89 +++++++++++++++++----------------- nalgebra-sparse/src/pattern.rs | 76 ++++++++++++++--------------- 4 files changed, 170 insertions(+), 168 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 3e3c7b4c..06788406 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -2,9 +2,6 @@ use crate::SparseFormatError; -#[cfg(feature = "serde-serialize")] -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - /// A COO representation of a sparse matrix. /// /// A COO matrix stores entries in coordinate-form, that is triplets `(i, j, v)`, where `i` and `j` @@ -278,52 +275,55 @@ impl CooMatrix { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize, Deserialize)] -struct CooMatrixSerializationData { - nrows: usize, - ncols: usize, - row_indices: Indices, - col_indices: Indices, - values: Values, -} +mod serde_serialize { + use super::CooMatrix; + use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -impl Serialize for CooMatrix -where - T: Serialize + Clone, -{ - fn serialize(&self, serializer: S) -> Result + #[derive(Serialize, Deserialize)] + struct CooMatrixSerializationData { + nrows: usize, + ncols: usize, + row_indices: Indices, + col_indices: Indices, + values: Values, + } + + impl Serialize for CooMatrix where - S: Serializer, + T: Serialize + Clone, { - CooMatrixSerializationData::<&[usize], &[T]> { - nrows: self.nrows(), - ncols: self.ncols(), - row_indices: self.row_indices(), - col_indices: self.col_indices(), - values: self.values(), + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CooMatrixSerializationData::<&[usize], &[T]> { + nrows: self.nrows(), + ncols: self.ncols(), + row_indices: self.row_indices(), + col_indices: self.col_indices(), + values: self.values(), + } + .serialize(serializer) } - .serialize(serializer) } -} -#[cfg(feature = "serde-serialize")] -impl<'de, T> Deserialize<'de> for CooMatrix -where - T: Deserialize<'de> + Clone, -{ - fn deserialize(deserializer: D) -> Result, D::Error> + impl<'de, T> Deserialize<'de> for CooMatrix where - D: Deserializer<'de>, + T: Deserialize<'de> + Clone, { - let de = CooMatrixSerializationData::, Vec>::deserialize(deserializer)?; - CooMatrix::try_from_triplets( - de.nrows, - de.ncols, - de.row_indices, - de.col_indices, - de.values, - ) - .map_err(|e| de::Error::custom(e)) + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CooMatrixSerializationData::, Vec>::deserialize(deserializer)?; + CooMatrix::try_from_triplets( + de.nrows, + de.ncols, + de.row_indices, + de.col_indices, + de.values, + ) + .map_err(|e| de::Error::custom(e)) + } } } diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 7c7832b5..29861684 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -10,8 +10,6 @@ use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKin use nalgebra::Scalar; use num_traits::One; -#[cfg(feature = "serde-serialize")] -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::slice::{Iter, IterMut}; /// A CSC representation of a sparse matrix. @@ -523,53 +521,56 @@ impl CscMatrix { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize, Deserialize)] -struct CscMatrixSerializationData { - nrows: usize, - ncols: usize, - col_offsets: Indices, - row_indices: Indices, - values: Values, -} +mod serde_serialize { + use super::CscMatrix; + use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -impl Serialize for CscMatrix -where - T: Serialize + Clone, -{ - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - CscMatrixSerializationData::<&[usize], &[T]> { - nrows: self.nrows(), - ncols: self.ncols(), - col_offsets: self.col_offsets(), - row_indices: self.row_indices(), - values: self.values(), - } - .serialize(serializer) + #[derive(Serialize, Deserialize)] + struct CscMatrixSerializationData { + nrows: usize, + ncols: usize, + col_offsets: Indices, + row_indices: Indices, + values: Values, } -} -#[cfg(feature = "serde-serialize")] -impl<'de, T> Deserialize<'de> for CscMatrix -where - T: Deserialize<'de> + Clone, -{ - fn deserialize(deserializer: D) -> Result, D::Error> + impl Serialize for CscMatrix where - D: Deserializer<'de>, + T: Serialize + Clone, { - let de = CscMatrixSerializationData::, Vec>::deserialize(deserializer)?; - CscMatrix::try_from_csc_data( - de.nrows, - de.ncols, - de.col_offsets, - de.row_indices, - de.values, - ) - .map_err(|e| de::Error::custom(e)) + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CscMatrixSerializationData::<&[usize], &[T]> { + nrows: self.nrows(), + ncols: self.ncols(), + col_offsets: self.col_offsets(), + row_indices: self.row_indices(), + values: self.values(), + } + .serialize(serializer) + } + } + + impl<'de, T> Deserialize<'de> for CscMatrix + where + T: Deserialize<'de> + Clone, + { + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CscMatrixSerializationData::, Vec>::deserialize(deserializer)?; + CscMatrix::try_from_csc_data( + de.nrows, + de.ncols, + de.col_offsets, + de.row_indices, + de.values, + ) + .map_err(|e| de::Error::custom(e)) + } } } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 786736ff..6be91f94 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -9,8 +9,6 @@ use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKin use nalgebra::Scalar; use num_traits::One; -#[cfg(feature = "serde-serialize")] -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::iter::FromIterator; use std::slice::{Iter, IterMut}; @@ -594,53 +592,56 @@ impl CsrMatrix { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize, Deserialize)] -struct CsrMatrixSerializationData { - nrows: usize, - ncols: usize, - row_offsets: Indices, - col_indices: Indices, - values: Values, -} +mod serde_serialize { + use super::CsrMatrix; + use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -impl Serialize for CsrMatrix -where - T: Serialize + Clone, -{ - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - CsrMatrixSerializationData::<&[usize], &[T]> { - nrows: self.nrows(), - ncols: self.ncols(), - row_offsets: self.row_offsets(), - col_indices: self.col_indices(), - values: self.values(), - } - .serialize(serializer) + #[derive(Serialize, Deserialize)] + struct CsrMatrixSerializationData { + nrows: usize, + ncols: usize, + row_offsets: Indices, + col_indices: Indices, + values: Values, } -} -#[cfg(feature = "serde-serialize")] -impl<'de, T> Deserialize<'de> for CsrMatrix -where - T: Deserialize<'de> + Clone, -{ - fn deserialize(deserializer: D) -> Result, D::Error> + impl Serialize for CsrMatrix where - D: Deserializer<'de>, + T: Serialize + Clone, { - let de = CsrMatrixSerializationData::, Vec>::deserialize(deserializer)?; - CsrMatrix::try_from_csr_data( - de.nrows, - de.ncols, - de.row_offsets, - de.col_indices, - de.values, - ) - .map_err(|e| de::Error::custom(e)) + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CsrMatrixSerializationData::<&[usize], &[T]> { + nrows: self.nrows(), + ncols: self.ncols(), + row_offsets: self.row_offsets(), + col_indices: self.col_indices(), + values: self.values(), + } + .serialize(serializer) + } + } + + impl<'de, T> Deserialize<'de> for CsrMatrix + where + T: Deserialize<'de> + Clone, + { + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CsrMatrixSerializationData::, Vec>::deserialize(deserializer)?; + CsrMatrix::try_from_csr_data( + de.nrows, + de.ncols, + de.row_offsets, + de.col_indices, + de.values, + ) + .map_err(|e| de::Error::custom(e)) + } } } diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index f7289438..1253982b 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -4,9 +4,6 @@ use crate::SparseFormatError; use std::error::Error; use std::fmt; -#[cfg(feature = "serde-serialize")] -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - /// A representation of the sparsity pattern of a CSR or CSC matrix. /// /// CSR and CSC matrices store matrices in a very similar fashion. In fact, in a certain sense, @@ -293,44 +290,47 @@ pub enum SparsityPatternFormatError { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize, Deserialize)] -struct SparsityPatternSerializationData { - major_dim: usize, - minor_dim: usize, - major_offsets: Indices, - minor_indices: Indices, -} +mod serde_serialize { + use super::SparsityPattern; + use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -impl Serialize for SparsityPattern { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - SparsityPatternSerializationData::<&[usize]> { - major_dim: self.major_dim(), - minor_dim: self.minor_dim(), - major_offsets: self.major_offsets(), - minor_indices: self.minor_indices(), - } - .serialize(serializer) + #[derive(Serialize, Deserialize)] + struct SparsityPatternSerializationData { + major_dim: usize, + minor_dim: usize, + major_offsets: Indices, + minor_indices: Indices, } -} -#[cfg(feature = "serde-serialize")] -impl<'de> Deserialize<'de> for SparsityPattern { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let de = SparsityPatternSerializationData::>::deserialize(deserializer)?; - SparsityPattern::try_from_offsets_and_indices( - de.major_dim, - de.minor_dim, - de.major_offsets, - de.minor_indices, - ) - .map_err(|e| de::Error::custom(e)) + impl Serialize for SparsityPattern { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + SparsityPatternSerializationData::<&[usize]> { + major_dim: self.major_dim(), + minor_dim: self.minor_dim(), + major_offsets: self.major_offsets(), + minor_indices: self.minor_indices(), + } + .serialize(serializer) + } + } + + impl<'de> Deserialize<'de> for SparsityPattern { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let de = SparsityPatternSerializationData::>::deserialize(deserializer)?; + SparsityPattern::try_from_offsets_and_indices( + de.major_dim, + de.minor_dim, + de.major_offsets, + de.minor_indices, + ) + .map_err(|e| de::Error::custom(e)) + } } } From 513178e03eb7e54d85aadb01819d051ec23879b7 Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 17:15:40 +0100 Subject: [PATCH 18/81] Revert "Updated more error messages" This reverts commit a42eae45e045c391ede37a7a4328c09cbbb87a0f. --- nalgebra-sparse/src/convert/serial.rs | 12 ++++++------ nalgebra-sparse/src/factorization/cholesky.rs | 6 +++--- nalgebra-sparse/src/ops/serial/cs.rs | 4 ++-- nalgebra-sparse/src/ops/serial/csc.rs | 6 +++--- nalgebra-sparse/src/ops/serial/mod.rs | 8 ++++---- nalgebra-sparse/src/ops/serial/pattern.rs | 8 ++++---- nalgebra-sparse/src/pattern.rs | 2 +- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/nalgebra-sparse/src/convert/serial.rs b/nalgebra-sparse/src/convert/serial.rs index 9fc3d04e..ecbe1dab 100644 --- a/nalgebra-sparse/src/convert/serial.rs +++ b/nalgebra-sparse/src/convert/serial.rs @@ -64,7 +64,7 @@ where // TODO: Avoid "try_from" since it validates the data? (requires unsafe, should benchmark // to see if it can be justified for performance reasons) CsrMatrix::try_from_csr_data(coo.nrows(), coo.ncols(), offsets, indices, values) - .expect("internal error: invalid CSR data during COO->CSR conversion") + .expect("Internal error: Invalid CSR data during COO->CSR conversion") } /// Converts a [`CsrMatrix`] to a [`CooMatrix`]. @@ -120,7 +120,7 @@ where // TODO: Consider circumventing the data validity check here // (would require unsafe, should benchmark) CsrMatrix::try_from_csr_data(dense.nrows(), dense.ncols(), row_offsets, col_idx, values) - .expect("internal error: invalid CsrMatrix format during dense -> CSR conversion") + .expect("Internal error: Invalid CsrMatrix format during dense-> CSR conversion") } /// Converts a [`CooMatrix`] to a [`CscMatrix`]. @@ -138,7 +138,7 @@ where // TODO: Avoid "try_from" since it validates the data? (requires unsafe, should benchmark // to see if it can be justified for performance reasons) CscMatrix::try_from_csc_data(coo.nrows(), coo.ncols(), offsets, indices, values) - .expect("internal error: invalid CSC data during COO -> CSC conversion") + .expect("Internal error: Invalid CSC data during COO->CSC conversion") } /// Converts a [`CscMatrix`] to a [`CooMatrix`]. @@ -194,7 +194,7 @@ where // TODO: Consider circumventing the data validity check here // (would require unsafe, should benchmark) CscMatrix::try_from_csc_data(dense.nrows(), dense.ncols(), col_offsets, row_idx, values) - .expect("internal error: invalid CscMatrix format during dense -> CSC conversion") + .expect("Internal error: Invalid CscMatrix format during dense-> CSC conversion") } /// Converts a [`CsrMatrix`] to a [`CscMatrix`]. @@ -212,7 +212,7 @@ where // TODO: Avoid data validity check? CscMatrix::try_from_csc_data(csr.nrows(), csr.ncols(), offsets, indices, values) - .expect("internal error: invalid CSC data during CSR -> CSC conversion") + .expect("Internal error: Invalid CSC data during CSR->CSC conversion") } /// Converts a [`CscMatrix`] to a [`CsrMatrix`]. @@ -230,7 +230,7 @@ where // TODO: Avoid data validity check? CsrMatrix::try_from_csr_data(csc.nrows(), csc.ncols(), offsets, indices, values) - .expect("internal error: invalid CSR data during CSC -> CSR conversion") + .expect("Internal error: Invalid CSR data during CSC->CSR conversion") } fn convert_coo_cs( diff --git a/nalgebra-sparse/src/factorization/cholesky.rs b/nalgebra-sparse/src/factorization/cholesky.rs index b306f104..1f653278 100644 --- a/nalgebra-sparse/src/factorization/cholesky.rs +++ b/nalgebra-sparse/src/factorization/cholesky.rs @@ -31,7 +31,7 @@ impl CscSymbolicCholesky { assert_eq!( pattern.major_dim(), pattern.minor_dim(), - "major and minor dimensions must be the same (square matrix)" + "Major and minor dimensions must be the same (square matrix)." ); let (l_pattern, u_pattern) = nonzero_pattern(&pattern); Self { @@ -82,7 +82,7 @@ pub enum CholeskyError { impl Display for CholeskyError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "matrix is not positive definite") + write!(f, "Matrix is not positive definite") } } @@ -279,7 +279,7 @@ impl CscCholesky { /// /// Panics if `b` is not square. pub fn solve_mut<'a>(&'a self, b: impl Into>) { - let expect_msg = "if the Cholesky factorization succeeded,\ + let expect_msg = "If the Cholesky factorization succeeded,\ then the triangular solve should never fail"; // Solve LY = B let mut y = b.into(); diff --git a/nalgebra-sparse/src/ops/serial/cs.rs b/nalgebra-sparse/src/ops/serial/cs.rs index 9cd46152..86484053 100644 --- a/nalgebra-sparse/src/ops/serial/cs.rs +++ b/nalgebra-sparse/src/ops/serial/cs.rs @@ -8,7 +8,7 @@ use num_traits::{One, Zero}; fn spmm_cs_unexpected_entry() -> OperationError { OperationError::from_kind_and_message( OperationErrorKind::InvalidPattern, - String::from("found unexpected entry that is not present in `c`"), + String::from("Found unexpected entry that is not present in `c`."), ) } @@ -62,7 +62,7 @@ where fn spadd_cs_unexpected_entry() -> OperationError { OperationError::from_kind_and_message( OperationErrorKind::InvalidPattern, - String::from("found entry in `op(a)` that is not present in `c`"), + String::from("Found entry in `op(a)` that is not present in `c`."), ) } diff --git a/nalgebra-sparse/src/ops/serial/csc.rs b/nalgebra-sparse/src/ops/serial/csc.rs index db7b81ba..e5c9ae4e 100644 --- a/nalgebra-sparse/src/ops/serial/csc.rs +++ b/nalgebra-sparse/src/ops/serial/csc.rs @@ -132,12 +132,12 @@ pub fn spsolve_csc_lower_triangular<'a, T: RealField>( assert_eq!( l_matrix.nrows(), l_matrix.ncols(), - "matrix must be square for triangular solve" + "Matrix must be square for triangular solve." ); assert_eq!( l_matrix.nrows(), b.nrows(), - "dimension mismatch in sparse lower triangular solver" + "Dimension mismatch in sparse lower triangular solver." ); match l { Op::NoOp(a) => spsolve_csc_lower_triangular_no_transpose(a, b), @@ -196,7 +196,7 @@ fn spsolve_csc_lower_triangular_no_transpose( } fn spsolve_encountered_zero_diagonal() -> Result<(), OperationError> { - let message = "matrix contains at least one diagonal entry that is zero"; + let message = "Matrix contains at least one diagonal entry that is zero."; Err(OperationError::from_kind_and_message( OperationErrorKind::Singular, String::from(message), diff --git a/nalgebra-sparse/src/ops/serial/mod.rs b/nalgebra-sparse/src/ops/serial/mod.rs index 3ce47a66..d8f1a343 100644 --- a/nalgebra-sparse/src/ops/serial/mod.rs +++ b/nalgebra-sparse/src/ops/serial/mod.rs @@ -108,16 +108,16 @@ impl OperationError { impl fmt::Display for OperationError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "sparse matrix operation error: ")?; + write!(f, "Sparse matrix operation error: ")?; match self.kind() { OperationErrorKind::InvalidPattern => { - write!(f, "invalid pattern")?; + write!(f, "InvalidPattern")?; } OperationErrorKind::Singular => { - write!(f, "singular")?; + write!(f, "Singular")?; } } - write!(f, " message: {}", self.message) + write!(f, " Message: {}", self.message) } } diff --git a/nalgebra-sparse/src/ops/serial/pattern.rs b/nalgebra-sparse/src/ops/serial/pattern.rs index 97137e46..b73f3375 100644 --- a/nalgebra-sparse/src/ops/serial/pattern.rs +++ b/nalgebra-sparse/src/ops/serial/pattern.rs @@ -16,12 +16,12 @@ pub fn spadd_pattern(a: &SparsityPattern, b: &SparsityPattern) -> SparsityPatter assert_eq!( a.major_dim(), b.major_dim(), - "patterns must have identical major dimensions" + "Patterns must have identical major dimensions." ); assert_eq!( a.minor_dim(), b.minor_dim(), - "patterns must have identical minor dimensions" + "Patterns must have identical minor dimensions." ); let mut offsets = Vec::new(); @@ -40,7 +40,7 @@ pub fn spadd_pattern(a: &SparsityPattern, b: &SparsityPattern) -> SparsityPatter // TODO: Consider circumventing format checks? (requires unsafe, should benchmark first) SparsityPattern::try_from_offsets_and_indices(a.major_dim(), a.minor_dim(), offsets, indices) - .expect("internal error: pattern must be valid by definition") + .expect("Internal error: Pattern must be valid by definition") } /// Sparse matrix multiplication pattern construction, `C <- A * B`. @@ -114,7 +114,7 @@ pub fn spmm_csr_pattern(a: &SparsityPattern, b: &SparsityPattern) -> SparsityPat } SparsityPattern::try_from_offsets_and_indices(a.major_dim(), b.minor_dim(), offsets, indices) - .expect("internal error: invalid pattern during matrix multiplication pattern construction") + .expect("Internal error: Invalid pattern during matrix multiplication pattern construction") } /// Iterate over the union of the two sets represented by sorted slices diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 1253982b..da16df7c 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -256,7 +256,7 @@ impl SparsityPattern { new_offsets, new_indices, ) - .expect("internal error: transpose should never fail") + .expect("internal error: Transpose should never fail") } } From fe70a80e417f1daefba2c9117929d33283c891cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20L=C3=B6schner?= Date: Tue, 28 Dec 2021 12:12:31 +0100 Subject: [PATCH 19/81] Partial revert "Use custom serde errors, make all sparse errs lowercase" --- nalgebra-sparse/src/coo.rs | 8 ++++---- nalgebra-sparse/src/csc.rs | 16 ++++++++-------- nalgebra-sparse/src/csr.rs | 22 +++++++++++----------- nalgebra-sparse/src/pattern.rs | 16 ++++++++-------- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 06788406..4ad382dc 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -124,12 +124,12 @@ impl CooMatrix { if row_indices.len() != col_indices.len() { return Err(SparseFormatError::from_kind_and_msg( InvalidStructure, - "number of row and col indices must be the same", + "Number of row and col indices must be the same.", )); } else if col_indices.len() != values.len() { return Err(SparseFormatError::from_kind_and_msg( InvalidStructure, - "number of col indices and values must be the same", + "Number of col indices and values must be the same.", )); } @@ -139,12 +139,12 @@ impl CooMatrix { if !row_indices_in_bounds { Err(SparseFormatError::from_kind_and_msg( IndexOutOfBounds, - "row index out of bounds", + "Row index out of bounds.", )) } else if !col_indices_in_bounds { Err(SparseFormatError::from_kind_and_msg( IndexOutOfBounds, - "col index out of bounds", + "Col index out of bounds.", )) } else { Ok(Self { diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 29861684..c3c843c7 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -181,7 +181,7 @@ impl CscMatrix { } else { Err(SparseFormatError::from_kind_and_msg( SparseFormatErrorKind::InvalidStructure, - "number of values and row indices must be the same", + "Number of values and row indices must be the same", )) } } @@ -587,28 +587,28 @@ fn pattern_format_error_to_csc_error(err: SparsityPatternFormatError) -> SparseF match err { InvalidOffsetArrayLength => E::from_kind_and_msg( K::InvalidStructure, - "length of col offset array is not equal to ncols + 1", + "Length of col offset array is not equal to ncols + 1.", ), InvalidOffsetFirstLast => E::from_kind_and_msg( K::InvalidStructure, - "first or last col offset is inconsistent with format specification", + "First or last col offset is inconsistent with format specification.", ), NonmonotonicOffsets => E::from_kind_and_msg( K::InvalidStructure, - "col offsets are not monotonically increasing", + "Col offsets are not monotonically increasing.", ), NonmonotonicMinorIndices => E::from_kind_and_msg( K::InvalidStructure, - "row indices are not monotonically increasing (sorted) within each column", + "Row indices are not monotonically increasing (sorted) within each column.", ), MajorIndexOutOfBounds => { - E::from_kind_and_msg(K::IndexOutOfBounds, "column indices are out of bounds") + E::from_kind_and_msg(K::IndexOutOfBounds, "Column indices are out of bounds.") } MinorIndexOutOfBounds => { - E::from_kind_and_msg(K::IndexOutOfBounds, "row indices are out of bounds") + E::from_kind_and_msg(K::IndexOutOfBounds, "Row indices are out of bounds.") } PatternDuplicateEntry => { - E::from_kind_and_msg(K::DuplicateEntry, "matrix data contains duplicate entries") + E::from_kind_and_msg(K::DuplicateEntry, "Matrix data contains duplicate entries.") } } } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 6be91f94..88c335b2 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -192,14 +192,14 @@ impl CsrMatrix { if col_indices.len() != values.len() { return Err(SparseFormatError::from_kind_and_msg( SparseFormatErrorKind::InvalidStructure, - "number of values and column indices must be the same", + "Number of values and column indices must be the same", )); } if row_offsets.len() == 0 { return Err(SparseFormatError::from_kind_and_msg( SparseFormatErrorKind::InvalidStructure, - "number of offsets should be greater than 0", + "Number of offsets should be greater than 0", )); } @@ -208,7 +208,7 @@ impl CsrMatrix { if next_offset > count { return Err(SparseFormatError::from_kind_and_msg( SparseFormatErrorKind::InvalidStructure, - "no row offset should be greater than the number of column indices", + "No row offset should be greater than the number of column indices", )); } if offset > next_offset { @@ -252,7 +252,7 @@ impl CsrMatrix { } else { Err(SparseFormatError::from_kind_and_msg( SparseFormatErrorKind::InvalidStructure, - "number of values and column indices must be the same", + "Number of values and column indices must be the same", )) } } @@ -658,28 +658,28 @@ fn pattern_format_error_to_csr_error(err: SparsityPatternFormatError) -> SparseF match err { InvalidOffsetArrayLength => E::from_kind_and_msg( K::InvalidStructure, - "length of row offset array is not equal to nrows + 1", + "Length of row offset array is not equal to nrows + 1.", ), InvalidOffsetFirstLast => E::from_kind_and_msg( K::InvalidStructure, - "first or last row offset is inconsistent with format specification", + "First or last row offset is inconsistent with format specification.", ), NonmonotonicOffsets => E::from_kind_and_msg( K::InvalidStructure, - "row offsets are not monotonically increasing", + "Row offsets are not monotonically increasing.", ), NonmonotonicMinorIndices => E::from_kind_and_msg( K::InvalidStructure, - "column indices are not monotonically increasing (sorted) within each row", + "Column indices are not monotonically increasing (sorted) within each row.", ), MajorIndexOutOfBounds => { - E::from_kind_and_msg(K::IndexOutOfBounds, "row indices are out of bounds") + E::from_kind_and_msg(K::IndexOutOfBounds, "Row indices are out of bounds.") } MinorIndexOutOfBounds => { - E::from_kind_and_msg(K::IndexOutOfBounds, "column indices are out of bounds") + E::from_kind_and_msg(K::IndexOutOfBounds, "Column indices are out of bounds.") } PatternDuplicateEntry => { - E::from_kind_and_msg(K::DuplicateEntry, "matrix data contains duplicate entries") + E::from_kind_and_msg(K::DuplicateEntry, "Matrix data contains duplicate entries.") } } } diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index da16df7c..82a93ffd 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -256,7 +256,7 @@ impl SparsityPattern { new_offsets, new_indices, ) - .expect("internal error: Transpose should never fail") + .expect("Internal error: Transpose should never fail.") } } @@ -363,27 +363,27 @@ impl fmt::Display for SparsityPatternFormatError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { SparsityPatternFormatError::InvalidOffsetArrayLength => { - write!(f, "length of offset array is not equal to (major_dim + 1)") + write!(f, "Length of offset array is not equal to (major_dim + 1).") } SparsityPatternFormatError::InvalidOffsetFirstLast => { - write!(f, "first or last offset is incompatible with format") + write!(f, "First or last offset is incompatible with format.") } SparsityPatternFormatError::NonmonotonicOffsets => { - write!(f, "offsets are not monotonically increasing") + write!(f, "Offsets are not monotonically increasing.") } SparsityPatternFormatError::MajorIndexOutOfBounds => { - write!(f, "a major index is out of bounds") + write!(f, "A major index is out of bounds.") } SparsityPatternFormatError::MinorIndexOutOfBounds => { - write!(f, "a minor index is out of bounds") + write!(f, "A minor index is out of bounds.") } SparsityPatternFormatError::DuplicateEntry => { - write!(f, "input data contains duplicate entries") + write!(f, "Input data contains duplicate entries.") } SparsityPatternFormatError::NonmonotonicMinorIndices => { write!( f, - "minor indices are not monotonically increasing within each lane" + "Minor indices are not monotonically increasing within each lane." ) } } From 583fde05fe8233cedb2e83280a66d453b73295f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20L=C3=B6schner?= Date: Tue, 28 Dec 2021 12:36:52 +0100 Subject: [PATCH 20/81] Add comment explaining intermediate types for serialization --- nalgebra-sparse/src/coo.rs | 15 +++++++++++++++ nalgebra-sparse/src/csc.rs | 15 +++++++++++++++ nalgebra-sparse/src/csr.rs | 15 +++++++++++++++ nalgebra-sparse/src/pattern.rs | 15 +++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 4ad382dc..91ba207d 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -279,6 +279,21 @@ mod serde_serialize { use super::CooMatrix; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + /// This is an intermediate type for (de)serializing `CooMatrix`. + /// + /// Deserialization requires using a `try_from_*` function for validation. We could have used + /// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows + /// to directly serialize/deserialize the original fields and combine it with validation. + /// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` + /// types. Instead, we decided that we want a more human-readable serialization format using + /// field names like `row_indices` and `col_indices`. The easiest way to achieve this is to + /// introduce an intermediate type. It also allows the serialization format to stay constant + /// even if the internal layout in `nalgebra` changes. + /// + /// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned + /// storage). Therefore, we use generic arguments to allow using slices during serialization and + /// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices + /// and `Vec`s should always (de)serialize identically. #[derive(Serialize, Deserialize)] struct CooMatrixSerializationData { nrows: usize, diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index c3c843c7..7c9bb74a 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -525,6 +525,21 @@ mod serde_serialize { use super::CscMatrix; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + /// This is an intermediate type for (de)serializing `CscMatrix`. + /// + /// Deserialization requires using a `try_from_*` function for validation. We could have used + /// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows + /// to directly serialize/deserialize the original fields and combine it with validation. + /// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` + /// types. Instead, we decided that we want a more human-readable serialization format using + /// field names like `col_offsets` and `row_indices`. The easiest way to achieve this is to + /// introduce an intermediate type. It also allows the serialization format to stay constant + /// even if the internal layout in `nalgebra` changes. + /// + /// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned + /// storage). Therefore, we use generic arguments to allow using slices during serialization and + /// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices + /// and `Vec`s should always (de)serialize identically. #[derive(Serialize, Deserialize)] struct CscMatrixSerializationData { nrows: usize, diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 88c335b2..eb35b335 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -596,6 +596,21 @@ mod serde_serialize { use super::CsrMatrix; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + /// This is an intermediate type for (de)serializing `CsrMatrix`. + /// + /// Deserialization requires using a `try_from_*` function for validation. We could have used + /// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows + /// to directly serialize/deserialize the original fields and combine it with validation. + /// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` + /// types. Instead, we decided that we want a more human-readable serialization format using + /// field names like `row_offsets` and `cal_indices`. The easiest way to achieve this is to + /// introduce an intermediate type. It also allows the serialization format to stay constant + /// even if the internal layout in `nalgebra` changes. + /// + /// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned + /// storage). Therefore, we use generic arguments to allow using slices during serialization and + /// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices + /// and `Vec`s should always (de)serialize identically. #[derive(Serialize, Deserialize)] struct CsrMatrixSerializationData { nrows: usize, diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 82a93ffd..2bc9e939 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -294,6 +294,21 @@ mod serde_serialize { use super::SparsityPattern; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + /// This is an intermediate type for (de)serializing `SparsityPattern`. + /// + /// Deserialization requires using a `try_from_*` function for validation. We could have used + /// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows + /// to directly serialize/deserialize the original fields and combine it with validation. + /// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` + /// types. Instead, we decided that we want a more human-readable serialization format using + /// field names like `major_offsets` and `minor_indices`. The easiest way to achieve this is to + /// introduce an intermediate type. It also allows the serialization format to stay constant + /// even when the internal layout in `nalgebra` changes. + /// + /// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned + /// storage). Therefore, we use generic arguments to allow using slices during serialization and + /// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices + /// and `Vec`s should always (de)serialize identically. #[derive(Serialize, Deserialize)] struct SparsityPatternSerializationData { major_dim: usize, From 38989ed5f0f2b2cd9ab24413d251fcd5bca652bd Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 4 Jan 2022 15:15:52 +0100 Subject: [PATCH 21/81] Move sparse matrix serialization to separate files --- nalgebra-sparse/src/coo.rs | 72 +------------------ nalgebra-sparse/src/coo/coo_serde.rs | 65 +++++++++++++++++ nalgebra-sparse/src/csc.rs | 72 +------------------ nalgebra-sparse/src/csc/csc_serde.rs | 65 +++++++++++++++++ nalgebra-sparse/src/csr.rs | 73 ++------------------ nalgebra-sparse/src/csr/csr_serde.rs | 65 +++++++++++++++++ nalgebra-sparse/src/pattern.rs | 64 ++--------------- nalgebra-sparse/src/pattern/pattern_serde.rs | 56 +++++++++++++++ 8 files changed, 265 insertions(+), 267 deletions(-) create mode 100644 nalgebra-sparse/src/coo/coo_serde.rs create mode 100644 nalgebra-sparse/src/csc/csc_serde.rs create mode 100644 nalgebra-sparse/src/csr/csr_serde.rs create mode 100644 nalgebra-sparse/src/pattern/pattern_serde.rs diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 91ba207d..2b302e37 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -1,5 +1,8 @@ //! An implementation of the COO sparse matrix format. +#[cfg(feature = "serde-serialize")] +mod coo_serde; + use crate::SparseFormatError; /// A COO representation of a sparse matrix. @@ -273,72 +276,3 @@ impl CooMatrix { (self.row_indices, self.col_indices, self.values) } } - -#[cfg(feature = "serde-serialize")] -mod serde_serialize { - use super::CooMatrix; - use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - - /// This is an intermediate type for (de)serializing `CooMatrix`. - /// - /// Deserialization requires using a `try_from_*` function for validation. We could have used - /// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows - /// to directly serialize/deserialize the original fields and combine it with validation. - /// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` - /// types. Instead, we decided that we want a more human-readable serialization format using - /// field names like `row_indices` and `col_indices`. The easiest way to achieve this is to - /// introduce an intermediate type. It also allows the serialization format to stay constant - /// even if the internal layout in `nalgebra` changes. - /// - /// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned - /// storage). Therefore, we use generic arguments to allow using slices during serialization and - /// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices - /// and `Vec`s should always (de)serialize identically. - #[derive(Serialize, Deserialize)] - struct CooMatrixSerializationData { - nrows: usize, - ncols: usize, - row_indices: Indices, - col_indices: Indices, - values: Values, - } - - impl Serialize for CooMatrix - where - T: Serialize + Clone, - { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - CooMatrixSerializationData::<&[usize], &[T]> { - nrows: self.nrows(), - ncols: self.ncols(), - row_indices: self.row_indices(), - col_indices: self.col_indices(), - values: self.values(), - } - .serialize(serializer) - } - } - - impl<'de, T> Deserialize<'de> for CooMatrix - where - T: Deserialize<'de> + Clone, - { - fn deserialize(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - let de = CooMatrixSerializationData::, Vec>::deserialize(deserializer)?; - CooMatrix::try_from_triplets( - de.nrows, - de.ncols, - de.row_indices, - de.col_indices, - de.values, - ) - .map_err(|e| de::Error::custom(e)) - } - } -} diff --git a/nalgebra-sparse/src/coo/coo_serde.rs b/nalgebra-sparse/src/coo/coo_serde.rs new file mode 100644 index 00000000..7ffcdf4a --- /dev/null +++ b/nalgebra-sparse/src/coo/coo_serde.rs @@ -0,0 +1,65 @@ +use crate::coo::CooMatrix; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +/// This is an intermediate type for (de)serializing `CooMatrix`. +/// +/// Deserialization requires using a `try_from_*` function for validation. We could have used +/// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows +/// to directly serialize/deserialize the original fields and combine it with validation. +/// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` +/// types. Instead, we decided that we want a more human-readable serialization format using +/// field names like `row_indices` and `col_indices`. The easiest way to achieve this is to +/// introduce an intermediate type. It also allows the serialization format to stay constant +/// even if the internal layout in `nalgebra` changes. +/// +/// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned +/// storage). Therefore, we use generic arguments to allow using slices during serialization and +/// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices +/// and `Vec`s should always (de)serialize identically. +#[derive(Serialize, Deserialize)] +struct CooMatrixSerializationData { + nrows: usize, + ncols: usize, + row_indices: Indices, + col_indices: Indices, + values: Values, +} + +impl Serialize for CooMatrix +where + T: Serialize + Clone, +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CooMatrixSerializationData::<&[usize], &[T]> { + nrows: self.nrows(), + ncols: self.ncols(), + row_indices: self.row_indices(), + col_indices: self.col_indices(), + values: self.values(), + } + .serialize(serializer) + } +} + +impl<'de, T> Deserialize<'de> for CooMatrix +where + T: Deserialize<'de> + Clone, +{ + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CooMatrixSerializationData::, Vec>::deserialize(deserializer)?; + CooMatrix::try_from_triplets( + de.nrows, + de.ncols, + de.row_indices, + de.col_indices, + de.values, + ) + .map_err(|e| de::Error::custom(e)) + } +} diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 7c9bb74a..85e4013c 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -3,6 +3,9 @@ //! This is the module-level documentation. See [`CscMatrix`] for the main documentation of the //! CSC implementation. +#[cfg(feature = "serde-serialize")] +mod csc_serde; + use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; use crate::csr::CsrMatrix; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; @@ -520,75 +523,6 @@ impl CscMatrix { } } -#[cfg(feature = "serde-serialize")] -mod serde_serialize { - use super::CscMatrix; - use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - - /// This is an intermediate type for (de)serializing `CscMatrix`. - /// - /// Deserialization requires using a `try_from_*` function for validation. We could have used - /// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows - /// to directly serialize/deserialize the original fields and combine it with validation. - /// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` - /// types. Instead, we decided that we want a more human-readable serialization format using - /// field names like `col_offsets` and `row_indices`. The easiest way to achieve this is to - /// introduce an intermediate type. It also allows the serialization format to stay constant - /// even if the internal layout in `nalgebra` changes. - /// - /// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned - /// storage). Therefore, we use generic arguments to allow using slices during serialization and - /// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices - /// and `Vec`s should always (de)serialize identically. - #[derive(Serialize, Deserialize)] - struct CscMatrixSerializationData { - nrows: usize, - ncols: usize, - col_offsets: Indices, - row_indices: Indices, - values: Values, - } - - impl Serialize for CscMatrix - where - T: Serialize + Clone, - { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - CscMatrixSerializationData::<&[usize], &[T]> { - nrows: self.nrows(), - ncols: self.ncols(), - col_offsets: self.col_offsets(), - row_indices: self.row_indices(), - values: self.values(), - } - .serialize(serializer) - } - } - - impl<'de, T> Deserialize<'de> for CscMatrix - where - T: Deserialize<'de> + Clone, - { - fn deserialize(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - let de = CscMatrixSerializationData::, Vec>::deserialize(deserializer)?; - CscMatrix::try_from_csc_data( - de.nrows, - de.ncols, - de.col_offsets, - de.row_indices, - de.values, - ) - .map_err(|e| de::Error::custom(e)) - } - } -} - /// Convert pattern format errors into more meaningful CSC-specific errors. /// /// This ensures that the terminology is consistent: we are talking about rows and columns, diff --git a/nalgebra-sparse/src/csc/csc_serde.rs b/nalgebra-sparse/src/csc/csc_serde.rs new file mode 100644 index 00000000..aab12d47 --- /dev/null +++ b/nalgebra-sparse/src/csc/csc_serde.rs @@ -0,0 +1,65 @@ +use crate::CscMatrix; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +/// This is an intermediate type for (de)serializing `CscMatrix`. +/// +/// Deserialization requires using a `try_from_*` function for validation. We could have used +/// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows +/// to directly serialize/deserialize the original fields and combine it with validation. +/// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` +/// types. Instead, we decided that we want a more human-readable serialization format using +/// field names like `col_offsets` and `row_indices`. The easiest way to achieve this is to +/// introduce an intermediate type. It also allows the serialization format to stay constant +/// even if the internal layout in `nalgebra` changes. +/// +/// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned +/// storage). Therefore, we use generic arguments to allow using slices during serialization and +/// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices +/// and `Vec`s should always (de)serialize identically. +#[derive(Serialize, Deserialize)] +struct CscMatrixSerializationData { + nrows: usize, + ncols: usize, + col_offsets: Indices, + row_indices: Indices, + values: Values, +} + +impl Serialize for CscMatrix +where + T: Serialize + Clone, +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CscMatrixSerializationData::<&[usize], &[T]> { + nrows: self.nrows(), + ncols: self.ncols(), + col_offsets: self.col_offsets(), + row_indices: self.row_indices(), + values: self.values(), + } + .serialize(serializer) + } +} + +impl<'de, T> Deserialize<'de> for CscMatrix +where + T: Deserialize<'de> + Clone, +{ + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CscMatrixSerializationData::, Vec>::deserialize(deserializer)?; + CscMatrix::try_from_csc_data( + de.nrows, + de.ncols, + de.col_offsets, + de.row_indices, + de.values, + ) + .map_err(|e| de::Error::custom(e)) + } +} diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index eb35b335..a87f923d 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -2,6 +2,10 @@ //! //! This is the module-level documentation. See [`CsrMatrix`] for the main documentation of the //! CSC implementation. + +#[cfg(feature = "serde-serialize")] +mod csr_serde; + use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; use crate::csc::CscMatrix; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; @@ -591,75 +595,6 @@ impl CsrMatrix { } } -#[cfg(feature = "serde-serialize")] -mod serde_serialize { - use super::CsrMatrix; - use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - - /// This is an intermediate type for (de)serializing `CsrMatrix`. - /// - /// Deserialization requires using a `try_from_*` function for validation. We could have used - /// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows - /// to directly serialize/deserialize the original fields and combine it with validation. - /// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` - /// types. Instead, we decided that we want a more human-readable serialization format using - /// field names like `row_offsets` and `cal_indices`. The easiest way to achieve this is to - /// introduce an intermediate type. It also allows the serialization format to stay constant - /// even if the internal layout in `nalgebra` changes. - /// - /// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned - /// storage). Therefore, we use generic arguments to allow using slices during serialization and - /// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices - /// and `Vec`s should always (de)serialize identically. - #[derive(Serialize, Deserialize)] - struct CsrMatrixSerializationData { - nrows: usize, - ncols: usize, - row_offsets: Indices, - col_indices: Indices, - values: Values, - } - - impl Serialize for CsrMatrix - where - T: Serialize + Clone, - { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - CsrMatrixSerializationData::<&[usize], &[T]> { - nrows: self.nrows(), - ncols: self.ncols(), - row_offsets: self.row_offsets(), - col_indices: self.col_indices(), - values: self.values(), - } - .serialize(serializer) - } - } - - impl<'de, T> Deserialize<'de> for CsrMatrix - where - T: Deserialize<'de> + Clone, - { - fn deserialize(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - let de = CsrMatrixSerializationData::, Vec>::deserialize(deserializer)?; - CsrMatrix::try_from_csr_data( - de.nrows, - de.ncols, - de.row_offsets, - de.col_indices, - de.values, - ) - .map_err(|e| de::Error::custom(e)) - } - } -} - /// Convert pattern format errors into more meaningful CSR-specific errors. /// /// This ensures that the terminology is consistent: we are talking about rows and columns, diff --git a/nalgebra-sparse/src/csr/csr_serde.rs b/nalgebra-sparse/src/csr/csr_serde.rs new file mode 100644 index 00000000..1b33fda0 --- /dev/null +++ b/nalgebra-sparse/src/csr/csr_serde.rs @@ -0,0 +1,65 @@ +use crate::CsrMatrix; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +/// This is an intermediate type for (de)serializing `CsrMatrix`. +/// +/// Deserialization requires using a `try_from_*` function for validation. We could have used +/// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows +/// to directly serialize/deserialize the original fields and combine it with validation. +/// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` +/// types. Instead, we decided that we want a more human-readable serialization format using +/// field names like `row_offsets` and `cal_indices`. The easiest way to achieve this is to +/// introduce an intermediate type. It also allows the serialization format to stay constant +/// even if the internal layout in `nalgebra` changes. +/// +/// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned +/// storage). Therefore, we use generic arguments to allow using slices during serialization and +/// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices +/// and `Vec`s should always (de)serialize identically. +#[derive(Serialize, Deserialize)] +struct CsrMatrixSerializationData { + nrows: usize, + ncols: usize, + row_offsets: Indices, + col_indices: Indices, + values: Values, +} + +impl Serialize for CsrMatrix +where + T: Serialize + Clone, +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CsrMatrixSerializationData::<&[usize], &[T]> { + nrows: self.nrows(), + ncols: self.ncols(), + row_offsets: self.row_offsets(), + col_indices: self.col_indices(), + values: self.values(), + } + .serialize(serializer) + } +} + +impl<'de, T> Deserialize<'de> for CsrMatrix +where + T: Deserialize<'de> + Clone, +{ + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CsrMatrixSerializationData::, Vec>::deserialize(deserializer)?; + CsrMatrix::try_from_csr_data( + de.nrows, + de.ncols, + de.row_offsets, + de.col_indices, + de.values, + ) + .map_err(|e| de::Error::custom(e)) + } +} diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 2bc9e939..59fe8949 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -1,4 +1,8 @@ //! Sparsity patterns for CSR and CSC matrices. + +#[cfg(feature = "serde-serialize")] +mod pattern_serde; + use crate::cs::transpose_cs; use crate::SparseFormatError; use std::error::Error; @@ -289,66 +293,6 @@ pub enum SparsityPatternFormatError { NonmonotonicMinorIndices, } -#[cfg(feature = "serde-serialize")] -mod serde_serialize { - use super::SparsityPattern; - use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - - /// This is an intermediate type for (de)serializing `SparsityPattern`. - /// - /// Deserialization requires using a `try_from_*` function for validation. We could have used - /// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows - /// to directly serialize/deserialize the original fields and combine it with validation. - /// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` - /// types. Instead, we decided that we want a more human-readable serialization format using - /// field names like `major_offsets` and `minor_indices`. The easiest way to achieve this is to - /// introduce an intermediate type. It also allows the serialization format to stay constant - /// even when the internal layout in `nalgebra` changes. - /// - /// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned - /// storage). Therefore, we use generic arguments to allow using slices during serialization and - /// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices - /// and `Vec`s should always (de)serialize identically. - #[derive(Serialize, Deserialize)] - struct SparsityPatternSerializationData { - major_dim: usize, - minor_dim: usize, - major_offsets: Indices, - minor_indices: Indices, - } - - impl Serialize for SparsityPattern { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - SparsityPatternSerializationData::<&[usize]> { - major_dim: self.major_dim(), - minor_dim: self.minor_dim(), - major_offsets: self.major_offsets(), - minor_indices: self.minor_indices(), - } - .serialize(serializer) - } - } - - impl<'de> Deserialize<'de> for SparsityPattern { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let de = SparsityPatternSerializationData::>::deserialize(deserializer)?; - SparsityPattern::try_from_offsets_and_indices( - de.major_dim, - de.minor_dim, - de.major_offsets, - de.minor_indices, - ) - .map_err(|e| de::Error::custom(e)) - } - } -} - impl From for SparseFormatError { fn from(err: SparsityPatternFormatError) -> Self { use crate::SparseFormatErrorKind; diff --git a/nalgebra-sparse/src/pattern/pattern_serde.rs b/nalgebra-sparse/src/pattern/pattern_serde.rs new file mode 100644 index 00000000..e11a550a --- /dev/null +++ b/nalgebra-sparse/src/pattern/pattern_serde.rs @@ -0,0 +1,56 @@ +use crate::pattern::SparsityPattern; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +/// This is an intermediate type for (de)serializing `SparsityPattern`. +/// +/// Deserialization requires using a `try_from_*` function for validation. We could have used +/// the `remote = "Self"` trick (https://github.com/serde-rs/serde/issues/1220) which allows +/// to directly serialize/deserialize the original fields and combine it with validation. +/// However, this would lead to nested serialization of the `CsMatrix` and `SparsityPattern` +/// types. Instead, we decided that we want a more human-readable serialization format using +/// field names like `major_offsets` and `minor_indices`. The easiest way to achieve this is to +/// introduce an intermediate type. It also allows the serialization format to stay constant +/// even when the internal layout in `nalgebra` changes. +/// +/// We want to avoid unnecessary copies when serializing (i.e. cloning slices into owned +/// storage). Therefore, we use generic arguments to allow using slices during serialization and +/// owned storage (i.e. `Vec`) during deserialization. Without a major update of serde, slices +/// and `Vec`s should always (de)serialize identically. +#[derive(Serialize, Deserialize)] +struct SparsityPatternSerializationData { + major_dim: usize, + minor_dim: usize, + major_offsets: Indices, + minor_indices: Indices, +} + +impl Serialize for SparsityPattern { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + SparsityPatternSerializationData::<&[usize]> { + major_dim: self.major_dim(), + minor_dim: self.minor_dim(), + major_offsets: self.major_offsets(), + minor_indices: self.minor_indices(), + } + .serialize(serializer) + } +} + +impl<'de> Deserialize<'de> for SparsityPattern { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let de = SparsityPatternSerializationData::>::deserialize(deserializer)?; + SparsityPattern::try_from_offsets_and_indices( + de.major_dim, + de.minor_dim, + de.major_offsets, + de.minor_indices, + ) + .map_err(|e| de::Error::custom(e)) + } +} From 89f1e855bb5a22b401695c1cbe8c350bfd5774dd Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 14:07:57 +0100 Subject: [PATCH 22/81] Revert "Fix panic in SparsityPattern::try_from_* if major index is out of bounds" This reverts commit 12afe2917af4c30fc4a17316e453d0830072642c to avoid conflict with #1015. --- nalgebra-sparse/src/csc.rs | 3 --- nalgebra-sparse/src/csr.rs | 3 --- nalgebra-sparse/src/pattern.rs | 11 ++--------- nalgebra-sparse/tests/serde.rs | 7 ++++--- nalgebra-sparse/tests/unit_tests/pattern.rs | 11 ----------- 5 files changed, 6 insertions(+), 29 deletions(-) diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 85e4013c..9180ae12 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -550,9 +550,6 @@ fn pattern_format_error_to_csc_error(err: SparsityPatternFormatError) -> SparseF K::InvalidStructure, "Row indices are not monotonically increasing (sorted) within each column.", ), - MajorIndexOutOfBounds => { - E::from_kind_and_msg(K::IndexOutOfBounds, "Column indices are out of bounds.") - } MinorIndexOutOfBounds => { E::from_kind_and_msg(K::IndexOutOfBounds, "Row indices are out of bounds.") } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index a87f923d..1f52a867 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -622,9 +622,6 @@ fn pattern_format_error_to_csr_error(err: SparsityPatternFormatError) -> SparseF K::InvalidStructure, "Column indices are not monotonically increasing (sorted) within each row.", ), - MajorIndexOutOfBounds => { - E::from_kind_and_msg(K::IndexOutOfBounds, "Row indices are out of bounds.") - } MinorIndexOutOfBounds => { E::from_kind_and_msg(K::IndexOutOfBounds, "Column indices are out of bounds.") } diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 59fe8949..50fae072 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -157,9 +157,7 @@ impl SparsityPattern { return Err(NonmonotonicOffsets); } - let minor_indices = minor_indices - .get(range_start..range_end) - .ok_or(MajorIndexOutOfBounds)?; + let minor_indices = &minor_indices[range_start..range_end]; // We test for in-bounds, uniqueness and monotonicity at the same time // to ensure that we only visit each minor index once @@ -280,8 +278,6 @@ pub enum SparsityPatternFormatError { InvalidOffsetFirstLast, /// Indicates that the major offsets are not monotonically increasing. NonmonotonicOffsets, - /// One or more major indices are out of bounds. - MajorIndexOutOfBounds, /// One or more minor indices are out of bounds. MinorIndexOutOfBounds, /// One or more duplicate entries were detected. @@ -306,7 +302,7 @@ impl From for SparseFormatError { | NonmonotonicMinorIndices => { SparseFormatError::from_kind_and_error(InvalidStructure, Box::from(err)) } - MajorIndexOutOfBounds | MinorIndexOutOfBounds => { + MinorIndexOutOfBounds => { SparseFormatError::from_kind_and_error(IndexOutOfBounds, Box::from(err)) } PatternDuplicateEntry => SparseFormatError::from_kind_and_error( @@ -330,9 +326,6 @@ impl fmt::Display for SparsityPatternFormatError { SparsityPatternFormatError::NonmonotonicOffsets => { write!(f, "Offsets are not monotonically increasing.") } - SparsityPatternFormatError::MajorIndexOutOfBounds => { - write!(f, "A major index is out of bounds.") - } SparsityPatternFormatError::MinorIndexOutOfBounds => { write!(f, "A minor index is out of bounds.") } diff --git a/nalgebra-sparse/tests/serde.rs b/nalgebra-sparse/tests/serde.rs index 7842fd1e..1ce1953f 100644 --- a/nalgebra-sparse/tests/serde.rs +++ b/nalgebra-sparse/tests/serde.rs @@ -58,7 +58,6 @@ fn pattern_deserialize_invalid() { assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2, 5],"minor_indices":[0, 2, 3, 1, 4]}"#).is_err()); assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2, 5],"minor_indices":[0, 6, 1, 2, 3]}"#).is_err()); assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 2, 2, 5],"minor_indices":[0, 5, 2, 2, 3]}"#).is_err()); - assert!(serde_json::from_str::(r#"{"major_dim":3,"minor_dim":6,"major_offsets":[0, 10, 2, 5],"minor_indices":[0, 5, 1, 2, 3]}"#).is_err()); } #[test] @@ -149,7 +148,8 @@ fn csc_deserialize_invalid() { assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4,5]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,8,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3,1,1],"values":[0,1,2,3,4]}"#).is_err()); - assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,10,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); + // The following actually panics ('range end index 10 out of range for slice of length 5', nalgebra-sparse\src\pattern.rs:156:38) + //assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,10,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); } @@ -188,7 +188,8 @@ fn csr_deserialize_invalid() { assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4,5]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,8,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,2,2,5],"col_indices":[0,5,1,2,3,1,1],"values":[0,1,2,3,4]}"#).is_err()); - assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,10,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); + // The following actually panics ('range end index 10 out of range for slice of length 5', nalgebra-sparse\src\pattern.rs:156:38) + //assert!(serde_json::from_str::>(r#"{"nrows":3,"ncols":6,"row_offsets":[0,10,2,5],"col_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); assert!(serde_json::from_str::>(r#"{"nrows":6,"ncols":3,"col_offsets":[0,2,2,5],"row_indices":[0,5,1,2,3],"values":[0,1,2,3,4]}"#).is_err()); } diff --git a/nalgebra-sparse/tests/unit_tests/pattern.rs b/nalgebra-sparse/tests/unit_tests/pattern.rs index e2706ee5..310cffae 100644 --- a/nalgebra-sparse/tests/unit_tests/pattern.rs +++ b/nalgebra-sparse/tests/unit_tests/pattern.rs @@ -133,17 +133,6 @@ fn sparsity_pattern_try_from_invalid_data() { ); } - { - // Major index out of bounds - let offsets = vec![0, 10, 2, 5]; - let indices = vec![0, 1, 2, 3, 4]; - let pattern = SparsityPattern::try_from_offsets_and_indices(3, 6, offsets, indices); - assert_eq!( - pattern, - Err(SparsityPatternFormatError::MajorIndexOutOfBounds) - ); - } - { // Minor index out of bounds let offsets = vec![0, 2, 2, 5]; From 99eb8c1589e5d6fcbb48a9746e177511eb55c9d4 Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 10:31:50 +0100 Subject: [PATCH 23/81] Revert "Rename nrows/ncols args for try_from_*_data functions for consistency" This reverts commit 2a3e657b565dadcd2af422750cd0fb5459be0f6f. --- nalgebra-sparse/src/csc.rs | 14 +++++++++----- nalgebra-sparse/src/csr.rs | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 9180ae12..a461658e 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -157,15 +157,19 @@ impl CscMatrix { /// An error is returned if the data given does not conform to the CSC storage format. /// See the documentation for [CscMatrix](struct.CscMatrix.html) for more information. pub fn try_from_csc_data( - nrows: usize, - ncols: usize, + num_rows: usize, + num_cols: usize, col_offsets: Vec, row_indices: Vec, values: Vec, ) -> Result { - let pattern = - SparsityPattern::try_from_offsets_and_indices(ncols, nrows, col_offsets, row_indices) - .map_err(pattern_format_error_to_csc_error)?; + let pattern = SparsityPattern::try_from_offsets_and_indices( + num_cols, + num_rows, + col_offsets, + row_indices, + ) + .map_err(pattern_format_error_to_csc_error)?; Self::try_from_pattern_and_values(pattern, values) } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 1f52a867..bd35bf6b 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -158,15 +158,19 @@ impl CsrMatrix { /// An error is returned if the data given does not conform to the CSR storage format. /// See the documentation for [CsrMatrix](struct.CsrMatrix.html) for more information. pub fn try_from_csr_data( - nrows: usize, - ncols: usize, + num_rows: usize, + num_cols: usize, row_offsets: Vec, col_indices: Vec, values: Vec, ) -> Result { - let pattern = - SparsityPattern::try_from_offsets_and_indices(nrows, ncols, row_offsets, col_indices) - .map_err(pattern_format_error_to_csr_error)?; + let pattern = SparsityPattern::try_from_offsets_and_indices( + num_rows, + num_cols, + row_offsets, + col_indices, + ) + .map_err(pattern_format_error_to_csr_error)?; Self::try_from_pattern_and_values(pattern, values) } From 7230ae1e638893d2aa87b30f96dfd3cfa1e73f46 Mon Sep 17 00:00:00 2001 From: metric-space Date: Tue, 18 Jan 2022 22:35:11 -0500 Subject: [PATCH 24/81] First attempt at xgges (qz decomposition), passing tests. Serialization failing across many modules --- nalgebra-lapack/src/lib.rs | 2 + nalgebra-lapack/src/qz.rs | 288 ++++++++++++++++++++++++++++ nalgebra-lapack/tests/linalg/mod.rs | 1 + nalgebra-lapack/tests/linalg/qz.rs | 27 +++ 4 files changed, 318 insertions(+) create mode 100644 nalgebra-lapack/src/qz.rs create mode 100644 nalgebra-lapack/tests/linalg/qz.rs diff --git a/nalgebra-lapack/src/lib.rs b/nalgebra-lapack/src/lib.rs index 9cf0d73d..e89ab160 100644 --- a/nalgebra-lapack/src/lib.rs +++ b/nalgebra-lapack/src/lib.rs @@ -86,6 +86,7 @@ mod eigen; mod hessenberg; mod lu; mod qr; +mod qz; mod schur; mod svd; mod symmetric_eigen; @@ -97,6 +98,7 @@ pub use self::eigen::Eigen; pub use self::hessenberg::Hessenberg; pub use self::lu::{LUScalar, LU}; pub use self::qr::QR; +pub use self::qz::QZ; pub use self::schur::Schur; pub use self::svd::SVD; pub use self::symmetric_eigen::SymmetricEigen; diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs new file mode 100644 index 00000000..47efc08c --- /dev/null +++ b/nalgebra-lapack/src/qz.rs @@ -0,0 +1,288 @@ +#[cfg(feature = "serde-serialize")] +use serde::{Deserialize, Serialize}; + +use num::Zero; +use num_complex::Complex; + +use simba::scalar::RealField; + +use crate::ComplexHelper; +use na::allocator::Allocator; +use na::dimension::{Const, Dim}; +use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar}; + +use lapack; + +/// Eigendecomposition of a real square matrix with complex eigenvalues. +#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] +#[cfg_attr( + feature = "serde-serialize", + serde( + bound(serialize = "DefaultAllocator: Allocator + Allocator, + OVector: Serialize, + OMatrix: Serialize") + ) +)] +#[cfg_attr( + feature = "serde-serialize", + serde( + bound(deserialize = "DefaultAllocator: Allocator + Allocator, + OVector: Serialize, + OMatrix: Deserialize<'de>") + ) +)] +#[derive(Clone, Debug)] +pub struct QZ +where + DefaultAllocator: Allocator + Allocator, +{ + alphar: OVector, + alphai: OVector, + beta: OVector, + vsl: OMatrix, + s: OMatrix, + vsr: OMatrix, + t: OMatrix +} + +impl Copy for QZ +where + DefaultAllocator: Allocator + Allocator, + OMatrix: Copy, + OVector: Copy, +{ +} + +impl QZ +where + DefaultAllocator: Allocator + Allocator, +{ + /// Computes the eigenvalues and real Schur form of the matrix `m`. + /// + /// Panics if the method did not converge. + pub fn new(a: OMatrix, b: OMatrix) -> Self { + Self::try_new(a,b).expect("Schur decomposition: convergence failed.") + } + + /// Computes the eigenvalues and real Schur form of the matrix `m`. + /// + /// Returns `None` if the method did not converge. + pub fn try_new(mut a: OMatrix, mut b: OMatrix) -> Option { + assert!( + a.is_square() && b.is_square(), + "Unable to compute the qz decomposition of non-square matrices." + ); + + // another assert to compare shape? + + let (nrows, ncols) = a.shape_generic(); + let n = nrows.value(); + + let lda = n as i32; + let ldb = lda.clone(); + + let mut info = 0; + + let mut alphar = Matrix::zeros_generic(nrows, Const::<1>); + let mut alphai = Matrix::zeros_generic(nrows, Const::<1>); + let mut beta = Matrix::zeros_generic(nrows, Const::<1>); + let mut vsl = Matrix::zeros_generic(nrows, ncols); + let mut vsr = Matrix::zeros_generic(nrows, ncols); + // Placeholders: + let mut bwork = [0i32]; + let mut unused = 0; + + let lwork = T::xgges_work_size( + b'V', + b'V', + b'N', + n as i32, + a.as_mut_slice(), + n as i32, + b.as_mut_slice(), + n as i32, + &mut unused, + alphar.as_mut_slice(), + alphai.as_mut_slice(), + beta.as_mut_slice(), + vsl.as_mut_slice(), + n as i32, + vsr.as_mut_slice(), + n as i32, + &mut bwork, + &mut info, + ); + lapack_check!(info); + + let mut work = vec![T::zero(); lwork as usize]; + + T::xgges( + b'V', + b'V', + b'N', + n as i32, + a.as_mut_slice(), + n as i32, + b.as_mut_slice(), + n as i32, + &mut unused, + alphar.as_mut_slice(), + alphai.as_mut_slice(), + beta.as_mut_slice(), + vsl.as_mut_slice(), + n as i32, + vsr.as_mut_slice(), + n as i32, + &mut work, + lwork, + &mut bwork, + &mut info, + ); + lapack_check!(info); + + Some(QZ {alphar, alphai, beta, + vsl, s:a, + vsr, t:b}) + } + + /// Retrieves the unitary matrix `Q` and the upper-quasitriangular matrix `T` such that the + /// decomposed matrix equals `Q * T * Q.transpose()`. + pub fn unpack(self) -> (OMatrix, OMatrix, OMatrix, OMatrix){ + (self.vsl, self.s, self.t, self.vsr) + } + + /// computes the generalized eigenvalues + #[must_use] + pub fn eigenvalues(&self) -> OVector, D> + where + DefaultAllocator: Allocator, D>, + { + let mut out = Matrix::zeros_generic(self.t.shape_generic().0, Const::<1>); + + for i in 0..out.len() { + out[i] = Complex::new(self.alphar[i].clone()/self.beta[i].clone(), + self.alphai[i].clone()/self.beta[i].clone()) + } + + out + } +} + +/* + * + * Lapack functions dispatch. + * + */ +/// Trait implemented by scalars for which Lapack implements the RealField QZ decomposition. +pub trait QZScalar: Scalar { + #[allow(missing_docs)] + fn xgges( + jobvsl: u8, + jobvsr: u8, + sort: u8, + // select: ??? + n: i32, + a: &mut [Self], + lda: i32, + b: &mut [Self], + ldb: i32, + sdim: &mut i32, + alphar: &mut [Self], + alphai: &mut [Self], + beta : &mut [Self], + vsl: &mut [Self], + ldvsl: i32, + vsr: &mut [Self], + ldvsr: i32, + work: &mut [Self], + lwork: i32, + bwork: &mut [i32], + info: &mut i32 + ); + + #[allow(missing_docs)] + fn xgges_work_size( + jobvsl: u8, + jobvsr: u8, + sort: u8, + // select: ??? + n: i32, + a: &mut [Self], + lda: i32, + b: &mut [Self], + ldb: i32, + sdim: &mut i32, + alphar: &mut [Self], + alphai: &mut [Self], + beta : &mut [Self], + vsl: &mut [Self], + ldvsl: i32, + vsr: &mut [Self], + ldvsr: i32, + bwork: &mut [i32], + info: &mut i32 + ) -> i32; +} + +macro_rules! real_eigensystem_scalar_impl ( + ($N: ty, $xgges: path) => ( + impl QZScalar for $N { + #[inline] + fn xgges(jobvsl: u8, + jobvsr: u8, + sort: u8, + // select: ??? + n: i32, + a: &mut [$N], + lda: i32, + b: &mut [$N], + ldb: i32, + sdim: &mut i32, + alphar: &mut [$N], + alphai: &mut [$N], + beta : &mut [$N], + vsl: &mut [$N], + ldvsl: i32, + vsr: &mut [$N], + ldvsr: i32, + work: &mut [$N], + lwork: i32, + bwork: &mut [i32], + info: &mut i32) { + unsafe { $xgges(jobvsl, jobvsr, sort, None, n, a, lda, b, ldb, sdim, alphar, alphai, beta, vsl, ldvsl, vsr, ldvsr, work, lwork, bwork, info); } + } + + + #[inline] + fn xgges_work_size(jobvsl: u8, + jobvsr: u8, + sort: u8, + // select: ??? + n: i32, + a: &mut [$N], + lda: i32, + b: &mut [$N], + ldb: i32, + sdim: &mut i32, + alphar: &mut [$N], + alphai: &mut [$N], + beta : &mut [$N], + vsl: &mut [$N], + ldvsl: i32, + vsr: &mut [$N], + ldvsr: i32, + bwork: &mut [i32], + info: &mut i32) + -> i32 { + let mut work = [ Zero::zero() ]; + let lwork = -1 as i32; + + unsafe { $xgges(jobvsl, jobvsr, sort, None, n, a, lda, b, ldb, sdim, alphar, alphai, beta, vsl, ldvsl, vsr, ldvsr, &mut work, lwork, bwork, info); } + ComplexHelper::real_part(work[0]) as i32 + } + } + ) +); + +real_eigensystem_scalar_impl!(f32, lapack::sgges); +real_eigensystem_scalar_impl!(f64, lapack::dgges); diff --git a/nalgebra-lapack/tests/linalg/mod.rs b/nalgebra-lapack/tests/linalg/mod.rs index a6742217..a4043469 100644 --- a/nalgebra-lapack/tests/linalg/mod.rs +++ b/nalgebra-lapack/tests/linalg/mod.rs @@ -1,6 +1,7 @@ mod cholesky; mod lu; mod qr; +mod qz; mod real_eigensystem; mod schur; mod svd; diff --git a/nalgebra-lapack/tests/linalg/qz.rs b/nalgebra-lapack/tests/linalg/qz.rs new file mode 100644 index 00000000..6b4efbda --- /dev/null +++ b/nalgebra-lapack/tests/linalg/qz.rs @@ -0,0 +1,27 @@ +use na::DMatrix; +use nl::QZ; +use std::cmp; + +use crate::proptest::*; +use proptest::{prop_assert, proptest}; + +proptest! { + #[test] + fn qz(n in PROPTEST_MATRIX_DIM) { + let n = cmp::max(1, cmp::min(n, 10)); + let a = DMatrix::::new_random(n, n); + let b = DMatrix::::new_random(n, n); + + let (vsl,s,t,vsr) = QZ::new(a.clone(), b.clone()).unpack(); + + prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, epsilon = 1.0e-7)); + prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b, epsilon = 1.0e-7)) + } + + #[test] + fn qz_static(a in matrix4(), b in matrix4()) { + let (vsl,s,t,vsr) = QZ::new(a.clone(), b.clone()).unpack(); + prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, epsilon = 1.0e-7)); + prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b, epsilon = 1.0e-7)) + } +} From 6f7ef387e5c2b53132f8fc453d9b52221f4624dc Mon Sep 17 00:00:00 2001 From: metric-space Date: Tue, 18 Jan 2022 22:42:12 -0500 Subject: [PATCH 25/81] Format file --- nalgebra-lapack/src/qz.rs | 113 +++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 49 deletions(-) diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index 47efc08c..d6abac22 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -38,11 +38,11 @@ where { alphar: OVector, alphai: OVector, - beta: OVector, - vsl: OMatrix, - s: OMatrix, - vsr: OMatrix, - t: OMatrix + beta: OVector, + vsl: OMatrix, + s: OMatrix, + vsr: OMatrix, + t: OMatrix, } impl Copy for QZ @@ -61,7 +61,7 @@ where /// /// Panics if the method did not converge. pub fn new(a: OMatrix, b: OMatrix) -> Self { - Self::try_new(a,b).expect("Schur decomposition: convergence failed.") + Self::try_new(a, b).expect("Schur decomposition: convergence failed.") } /// Computes the eigenvalues and real Schur form of the matrix `m`. @@ -85,9 +85,9 @@ where let mut alphar = Matrix::zeros_generic(nrows, Const::<1>); let mut alphai = Matrix::zeros_generic(nrows, Const::<1>); - let mut beta = Matrix::zeros_generic(nrows, Const::<1>); - let mut vsl = Matrix::zeros_generic(nrows, ncols); - let mut vsr = Matrix::zeros_generic(nrows, ncols); + let mut beta = Matrix::zeros_generic(nrows, Const::<1>); + let mut vsl = Matrix::zeros_generic(nrows, ncols); + let mut vsr = Matrix::zeros_generic(nrows, ncols); // Placeholders: let mut bwork = [0i32]; let mut unused = 0; @@ -140,14 +140,27 @@ where ); lapack_check!(info); - Some(QZ {alphar, alphai, beta, - vsl, s:a, - vsr, t:b}) + Some(QZ { + alphar, + alphai, + beta, + vsl, + s: a, + vsr, + t: b, + }) } /// Retrieves the unitary matrix `Q` and the upper-quasitriangular matrix `T` such that the /// decomposed matrix equals `Q * T * Q.transpose()`. - pub fn unpack(self) -> (OMatrix, OMatrix, OMatrix, OMatrix){ + pub fn unpack( + self, + ) -> ( + OMatrix, + OMatrix, + OMatrix, + OMatrix, + ) { (self.vsl, self.s, self.t, self.vsr) } @@ -160,8 +173,10 @@ where let mut out = Matrix::zeros_generic(self.t.shape_generic().0, Const::<1>); for i in 0..out.len() { - out[i] = Complex::new(self.alphar[i].clone()/self.beta[i].clone(), - self.alphai[i].clone()/self.beta[i].clone()) + out[i] = Complex::new( + self.alphar[i].clone() / self.beta[i].clone(), + self.alphai[i].clone() / self.beta[i].clone(), + ) } out @@ -177,50 +192,50 @@ where pub trait QZScalar: Scalar { #[allow(missing_docs)] fn xgges( - jobvsl: u8, - jobvsr: u8, - sort: u8, + jobvsl: u8, + jobvsr: u8, + sort: u8, // select: ??? - n: i32, - a: &mut [Self], - lda: i32, - b: &mut [Self], - ldb: i32, - sdim: &mut i32, + n: i32, + a: &mut [Self], + lda: i32, + b: &mut [Self], + ldb: i32, + sdim: &mut i32, alphar: &mut [Self], alphai: &mut [Self], - beta : &mut [Self], - vsl: &mut [Self], - ldvsl: i32, - vsr: &mut [Self], - ldvsr: i32, - work: &mut [Self], - lwork: i32, - bwork: &mut [i32], - info: &mut i32 + beta: &mut [Self], + vsl: &mut [Self], + ldvsl: i32, + vsr: &mut [Self], + ldvsr: i32, + work: &mut [Self], + lwork: i32, + bwork: &mut [i32], + info: &mut i32, ); #[allow(missing_docs)] fn xgges_work_size( - jobvsl: u8, - jobvsr: u8, - sort: u8, + jobvsl: u8, + jobvsr: u8, + sort: u8, // select: ??? - n: i32, - a: &mut [Self], - lda: i32, - b: &mut [Self], - ldb: i32, - sdim: &mut i32, + n: i32, + a: &mut [Self], + lda: i32, + b: &mut [Self], + ldb: i32, + sdim: &mut i32, alphar: &mut [Self], alphai: &mut [Self], - beta : &mut [Self], - vsl: &mut [Self], - ldvsl: i32, - vsr: &mut [Self], - ldvsr: i32, - bwork: &mut [i32], - info: &mut i32 + beta: &mut [Self], + vsl: &mut [Self], + ldvsl: i32, + vsr: &mut [Self], + ldvsr: i32, + bwork: &mut [i32], + info: &mut i32, ) -> i32; } From 769f20ce6f813c8d7df2065092d21792da978319 Mon Sep 17 00:00:00 2001 From: metric-space Date: Wed, 19 Jan 2022 02:42:22 -0500 Subject: [PATCH 26/81] Comments more tailored to QZ --- nalgebra-lapack/src/qz.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index d6abac22..06ce0ba3 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -13,7 +13,7 @@ use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar}; use lapack; -/// Eigendecomposition of a real square matrix with complex eigenvalues. +/// Generalized eigendecomposition of a pair of N*N square matrices. #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] #[cfg_attr( feature = "serde-serialize", @@ -57,14 +57,15 @@ impl QZ where DefaultAllocator: Allocator + Allocator, { - /// Computes the eigenvalues and real Schur form of the matrix `m`. + /// Attempts to compute the QZ decomposition of input square matrices `a` and `b`. /// /// Panics if the method did not converge. pub fn new(a: OMatrix, b: OMatrix) -> Self { - Self::try_new(a, b).expect("Schur decomposition: convergence failed.") + Self::try_new(a, b).expect("QZ decomposition: convergence failed.") } - /// Computes the eigenvalues and real Schur form of the matrix `m`. + /// Computes the decomposition of input matrices `a` and `b` into a pair of matrices of Schur vectors + /// , a quasi-upper triangular matrix and an upper-triangular matrix . /// /// Returns `None` if the method did not converge. pub fn try_new(mut a: OMatrix, mut b: OMatrix) -> Option { @@ -73,14 +74,14 @@ where "Unable to compute the qz decomposition of non-square matrices." ); - // another assert to compare shape? + assert!( + a.shape_generic() == b.shape_generic(), + "Unable to compute the qz decomposition of two square matrices of different dimensions." + ); let (nrows, ncols) = a.shape_generic(); let n = nrows.value(); - let lda = n as i32; - let ldb = lda.clone(); - let mut info = 0; let mut alphar = Matrix::zeros_generic(nrows, Const::<1>); @@ -151,8 +152,10 @@ where }) } - /// Retrieves the unitary matrix `Q` and the upper-quasitriangular matrix `T` such that the - /// decomposed matrix equals `Q * T * Q.transpose()`. + /// Retrieves the left and right matrices of Schur Vectors (VSL and VSR) + /// the upper-quasitriangular matrix `S` and upper triangular matrix `T` such that the + /// decomposed matrix `A` equals `VSL * S * VSL.transpose()` and + /// decomposed matrix `B` equals `VSL * T * VSL.transpose()`. pub fn unpack( self, ) -> ( From b2c6c6b02d9b295c0984efcbde139e016d8b36ba Mon Sep 17 00:00:00 2001 From: metric-space Date: Wed, 19 Jan 2022 21:47:44 -0500 Subject: [PATCH 27/81] Add non-naive way of calculate generalized eigenvalue, write spotty test for generalized eigenvalues --- nalgebra-lapack/src/qz.rs | 15 +++++++++++---- nalgebra-lapack/tests/linalg/qz.rs | 18 ++++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index 06ce0ba3..b02a095f 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -176,10 +176,17 @@ where let mut out = Matrix::zeros_generic(self.t.shape_generic().0, Const::<1>); for i in 0..out.len() { - out[i] = Complex::new( - self.alphar[i].clone() / self.beta[i].clone(), - self.alphai[i].clone() / self.beta[i].clone(), - ) + let b = self.beta[i].clone(); + out[i] = { + if b < T::RealField::zero() { + Complex::::zero() + } else { + Complex::new( + self.alphar[i].clone() / b.clone(), + self.alphai[i].clone() / b.clone(), + ) + } + } } out diff --git a/nalgebra-lapack/tests/linalg/qz.rs b/nalgebra-lapack/tests/linalg/qz.rs index 6b4efbda..2b7730a7 100644 --- a/nalgebra-lapack/tests/linalg/qz.rs +++ b/nalgebra-lapack/tests/linalg/qz.rs @@ -1,5 +1,6 @@ -use na::DMatrix; +use na::{zero, DMatrix, Normed}; use nl::QZ; +use num_complex::Complex; use std::cmp; use crate::proptest::*; @@ -12,10 +13,19 @@ proptest! { let a = DMatrix::::new_random(n, n); let b = DMatrix::::new_random(n, n); - let (vsl,s,t,vsr) = QZ::new(a.clone(), b.clone()).unpack(); + let qz = QZ::new(a.clone(), b.clone()); + let (vsl,s,t,vsr) = qz.clone().unpack(); + let eigenvalues = qz.eigenvalues(); + let a_c = a.clone().map(|x| Complex::new(x, zero::())); - prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, epsilon = 1.0e-7)); - prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b, epsilon = 1.0e-7)) + prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a.clone(), epsilon = 1.0e-7)); + prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b.clone(), epsilon = 1.0e-7)); + // spotty test that skips over the first eiegenvalue which in some cases is extremely large relative to the other ones + // and fails the condition + for i in 1..n { + let b_c = b.clone().map(|x| eigenvalues[i]*Complex::new(x,zero::())); + prop_assert!(relative_eq!((&a_c - &b_c).determinant().norm(), 0.0, epsilon = 1.0e-6)); + } } #[test] From 6a283060743c0abd7edcf08766dcdd1213546a1d Mon Sep 17 00:00:00 2001 From: metric-space Date: Wed, 19 Jan 2022 23:51:46 -0500 Subject: [PATCH 28/81] Commented out failing tests, refactored checks for almost zeroes --- nalgebra-lapack/src/qz.rs | 28 ++++++++++++++++++--------- nalgebra-lapack/tests/linalg/qz.rs | 31 ++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index b02a095f..477ddfb7 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -176,16 +176,26 @@ where let mut out = Matrix::zeros_generic(self.t.shape_generic().0, Const::<1>); for i in 0..out.len() { - let b = self.beta[i].clone(); - out[i] = { - if b < T::RealField::zero() { - Complex::::zero() + out[i] = if self.beta[i].clone() < T::RealField::default_epsilon() { + Complex::zero() + } else { + let mut cr = self.alphar[i].clone(); + let mut ci = self.alphai[i].clone(); + let b = self.beta[i].clone(); + + if cr < T::RealField::default_epsilon() { + cr = T::RealField::zero() } else { - Complex::new( - self.alphar[i].clone() / b.clone(), - self.alphai[i].clone() / b.clone(), - ) - } + cr = cr / b.clone() + }; + + if ci < T::RealField::default_epsilon() { + ci = T::RealField::zero() + } else { + ci = ci / b + }; + + Complex::new(cr, ci) } } diff --git a/nalgebra-lapack/tests/linalg/qz.rs b/nalgebra-lapack/tests/linalg/qz.rs index 2b7730a7..84a7b030 100644 --- a/nalgebra-lapack/tests/linalg/qz.rs +++ b/nalgebra-lapack/tests/linalg/qz.rs @@ -1,6 +1,7 @@ -use na::{zero, DMatrix, Normed}; +use na::{zero, DMatrix, SMatrix}; use nl::QZ; use num_complex::Complex; +use simba::scalar::ComplexField; use std::cmp; use crate::proptest::*; @@ -15,23 +16,33 @@ proptest! { let qz = QZ::new(a.clone(), b.clone()); let (vsl,s,t,vsr) = qz.clone().unpack(); - let eigenvalues = qz.eigenvalues(); - let a_c = a.clone().map(|x| Complex::new(x, zero::())); + //let eigenvalues = qz.eigenvalues(); + //let a_c = a.clone().map(|x| Complex::new(x, zero::())); prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a.clone(), epsilon = 1.0e-7)); prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b.clone(), epsilon = 1.0e-7)); - // spotty test that skips over the first eiegenvalue which in some cases is extremely large relative to the other ones + // spotty test that skips over the first eigenvalue which in some cases is extremely large relative to the other ones // and fails the condition - for i in 1..n { - let b_c = b.clone().map(|x| eigenvalues[i]*Complex::new(x,zero::())); - prop_assert!(relative_eq!((&a_c - &b_c).determinant().norm(), 0.0, epsilon = 1.0e-6)); - } + //for i in 1..n { + // let b_c = b.clone().map(|x| eigenvalues[i]*Complex::new(x,zero::())); + // prop_assert!(relative_eq!((&a_c - &b_c).determinant().modulus(), 0.0, epsilon = 1.0e-6)); + //} } #[test] fn qz_static(a in matrix4(), b in matrix4()) { - let (vsl,s,t,vsr) = QZ::new(a.clone(), b.clone()).unpack(); + let qz = QZ::new(a.clone(), b.clone()); + let (vsl,s,t,vsr) = qz.unpack(); + //let eigenvalues = qz.eigenvalues(); + //let a_c = a.clone().map(|x| Complex::new(x, zero::())); + prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, epsilon = 1.0e-7)); - prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b, epsilon = 1.0e-7)) + prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b, epsilon = 1.0e-7)); + + //for i in 0..4 { + // let b_c = b.clone().map(|x| eigenvalues[i]*Complex::new(x,zero::())); + // println!("{}",eigenvalues); + // prop_assert!(relative_eq!((&a_c - &b_c).determinant().modulus(), 0.0, epsilon = 1.0e-4)) + //} } } From 7e9345d91edd068ce0a965912a1e514b3bd1e342 Mon Sep 17 00:00:00 2001 From: metric-space Date: Fri, 21 Jan 2022 06:41:06 -0500 Subject: [PATCH 29/81] Correction for not calculating absolurte value --- nalgebra-lapack/src/qz.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index 477ddfb7..e3319452 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -167,6 +167,8 @@ where (self.vsl, self.s, self.t, self.vsr) } + + /// computes the generalized eigenvalues #[must_use] pub fn eigenvalues(&self) -> OVector, D> @@ -176,20 +178,20 @@ where let mut out = Matrix::zeros_generic(self.t.shape_generic().0, Const::<1>); for i in 0..out.len() { - out[i] = if self.beta[i].clone() < T::RealField::default_epsilon() { + out[i] = if self.beta[i].clone().abs() < T::RealField::default_epsilon() { Complex::zero() } else { let mut cr = self.alphar[i].clone(); let mut ci = self.alphai[i].clone(); let b = self.beta[i].clone(); - if cr < T::RealField::default_epsilon() { + if cr.clone().abs() < T::RealField::default_epsilon() { cr = T::RealField::zero() } else { cr = cr / b.clone() }; - if ci < T::RealField::default_epsilon() { + if ci.clone().abs() < T::RealField::default_epsilon() { ci = T::RealField::zero() } else { ci = ci / b From 7d8fb3d3848978e42ad0c65732a00267aa428202 Mon Sep 17 00:00:00 2001 From: metric-space Date: Mon, 24 Jan 2022 23:56:44 -0500 Subject: [PATCH 30/81] New wrapper for generalized eigenvalues and associated eigenvectors via LAPACK routines sggev/dggev --- .../src/generalized_eigenvalues.rs | 339 ++++++++++++++++++ nalgebra-lapack/src/lib.rs | 2 + .../tests/linalg/generalized_eigenvalues.rs | 59 +++ nalgebra-lapack/tests/linalg/mod.rs | 1 + 4 files changed, 401 insertions(+) create mode 100644 nalgebra-lapack/src/generalized_eigenvalues.rs create mode 100644 nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs new file mode 100644 index 00000000..6332f2db --- /dev/null +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -0,0 +1,339 @@ +#[cfg(feature = "serde-serialize")] +use serde::{Deserialize, Serialize}; + +use num::Zero; +use num_complex::Complex; + +use simba::scalar:: RealField; + +use crate::ComplexHelper; +use na::allocator::Allocator; +use na::dimension::{Const, Dim}; +use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar}; + +use lapack; + +/// Generalized eigenvalues and generalized eigenvectors(left and right) of a pair of N*N square matrices. +#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] +#[cfg_attr( + feature = "serde-serialize", + serde( + bound(serialize = "DefaultAllocator: Allocator + Allocator, + OVector: Serialize, + OMatrix: Serialize") + ) +)] +#[cfg_attr( + feature = "serde-serialize", + serde( + bound(deserialize = "DefaultAllocator: Allocator + Allocator, + OVector: Serialize, + OMatrix: Deserialize<'de>") + ) +)] +#[derive(Clone, Debug)] +pub struct GE +where + DefaultAllocator: Allocator + Allocator, +{ + alphar: OVector, + alphai: OVector, + beta: OVector, + vsl: OMatrix, + vsr: OMatrix, +} + +impl Copy for GE +where + DefaultAllocator: Allocator + Allocator, + OMatrix: Copy, + OVector: Copy, +{ +} + +impl GE +where + DefaultAllocator: Allocator + Allocator, +{ + /// Attempts to compute the generalized eigenvalues (and eigenvectors) via the raw returns from LAPACK's + /// dggev and sggev routines + /// + /// For each e in generalized eigenvalues and the associated eigenvectors e_l and e_r (left andf right) + /// it satisfies e_l*a = e*e_l*b and a*e_r = e*b*e_r + /// + /// Panics if the method did not converge. + pub fn new(a: OMatrix, b: OMatrix) -> Self { + Self::try_new(a, b).expect("Calculation of generalized eigenvalues failed.") + } + + /// Attempts to compute the generalized eigenvalues (and eigenvectors) via the raw returns from LAPACK's + /// dggev and sggev routines + /// + /// For each e in generalized eigenvalues and the associated eigenvectors e_l and e_r (left andf right) + /// it satisfies e_l*a = e*e_l*b and a*e_r = e*b*e_r + /// + /// Returns `None` if the method did not converge. + pub fn try_new(mut a: OMatrix, mut b: OMatrix) -> Option { + assert!( + a.is_square() && b.is_square(), + "Unable to compute the generalized eigenvalues of non-square matrices." + ); + + assert!( + a.shape_generic() == b.shape_generic(), + "Unable to compute the generalized eigenvalues of two square matrices of different dimensions." + ); + + let (nrows, ncols) = a.shape_generic(); + let n = nrows.value(); + + let mut info = 0; + + let mut alphar = Matrix::zeros_generic(nrows, Const::<1>); + let mut alphai = Matrix::zeros_generic(nrows, Const::<1>); + let mut beta = Matrix::zeros_generic(nrows, Const::<1>); + let mut vsl = Matrix::zeros_generic(nrows, ncols); + let mut vsr = Matrix::zeros_generic(nrows, ncols); + + let lwork = T::xggev_work_size( + b'V', + b'V', + n as i32, + a.as_mut_slice(), + n as i32, + b.as_mut_slice(), + n as i32, + alphar.as_mut_slice(), + alphai.as_mut_slice(), + beta.as_mut_slice(), + vsl.as_mut_slice(), + n as i32, + vsr.as_mut_slice(), + n as i32, + &mut info, + ); + lapack_check!(info); + + let mut work = vec![T::zero(); lwork as usize]; + + T::xggev( + b'V', + b'V', + n as i32, + a.as_mut_slice(), + n as i32, + b.as_mut_slice(), + n as i32, + alphar.as_mut_slice(), + alphai.as_mut_slice(), + beta.as_mut_slice(), + vsl.as_mut_slice(), + n as i32, + vsr.as_mut_slice(), + n as i32, + &mut work, + lwork, + &mut info, + ); + lapack_check!(info); + + Some(GE { + alphar, + alphai, + beta, + vsl, + vsr, + }) + } + + /// Calculates the generalized eigenvectors (left and right) associated with the generalized eigenvalues + pub fn eigenvectors(self) -> (OMatrix, D, D>, OMatrix, D, D>) + where + DefaultAllocator: Allocator, D, D> + Allocator, D>, + { + let n = self.vsl.shape().0; + let mut l = self + .vsl + .clone() + .map(|x| Complex::new(x, T::RealField::zero())); + let mut r = self + .vsr + .clone() + .map(|x| Complex::new(x, T::RealField::zero())); + + let eigenvalues = &self.eigenvalues(); + + let mut ll; + let mut c = 0; + while c < n { + if eigenvalues[c].im.abs() > T::RealField::default_epsilon() && c + 1 < n && { + let e_conj = eigenvalues[c].conj(); + let e = eigenvalues[c + 1]; + ((e_conj.re - e.re).abs() < T::RealField::default_epsilon()) + && ((e_conj.im - e.im).abs() < T::RealField::default_epsilon()) + } { + ll = l.column(c + 1).into_owned(); + l.column_mut(c).zip_apply(&ll, |r, i| { + *r = Complex::new(r.re.clone(), i.re); + }); + ll.copy_from(&l.column(c)); + l.column_mut(c + 1).zip_apply(&ll, |r, i| { + *r = i.conj(); + }); + + ll.copy_from(&r.column(c + 1)); + r.column_mut(c).zip_apply(&ll, |r, i| { + *r = Complex::new(r.re, i.re); + }); + ll.copy_from(&r.column(c)); + r.column_mut(c + 1).zip_apply(&ll, |r, i| { + *r = i.conj(); + }); + + c += 2; + } else { + c += 1; + } + } + + (l, r) + } + + /// computes the generalized eigenvalues + #[must_use] + pub fn eigenvalues(&self) -> OVector, D> + where + DefaultAllocator: Allocator, D>, + { + let mut out = Matrix::zeros_generic(self.vsl.shape_generic().0, Const::<1>); + + for i in 0..out.len() { + out[i] = if self.beta[i].clone().abs() < T::RealField::default_epsilon() { + Complex::zero() + } else { + let mut cr = self.alphar[i].clone(); + let mut ci = self.alphai[i].clone(); + let b = self.beta[i].clone(); + + if cr.clone().abs() < T::RealField::default_epsilon() { + cr = T::RealField::zero() + } else { + cr = cr / b.clone() + }; + + if ci.clone().abs() < T::RealField::default_epsilon() { + ci = T::RealField::zero() + } else { + ci = ci / b + }; + + Complex::new(cr, ci) + } + } + + out + } +} + +/* + * + * Lapack functions dispatch. + * + */ +/// Trait implemented by scalars for which Lapack implements the RealField GE decomposition. +pub trait GEScalar: Scalar { + #[allow(missing_docs)] + fn xggev( + jobvsl: u8, + jobvsr: u8, + n: i32, + a: &mut [Self], + lda: i32, + b: &mut [Self], + ldb: i32, + alphar: &mut [Self], + alphai: &mut [Self], + beta: &mut [Self], + vsl: &mut [Self], + ldvsl: i32, + vsr: &mut [Self], + ldvsr: i32, + work: &mut [Self], + lwork: i32, + info: &mut i32, + ); + + #[allow(missing_docs)] + fn xggev_work_size( + jobvsl: u8, + jobvsr: u8, + n: i32, + a: &mut [Self], + lda: i32, + b: &mut [Self], + ldb: i32, + alphar: &mut [Self], + alphai: &mut [Self], + beta: &mut [Self], + vsl: &mut [Self], + ldvsl: i32, + vsr: &mut [Self], + ldvsr: i32, + info: &mut i32, + ) -> i32; +} + +macro_rules! real_eigensystem_scalar_impl ( + ($N: ty, $xggev: path) => ( + impl GEScalar for $N { + #[inline] + fn xggev(jobvsl: u8, + jobvsr: u8, + n: i32, + a: &mut [$N], + lda: i32, + b: &mut [$N], + ldb: i32, + alphar: &mut [$N], + alphai: &mut [$N], + beta : &mut [$N], + vsl: &mut [$N], + ldvsl: i32, + vsr: &mut [$N], + ldvsr: i32, + work: &mut [$N], + lwork: i32, + info: &mut i32) { + unsafe { $xggev(jobvsl, jobvsr, n, a, lda, b, ldb, alphar, alphai, beta, vsl, ldvsl, vsr, ldvsr, work, lwork, info); } + } + + + #[inline] + fn xggev_work_size(jobvsl: u8, + jobvsr: u8, + n: i32, + a: &mut [$N], + lda: i32, + b: &mut [$N], + ldb: i32, + alphar: &mut [$N], + alphai: &mut [$N], + beta : &mut [$N], + vsl: &mut [$N], + ldvsl: i32, + vsr: &mut [$N], + ldvsr: i32, + info: &mut i32) + -> i32 { + let mut work = [ Zero::zero() ]; + let lwork = -1 as i32; + + unsafe { $xggev(jobvsl, jobvsr, n, a, lda, b, ldb, alphar, alphai, beta, vsl, ldvsl, vsr, ldvsr, &mut work, lwork, info); } + ComplexHelper::real_part(work[0]) as i32 + } + } + ) +); + +real_eigensystem_scalar_impl!(f32, lapack::sggev); +real_eigensystem_scalar_impl!(f64, lapack::dggev); diff --git a/nalgebra-lapack/src/lib.rs b/nalgebra-lapack/src/lib.rs index e89ab160..dec4daac 100644 --- a/nalgebra-lapack/src/lib.rs +++ b/nalgebra-lapack/src/lib.rs @@ -87,6 +87,7 @@ mod hessenberg; mod lu; mod qr; mod qz; +mod generalized_eigenvalues; mod schur; mod svd; mod symmetric_eigen; @@ -99,6 +100,7 @@ pub use self::hessenberg::Hessenberg; pub use self::lu::{LUScalar, LU}; pub use self::qr::QR; pub use self::qz::QZ; +pub use self::generalized_eigenvalues::GE; pub use self::schur::Schur; pub use self::svd::SVD; pub use self::symmetric_eigen::SymmetricEigen; diff --git a/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs b/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs new file mode 100644 index 00000000..275691c8 --- /dev/null +++ b/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs @@ -0,0 +1,59 @@ +use na::dimension::{Const, Dynamic}; +use na::{DMatrix, EuclideanNorm, Norm, OMatrix}; +use nl::GE; +use num_complex::Complex; +use simba::scalar::ComplexField; +use std::cmp; + +use crate::proptest::*; +use proptest::{prop_assert, proptest}; + +proptest! { + #[test] + fn ge(n in PROPTEST_MATRIX_DIM) { + let n = cmp::max(1, cmp::min(n, 10)); + let a = DMatrix::::new_random(n, n); + let b = DMatrix::::new_random(n, n); + let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a))); + let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b))); + + if a_condition_no.unwrap_or(200000.0) < 10.0 && b_condition_no.unwrap_or(200000.0) < 10.0 { + let a_c =a.clone().map(|x| Complex::new(x, 0.0)); + let b_c = b.clone().map(|x| Complex::new(x, 0.0)); + + let ge = GE::new(a.clone(), b.clone()); + let (vsl,vsr) = ge.clone().eigenvectors(); + let eigenvalues = ge.clone().eigenvalues(); + + for i in 0..n { + let left_eigenvector = &vsl.column(i); + prop_assert!(relative_eq!((left_eigenvector.transpose()*&a_c - left_eigenvector.transpose()*&b_c*eigenvalues[i]).map(|x| x.modulus()), OMatrix::zeros_generic(Const::<1>,Dynamic::new(n)) ,epsilon = 1.0e-7)); + + let right_eigenvector = &vsr.column(i); + prop_assert!(relative_eq!((&a_c*right_eigenvector - &b_c*right_eigenvector*eigenvalues[i]).map(|x| x.modulus()), OMatrix::zeros_generic(Dynamic::new(n), Const::<1>) ,epsilon = 1.0e-7)); + }; + }; + } + + #[test] + fn ge_static(a in matrix4(), b in matrix4()) { + let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a))); + let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b))); + + if a_condition_no.unwrap_or(200000.0) < 10.0 && b_condition_no.unwrap_or(200000.0) < 10.0{ + let ge = GE::new(a.clone(), b.clone()); + let a_c =a.clone().map(|x| Complex::new(x, 0.0)); + let b_c = b.clone().map(|x| Complex::new(x, 0.0)); + let (vsl,vsr) = ge.eigenvectors(); + let eigenvalues = ge.eigenvalues(); + + for i in 0..4 { + let left_eigenvector = &vsl.column(i); + prop_assert!(relative_eq!((left_eigenvector.transpose()*&a_c - left_eigenvector.transpose()*&b_c*eigenvalues[i]).map(|x| x.modulus()), OMatrix::zeros_generic(Const::<1>,Const::<4>) ,epsilon = 1.0e-7)); + + let right_eigenvector = &vsr.column(i); + prop_assert!(relative_eq!((&a_c*right_eigenvector - &b_c*right_eigenvector*eigenvalues[i]).map(|x| x.modulus()), OMatrix::zeros_generic(Const::<4>, Const::<1>) ,epsilon = 1.0e-7)); + }; + }; + } +} diff --git a/nalgebra-lapack/tests/linalg/mod.rs b/nalgebra-lapack/tests/linalg/mod.rs index a4043469..9fd539c4 100644 --- a/nalgebra-lapack/tests/linalg/mod.rs +++ b/nalgebra-lapack/tests/linalg/mod.rs @@ -2,6 +2,7 @@ mod cholesky; mod lu; mod qr; mod qz; +mod generalized_eigenvalues; mod real_eigensystem; mod schur; mod svd; From 748848fea756d87daa1a2ab9128a28caa6646d1f Mon Sep 17 00:00:00 2001 From: metric-space Date: Mon, 24 Jan 2022 23:58:21 -0500 Subject: [PATCH 31/81] Cleanup of QZ module and added GE's calculation of eigenvalues as a test for QZ's calculation of eigenvalues --- nalgebra-lapack/src/qz.rs | 17 +++++++++++----- nalgebra-lapack/tests/linalg/qz.rs | 31 ++++++++++++------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index e3319452..ea775ea6 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -13,7 +13,11 @@ use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar}; use lapack; -/// Generalized eigendecomposition of a pair of N*N square matrices. +/// QZ decomposition of a pair of N*N square matrices. +/// Retrieves the left and right matrices of Schur Vectors (VSL and VSR) +/// the upper-quasitriangular matrix `S` and upper triangular matrix `T` such that the +/// decomposed input matrix `a` equals `VSL * S * VSL.transpose()` and +/// decomposed input matrix `b` equals `VSL * T * VSL.transpose()`. #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] #[cfg_attr( feature = "serde-serialize", @@ -59,6 +63,11 @@ where { /// Attempts to compute the QZ decomposition of input square matrices `a` and `b`. /// + /// i.e retrieves the left and right matrices of Schur Vectors (VSL and VSR) + /// the upper-quasitriangular matrix `S` and upper triangular matrix `T` such that the + /// decomposed matrix `a` equals `VSL * S * VSL.transpose()` and + /// decomposed matrix `b` equals `VSL * T * VSL.transpose()`. + /// /// Panics if the method did not converge. pub fn new(a: OMatrix, b: OMatrix) -> Self { Self::try_new(a, b).expect("QZ decomposition: convergence failed.") @@ -154,8 +163,8 @@ where /// Retrieves the left and right matrices of Schur Vectors (VSL and VSR) /// the upper-quasitriangular matrix `S` and upper triangular matrix `T` such that the - /// decomposed matrix `A` equals `VSL * S * VSL.transpose()` and - /// decomposed matrix `B` equals `VSL * T * VSL.transpose()`. + /// decomposed input matrix `a` equals `VSL * S * VSL.transpose()` and + /// decomposed input matrix `b` equals `VSL * T * VSL.transpose()`. pub fn unpack( self, ) -> ( @@ -167,8 +176,6 @@ where (self.vsl, self.s, self.t, self.vsr) } - - /// computes the generalized eigenvalues #[must_use] pub fn eigenvalues(&self) -> OVector, D> diff --git a/nalgebra-lapack/tests/linalg/qz.rs b/nalgebra-lapack/tests/linalg/qz.rs index 84a7b030..d7fe4132 100644 --- a/nalgebra-lapack/tests/linalg/qz.rs +++ b/nalgebra-lapack/tests/linalg/qz.rs @@ -1,7 +1,5 @@ -use na::{zero, DMatrix, SMatrix}; -use nl::QZ; -use num_complex::Complex; -use simba::scalar::ComplexField; +use na::DMatrix; +use nl::{GE, QZ}; use std::cmp; use crate::proptest::*; @@ -16,33 +14,28 @@ proptest! { let qz = QZ::new(a.clone(), b.clone()); let (vsl,s,t,vsr) = qz.clone().unpack(); - //let eigenvalues = qz.eigenvalues(); - //let a_c = a.clone().map(|x| Complex::new(x, zero::())); + let eigenvalues = qz.eigenvalues(); + + let ge = GE::new(a.clone(), b.clone()); + let eigenvalues2 = ge.eigenvalues(); prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a.clone(), epsilon = 1.0e-7)); prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b.clone(), epsilon = 1.0e-7)); - // spotty test that skips over the first eigenvalue which in some cases is extremely large relative to the other ones - // and fails the condition - //for i in 1..n { - // let b_c = b.clone().map(|x| eigenvalues[i]*Complex::new(x,zero::())); - // prop_assert!(relative_eq!((&a_c - &b_c).determinant().modulus(), 0.0, epsilon = 1.0e-6)); - //} + prop_assert!(eigenvalues == eigenvalues2); } #[test] fn qz_static(a in matrix4(), b in matrix4()) { let qz = QZ::new(a.clone(), b.clone()); + let ge = GE::new(a.clone(), b.clone()); let (vsl,s,t,vsr) = qz.unpack(); - //let eigenvalues = qz.eigenvalues(); - //let a_c = a.clone().map(|x| Complex::new(x, zero::())); + let eigenvalues = qz.eigenvalues(); + let eigenvalues2 = ge.eigenvalues(); prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, epsilon = 1.0e-7)); prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b, epsilon = 1.0e-7)); - //for i in 0..4 { - // let b_c = b.clone().map(|x| eigenvalues[i]*Complex::new(x,zero::())); - // println!("{}",eigenvalues); - // prop_assert!(relative_eq!((&a_c - &b_c).determinant().modulus(), 0.0, epsilon = 1.0e-4)) - //} + prop_assert!(eigenvalues == eigenvalues2); + } } From ae35d1cf976b9d2779754fe8814f2289e66843b9 Mon Sep 17 00:00:00 2001 From: metric-space Date: Thu, 3 Feb 2022 06:36:10 -0500 Subject: [PATCH 32/81] New code and modified tests for generalized_eigenvalues --- .../src/generalized_eigenvalues.rs | 89 ++++++++++++++++--- .../tests/linalg/generalized_eigenvalues.rs | 63 ++++++++----- 2 files changed, 118 insertions(+), 34 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index 6332f2db..5c273e9b 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use num::Zero; use num_complex::Complex; -use simba::scalar:: RealField; +use simba::scalar::RealField; use crate::ComplexHelper; use na::allocator::Allocator; @@ -14,6 +14,19 @@ use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar}; use lapack; /// Generalized eigenvalues and generalized eigenvectors(left and right) of a pair of N*N square matrices. +/// +/// Each generalized eigenvalue (lambda) satisfies determinant(A - lambda*B) = 0 +/// +/// The right eigenvector v(j) corresponding to the eigenvalue lambda(j) +/// of (A,B) satisfies +/// +/// A * v(j) = lambda(j) * B * v(j). +/// +/// The left eigenvector u(j) corresponding to the eigenvalue lambda(j) +/// of (A,B) satisfies +/// +/// u(j)**H * A = lambda(j) * u(j)**H * B . +/// where u(j)**H is the conjugate-transpose of u(j). #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] #[cfg_attr( feature = "serde-serialize", @@ -55,11 +68,21 @@ impl GE where DefaultAllocator: Allocator + Allocator, { - /// Attempts to compute the generalized eigenvalues (and eigenvectors) via the raw returns from LAPACK's - /// dggev and sggev routines + /// Attempts to compute the generalized eigenvalues, and left and right associated eigenvectors + /// via the raw returns from LAPACK's dggev and sggev routines /// - /// For each e in generalized eigenvalues and the associated eigenvectors e_l and e_r (left andf right) - /// it satisfies e_l*a = e*e_l*b and a*e_r = e*b*e_r + /// Each generalized eigenvalue (lambda) satisfies determinant(A - lambda*B) = 0 + /// + /// The right eigenvector v(j) corresponding to the eigenvalue lambda(j) + /// of (A,B) satisfies + /// + /// A * v(j) = lambda(j) * B * v(j). + /// + /// The left eigenvector u(j) corresponding to the eigenvalue lambda(j) + /// of (A,B) satisfies + /// + /// u(j)**H * A = lambda(j) * u(j)**H * B . + /// where u(j)**H is the conjugate-transpose of u(j). /// /// Panics if the method did not converge. pub fn new(a: OMatrix, b: OMatrix) -> Self { @@ -69,8 +92,18 @@ where /// Attempts to compute the generalized eigenvalues (and eigenvectors) via the raw returns from LAPACK's /// dggev and sggev routines /// - /// For each e in generalized eigenvalues and the associated eigenvectors e_l and e_r (left andf right) - /// it satisfies e_l*a = e*e_l*b and a*e_r = e*b*e_r + /// Each generalized eigenvalue (lambda) satisfies determinant(A - lambda*B) = 0 + /// + /// The right eigenvector v(j) corresponding to the eigenvalue lambda(j) + /// of (A,B) satisfies + /// + /// A * v(j) = lambda(j) * B * v(j). + /// + /// The left eigenvector u(j) corresponding to the eigenvalue lambda(j) + /// of (A,B) satisfies + /// + /// u(j)**H * A = lambda(j) * u(j)**H * B . + /// where u(j)**H is the conjugate-transpose of u(j). /// /// Returns `None` if the method did not converge. pub fn try_new(mut a: OMatrix, mut b: OMatrix) -> Option { @@ -147,9 +180,24 @@ where } /// Calculates the generalized eigenvectors (left and right) associated with the generalized eigenvalues + /// Outputs two matrices, the first one containing the left eigenvectors of the generalized eigenvalues + /// as columns and the second matrix contains the right eigenvectors of the generalized eigenvalues + /// as columns + /// + /// The right eigenvector v(j) corresponding to the eigenvalue lambda(j) + /// of (A,B) satisfies + /// + /// A * v(j) = lambda(j) * B * v(j). + /// + /// The left eigenvector u(j) corresponding to the eigenvalue lambda(j) + /// of (A,B) satisfies + /// + /// u(j)**H * A = lambda(j) * u(j)**H * B . + /// where u(j)**H is the conjugate-transpose of u(j). pub fn eigenvectors(self) -> (OMatrix, D, D>, OMatrix, D, D>) where - DefaultAllocator: Allocator, D, D> + Allocator, D>, + DefaultAllocator: + Allocator, D, D> + Allocator, D> + Allocator<(Complex, T), D>, { let n = self.vsl.shape().0; let mut l = self @@ -199,9 +247,10 @@ where (l, r) } - /// computes the generalized eigenvalues + /// computes the generalized eigenvalues i.e values of lambda that satisfy the following equation + /// determinant(A - lambda* B) = 0 #[must_use] - pub fn eigenvalues(&self) -> OVector, D> + fn eigenvalues(&self) -> OVector, D> where DefaultAllocator: Allocator, D>, { @@ -233,6 +282,26 @@ where out } + + /// outputs the unprocessed (almost) version of generalized eigenvalues ((alphar, alpai), beta) + /// straight from LAPACK + #[must_use] + pub fn raw_eigenvalues(&self) -> OVector<(Complex, T), D> + where + DefaultAllocator: Allocator<(Complex, T), D>, + { + let mut out = Matrix::from_element_generic( + self.vsl.shape_generic().0, + Const::<1>, + (Complex::zero(), T::RealField::zero()), + ); + + for i in 0..out.len() { + out[i] = (Complex::new(self.alphar[i], self.alphai[i]), self.beta[i]) + } + + out + } } /* diff --git a/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs b/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs index 275691c8..8da21b30 100644 --- a/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs +++ b/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs @@ -17,21 +17,29 @@ proptest! { let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a))); let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b))); - if a_condition_no.unwrap_or(200000.0) < 10.0 && b_condition_no.unwrap_or(200000.0) < 10.0 { - let a_c =a.clone().map(|x| Complex::new(x, 0.0)); - let b_c = b.clone().map(|x| Complex::new(x, 0.0)); + if a_condition_no.unwrap_or(200000.0) < 5.0 && b_condition_no.unwrap_or(200000.0) < 5.0 { + let a_c = a.clone().map(|x| Complex::new(x, 0.0)); + let b_c = b.clone().map(|x| Complex::new(x, 0.0)); - let ge = GE::new(a.clone(), b.clone()); - let (vsl,vsr) = ge.clone().eigenvectors(); - let eigenvalues = ge.clone().eigenvalues(); + let ge = GE::new(a.clone(), b.clone()); + let (vsl,vsr) = ge.clone().eigenvectors(); - for i in 0..n { - let left_eigenvector = &vsl.column(i); - prop_assert!(relative_eq!((left_eigenvector.transpose()*&a_c - left_eigenvector.transpose()*&b_c*eigenvalues[i]).map(|x| x.modulus()), OMatrix::zeros_generic(Const::<1>,Dynamic::new(n)) ,epsilon = 1.0e-7)); + for (i,(alpha,beta)) in ge.raw_eigenvalues().iter().enumerate() { + let l_a = a_c.clone() * Complex::new(*beta, 0.0); + let l_b = b_c.clone() * *alpha; - let right_eigenvector = &vsr.column(i); - prop_assert!(relative_eq!((&a_c*right_eigenvector - &b_c*right_eigenvector*eigenvalues[i]).map(|x| x.modulus()), OMatrix::zeros_generic(Dynamic::new(n), Const::<1>) ,epsilon = 1.0e-7)); - }; + prop_assert!( + relative_eq!( + ((&l_a - &l_b)*vsr.column(i)).map(|x| x.modulus()), + OMatrix::zeros_generic(Dynamic::new(n), Const::<1>), + epsilon = 1.0e-7)); + + prop_assert!( + relative_eq!( + (vsl.column(i).adjoint()*(&l_a - &l_b)).map(|x| x.modulus()), + OMatrix::zeros_generic(Const::<1>, Dynamic::new(n)), + epsilon = 1.0e-7)) + }; }; } @@ -40,20 +48,27 @@ proptest! { let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a))); let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b))); - if a_condition_no.unwrap_or(200000.0) < 10.0 && b_condition_no.unwrap_or(200000.0) < 10.0{ - let ge = GE::new(a.clone(), b.clone()); - let a_c =a.clone().map(|x| Complex::new(x, 0.0)); - let b_c = b.clone().map(|x| Complex::new(x, 0.0)); - let (vsl,vsr) = ge.eigenvectors(); - let eigenvalues = ge.eigenvalues(); + if a_condition_no.unwrap_or(200000.0) < 5.0 && b_condition_no.unwrap_or(200000.0) < 5.0 { + let ge = GE::new(a.clone(), b.clone()); + let a_c =a.clone().map(|x| Complex::new(x, 0.0)); + let b_c = b.clone().map(|x| Complex::new(x, 0.0)); + let (vsl,vsr) = ge.eigenvectors(); + let eigenvalues = ge.raw_eigenvalues(); - for i in 0..4 { - let left_eigenvector = &vsl.column(i); - prop_assert!(relative_eq!((left_eigenvector.transpose()*&a_c - left_eigenvector.transpose()*&b_c*eigenvalues[i]).map(|x| x.modulus()), OMatrix::zeros_generic(Const::<1>,Const::<4>) ,epsilon = 1.0e-7)); + for (i,(alpha,beta)) in eigenvalues.iter().enumerate() { + let l_a = a_c.clone() * Complex::new(*beta, 0.0); + let l_b = b_c.clone() * *alpha; - let right_eigenvector = &vsr.column(i); - prop_assert!(relative_eq!((&a_c*right_eigenvector - &b_c*right_eigenvector*eigenvalues[i]).map(|x| x.modulus()), OMatrix::zeros_generic(Const::<4>, Const::<1>) ,epsilon = 1.0e-7)); - }; + prop_assert!( + relative_eq!( + ((&l_a - &l_b)*vsr.column(i)).map(|x| x.modulus()), + OMatrix::zeros_generic(Const::<4>, Const::<1>), + epsilon = 1.0e-7)); + prop_assert!( + relative_eq!((vsl.column(i).adjoint()*(&l_a - &l_b)).map(|x| x.modulus()), + OMatrix::zeros_generic(Const::<1>, Const::<4>), + epsilon = 1.0e-7)) + } }; } } From 162bb3218de2423648f82b52924a9c37aebaf27b Mon Sep 17 00:00:00 2001 From: metric-space Date: Thu, 3 Feb 2022 06:36:41 -0500 Subject: [PATCH 33/81] New code and modified tests for qz --- nalgebra-lapack/src/qz.rs | 43 +++++++---------------- nalgebra-lapack/tests/linalg/qz.rs | 55 ++++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 41 deletions(-) diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index ea775ea6..ee0e6208 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -42,11 +42,11 @@ where { alphar: OVector, alphai: OVector, - beta: OVector, - vsl: OMatrix, - s: OMatrix, - vsr: OMatrix, - t: OMatrix, + beta: OVector, + vsl: OMatrix, + s: OMatrix, + vsr: OMatrix, + t: OMatrix, } impl Copy for QZ @@ -176,36 +176,19 @@ where (self.vsl, self.s, self.t, self.vsr) } - /// computes the generalized eigenvalues + /// outputs the unprocessed (almost) version of generalized eigenvalues ((alphar, alpai), beta) + /// straight from LAPACK #[must_use] - pub fn eigenvalues(&self) -> OVector, D> + pub fn raw_eigenvalues(&self) -> OVector<(Complex, T), D> where - DefaultAllocator: Allocator, D>, + DefaultAllocator: Allocator<(Complex, T), D>, { - let mut out = Matrix::zeros_generic(self.t.shape_generic().0, Const::<1>); + let mut out = Matrix::from_element_generic(self.vsl.shape_generic().0, Const::<1>, (Complex::zero(), T::RealField::zero())); for i in 0..out.len() { - out[i] = if self.beta[i].clone().abs() < T::RealField::default_epsilon() { - Complex::zero() - } else { - let mut cr = self.alphar[i].clone(); - let mut ci = self.alphai[i].clone(); - let b = self.beta[i].clone(); - - if cr.clone().abs() < T::RealField::default_epsilon() { - cr = T::RealField::zero() - } else { - cr = cr / b.clone() - }; - - if ci.clone().abs() < T::RealField::default_epsilon() { - ci = T::RealField::zero() - } else { - ci = ci / b - }; - - Complex::new(cr, ci) - } + out[i] = (Complex::new(self.alphar[i].clone(), + self.alphai[i].clone()), + self.beta[i].clone()) } out diff --git a/nalgebra-lapack/tests/linalg/qz.rs b/nalgebra-lapack/tests/linalg/qz.rs index d7fe4132..6f9cf7f8 100644 --- a/nalgebra-lapack/tests/linalg/qz.rs +++ b/nalgebra-lapack/tests/linalg/qz.rs @@ -1,5 +1,7 @@ -use na::DMatrix; -use nl::{GE, QZ}; +use na::{DMatrix, EuclideanNorm, Norm}; +use nl::QZ; +use num_complex::Complex; +use simba::scalar::ComplexField; use std::cmp; use crate::proptest::*; @@ -14,28 +16,59 @@ proptest! { let qz = QZ::new(a.clone(), b.clone()); let (vsl,s,t,vsr) = qz.clone().unpack(); - let eigenvalues = qz.eigenvalues(); - - let ge = GE::new(a.clone(), b.clone()); - let eigenvalues2 = ge.eigenvalues(); + let eigenvalues = qz.raw_eigenvalues(); prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a.clone(), epsilon = 1.0e-7)); prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b.clone(), epsilon = 1.0e-7)); - prop_assert!(eigenvalues == eigenvalues2); + + let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a))); + let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b))); + + if a_condition_no.unwrap_or(200000.0) < 5.0 && b_condition_no.unwrap_or(200000.0) < 5.0 { + let a_c = a.clone().map(|x| Complex::new(x, 0.0)); + let b_c = b.clone().map(|x| Complex::new(x, 0.0)); + + + for (alpha,beta) in eigenvalues.iter() { + let l_a = a_c.clone() * Complex::new(*beta, 0.0); + let l_b = b_c.clone() * *alpha; + + prop_assert!( + relative_eq!( + (&l_a - &l_b).determinant().modulus(), + 0.0, + epsilon = 1.0e-7)); + + }; + }; } #[test] fn qz_static(a in matrix4(), b in matrix4()) { let qz = QZ::new(a.clone(), b.clone()); - let ge = GE::new(a.clone(), b.clone()); let (vsl,s,t,vsr) = qz.unpack(); - let eigenvalues = qz.eigenvalues(); - let eigenvalues2 = ge.eigenvalues(); + let eigenvalues = qz.raw_eigenvalues(); prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, epsilon = 1.0e-7)); prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b, epsilon = 1.0e-7)); - prop_assert!(eigenvalues == eigenvalues2); + let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a))); + let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b))); + if a_condition_no.unwrap_or(200000.0) < 5.0 && b_condition_no.unwrap_or(200000.0) < 5.0 { + let a_c =a.clone().map(|x| Complex::new(x, 0.0)); + let b_c = b.clone().map(|x| Complex::new(x, 0.0)); + + for (alpha,beta) in eigenvalues.iter() { + let l_a = a_c.clone() * Complex::new(*beta, 0.0); + let l_b = b_c.clone() * *alpha; + + prop_assert!( + relative_eq!( + (&l_a - &l_b).determinant().modulus(), + 0.0, + epsilon = 1.0e-7)); + } + }; } } From d88337efa95f167a82e9dc4958aceb225647be45 Mon Sep 17 00:00:00 2001 From: metric-space Date: Thu, 3 Feb 2022 06:43:41 -0500 Subject: [PATCH 34/81] Formatting --- nalgebra-lapack/src/qz.rs | 23 ++++++++++++++--------- nalgebra-lapack/tests/linalg/mod.rs | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index ee0e6208..a322f8fa 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -42,11 +42,11 @@ where { alphar: OVector, alphai: OVector, - beta: OVector, - vsl: OMatrix, - s: OMatrix, - vsr: OMatrix, - t: OMatrix, + beta: OVector, + vsl: OMatrix, + s: OMatrix, + vsr: OMatrix, + t: OMatrix, } impl Copy for QZ @@ -183,12 +183,17 @@ where where DefaultAllocator: Allocator<(Complex, T), D>, { - let mut out = Matrix::from_element_generic(self.vsl.shape_generic().0, Const::<1>, (Complex::zero(), T::RealField::zero())); + let mut out = Matrix::from_element_generic( + self.vsl.shape_generic().0, + Const::<1>, + (Complex::zero(), T::RealField::zero()), + ); for i in 0..out.len() { - out[i] = (Complex::new(self.alphar[i].clone(), - self.alphai[i].clone()), - self.beta[i].clone()) + out[i] = ( + Complex::new(self.alphar[i].clone(), self.alphai[i].clone()), + self.beta[i].clone(), + ) } out diff --git a/nalgebra-lapack/tests/linalg/mod.rs b/nalgebra-lapack/tests/linalg/mod.rs index 9fd539c4..251bbe7b 100644 --- a/nalgebra-lapack/tests/linalg/mod.rs +++ b/nalgebra-lapack/tests/linalg/mod.rs @@ -1,8 +1,8 @@ mod cholesky; +mod generalized_eigenvalues; mod lu; mod qr; mod qz; -mod generalized_eigenvalues; mod real_eigensystem; mod schur; mod svd; From 55fdd84e1d526e86846cb1a25badd12619833a8d Mon Sep 17 00:00:00 2001 From: metric-space Date: Thu, 3 Feb 2022 06:45:29 -0500 Subject: [PATCH 35/81] Formatting --- nalgebra-lapack/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nalgebra-lapack/src/lib.rs b/nalgebra-lapack/src/lib.rs index dec4daac..1e6c396f 100644 --- a/nalgebra-lapack/src/lib.rs +++ b/nalgebra-lapack/src/lib.rs @@ -83,11 +83,11 @@ mod lapack_check; mod cholesky; mod eigen; +mod generalized_eigenvalues; mod hessenberg; mod lu; mod qr; mod qz; -mod generalized_eigenvalues; mod schur; mod svd; mod symmetric_eigen; @@ -96,11 +96,11 @@ use num_complex::Complex; pub use self::cholesky::{Cholesky, CholeskyScalar}; pub use self::eigen::Eigen; +pub use self::generalized_eigenvalues::GE; pub use self::hessenberg::Hessenberg; pub use self::lu::{LUScalar, LU}; pub use self::qr::QR; pub use self::qz::QZ; -pub use self::generalized_eigenvalues::GE; pub use self::schur::Schur; pub use self::svd::SVD; pub use self::symmetric_eigen::SymmetricEigen; From 4362f0004cee3acbecb3016a4e4004ac95eedcd8 Mon Sep 17 00:00:00 2001 From: metric-space Date: Fri, 4 Feb 2022 00:09:29 -0500 Subject: [PATCH 36/81] Added comment on logic --- nalgebra-lapack/src/generalized_eigenvalues.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index 5c273e9b..4a2a293f 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -194,6 +194,11 @@ where /// /// u(j)**H * A = lambda(j) * u(j)**H * B . /// where u(j)**H is the conjugate-transpose of u(j). + /// + /// What is going on below? + /// If the j-th and (j+1)-th eigenvalues form a complex conjugate pair, + /// then v_l(j) = VSL(:,j)+i*VSL(:,j+1) and v_l(j+1) = VSL(:,j)-i*VSL(:,j+1). + /// and then v_r(j) = VSR(:,j)+i*VSR(:,j+1) and v_r(j+1) = VSR(:,j)-i*VSR(:,j+1). pub fn eigenvectors(self) -> (OMatrix, D, D>, OMatrix, D, D>) where DefaultAllocator: From 4038e6627aead7e0316e1f733a595b69ca5b76c8 Mon Sep 17 00:00:00 2001 From: metric-space Date: Fri, 4 Feb 2022 00:13:01 -0500 Subject: [PATCH 37/81] Correction to keep naming of left and right eigenvector matrices consistent --- nalgebra-lapack/src/generalized_eigenvalues.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index 4a2a293f..c9fb0e57 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -197,8 +197,8 @@ where /// /// What is going on below? /// If the j-th and (j+1)-th eigenvalues form a complex conjugate pair, - /// then v_l(j) = VSL(:,j)+i*VSL(:,j+1) and v_l(j+1) = VSL(:,j)-i*VSL(:,j+1). - /// and then v_r(j) = VSR(:,j)+i*VSR(:,j+1) and v_r(j+1) = VSR(:,j)-i*VSR(:,j+1). + /// then u(j) = VSL(:,j)+i*VSL(:,j+1) and u(j+1) = VSL(:,j)-i*VSL(:,j+1). + /// and then v(j) = VSR(:,j)+i*VSR(:,j+1) and v(j+1) = VSR(:,j)-i*VSR(:,j+1). pub fn eigenvectors(self) -> (OMatrix, D, D>, OMatrix, D, D>) where DefaultAllocator: From 698e130c3b7bbc69add13b1325001fd45c67b737 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sat, 5 Feb 2022 17:05:59 -0500 Subject: [PATCH 38/81] Remove abomonation support Abomonation has numerous soundness problems which have been well-documented in its issue tracker for over 2 years. Some of them could be fixed, but some are fundamental to its design. If a user wants super-fast ser/de, they should use rkyv. --- .github/workflows/nalgebra-ci-build.yml | 4 +- Cargo.toml | 2 - nalgebra-glm/Cargo.toml | 1 - src/base/array_storage.rs | 31 --------------- src/base/matrix.rs | 20 ---------- src/base/unit.rs | 20 ---------- src/base/vec_storage.rs | 20 ---------- src/geometry/isometry.rs | 28 -------------- src/geometry/point.rs | 25 ------------ src/geometry/quaternion.rs | 23 ----------- src/geometry/rotation.rs | 24 ------------ src/geometry/scale.rs | 24 ------------ src/geometry/similarity.rs | 24 ------------ src/geometry/translation.rs | 24 ------------ tests/core/abomonation.rs | 51 ------------------------- tests/core/mod.rs | 2 - tests/lib.rs | 2 - 17 files changed, 2 insertions(+), 323 deletions(-) delete mode 100644 tests/core/abomonation.rs diff --git a/.github/workflows/nalgebra-ci-build.yml b/.github/workflows/nalgebra-ci-build.yml index b9df85da..bc2f9ca6 100644 --- a/.github/workflows/nalgebra-ci-build.yml +++ b/.github/workflows/nalgebra-ci-build.yml @@ -61,13 +61,13 @@ jobs: steps: - uses: actions/checkout@v2 - name: test - run: cargo test --features arbitrary,rand,serde-serialize,abomonation-serialize,sparse,debug,io,compare,libm,proptest-support,slow-tests; + run: cargo test --features arbitrary,rand,serde-serialize,sparse,debug,io,compare,libm,proptest-support,slow-tests; test-nalgebra-glm: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: test nalgebra-glm - run: cargo test -p nalgebra-glm --features arbitrary,serde-serialize,abomonation-serialize; + run: cargo test -p nalgebra-glm --features arbitrary,serde-serialize; test-nalgebra-sparse: runs-on: ubuntu-latest steps: diff --git a/Cargo.toml b/Cargo.toml index 13f0584c..15a24d41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,6 @@ convert-glam020 = [ "glam020" ] ## `serde-serialize`. serde-serialize-no-std = [ "serde", "num-complex/serde" ] serde-serialize = [ "serde-serialize-no-std", "serde/std" ] -abomonation-serialize = [ "abomonation" ] rkyv-serialize-no-std = [ "rkyv" ] rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std" ] @@ -81,7 +80,6 @@ alga = { version = "0.9", default-features = false, optional = true } rand_distr = { version = "0.4", default-features = false, optional = true } matrixmultiply = { version = "0.3", optional = true } serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true } -abomonation = { version = "0.7", optional = true } rkyv = { version = "~0.6.4", default-features = false, features = ["const_generics"], optional = true } mint = { version = "0.5", optional = true } quickcheck = { version = "1", optional = true } diff --git a/nalgebra-glm/Cargo.toml b/nalgebra-glm/Cargo.toml index 287bb8c7..f8087581 100644 --- a/nalgebra-glm/Cargo.toml +++ b/nalgebra-glm/Cargo.toml @@ -21,7 +21,6 @@ default = [ "std" ] std = [ "nalgebra/std", "simba/std" ] arbitrary = [ "nalgebra/arbitrary" ] serde-serialize = [ "nalgebra/serde-serialize-no-std" ] -abomonation-serialize = [ "nalgebra/abomonation-serialize" ] cuda = [ "nalgebra/cuda" ] # Conversion diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index b46d442f..6851c381 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -1,7 +1,5 @@ use std::fmt::{self, Debug, Formatter}; // use std::hash::{Hash, Hasher}; -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; use std::ops::Mul; #[cfg(feature = "serde-serialize-no-std")] @@ -13,9 +11,6 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "serde-serialize-no-std")] use std::marker::PhantomData; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; - use crate::base::allocator::Allocator; use crate::base::default_allocator::DefaultAllocator; use crate::base::dimension::{Const, ToTypenum}; @@ -282,32 +277,6 @@ unsafe impl by { } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for ArrayStorage -where - T: Scalar + Abomonation, -{ - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - for element in self.as_slice() { - element.entomb(writer)?; - } - - Ok(()) - } - - unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - for element in self.as_mut_slice() { - let temp = bytes; - bytes = element.exhume(temp)? - } - Some(bytes) - } - - fn extent(&self) -> usize { - self.as_slice().iter().fold(0, |acc, e| acc + e.extent()) - } -} - #[cfg(feature = "rkyv-serialize-no-std")] mod rkyv_impl { use super::ArrayStorage; diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 652eace1..bdf3a8c7 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1,6 +1,4 @@ use num::{One, Zero}; -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use std::any::TypeId; @@ -13,9 +11,6 @@ use std::mem; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; - use simba::scalar::{ClosedAdd, ClosedMul, ClosedSub, Field, SupersetOf}; use simba::simd::SimdPartialOrd; @@ -254,21 +249,6 @@ where } } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for Matrix { - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - self.data.entomb(writer) - } - - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.data.exhume(bytes) - } - - fn extent(&self) -> usize { - self.data.extent() - } -} - #[cfg(feature = "compare")] impl> matrixcompare_core::Matrix for Matrix diff --git a/src/base/unit.rs b/src/base/unit.rs index 60281b8f..9336a5e5 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -1,14 +1,9 @@ use std::fmt; -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; use std::ops::Deref; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; - use crate::allocator::Allocator; use crate::base::DefaultAllocator; use crate::storage::RawStorage; @@ -66,21 +61,6 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit { } } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for Unit { - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - self.value.entomb(writer) - } - - fn extent(&self) -> usize { - self.value.extent() - } - - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.value.exhume(bytes) - } -} - #[cfg(feature = "rkyv-serialize-no-std")] mod rkyv_impl { use super::Unit; diff --git a/src/base/vec_storage.rs b/src/base/vec_storage.rs index bf73661d..414354cd 100644 --- a/src/base/vec_storage.rs +++ b/src/base/vec_storage.rs @@ -1,6 +1,3 @@ -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; - #[cfg(all(feature = "alloc", not(feature = "std")))] use alloc::vec::Vec; @@ -18,8 +15,6 @@ use serde::{ }; use crate::Storage; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; use std::mem::MaybeUninit; /* @@ -402,21 +397,6 @@ where } } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for VecStorage { - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - self.data.entomb(writer) - } - - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.data.exhume(bytes) - } - - fn extent(&self) -> usize { - self.data.extent() - } -} - impl Extend for VecStorage { /// Extends the number of columns of the `VecStorage` with elements /// from the given iterator. diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index f020c0e9..8cdd1bfc 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -1,15 +1,10 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use std::fmt; use std::hash; -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Serialize}; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; - use simba::scalar::{RealField, SubsetOf}; use simba::simd::SimdRealField; @@ -81,29 +76,6 @@ pub struct Isometry { pub translation: Translation, } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for Isometry -where - T: SimdRealField, - R: Abomonation, - Translation: Abomonation, -{ - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - self.rotation.entomb(writer)?; - self.translation.entomb(writer) - } - - fn extent(&self) -> usize { - self.rotation.extent() + self.translation.extent() - } - - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.rotation - .exhume(bytes) - .and_then(|bytes| self.translation.exhume(bytes)) - } -} - #[cfg(feature = "rkyv-serialize-no-std")] mod rkyv_impl { use super::Isometry; diff --git a/src/geometry/point.rs b/src/geometry/point.rs index 0953a9ce..b62998c3 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -3,15 +3,10 @@ use num::One; use std::cmp::Ordering; use std::fmt; use std::hash; -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; - use simba::simd::SimdPartialOrd; use crate::base::allocator::Allocator; @@ -130,26 +125,6 @@ where } } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for OPoint -where - T: Scalar, - OVector: Abomonation, - DefaultAllocator: Allocator, -{ - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - self.coords.entomb(writer) - } - - fn extent(&self) -> usize { - self.coords.extent() - } - - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.coords.exhume(bytes) - } -} - impl OPoint where DefaultAllocator: Allocator, diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index cae06251..0aa7f3d3 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -2,17 +2,12 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num::Zero; use std::fmt; use std::hash::{Hash, Hasher}; -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize-no-std")] use crate::base::storage::Owned; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; - use simba::scalar::{ClosedNeg, RealField}; use simba::simd::{SimdBool, SimdOption, SimdRealField}; @@ -77,24 +72,6 @@ where { } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for Quaternion -where - Vector4: Abomonation, -{ - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - self.coords.entomb(writer) - } - - fn extent(&self) -> usize { - self.coords.extent() - } - - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.coords.exhume(bytes) - } -} - #[cfg(feature = "serde-serialize-no-std")] impl Serialize for Quaternion where diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index 2d5e5d24..69c4a355 100755 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -2,8 +2,6 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num::{One, Zero}; use std::fmt; use std::hash; -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -11,9 +9,6 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "serde-serialize-no-std")] use crate::base::storage::Owned; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; - use simba::scalar::RealField; use simba::simd::SimdRealField; @@ -94,25 +89,6 @@ where { } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for Rotation -where - T: Scalar, - SMatrix: Abomonation, -{ - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - self.matrix.entomb(writer) - } - - fn extent(&self) -> usize { - self.matrix.extent() - } - - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.matrix.exhume(bytes) - } -} - #[cfg(feature = "serde-serialize-no-std")] impl Serialize for Rotation where diff --git a/src/geometry/scale.rs b/src/geometry/scale.rs index b1d278d6..064e0075 100755 --- a/src/geometry/scale.rs +++ b/src/geometry/scale.rs @@ -2,15 +2,10 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num::{One, Zero}; use std::fmt; use std::hash; -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; - use crate::base::allocator::Allocator; use crate::base::dimension::{DimNameAdd, DimNameSum, U1}; use crate::base::storage::Owned; @@ -64,25 +59,6 @@ where { } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for Scale -where - T: Scalar, - SVector: Abomonation, -{ - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - self.vector.entomb(writer) - } - - fn extent(&self) -> usize { - self.vector.extent() - } - - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.vector.exhume(bytes) - } -} - #[cfg(feature = "serde-serialize-no-std")] impl Serialize for Scale where diff --git a/src/geometry/similarity.rs b/src/geometry/similarity.rs index fd6b1d36..46c86f5d 100755 --- a/src/geometry/similarity.rs +++ b/src/geometry/similarity.rs @@ -3,15 +3,9 @@ use num::Zero; use std::fmt; use std::hash; -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; - #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Serialize}; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; - use simba::scalar::{RealField, SubsetOf}; use simba::simd::SimdRealField; @@ -49,24 +43,6 @@ pub struct Similarity { scaling: T, } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for Similarity -where - Isometry: Abomonation, -{ - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - self.isometry.entomb(writer) - } - - fn extent(&self) -> usize { - self.isometry.extent() - } - - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.isometry.exhume(bytes) - } -} - impl hash::Hash for Similarity where Owned>: hash::Hash, diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index 1ce8cba3..b07cce20 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -2,15 +2,10 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num::{One, Zero}; use std::fmt; use std::hash; -#[cfg(feature = "abomonation-serialize")] -use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "abomonation-serialize")] -use abomonation::Abomonation; - use simba::scalar::{ClosedAdd, ClosedNeg, ClosedSub}; use crate::base::allocator::Allocator; @@ -64,25 +59,6 @@ where { } -#[cfg(feature = "abomonation-serialize")] -impl Abomonation for Translation -where - T: Scalar, - SVector: Abomonation, -{ - unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { - self.vector.entomb(writer) - } - - fn extent(&self) -> usize { - self.vector.extent() - } - - unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - self.vector.exhume(bytes) - } -} - #[cfg(feature = "serde-serialize-no-std")] impl Serialize for Translation where diff --git a/tests/core/abomonation.rs b/tests/core/abomonation.rs deleted file mode 100644 index 5148db81..00000000 --- a/tests/core/abomonation.rs +++ /dev/null @@ -1,51 +0,0 @@ -use abomonation::{decode, encode, Abomonation}; -use na::{ - DMatrix, Isometry3, IsometryMatrix3, Matrix3x4, Point3, Quaternion, Rotation3, Similarity3, - SimilarityMatrix3, Translation3, -}; -use rand::random; - -#[test] -fn abomonate_dmatrix() { - assert_encode_and_decode(DMatrix::::new_random(3, 5)); -} - -macro_rules! test_abomonation( - ($($test: ident, $ty: ty);* $(;)*) => {$( - #[test] - fn $test() { - assert_encode_and_decode(random::<$ty>()); - } - )*} -); - -test_abomonation! { - abomonate_matrix3x4, Matrix3x4; - abomonate_point3, Point3; - abomonate_translation3, Translation3; - abomonate_rotation3, Rotation3; - abomonate_isometry3, Isometry3; - abomonate_isometry_matrix3, IsometryMatrix3; - abomonate_similarity3, Similarity3; - abomonate_similarity_matrix3, SimilarityMatrix3; - abomonate_quaternion, Quaternion; -} - -fn assert_encode_and_decode(original_data: T) { - // Hold on to a clone for later comparison - let data = original_data.clone(); - - // Encode - let mut bytes = Vec::new(); - unsafe { - let _ = encode(&original_data, &mut bytes); - } - - // Drop the original, so that dangling pointers are revealed by the test - drop(original_data); - - if let Some((result, rest)) = unsafe { decode::(&mut bytes) } { - assert!(result == &data); - assert!(rest.len() == 0, "binary data was not decoded completely"); - } -} diff --git a/tests/core/mod.rs b/tests/core/mod.rs index c03461dc..5fb8c82b 100644 --- a/tests/core/mod.rs +++ b/tests/core/mod.rs @@ -1,5 +1,3 @@ -#[cfg(feature = "abomonation-serialize")] -mod abomonation; mod blas; mod cg; mod conversion; diff --git a/tests/lib.rs b/tests/lib.rs index 11c1e23f..546aa8a7 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -9,8 +9,6 @@ compile_error!( Example: `cargo test --features debug,compare,rand,macros`" ); -#[cfg(feature = "abomonation-serialize")] -extern crate abomonation; #[cfg(all(feature = "debug", feature = "compare", feature = "rand"))] #[macro_use] extern crate approx; From d5069f318eb77f411090eda63c5b053f9dafd69d Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 5 Feb 2022 23:44:05 -0500 Subject: [PATCH 39/81] Removed extra memory allocation for buffer (now redundant) --- .../src/generalized_eigenvalues.rs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index c9fb0e57..f60f9df2 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -205,10 +205,12 @@ where Allocator, D, D> + Allocator, D> + Allocator<(Complex, T), D>, { let n = self.vsl.shape().0; + let mut l = self .vsl .clone() .map(|x| Complex::new(x, T::RealField::zero())); + let mut r = self .vsr .clone() @@ -216,8 +218,8 @@ where let eigenvalues = &self.eigenvalues(); - let mut ll; let mut c = 0; + while c < n { if eigenvalues[c].im.abs() > T::RealField::default_epsilon() && c + 1 < n && { let e_conj = eigenvalues[c].conj(); @@ -225,22 +227,20 @@ where ((e_conj.re - e.re).abs() < T::RealField::default_epsilon()) && ((e_conj.im - e.im).abs() < T::RealField::default_epsilon()) } { - ll = l.column(c + 1).into_owned(); - l.column_mut(c).zip_apply(&ll, |r, i| { - *r = Complex::new(r.re.clone(), i.re); + // taking care of the left eigenvector matrix + l.column_mut(c).zip_apply(&self.vsl.column(c + 1), |r, i| { + *r = Complex::new(r.re.clone(), i.clone()); }); - ll.copy_from(&l.column(c)); - l.column_mut(c + 1).zip_apply(&ll, |r, i| { - *r = i.conj(); + l.column_mut(c + 1).zip_apply(&self.vsl.column(c), |i, r| { + *i = Complex::new(r.clone(), -i.re.clone()); }); - ll.copy_from(&r.column(c + 1)); - r.column_mut(c).zip_apply(&ll, |r, i| { - *r = Complex::new(r.re, i.re); + // taking care of the right eigenvector matrix + r.column_mut(c).zip_apply(&self.vsr.column(c + 1), |r, i| { + *r = Complex::new(r.re.clone(), i.clone()); }); - ll.copy_from(&r.column(c)); - r.column_mut(c + 1).zip_apply(&ll, |r, i| { - *r = i.conj(); + r.column_mut(c + 1).zip_apply(&self.vsr.column(c), |i, r| { + *i = Complex::new(r.clone(), -i.re.clone()); }); c += 2; From 18730dd9868c326cba4afa981527fae5c8541dd3 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sun, 6 Feb 2022 10:03:22 -0500 Subject: [PATCH 40/81] From a4de6a83cca2a0562418e5feeaffef4756468503 Mon Sep 17 00:00:00 2001 From: metric-space Date: Wed, 9 Feb 2022 08:48:38 -0500 Subject: [PATCH 41/81] Corrected deserialization term in serialization impls --- nalgebra-lapack/src/generalized_eigenvalues.rs | 2 +- nalgebra-lapack/src/qz.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index f60f9df2..a14420e6 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -40,7 +40,7 @@ use lapack; feature = "serde-serialize", serde( bound(deserialize = "DefaultAllocator: Allocator + Allocator, - OVector: Serialize, + OVector: Deserialize<'de>, OMatrix: Deserialize<'de>") ) )] diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index a322f8fa..6004d68a 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -31,7 +31,7 @@ use lapack; feature = "serde-serialize", serde( bound(deserialize = "DefaultAllocator: Allocator + Allocator, - OVector: Serialize, + OVector: Deserialize<'de>, OMatrix: Deserialize<'de>") ) )] From 497a4d8bd97b0930919226902949ec0d1cd6a4f7 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 12 Feb 2022 02:26:46 -0500 Subject: [PATCH 42/81] Correction in eigenvector matrices build up algorithm --- .../src/generalized_eigenvalues.rs | 28 +++++-------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index a14420e6..e9057792 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -220,12 +220,13 @@ where let mut c = 0; + let epsilon = T::RealField::default_epsilon(); + while c < n { - if eigenvalues[c].im.abs() > T::RealField::default_epsilon() && c + 1 < n && { + if eigenvalues[c].im.abs() > epsilon && c + 1 < n && { let e_conj = eigenvalues[c].conj(); let e = eigenvalues[c + 1]; - ((e_conj.re - e.re).abs() < T::RealField::default_epsilon()) - && ((e_conj.im - e.im).abs() < T::RealField::default_epsilon()) + (&e_conj.re).ulps_eq(&e.re, epsilon, 6) && (&e_conj.im).ulps_eq(&e.im, epsilon, 6) } { // taking care of the left eigenvector matrix l.column_mut(c).zip_apply(&self.vsl.column(c + 1), |r, i| { @@ -255,7 +256,7 @@ where /// computes the generalized eigenvalues i.e values of lambda that satisfy the following equation /// determinant(A - lambda* B) = 0 #[must_use] - fn eigenvalues(&self) -> OVector, D> + pub fn eigenvalues(&self) -> OVector, D> where DefaultAllocator: Allocator, D>, { @@ -265,23 +266,8 @@ where out[i] = if self.beta[i].clone().abs() < T::RealField::default_epsilon() { Complex::zero() } else { - let mut cr = self.alphar[i].clone(); - let mut ci = self.alphai[i].clone(); - let b = self.beta[i].clone(); - - if cr.clone().abs() < T::RealField::default_epsilon() { - cr = T::RealField::zero() - } else { - cr = cr / b.clone() - }; - - if ci.clone().abs() < T::RealField::default_epsilon() { - ci = T::RealField::zero() - } else { - ci = ci / b - }; - - Complex::new(cr, ci) + Complex::new(self.alphar[i].clone(), self.alphai[i].clone()) + * (Complex::new(self.beta[i].clone(), T::RealField::zero()).inv()) } } From fb0cb513e729ba0c3f6a3ad0a9c9de4d6a2fdc57 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 12 Feb 2022 02:27:29 -0500 Subject: [PATCH 43/81] Remove condition number, tests pass without. Add proper test generator for dynamic f64 type square matrices --- .../tests/linalg/generalized_eigenvalues.rs | 98 +++++++++---------- nalgebra-lapack/tests/linalg/qz.rs | 64 +++--------- 2 files changed, 58 insertions(+), 104 deletions(-) diff --git a/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs b/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs index 8da21b30..8b868fc9 100644 --- a/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs +++ b/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs @@ -1,74 +1,70 @@ -use na::dimension::{Const, Dynamic}; -use na::{DMatrix, EuclideanNorm, Norm, OMatrix}; +use na::dimension::Const; +use na::{DMatrix, OMatrix}; use nl::GE; use num_complex::Complex; use simba::scalar::ComplexField; -use std::cmp; use crate::proptest::*; -use proptest::{prop_assert, proptest}; +use proptest::{prop_assert, prop_compose, proptest}; + +prop_compose! { +fn f64_squares() (n in PROPTEST_MATRIX_DIM) (a in matrix(PROPTEST_F64,n,n), b in matrix(PROPTEST_F64,n,n)) -> (DMatrix, DMatrix){ + (a,b) +}} proptest! { #[test] - fn ge(n in PROPTEST_MATRIX_DIM) { - let n = cmp::max(1, cmp::min(n, 10)); - let a = DMatrix::::new_random(n, n); - let b = DMatrix::::new_random(n, n); - let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a))); - let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b))); + fn ge((a,b) in f64_squares()){ - if a_condition_no.unwrap_or(200000.0) < 5.0 && b_condition_no.unwrap_or(200000.0) < 5.0 { - let a_c = a.clone().map(|x| Complex::new(x, 0.0)); - let b_c = b.clone().map(|x| Complex::new(x, 0.0)); + let a_c = a.clone().map(|x| Complex::new(x, 0.0)); + let b_c = b.clone().map(|x| Complex::new(x, 0.0)); + let n = a.shape_generic().0; - let ge = GE::new(a.clone(), b.clone()); - let (vsl,vsr) = ge.clone().eigenvectors(); + let ge = GE::new(a.clone(), b.clone()); + let (vsl,vsr) = ge.clone().eigenvectors(); - for (i,(alpha,beta)) in ge.raw_eigenvalues().iter().enumerate() { - let l_a = a_c.clone() * Complex::new(*beta, 0.0); - let l_b = b_c.clone() * *alpha; - prop_assert!( - relative_eq!( - ((&l_a - &l_b)*vsr.column(i)).map(|x| x.modulus()), - OMatrix::zeros_generic(Dynamic::new(n), Const::<1>), - epsilon = 1.0e-7)); + for (i,(alpha,beta)) in ge.raw_eigenvalues().iter().enumerate() { + let l_a = a_c.clone() * Complex::new(*beta, 0.0); + let l_b = b_c.clone() * *alpha; - prop_assert!( - relative_eq!( - (vsl.column(i).adjoint()*(&l_a - &l_b)).map(|x| x.modulus()), - OMatrix::zeros_generic(Const::<1>, Dynamic::new(n)), - epsilon = 1.0e-7)) - }; + prop_assert!( + relative_eq!( + ((&l_a - &l_b)*vsr.column(i)).map(|x| x.modulus()), + OMatrix::zeros_generic(n, Const::<1>), + epsilon = 1.0e-5)); + + prop_assert!( + relative_eq!( + (vsl.column(i).adjoint()*(&l_a - &l_b)).map(|x| x.modulus()), + OMatrix::zeros_generic(Const::<1>, n), + epsilon = 1.0e-5)) }; } #[test] fn ge_static(a in matrix4(), b in matrix4()) { - let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a))); - let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b))); - if a_condition_no.unwrap_or(200000.0) < 5.0 && b_condition_no.unwrap_or(200000.0) < 5.0 { - let ge = GE::new(a.clone(), b.clone()); - let a_c =a.clone().map(|x| Complex::new(x, 0.0)); - let b_c = b.clone().map(|x| Complex::new(x, 0.0)); - let (vsl,vsr) = ge.eigenvectors(); - let eigenvalues = ge.raw_eigenvalues(); + let ge = GE::new(a.clone(), b.clone()); + let a_c =a.clone().map(|x| Complex::new(x, 0.0)); + let b_c = b.clone().map(|x| Complex::new(x, 0.0)); + let (vsl,vsr) = ge.eigenvectors(); + let eigenvalues = ge.raw_eigenvalues(); - for (i,(alpha,beta)) in eigenvalues.iter().enumerate() { - let l_a = a_c.clone() * Complex::new(*beta, 0.0); - let l_b = b_c.clone() * *alpha; + for (i,(alpha,beta)) in eigenvalues.iter().enumerate() { + let l_a = a_c.clone() * Complex::new(*beta, 0.0); + let l_b = b_c.clone() * *alpha; - prop_assert!( - relative_eq!( - ((&l_a - &l_b)*vsr.column(i)).map(|x| x.modulus()), - OMatrix::zeros_generic(Const::<4>, Const::<1>), - epsilon = 1.0e-7)); - prop_assert!( - relative_eq!((vsl.column(i).adjoint()*(&l_a - &l_b)).map(|x| x.modulus()), - OMatrix::zeros_generic(Const::<1>, Const::<4>), - epsilon = 1.0e-7)) - } - }; + prop_assert!( + relative_eq!( + ((&l_a - &l_b)*vsr.column(i)).map(|x| x.modulus()), + OMatrix::zeros_generic(Const::<4>, Const::<1>), + epsilon = 1.0e-5)); + prop_assert!( + relative_eq!((vsl.column(i).adjoint()*(&l_a - &l_b)).map(|x| x.modulus()), + OMatrix::zeros_generic(Const::<1>, Const::<4>), + epsilon = 1.0e-5)) + } } + } diff --git a/nalgebra-lapack/tests/linalg/qz.rs b/nalgebra-lapack/tests/linalg/qz.rs index 6f9cf7f8..f70f1c9e 100644 --- a/nalgebra-lapack/tests/linalg/qz.rs +++ b/nalgebra-lapack/tests/linalg/qz.rs @@ -1,74 +1,32 @@ -use na::{DMatrix, EuclideanNorm, Norm}; +use na::DMatrix; use nl::QZ; -use num_complex::Complex; -use simba::scalar::ComplexField; -use std::cmp; use crate::proptest::*; -use proptest::{prop_assert, proptest}; +use proptest::{prop_assert, prop_compose, proptest}; + +prop_compose! { +fn f64_squares() (n in PROPTEST_MATRIX_DIM) (a in matrix(PROPTEST_F64,n,n), b in matrix(PROPTEST_F64,n,n)) -> (DMatrix, DMatrix){ + (a,b) +}} proptest! { #[test] - fn qz(n in PROPTEST_MATRIX_DIM) { - let n = cmp::max(1, cmp::min(n, 10)); - let a = DMatrix::::new_random(n, n); - let b = DMatrix::::new_random(n, n); + fn qz((a,b) in f64_squares()) { - let qz = QZ::new(a.clone(), b.clone()); + let qz = QZ::new(a.clone(), b.clone()); let (vsl,s,t,vsr) = qz.clone().unpack(); - let eigenvalues = qz.raw_eigenvalues(); - prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a.clone(), epsilon = 1.0e-7)); - prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b.clone(), epsilon = 1.0e-7)); + prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, epsilon = 1.0e-7)); + prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b, epsilon = 1.0e-7)); - let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a))); - let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b))); - - if a_condition_no.unwrap_or(200000.0) < 5.0 && b_condition_no.unwrap_or(200000.0) < 5.0 { - let a_c = a.clone().map(|x| Complex::new(x, 0.0)); - let b_c = b.clone().map(|x| Complex::new(x, 0.0)); - - - for (alpha,beta) in eigenvalues.iter() { - let l_a = a_c.clone() * Complex::new(*beta, 0.0); - let l_b = b_c.clone() * *alpha; - - prop_assert!( - relative_eq!( - (&l_a - &l_b).determinant().modulus(), - 0.0, - epsilon = 1.0e-7)); - - }; - }; } #[test] fn qz_static(a in matrix4(), b in matrix4()) { let qz = QZ::new(a.clone(), b.clone()); let (vsl,s,t,vsr) = qz.unpack(); - let eigenvalues = qz.raw_eigenvalues(); prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, epsilon = 1.0e-7)); prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b, epsilon = 1.0e-7)); - - let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a))); - let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b))); - - if a_condition_no.unwrap_or(200000.0) < 5.0 && b_condition_no.unwrap_or(200000.0) < 5.0 { - let a_c =a.clone().map(|x| Complex::new(x, 0.0)); - let b_c = b.clone().map(|x| Complex::new(x, 0.0)); - - for (alpha,beta) in eigenvalues.iter() { - let l_a = a_c.clone() * Complex::new(*beta, 0.0); - let l_b = b_c.clone() * *alpha; - - prop_assert!( - relative_eq!( - (&l_a - &l_b).determinant().modulus(), - 0.0, - epsilon = 1.0e-7)); - } - }; } } From b7fe6b9dc1fbd827c53e4dcf289460e97b91218d Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 12 Feb 2022 02:37:26 -0500 Subject: [PATCH 44/81] Name change --- nalgebra-lapack/src/generalized_eigenvalues.rs | 14 +++++++------- nalgebra-lapack/src/lib.rs | 2 +- .../tests/linalg/generalized_eigenvalues.rs | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index e9057792..467d24da 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -45,7 +45,7 @@ use lapack; ) )] #[derive(Clone, Debug)] -pub struct GE +pub struct GeneralizedEigen where DefaultAllocator: Allocator + Allocator, { @@ -56,7 +56,7 @@ where vsr: OMatrix, } -impl Copy for GE +impl Copy for GeneralizedEigen where DefaultAllocator: Allocator + Allocator, OMatrix: Copy, @@ -64,7 +64,7 @@ where { } -impl GE +impl GeneralizedEigen where DefaultAllocator: Allocator + Allocator, { @@ -170,7 +170,7 @@ where ); lapack_check!(info); - Some(GE { + Some(GeneralizedEigen { alphar, alphai, beta, @@ -300,8 +300,8 @@ where * Lapack functions dispatch. * */ -/// Trait implemented by scalars for which Lapack implements the RealField GE decomposition. -pub trait GEScalar: Scalar { +/// Trait implemented by scalars for which Lapack implements the RealField GeneralizedEigen decomposition. +pub trait GeneralizedEigenScalar: Scalar { #[allow(missing_docs)] fn xggev( jobvsl: u8, @@ -345,7 +345,7 @@ pub trait GEScalar: Scalar { macro_rules! real_eigensystem_scalar_impl ( ($N: ty, $xggev: path) => ( - impl GEScalar for $N { + impl GeneralizedEigenScalar for $N { #[inline] fn xggev(jobvsl: u8, jobvsr: u8, diff --git a/nalgebra-lapack/src/lib.rs b/nalgebra-lapack/src/lib.rs index 1e6c396f..ea2e2b53 100644 --- a/nalgebra-lapack/src/lib.rs +++ b/nalgebra-lapack/src/lib.rs @@ -96,7 +96,7 @@ use num_complex::Complex; pub use self::cholesky::{Cholesky, CholeskyScalar}; pub use self::eigen::Eigen; -pub use self::generalized_eigenvalues::GE; +pub use self::generalized_eigenvalues::GeneralizedEigen; pub use self::hessenberg::Hessenberg; pub use self::lu::{LUScalar, LU}; pub use self::qr::QR; diff --git a/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs b/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs index 8b868fc9..ab678bf3 100644 --- a/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs +++ b/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs @@ -1,6 +1,6 @@ use na::dimension::Const; use na::{DMatrix, OMatrix}; -use nl::GE; +use nl::GeneralizedEigen; use num_complex::Complex; use simba::scalar::ComplexField; @@ -20,7 +20,7 @@ proptest! { let b_c = b.clone().map(|x| Complex::new(x, 0.0)); let n = a.shape_generic().0; - let ge = GE::new(a.clone(), b.clone()); + let ge = GeneralizedEigen::new(a.clone(), b.clone()); let (vsl,vsr) = ge.clone().eigenvectors(); @@ -45,7 +45,7 @@ proptest! { #[test] fn ge_static(a in matrix4(), b in matrix4()) { - let ge = GE::new(a.clone(), b.clone()); + let ge = GeneralizedEigen::new(a.clone(), b.clone()); let a_c =a.clone().map(|x| Complex::new(x, 0.0)); let b_c = b.clone().map(|x| Complex::new(x, 0.0)); let (vsl,vsr) = ge.eigenvectors(); From 91b7e05072f2bf61cfa442c533d24f041952df78 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 12 Feb 2022 02:42:13 -0500 Subject: [PATCH 45/81] Change name of test generator --- nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs | 6 ++++-- nalgebra-lapack/tests/linalg/qz.rs | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs b/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs index ab678bf3..b0d9777c 100644 --- a/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs +++ b/nalgebra-lapack/tests/linalg/generalized_eigenvalues.rs @@ -8,13 +8,15 @@ use crate::proptest::*; use proptest::{prop_assert, prop_compose, proptest}; prop_compose! { -fn f64_squares() (n in PROPTEST_MATRIX_DIM) (a in matrix(PROPTEST_F64,n,n), b in matrix(PROPTEST_F64,n,n)) -> (DMatrix, DMatrix){ + fn f64_dynamic_dim_squares() + (n in PROPTEST_MATRIX_DIM) + (a in matrix(PROPTEST_F64,n,n), b in matrix(PROPTEST_F64,n,n)) -> (DMatrix, DMatrix){ (a,b) }} proptest! { #[test] - fn ge((a,b) in f64_squares()){ + fn ge((a,b) in f64_dynamic_dim_squares()){ let a_c = a.clone().map(|x| Complex::new(x, 0.0)); let b_c = b.clone().map(|x| Complex::new(x, 0.0)); diff --git a/nalgebra-lapack/tests/linalg/qz.rs b/nalgebra-lapack/tests/linalg/qz.rs index f70f1c9e..c7a702ca 100644 --- a/nalgebra-lapack/tests/linalg/qz.rs +++ b/nalgebra-lapack/tests/linalg/qz.rs @@ -5,13 +5,15 @@ use crate::proptest::*; use proptest::{prop_assert, prop_compose, proptest}; prop_compose! { -fn f64_squares() (n in PROPTEST_MATRIX_DIM) (a in matrix(PROPTEST_F64,n,n), b in matrix(PROPTEST_F64,n,n)) -> (DMatrix, DMatrix){ + fn f64_dynamic_dim_squares() + (n in PROPTEST_MATRIX_DIM) + (a in matrix(PROPTEST_F64,n,n), b in matrix(PROPTEST_F64,n,n)) -> (DMatrix, DMatrix){ (a,b) }} proptest! { #[test] - fn qz((a,b) in f64_squares()) { + fn qz((a,b) in f64_dynamic_dim_squares()) { let qz = QZ::new(a.clone(), b.clone()); let (vsl,s,t,vsr) = qz.clone().unpack(); From 7510d48673cbe2a437e3ef95f10cd37559389df4 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 12 Feb 2022 02:52:18 -0500 Subject: [PATCH 46/81] Doc string corrections --- nalgebra-lapack/src/generalized_eigenvalues.rs | 2 +- nalgebra-lapack/src/qz.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index 467d24da..ff58abe4 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -13,7 +13,7 @@ use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar}; use lapack; -/// Generalized eigenvalues and generalized eigenvectors(left and right) of a pair of N*N square matrices. +/// Generalized eigenvalues and generalized eigenvectors (left and right) of a pair of N*N square matrices. /// /// Each generalized eigenvalue (lambda) satisfies determinant(A - lambda*B) = 0 /// diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index 6004d68a..c38af508 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -14,6 +14,7 @@ use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar}; use lapack; /// QZ decomposition of a pair of N*N square matrices. +/// /// Retrieves the left and right matrices of Schur Vectors (VSL and VSR) /// the upper-quasitriangular matrix `S` and upper triangular matrix `T` such that the /// decomposed input matrix `a` equals `VSL * S * VSL.transpose()` and From e52f11700f70b719fbc0c2df76dac36fde5af410 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 12 Feb 2022 02:59:04 -0500 Subject: [PATCH 47/81] Change name of copied macro base --- nalgebra-lapack/src/generalized_eigenvalues.rs | 6 +++--- nalgebra-lapack/src/qz.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index ff58abe4..aede9e07 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -343,7 +343,7 @@ pub trait GeneralizedEigenScalar: Scalar { ) -> i32; } -macro_rules! real_eigensystem_scalar_impl ( +macro_rules! generalized_eigen_scalar_impl ( ($N: ty, $xggev: path) => ( impl GeneralizedEigenScalar for $N { #[inline] @@ -395,5 +395,5 @@ macro_rules! real_eigensystem_scalar_impl ( ) ); -real_eigensystem_scalar_impl!(f32, lapack::sggev); -real_eigensystem_scalar_impl!(f64, lapack::dggev); +generalized_eigen_scalar_impl!(f32, lapack::sggev); +generalized_eigen_scalar_impl!(f64, lapack::dggev); diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index c38af508..17342a2e 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -257,7 +257,7 @@ pub trait QZScalar: Scalar { ) -> i32; } -macro_rules! real_eigensystem_scalar_impl ( +macro_rules! qz_scalar_impl ( ($N: ty, $xgges: path) => ( impl QZScalar for $N { #[inline] @@ -317,5 +317,5 @@ macro_rules! real_eigensystem_scalar_impl ( ) ); -real_eigensystem_scalar_impl!(f32, lapack::sgges); -real_eigensystem_scalar_impl!(f64, lapack::dgges); +qz_scalar_impl!(f32, lapack::sgges); +qz_scalar_impl!(f64, lapack::dgges); From 104cb944b91436b552a7a87095ebdbc17e22d073 Mon Sep 17 00:00:00 2001 From: Wanja Zaeske Date: Mon, 14 Feb 2022 13:55:16 +0100 Subject: [PATCH 48/81] fix #1073: typo in name of macros feature --- nalgebra-macros/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/nalgebra-macros/src/lib.rs b/nalgebra-macros/src/lib.rs index 9a403e0d..4bd791ae 100644 --- a/nalgebra-macros/src/lib.rs +++ b/nalgebra-macros/src/lib.rs @@ -111,7 +111,7 @@ impl Parse for Matrix { /// Construct a fixed-size matrix directly from data. /// -/// **Note: Requires the `macro` feature to be enabled (enabled by default)**. +/// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// This macro facilitates easy construction of matrices when the entries of the matrix are known /// (either as constants or expressions). This macro produces an instance of `SMatrix`. This means @@ -164,7 +164,7 @@ pub fn matrix(stream: TokenStream) -> TokenStream { /// Construct a dynamic matrix directly from data. /// -/// **Note: Requires the `macro` feature to be enabled (enabled by default)**. +/// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// The syntax is exactly the same as for [`matrix!`], but instead of producing instances of /// `SMatrix`, it produces instances of `DMatrix`. At the moment it is not usable @@ -233,7 +233,7 @@ impl Parse for Vector { /// Construct a fixed-size column vector directly from data. /// -/// **Note: Requires the `macro` feature to be enabled (enabled by default)**. +/// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// Similarly to [`matrix!`], this macro facilitates easy construction of fixed-size vectors. /// However, whereas the [`matrix!`] macro expects each row to be separated by a semi-colon, @@ -265,7 +265,7 @@ pub fn vector(stream: TokenStream) -> TokenStream { /// Construct a dynamic column vector directly from data. /// -/// **Note: Requires the `macro` feature to be enabled (enabled by default)**. +/// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// The syntax is exactly the same as for [`vector!`], but instead of producing instances of /// `SVector`, it produces instances of `DVector`. At the moment it is not usable @@ -294,7 +294,7 @@ pub fn dvector(stream: TokenStream) -> TokenStream { /// Construct a fixed-size point directly from data. /// -/// **Note: Requires the `macro` feature to be enabled (enabled by default)**. +/// **Note: Requires the `macros` feature to be enabled (enabled by default)**. /// /// Similarly to [`vector!`], this macro facilitates easy construction of points. /// From 5e10ca46cbbf9e6b5c68b26d4b3570669650db7b Mon Sep 17 00:00:00 2001 From: metric-space Date: Tue, 15 Feb 2022 01:45:33 -0500 Subject: [PATCH 49/81] Add another case for when eigenvalues should be mapped to zero. Make method private --- nalgebra-lapack/src/generalized_eigenvalues.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index aede9e07..dac8004c 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -253,17 +253,21 @@ where (l, r) } - /// computes the generalized eigenvalues i.e values of lambda that satisfy the following equation - /// determinant(A - lambda* B) = 0 - #[must_use] - pub fn eigenvalues(&self) -> OVector, D> + // only used for internal calculation for assembling eigenvectors based on realness of + // eigenvalues and complex-conjugate checks of subsequent non-real eigenvalues + fn eigenvalues(&self) -> OVector, D> where DefaultAllocator: Allocator, D>, { let mut out = Matrix::zeros_generic(self.vsl.shape_generic().0, Const::<1>); + let epsilon = T::RealField::default_epsilon(); + for i in 0..out.len() { - out[i] = if self.beta[i].clone().abs() < T::RealField::default_epsilon() { + out[i] = if self.beta[i].clone().abs() < epsilon + || (self.alphai[i].clone().abs() < epsilon + && self.alphar[i].clone().abs() < epsilon) + { Complex::zero() } else { Complex::new(self.alphar[i].clone(), self.alphai[i].clone()) From c8a920ff2c0a0b433e04e039502eb9d951de742e Mon Sep 17 00:00:00 2001 From: metric-space Date: Sun, 27 Feb 2022 17:17:31 -0500 Subject: [PATCH 50/81] Minimal post-processing and fix to documentation --- .../src/generalized_eigenvalues.rs | 82 ++++++++----------- 1 file changed, 36 insertions(+), 46 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index dac8004c..132be1b7 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -180,25 +180,44 @@ where } /// Calculates the generalized eigenvectors (left and right) associated with the generalized eigenvalues - /// Outputs two matrices, the first one containing the left eigenvectors of the generalized eigenvalues - /// as columns and the second matrix contains the right eigenvectors of the generalized eigenvalues - /// as columns + /// Outputs two matrices. + /// The first output matix contains the left eigenvectors of the generalized eigenvalues + /// as columns. + /// The second matrix contains the right eigenvectors of the generalized eigenvalues + /// as columns. /// - /// The right eigenvector v(j) corresponding to the eigenvalue lambda(j) - /// of (A,B) satisfies + /// The right eigenvector v(j) corresponding to the eigenvalue lambda(j) + /// of (A,B) satisfies /// - /// A * v(j) = lambda(j) * B * v(j). + /// A * v(j) = lambda(j) * B * v(j) /// - /// The left eigenvector u(j) corresponding to the eigenvalue lambda(j) - /// of (A,B) satisfies + /// The left eigenvector u(j) corresponding to the eigenvalue lambda(j) + /// of (A,B) satisfies /// - /// u(j)**H * A = lambda(j) * u(j)**H * B . - /// where u(j)**H is the conjugate-transpose of u(j). + /// u(j)**H * A = lambda(j) * u(j)**H * B + /// where u(j)**H is the conjugate-transpose of u(j). + /// + /// How the eigenvectors are build up: + /// + /// Since the input entries are all real, the generalized eigenvalues if complex come in pairs + /// as a consequence of + /// The Lapack routine output reflects this by expecting the user to unpack the complex eigenvalues associated + /// eigenvectors from the real matrix output via the following procedure + /// + /// (Note: VL stands for the lapack real matrix output containing the left eigenvectors as columns, + /// VR stands for the lapack real matrix output containing the right eigenvectors as columns) + /// + /// If the j-th and (j+1)-th eigenvalues form a complex conjugate pair, + /// then + /// + /// u(j) = VL(:,j)+i*VL(:,j+1) + /// u(j+1) = VL(:,j)-i*VL(:,j+1) + /// + /// and + /// + /// u(j) = VR(:,j)+i*VR(:,j+1) + /// v(j+1) = VR(:,j)-i*VR(:,j+1). /// - /// What is going on below? - /// If the j-th and (j+1)-th eigenvalues form a complex conjugate pair, - /// then u(j) = VSL(:,j)+i*VSL(:,j+1) and u(j+1) = VSL(:,j)-i*VSL(:,j+1). - /// and then v(j) = VSR(:,j)+i*VSR(:,j+1) and v(j+1) = VSR(:,j)-i*VSR(:,j+1). pub fn eigenvectors(self) -> (OMatrix, D, D>, OMatrix, D, D>) where DefaultAllocator: @@ -216,18 +235,14 @@ where .clone() .map(|x| Complex::new(x, T::RealField::zero())); - let eigenvalues = &self.eigenvalues(); + let eigenvalues = &self.raw_eigenvalues(); let mut c = 0; let epsilon = T::RealField::default_epsilon(); while c < n { - if eigenvalues[c].im.abs() > epsilon && c + 1 < n && { - let e_conj = eigenvalues[c].conj(); - let e = eigenvalues[c + 1]; - (&e_conj.re).ulps_eq(&e.re, epsilon, 6) && (&e_conj.im).ulps_eq(&e.im, epsilon, 6) - } { + if eigenvalues[c].0.im.abs() > epsilon && c + 1 < n { // taking care of the left eigenvector matrix l.column_mut(c).zip_apply(&self.vsl.column(c + 1), |r, i| { *r = Complex::new(r.re.clone(), i.clone()); @@ -253,32 +268,7 @@ where (l, r) } - // only used for internal calculation for assembling eigenvectors based on realness of - // eigenvalues and complex-conjugate checks of subsequent non-real eigenvalues - fn eigenvalues(&self) -> OVector, D> - where - DefaultAllocator: Allocator, D>, - { - let mut out = Matrix::zeros_generic(self.vsl.shape_generic().0, Const::<1>); - - let epsilon = T::RealField::default_epsilon(); - - for i in 0..out.len() { - out[i] = if self.beta[i].clone().abs() < epsilon - || (self.alphai[i].clone().abs() < epsilon - && self.alphar[i].clone().abs() < epsilon) - { - Complex::zero() - } else { - Complex::new(self.alphar[i].clone(), self.alphai[i].clone()) - * (Complex::new(self.beta[i].clone(), T::RealField::zero()).inv()) - } - } - - out - } - - /// outputs the unprocessed (almost) version of generalized eigenvalues ((alphar, alpai), beta) + /// outputs the unprocessed (almost) version of generalized eigenvalues ((alphar, alphai), beta) /// straight from LAPACK #[must_use] pub fn raw_eigenvalues(&self) -> OVector<(Complex, T), D> From 757b99e8439c15fa9901c7ffb695da4511a2e60f Mon Sep 17 00:00:00 2001 From: Anton Arsenij <11193727+aarsenij@users.noreply.github.com> Date: Thu, 3 Mar 2022 10:14:16 +0100 Subject: [PATCH 51/81] CSC: Create constructor for unsorted but otherwise valid data (#1015) * CSC: Create constructor for unsorted but otherwise valid data * Test creating csc matrix from unsorted but valid data * Add function for validation and sorting * Move validation function to cs.rs * Restore pattern unit test * Add unit test for 'major offset out of bounds' case * Avoid permutation allocations on 'happy path' * Reuse allocated permutation * Fix comments for test-data examples * Remove unnecessary iter variable * Set up buffers for sorting up front * Use common apply_permutation function * Use common compute_sort_permutation function * Move unsafe down to unchecked call * Add panic cases to documentation * Remove unnecessary Zero bound * Move buffer set up away from loop * Lift T::Zero from cs.rs * Improve checking if values are provided * Simplify copying from slices & add test for wrong values length * Check duplicates after sorting * Fix formatting * Check values length at the beginning * Check length of values if values != None --- nalgebra-sparse/src/convert/serial.rs | 20 +-- nalgebra-sparse/src/cs.rs | 151 +++++++++++++++- nalgebra-sparse/src/csc.rs | 45 +++++ nalgebra-sparse/src/csr.rs | 74 +++----- nalgebra-sparse/src/lib.rs | 1 + nalgebra-sparse/src/pattern.rs | 29 ++++ nalgebra-sparse/src/utils.rs | 26 +++ nalgebra-sparse/tests/unit_tests/csc.rs | 164 +++++++++++++++--- nalgebra-sparse/tests/unit_tests/csr.rs | 58 +++++-- .../tests/unit_tests/test_data_examples.rs | 44 ++++- 10 files changed, 497 insertions(+), 115 deletions(-) create mode 100644 nalgebra-sparse/src/utils.rs diff --git a/nalgebra-sparse/src/convert/serial.rs b/nalgebra-sparse/src/convert/serial.rs index ecbe1dab..50fc50e4 100644 --- a/nalgebra-sparse/src/convert/serial.rs +++ b/nalgebra-sparse/src/convert/serial.rs @@ -14,6 +14,7 @@ use crate::coo::CooMatrix; use crate::cs; use crate::csc::CscMatrix; use crate::csr::CsrMatrix; +use crate::utils::{apply_permutation, compute_sort_permutation}; /// Converts a dense matrix to [`CooMatrix`]. pub fn convert_dense_coo(dense: &Matrix) -> CooMatrix @@ -376,29 +377,12 @@ fn sort_lane( assert_eq!(values.len(), workspace.len()); let permutation = workspace; - // Set permutation to identity - for (i, p) in permutation.iter_mut().enumerate() { - *p = i; - } - - // Compute permutation needed to bring minor indices into sorted order - // Note: Using sort_unstable here avoids internal allocations, which is crucial since - // each lane might have a small number of elements - permutation.sort_unstable_by_key(|idx| minor_idx[*idx]); + compute_sort_permutation(permutation, minor_idx); apply_permutation(minor_idx_result, minor_idx, permutation); apply_permutation(values_result, values, permutation); } -// TODO: Move this into `utils` or something? -fn apply_permutation(out_slice: &mut [T], in_slice: &[T], permutation: &[usize]) { - assert_eq!(out_slice.len(), in_slice.len()); - assert_eq!(out_slice.len(), permutation.len()); - for (out_element, old_pos) in out_slice.iter_mut().zip(permutation) { - *out_element = in_slice[*old_pos].clone(); - } -} - /// Given *sorted* indices and corresponding scalar values, combines duplicates with the given /// associative combiner and calls the provided produce methods with combined indices and values. fn combine_duplicates( diff --git a/nalgebra-sparse/src/cs.rs b/nalgebra-sparse/src/cs.rs index cffdd6c7..474eb2c0 100644 --- a/nalgebra-sparse/src/cs.rs +++ b/nalgebra-sparse/src/cs.rs @@ -6,7 +6,8 @@ use num_traits::One; use nalgebra::Scalar; use crate::pattern::SparsityPattern; -use crate::{SparseEntry, SparseEntryMut}; +use crate::utils::{apply_permutation, compute_sort_permutation}; +use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind}; /// An abstract compressed matrix. /// @@ -543,3 +544,151 @@ pub fn convert_counts_to_offsets(counts: &mut [usize]) { offset += count; } } + +/// Validates cs data, optionally sorts minor indices and values +pub(crate) fn validate_and_optionally_sort_cs_data( + major_dim: usize, + minor_dim: usize, + major_offsets: &[usize], + minor_indices: &mut [usize], + values: Option<&mut [T]>, + sort: bool, +) -> Result<(), SparseFormatError> +where + T: Scalar, +{ + let mut values_option = values; + + if let Some(values) = values_option.as_mut() { + if minor_indices.len() != values.len() { + return Err(SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::InvalidStructure, + "Number of values and minor indices must be the same.", + )); + } + } else if sort { + unreachable!("Internal error: Sorting currently not supported if no values are present."); + } + if major_offsets.len() == 0 { + return Err(SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::InvalidStructure, + "Number of offsets should be greater than 0.", + )); + } + if major_offsets.len() != major_dim + 1 { + return Err(SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::InvalidStructure, + "Length of offset array is not equal to (major_dim + 1).", + )); + } + + // Check that the first and last offsets conform to the specification + { + let first_offset_ok = *major_offsets.first().unwrap() == 0; + let last_offset_ok = *major_offsets.last().unwrap() == minor_indices.len(); + if !first_offset_ok || !last_offset_ok { + return Err(SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::InvalidStructure, + "First or last offset is incompatible with format.", + )); + } + } + + // Set up required buffers up front + let mut minor_idx_buffer: Vec = Vec::new(); + let mut values_buffer: Vec = Vec::new(); + let mut minor_index_permutation: Vec = Vec::new(); + + // Test that each lane has strictly monotonically increasing minor indices, i.e. + // minor indices within a lane are sorted, unique. Sort minor indices within a lane if needed. + // In addition, each minor index must be in bounds with respect to the minor dimension. + { + for lane_idx in 0..major_dim { + let range_start = major_offsets[lane_idx]; + let range_end = major_offsets[lane_idx + 1]; + + // Test that major offsets are monotonically increasing + if range_start > range_end { + return Err(SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::InvalidStructure, + "Offsets are not monotonically increasing.", + )); + } + + let minor_idx_in_lane = minor_indices.get(range_start..range_end).ok_or( + SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::IndexOutOfBounds, + "A major offset is out of bounds.", + ), + )?; + + // We test for in-bounds, uniqueness and monotonicity at the same time + // to ensure that we only visit each minor index once + let mut prev = None; + let mut monotonic = true; + + for &minor_idx in minor_idx_in_lane { + if minor_idx >= minor_dim { + return Err(SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::IndexOutOfBounds, + "A minor index is out of bounds.", + )); + } + + if let Some(prev) = prev { + if prev >= minor_idx { + if !sort { + return Err(SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::InvalidStructure, + "Minor indices are not strictly monotonically increasing in each lane.", + )); + } + monotonic = false; + } + } + prev = Some(minor_idx); + } + + // sort if indices are not monotonic and sorting is expected + if !monotonic && sort { + let range_size = range_end - range_start; + minor_index_permutation.resize(range_size, 0); + compute_sort_permutation(&mut minor_index_permutation, &minor_idx_in_lane); + minor_idx_buffer.clear(); + minor_idx_buffer.extend_from_slice(&minor_idx_in_lane); + apply_permutation( + &mut minor_indices[range_start..range_end], + &minor_idx_buffer, + &minor_index_permutation, + ); + + // check duplicates + prev = None; + for &minor_idx in &minor_indices[range_start..range_end] { + if let Some(prev) = prev { + if prev == minor_idx { + return Err(SparseFormatError::from_kind_and_msg( + SparseFormatErrorKind::DuplicateEntry, + "Input data contains duplicate entries.", + )); + } + } + prev = Some(minor_idx); + } + + // sort values if they exist + if let Some(values) = values_option.as_mut() { + values_buffer.clear(); + values_buffer.extend_from_slice(&values[range_start..range_end]); + apply_permutation( + &mut values[range_start..range_end], + &values_buffer, + &minor_index_permutation, + ); + } + } + } + } + + Ok(()) +} diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index a461658e..d926dafb 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -6,6 +6,7 @@ #[cfg(feature = "serde-serialize")] mod csc_serde; +use crate::cs; use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; use crate::csr::CsrMatrix; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; @@ -173,6 +174,50 @@ impl CscMatrix { Self::try_from_pattern_and_values(pattern, values) } + /// Try to construct a CSC matrix from raw CSC data with unsorted row indices. + /// + /// It is assumed that each column contains unique row indices that are in + /// bounds with respect to the number of rows in the matrix. If this is not the case, + /// an error is returned to indicate the failure. + /// + /// An error is returned if the data given does not conform to the CSC storage format + /// with the exception of having unsorted row indices and values. + /// See the documentation for [CscMatrix](struct.CscMatrix.html) for more information. + pub fn try_from_unsorted_csc_data( + num_rows: usize, + num_cols: usize, + col_offsets: Vec, + mut row_indices: Vec, + mut values: Vec, + ) -> Result + where + T: Scalar, + { + let result = cs::validate_and_optionally_sort_cs_data( + num_cols, + num_rows, + &col_offsets, + &mut row_indices, + Some(&mut values), + true, + ); + + match result { + Ok(()) => { + let pattern = unsafe { + SparsityPattern::from_offset_and_indices_unchecked( + num_cols, + num_rows, + col_offsets, + row_indices, + ) + }; + Self::try_from_pattern_and_values(pattern, values) + } + Err(err) => Err(err), + } + } + /// Try to construct a CSC matrix from a sparsity pattern and associated non-zero values. /// /// Returns an error if the number of values does not match the number of minor indices diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index bd35bf6b..90be35f1 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -6,6 +6,7 @@ #[cfg(feature = "serde-serialize")] mod csr_serde; +use crate::cs; use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; use crate::csc::CscMatrix; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; @@ -13,7 +14,7 @@ use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKin use nalgebra::Scalar; use num_traits::One; -use std::iter::FromIterator; + use std::slice::{Iter, IterMut}; /// A CSR representation of a sparse matrix. @@ -187,62 +188,35 @@ impl CsrMatrix { num_rows: usize, num_cols: usize, row_offsets: Vec, - col_indices: Vec, - values: Vec, + mut col_indices: Vec, + mut values: Vec, ) -> Result where T: Scalar, { - use SparsityPatternFormatError::*; - let count = col_indices.len(); - let mut p: Vec = (0..count).collect(); - - if col_indices.len() != values.len() { - return Err(SparseFormatError::from_kind_and_msg( - SparseFormatErrorKind::InvalidStructure, - "Number of values and column indices must be the same", - )); - } - - if row_offsets.len() == 0 { - return Err(SparseFormatError::from_kind_and_msg( - SparseFormatErrorKind::InvalidStructure, - "Number of offsets should be greater than 0", - )); - } - - for (index, &offset) in row_offsets[0..row_offsets.len() - 1].iter().enumerate() { - let next_offset = row_offsets[index + 1]; - if next_offset > count { - return Err(SparseFormatError::from_kind_and_msg( - SparseFormatErrorKind::InvalidStructure, - "No row offset should be greater than the number of column indices", - )); - } - if offset > next_offset { - return Err(NonmonotonicOffsets).map_err(pattern_format_error_to_csr_error); - } - p[offset..next_offset].sort_by(|a, b| { - let x = &col_indices[*a]; - let y = &col_indices[*b]; - x.partial_cmp(y).unwrap() - }); - } - - // permute indices - let sorted_col_indices: Vec = - Vec::from_iter((p.iter().map(|i| &col_indices[*i])).cloned()); - - // permute values - let sorted_values: Vec = Vec::from_iter((p.iter().map(|i| &values[*i])).cloned()); - - return Self::try_from_csr_data( + let result = cs::validate_and_optionally_sort_cs_data( num_rows, num_cols, - row_offsets, - sorted_col_indices, - sorted_values, + &row_offsets, + &mut col_indices, + Some(&mut values), + true, ); + + match result { + Ok(()) => { + let pattern = unsafe { + SparsityPattern::from_offset_and_indices_unchecked( + num_rows, + num_cols, + row_offsets, + col_indices, + ) + }; + Self::try_from_pattern_and_values(pattern, values) + } + Err(err) => Err(err), + } } /// Try to construct a CSR matrix from a sparsity pattern and associated non-zero values. diff --git a/nalgebra-sparse/src/lib.rs b/nalgebra-sparse/src/lib.rs index edbf83bd..8567261a 100644 --- a/nalgebra-sparse/src/lib.rs +++ b/nalgebra-sparse/src/lib.rs @@ -160,6 +160,7 @@ pub mod ops; pub mod pattern; pub(crate) mod cs; +pub(crate) mod utils; #[cfg(feature = "proptest-support")] pub mod proptest; diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 50fae072..c51945b7 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -188,6 +188,35 @@ impl SparsityPattern { }) } + /// Try to construct a sparsity pattern from the given dimensions, major offsets + /// and minor indices. + /// + /// # Panics + /// + /// Panics if the number of major offsets is not exactly one greater than the major dimension + /// or if major offsets do not start with 0 and end with the number of minor indices. + pub unsafe fn from_offset_and_indices_unchecked( + major_dim: usize, + minor_dim: usize, + major_offsets: Vec, + minor_indices: Vec, + ) -> Self { + assert_eq!(major_offsets.len(), major_dim + 1); + + // Check that the first and last offsets conform to the specification + { + let first_offset_ok = *major_offsets.first().unwrap() == 0; + let last_offset_ok = *major_offsets.last().unwrap() == minor_indices.len(); + assert!(first_offset_ok && last_offset_ok); + } + + Self { + major_offsets, + minor_indices, + minor_dim, + } + } + /// An iterator over the explicitly stored "non-zero" entries (i, j). /// /// The iteration happens in a lane-major fashion, meaning that the lane index i diff --git a/nalgebra-sparse/src/utils.rs b/nalgebra-sparse/src/utils.rs new file mode 100644 index 00000000..73d4e967 --- /dev/null +++ b/nalgebra-sparse/src/utils.rs @@ -0,0 +1,26 @@ +//! Helper functions for sparse matrix computations + +/// permutes entries of in_slice according to permutation slice and puts them to out_slice +#[inline] +pub fn apply_permutation(out_slice: &mut [T], in_slice: &[T], permutation: &[usize]) { + assert_eq!(out_slice.len(), in_slice.len()); + assert_eq!(out_slice.len(), permutation.len()); + for (out_element, old_pos) in out_slice.iter_mut().zip(permutation) { + *out_element = in_slice[*old_pos].clone(); + } +} + +/// computes permutation by using provided indices as keys +#[inline] +pub fn compute_sort_permutation(permutation: &mut [usize], indices: &[usize]) { + assert_eq!(permutation.len(), indices.len()); + // Set permutation to identity + for (i, p) in permutation.iter_mut().enumerate() { + *p = i; + } + + // Compute permutation needed to bring minor indices into sorted order + // Note: Using sort_unstable here avoids internal allocations, which is crucial since + // each lane might have a small number of elements + permutation.sort_unstable_by_key(|idx| indices[*idx]); +} diff --git a/nalgebra-sparse/tests/unit_tests/csc.rs b/nalgebra-sparse/tests/unit_tests/csc.rs index 7fb0de54..1554b8a6 100644 --- a/nalgebra-sparse/tests/unit_tests/csc.rs +++ b/nalgebra-sparse/tests/unit_tests/csc.rs @@ -5,6 +5,8 @@ use nalgebra_sparse::{SparseEntry, SparseEntryMut, SparseFormatErrorKind}; use proptest::prelude::*; use proptest::sample::subsequence; +use super::test_data_examples::{InvalidCsDataExamples, ValidCsDataExamples}; + use crate::assert_panics; use crate::common::csc_strategy; @@ -171,11 +173,26 @@ fn csc_matrix_valid_data() { } } +#[test] +fn csc_matrix_valid_data_unsorted_column_indices() { + let valid_data: ValidCsDataExamples = ValidCsDataExamples::new(); + + let (offsets, indices, values) = valid_data.valid_unsorted_cs_data; + let csc = CscMatrix::try_from_unsorted_csc_data(5, 4, offsets, indices, values).unwrap(); + + let (offsets2, indices2, values2) = valid_data.valid_cs_data; + let expected_csc = CscMatrix::try_from_csc_data(5, 4, offsets2, indices2, values2).unwrap(); + + assert_eq!(csc, expected_csc); +} + #[test] fn csc_matrix_try_from_invalid_csc_data() { + let invalid_data: InvalidCsDataExamples = InvalidCsDataExamples::new(); { // Empty offset array (invalid length) - let matrix = CscMatrix::try_from_csc_data(0, 0, Vec::new(), Vec::new(), Vec::::new()); + let (offsets, indices, values) = invalid_data.empty_offset_array; + let matrix = CscMatrix::try_from_csc_data(0, 0, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), &SparseFormatErrorKind::InvalidStructure @@ -184,10 +201,8 @@ fn csc_matrix_try_from_invalid_csc_data() { { // Offset array invalid length for arbitrary data - let offsets = vec![0, 3, 5]; - let indices = vec![0, 1, 2, 3, 5]; - let values = vec![0, 1, 2, 3, 4]; - + let (offsets, indices, values) = + invalid_data.offset_array_invalid_length_for_arbitrary_data; let matrix = CscMatrix::try_from_csc_data(6, 3, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -197,9 +212,7 @@ fn csc_matrix_try_from_invalid_csc_data() { { // Invalid first entry in offsets array - let offsets = vec![1, 2, 2, 5]; - let indices = vec![0, 5, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.invalid_first_entry_in_offsets_array; let matrix = CscMatrix::try_from_csc_data(6, 3, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -209,9 +222,7 @@ fn csc_matrix_try_from_invalid_csc_data() { { // Invalid last entry in offsets array - let offsets = vec![0, 2, 2, 4]; - let indices = vec![0, 5, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.invalid_last_entry_in_offsets_array; let matrix = CscMatrix::try_from_csc_data(6, 3, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -221,9 +232,7 @@ fn csc_matrix_try_from_invalid_csc_data() { { // Invalid length of offsets array - let offsets = vec![0, 2, 2]; - let indices = vec![0, 5, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.invalid_length_of_offsets_array; let matrix = CscMatrix::try_from_csc_data(6, 3, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -233,9 +242,7 @@ fn csc_matrix_try_from_invalid_csc_data() { { // Nonmonotonic offsets - let offsets = vec![0, 3, 2, 5]; - let indices = vec![0, 1, 2, 3, 4]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.nonmonotonic_offsets; let matrix = CscMatrix::try_from_csc_data(6, 3, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -257,9 +264,7 @@ fn csc_matrix_try_from_invalid_csc_data() { { // Minor index out of bounds - let offsets = vec![0, 2, 2, 5]; - let indices = vec![0, 6, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.minor_index_out_of_bounds; let matrix = CscMatrix::try_from_csc_data(6, 3, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -269,9 +274,7 @@ fn csc_matrix_try_from_invalid_csc_data() { { // Duplicate entry - let offsets = vec![0, 2, 2, 5]; - let indices = vec![0, 5, 2, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.duplicate_entry; let matrix = CscMatrix::try_from_csc_data(6, 3, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -280,6 +283,121 @@ fn csc_matrix_try_from_invalid_csc_data() { } } +#[test] +fn csc_matrix_try_from_unsorted_invalid_csc_data() { + let invalid_data: InvalidCsDataExamples = InvalidCsDataExamples::new(); + { + // Empty offset array (invalid length) + let (offsets, indices, values) = invalid_data.empty_offset_array; + let matrix = CscMatrix::try_from_unsorted_csc_data(0, 0, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::InvalidStructure + ); + } + + { + // Offset array invalid length for arbitrary data + let (offsets, indices, values) = + invalid_data.offset_array_invalid_length_for_arbitrary_data; + let matrix = CscMatrix::try_from_unsorted_csc_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::InvalidStructure + ); + } + + { + // Invalid first entry in offsets array + let (offsets, indices, values) = invalid_data.invalid_first_entry_in_offsets_array; + let matrix = CscMatrix::try_from_unsorted_csc_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::InvalidStructure + ); + } + + { + // Invalid last entry in offsets array + let (offsets, indices, values) = invalid_data.invalid_last_entry_in_offsets_array; + let matrix = CscMatrix::try_from_unsorted_csc_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::InvalidStructure + ); + } + + { + // Invalid length of offsets array + let (offsets, indices, values) = invalid_data.invalid_length_of_offsets_array; + let matrix = CscMatrix::try_from_unsorted_csc_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::InvalidStructure + ); + } + + { + // Nonmonotonic offsets + let (offsets, indices, values) = invalid_data.nonmonotonic_offsets; + let matrix = CscMatrix::try_from_unsorted_csc_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::InvalidStructure + ); + } + + { + // Major offset out of bounds + let (offsets, indices, values) = invalid_data.major_offset_out_of_bounds; + let matrix = CscMatrix::try_from_unsorted_csc_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::IndexOutOfBounds + ); + } + + { + // Minor index out of bounds + let (offsets, indices, values) = invalid_data.minor_index_out_of_bounds; + let matrix = CscMatrix::try_from_unsorted_csc_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::IndexOutOfBounds + ); + } + + { + // Duplicate entry + let (offsets, indices, values) = invalid_data.duplicate_entry; + let matrix = CscMatrix::try_from_unsorted_csc_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::DuplicateEntry + ); + } + + { + // Duplicate entry in unsorted lane + let (offsets, indices, values) = invalid_data.duplicate_entry_unsorted; + let matrix = CscMatrix::try_from_unsorted_csc_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::DuplicateEntry + ); + } + + { + // Wrong values length + let (offsets, indices, values) = invalid_data.wrong_values_length; + let matrix = CscMatrix::try_from_unsorted_csc_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::InvalidStructure + ); + } +} + #[test] fn csc_disassemble_avoids_clone_when_owned() { // Test that disassemble avoids cloning the sparsity pattern when it holds the sole reference diff --git a/nalgebra-sparse/tests/unit_tests/csr.rs b/nalgebra-sparse/tests/unit_tests/csr.rs index 3ca2f0dc..a00470d5 100644 --- a/nalgebra-sparse/tests/unit_tests/csr.rs +++ b/nalgebra-sparse/tests/unit_tests/csr.rs @@ -5,7 +5,7 @@ use nalgebra_sparse::{SparseEntry, SparseEntryMut, SparseFormatErrorKind}; use proptest::prelude::*; use proptest::sample::subsequence; -use super::test_data_examples::InvalidCsrDataExamples; +use super::test_data_examples::{InvalidCsDataExamples, ValidCsDataExamples}; use crate::assert_panics; use crate::common::csr_strategy; @@ -175,30 +175,20 @@ fn csr_matrix_valid_data() { #[test] fn csr_matrix_valid_data_unsorted_column_indices() { - let csr = CsrMatrix::try_from_unsorted_csr_data( - 4, - 5, - vec![0, 3, 5, 8, 11], - vec![4, 1, 3, 3, 1, 2, 3, 0, 3, 4, 1], - vec![5, 1, 4, 7, 4, 2, 3, 1, 8, 9, 6], - ) - .unwrap(); + let valid_data: ValidCsDataExamples = ValidCsDataExamples::new(); - let expected_csr = CsrMatrix::try_from_csr_data( - 4, - 5, - vec![0, 3, 5, 8, 11], - vec![1, 3, 4, 1, 3, 0, 2, 3, 1, 3, 4], - vec![1, 4, 5, 4, 7, 1, 2, 3, 6, 8, 9], - ) - .unwrap(); + let (offsets, indices, values) = valid_data.valid_unsorted_cs_data; + let csr = CsrMatrix::try_from_unsorted_csr_data(4, 5, offsets, indices, values).unwrap(); + + let (offsets2, indices2, values2) = valid_data.valid_cs_data; + let expected_csr = CsrMatrix::try_from_csr_data(4, 5, offsets2, indices2, values2).unwrap(); assert_eq!(csr, expected_csr); } #[test] fn csr_matrix_try_from_invalid_csr_data() { - let invalid_data: InvalidCsrDataExamples = InvalidCsrDataExamples::new(); + let invalid_data: InvalidCsDataExamples = InvalidCsDataExamples::new(); { // Empty offset array (invalid length) let (offsets, indices, values) = invalid_data.empty_offset_array; @@ -293,7 +283,7 @@ fn csr_matrix_try_from_invalid_csr_data() { #[test] fn csr_matrix_try_from_unsorted_invalid_csr_data() { - let invalid_data: InvalidCsrDataExamples = InvalidCsrDataExamples::new(); + let invalid_data: InvalidCsDataExamples = InvalidCsDataExamples::new(); { // Empty offset array (invalid length) let (offsets, indices, values) = invalid_data.empty_offset_array; @@ -355,6 +345,16 @@ fn csr_matrix_try_from_unsorted_invalid_csr_data() { ); } + { + // Major offset out of bounds + let (offsets, indices, values) = invalid_data.major_offset_out_of_bounds; + let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::IndexOutOfBounds + ); + } + { // Minor index out of bounds let (offsets, indices, values) = invalid_data.minor_index_out_of_bounds; @@ -374,6 +374,26 @@ fn csr_matrix_try_from_unsorted_invalid_csr_data() { &SparseFormatErrorKind::DuplicateEntry ); } + + { + // Duplicate entry in unsorted lane + let (offsets, indices, values) = invalid_data.duplicate_entry_unsorted; + let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::DuplicateEntry + ); + } + + { + // Wrong values length + let (offsets, indices, values) = invalid_data.wrong_values_length; + let matrix = CsrMatrix::try_from_unsorted_csr_data(6, 3, offsets, indices, values); + assert_eq!( + matrix.unwrap_err().kind(), + &SparseFormatErrorKind::InvalidStructure + ); + } } #[test] diff --git a/nalgebra-sparse/tests/unit_tests/test_data_examples.rs b/nalgebra-sparse/tests/unit_tests/test_data_examples.rs index 20721087..a80b5064 100644 --- a/nalgebra-sparse/tests/unit_tests/test_data_examples.rs +++ b/nalgebra-sparse/tests/unit_tests/test_data_examples.rs @@ -1,5 +1,31 @@ -/// Examples of *invalid* raw CSR data `(offsets, indices, values)`. -pub struct InvalidCsrDataExamples { +/// Examples of *valid* raw CS data `(offsets, indices, values)`. +pub struct ValidCsDataExamples { + pub valid_cs_data: (Vec, Vec, Vec), + pub valid_unsorted_cs_data: (Vec, Vec, Vec), +} + +impl ValidCsDataExamples { + pub fn new() -> Self { + let valid_cs_data = ( + vec![0, 3, 5, 8, 11], + vec![1, 3, 4, 1, 3, 0, 2, 3, 1, 3, 4], + vec![1, 4, 5, 4, 7, 1, 2, 3, 6, 8, 9], + ); + let valid_unsorted_cs_data = ( + vec![0, 3, 5, 8, 11], + vec![4, 1, 3, 3, 1, 2, 3, 0, 3, 4, 1], + vec![5, 1, 4, 7, 4, 2, 3, 1, 8, 9, 6], + ); + + return Self { + valid_cs_data, + valid_unsorted_cs_data, + }; + } +} + +/// Examples of *invalid* raw CS data `(offsets, indices, values)`. +pub struct InvalidCsDataExamples { pub empty_offset_array: (Vec, Vec, Vec), pub offset_array_invalid_length_for_arbitrary_data: (Vec, Vec, Vec), pub invalid_first_entry_in_offsets_array: (Vec, Vec, Vec), @@ -7,11 +33,14 @@ pub struct InvalidCsrDataExamples { pub invalid_length_of_offsets_array: (Vec, Vec, Vec), pub nonmonotonic_offsets: (Vec, Vec, Vec), pub nonmonotonic_minor_indices: (Vec, Vec, Vec), + pub major_offset_out_of_bounds: (Vec, Vec, Vec), pub minor_index_out_of_bounds: (Vec, Vec, Vec), pub duplicate_entry: (Vec, Vec, Vec), + pub duplicate_entry_unsorted: (Vec, Vec, Vec), + pub wrong_values_length: (Vec, Vec, Vec), } -impl InvalidCsrDataExamples { +impl InvalidCsDataExamples { pub fn new() -> Self { let empty_offset_array = (Vec::::new(), Vec::::new(), Vec::::new()); let offset_array_invalid_length_for_arbitrary_data = @@ -25,9 +54,13 @@ impl InvalidCsrDataExamples { let nonmonotonic_offsets = (vec![0, 3, 2, 5], vec![0, 1, 2, 3, 4], vec![0, 1, 2, 3, 4]); let nonmonotonic_minor_indices = (vec![0, 2, 2, 5], vec![0, 2, 3, 1, 4], vec![0, 1, 2, 3, 4]); + let major_offset_out_of_bounds = + (vec![0, 7, 2, 5], vec![0, 2, 3, 1, 4], vec![0, 1, 2, 3, 4]); let minor_index_out_of_bounds = (vec![0, 2, 2, 5], vec![0, 6, 1, 2, 3], vec![0, 1, 2, 3, 4]); - let duplicate_entry = (vec![0, 2, 2, 5], vec![0, 5, 2, 2, 3], vec![0, 1, 2, 3, 4]); + let duplicate_entry = (vec![0, 1, 2, 5], vec![1, 3, 2, 3, 3], vec![0, 1, 2, 3, 4]); + let duplicate_entry_unsorted = (vec![0, 1, 4, 5], vec![1, 3, 2, 3, 3], vec![0, 1, 2, 3, 4]); + let wrong_values_length = (vec![0, 1, 2, 5], vec![1, 3, 2, 3, 0], vec![5, 4]); return Self { empty_offset_array, @@ -37,8 +70,11 @@ impl InvalidCsrDataExamples { invalid_length_of_offsets_array, nonmonotonic_minor_indices, nonmonotonic_offsets, + major_offset_out_of_bounds, minor_index_out_of_bounds, duplicate_entry, + duplicate_entry_unsorted, + wrong_values_length, }; } } From 3413ab7da82cc4fb7662952c6687817785d80f22 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 5 Mar 2022 13:52:42 -0500 Subject: [PATCH 52/81] Correct typos, move doc portion to comment and fix borrow to clone --- .../src/generalized_eigenvalues.rs | 53 +++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index 132be1b7..e365f96a 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -181,7 +181,7 @@ where /// Calculates the generalized eigenvectors (left and right) associated with the generalized eigenvalues /// Outputs two matrices. - /// The first output matix contains the left eigenvectors of the generalized eigenvalues + /// The first output matrix contains the left eigenvectors of the generalized eigenvalues /// as columns. /// The second matrix contains the right eigenvectors of the generalized eigenvalues /// as columns. @@ -196,46 +196,45 @@ where /// /// u(j)**H * A = lambda(j) * u(j)**H * B /// where u(j)**H is the conjugate-transpose of u(j). - /// - /// How the eigenvectors are build up: - /// - /// Since the input entries are all real, the generalized eigenvalues if complex come in pairs - /// as a consequence of - /// The Lapack routine output reflects this by expecting the user to unpack the complex eigenvalues associated - /// eigenvectors from the real matrix output via the following procedure - /// - /// (Note: VL stands for the lapack real matrix output containing the left eigenvectors as columns, - /// VR stands for the lapack real matrix output containing the right eigenvectors as columns) - /// - /// If the j-th and (j+1)-th eigenvalues form a complex conjugate pair, - /// then - /// - /// u(j) = VL(:,j)+i*VL(:,j+1) - /// u(j+1) = VL(:,j)-i*VL(:,j+1) - /// - /// and - /// - /// u(j) = VR(:,j)+i*VR(:,j+1) - /// v(j+1) = VR(:,j)-i*VR(:,j+1). - /// - pub fn eigenvectors(self) -> (OMatrix, D, D>, OMatrix, D, D>) + pub fn eigenvectors(&self) -> (OMatrix, D, D>, OMatrix, D, D>) where DefaultAllocator: Allocator, D, D> + Allocator, D> + Allocator<(Complex, T), D>, { + /* + How the eigenvectors are built up: + + Since the input entries are all real, the generalized eigenvalues if complex come in pairs + as a consequence of the [complex conjugate root thorem](https://en.wikipedia.org/wiki/Complex_conjugate_root_theorem) + The Lapack routine output reflects this by expecting the user to unpack the complex eigenvalues associated + eigenvectors from the real matrix output via the following procedure + + (Note: VL stands for the lapack real matrix output containing the left eigenvectors as columns, + VR stands for the lapack real matrix output containing the right eigenvectors as columns) + + If the j-th and (j+1)-th eigenvalues form a complex conjugate pair, + then + + u(j) = VL(:,j)+i*VL(:,j+1) + u(j+1) = VL(:,j)-i*VL(:,j+1) + + and + + u(j) = VR(:,j)+i*VR(:,j+1) + v(j+1) = VR(:,j)-i*VR(:,j+1). + */ + let n = self.vsl.shape().0; let mut l = self .vsl - .clone() .map(|x| Complex::new(x, T::RealField::zero())); let mut r = self .vsr - .clone() .map(|x| Complex::new(x, T::RealField::zero())); - let eigenvalues = &self.raw_eigenvalues(); + let eigenvalues = self.raw_eigenvalues(); let mut c = 0; From 4413a35a1c36f25108797516e5d8caddf8385abe Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 5 Mar 2022 14:39:22 -0500 Subject: [PATCH 53/81] Fix doc --- nalgebra-lapack/src/generalized_eigenvalues.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index e365f96a..91b4e597 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -206,7 +206,7 @@ where Since the input entries are all real, the generalized eigenvalues if complex come in pairs as a consequence of the [complex conjugate root thorem](https://en.wikipedia.org/wiki/Complex_conjugate_root_theorem) - The Lapack routine output reflects this by expecting the user to unpack the complex eigenvalues associated + The Lapack routine output reflects this by expecting the user to unpack the real and complex eigenvalues associated eigenvectors from the real matrix output via the following procedure (Note: VL stands for the lapack real matrix output containing the left eigenvectors as columns, From adf50a617384ac33202f36df113c03326a1943de Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 5 Mar 2022 14:43:50 -0500 Subject: [PATCH 54/81] Fix formatting --- nalgebra-lapack/src/generalized_eigenvalues.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index 91b4e597..95db3e18 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -226,13 +226,9 @@ where let n = self.vsl.shape().0; - let mut l = self - .vsl - .map(|x| Complex::new(x, T::RealField::zero())); + let mut l = self.vsl.map(|x| Complex::new(x, T::RealField::zero())); - let mut r = self - .vsr - .map(|x| Complex::new(x, T::RealField::zero())); + let mut r = self.vsr.map(|x| Complex::new(x, T::RealField::zero())); let eigenvalues = self.raw_eigenvalues(); From 2743eef87ec8bacb5bb2ea43fd92888062698ec7 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 5 Mar 2022 15:01:22 -0500 Subject: [PATCH 55/81] Add in explicit type of matrix element to module overview docs --- nalgebra-lapack/src/generalized_eigenvalues.rs | 2 +- nalgebra-lapack/src/qz.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index 95db3e18..db758332 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -13,7 +13,7 @@ use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar}; use lapack; -/// Generalized eigenvalues and generalized eigenvectors (left and right) of a pair of N*N square matrices. +/// Generalized eigenvalues and generalized eigenvectors (left and right) of a pair of N*N real square matrices. /// /// Each generalized eigenvalue (lambda) satisfies determinant(A - lambda*B) = 0 /// diff --git a/nalgebra-lapack/src/qz.rs b/nalgebra-lapack/src/qz.rs index 17342a2e..99f3c374 100644 --- a/nalgebra-lapack/src/qz.rs +++ b/nalgebra-lapack/src/qz.rs @@ -62,7 +62,7 @@ impl QZ where DefaultAllocator: Allocator + Allocator, { - /// Attempts to compute the QZ decomposition of input square matrices `a` and `b`. + /// Attempts to compute the QZ decomposition of input real square matrices `a` and `b`. /// /// i.e retrieves the left and right matrices of Schur Vectors (VSL and VSR) /// the upper-quasitriangular matrix `S` and upper triangular matrix `T` such that the From 325618ba22660b5ee22f308b4f06ac5f0196a9ae Mon Sep 17 00:00:00 2001 From: YuhanLiin Date: Wed, 9 Mar 2022 02:13:12 -0500 Subject: [PATCH 56/81] Fix SVD instability bug --- src/linalg/svd.rs | 22 +++++++--------------- tests/linalg/svd.rs | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/linalg/svd.rs b/src/linalg/svd.rs index 3f945a65..42a6abb3 100644 --- a/src/linalg/svd.rs +++ b/src/linalg/svd.rs @@ -2,7 +2,6 @@ use serde::{Deserialize, Serialize}; use std::any::TypeId; -use approx::AbsDiffEq; use num::{One, Zero}; use crate::allocator::Allocator; @@ -94,14 +93,7 @@ where /// The singular values are not guaranteed to be sorted in any particular order. /// If a descending order is required, consider using `new` instead. pub fn new_unordered(matrix: OMatrix, compute_u: bool, compute_v: bool) -> Self { - Self::try_new_unordered( - matrix, - compute_u, - compute_v, - T::RealField::default_epsilon(), - 0, - ) - .unwrap() + Self::try_new_unordered(matrix, compute_u, compute_v, crate::convert(1e-15), 0).unwrap() } /// Attempts to compute the Singular Value Decomposition of `matrix` using implicit shift. @@ -888,13 +880,13 @@ fn compute_2x2_uptrig_svd( v_t = Some(csv.clone()); } - if compute_u { - let cu = (m11.scale(csv.c()) + m12 * csv.s()) / v1.clone(); - let su = (m22 * csv.s()) / v1.clone(); - let (csu, sgn_u) = GivensRotation::new(cu, su); + let cu = (m11.scale(csv.c()) + m12 * csv.s()) / v1.clone(); + let su = (m22 * csv.s()) / v1.clone(); + let (csu, sgn_u) = GivensRotation::new(cu, su); + v1 *= sgn_u.clone(); + v2 *= sgn_u; - v1 *= sgn_u.clone(); - v2 *= sgn_u; + if compute_u { u = Some(csu); } } diff --git a/tests/linalg/svd.rs b/tests/linalg/svd.rs index deb3d38d..7eabe3d0 100644 --- a/tests/linalg/svd.rs +++ b/tests/linalg/svd.rs @@ -460,3 +460,27 @@ fn svd_sorted() { epsilon = 1.0e-5 ); } + +#[test] +// Exercises bug reported in issue #983 of nalgebra +fn svd_consistent() { + let m = nalgebra::dmatrix![ + 10.74785316637712f64, -5.994983325167452, -6.064492921857296; + -4.149751381521569, 20.654504205822462, -4.470436210703133; + -22.772715014220207, -1.4554372570788008, 18.108113992170573 + ] + .transpose(); + let svd1 = m.clone().svd(true, true); + let svd2 = m.clone().svd(false, true); + let svd3 = m.clone().svd(true, false); + let svd4 = m.svd(false, false); + + assert_relative_eq!(svd1.singular_values, svd2.singular_values, epsilon = 1e-5); + assert_relative_eq!(svd1.singular_values, svd3.singular_values, epsilon = 1e-5); + assert_relative_eq!(svd1.singular_values, svd4.singular_values, epsilon = 1e-5); + assert_relative_eq!( + svd1.singular_values, + nalgebra::dvector![3.16188022e+01, 2.23811978e+01, 0.], + epsilon = 1e-5 + ); +} From 1acd48f6f1b684317e7cd893d973ba67ab4f7290 Mon Sep 17 00:00:00 2001 From: YuhanLiin Date: Wed, 9 Mar 2022 21:04:43 -0500 Subject: [PATCH 57/81] Address review comments --- src/linalg/svd.rs | 10 +++++++++- tests/linalg/svd.rs | 12 ++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/linalg/svd.rs b/src/linalg/svd.rs index 42a6abb3..06bae4a3 100644 --- a/src/linalg/svd.rs +++ b/src/linalg/svd.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; use std::any::TypeId; +use approx::AbsDiffEq; use num::{One, Zero}; use crate::allocator::Allocator; @@ -93,7 +94,14 @@ where /// The singular values are not guaranteed to be sorted in any particular order. /// If a descending order is required, consider using `new` instead. pub fn new_unordered(matrix: OMatrix, compute_u: bool, compute_v: bool) -> Self { - Self::try_new_unordered(matrix, compute_u, compute_v, crate::convert(1e-15), 0).unwrap() + Self::try_new_unordered( + matrix, + compute_u, + compute_v, + T::RealField::default_epsilon() * crate::convert(5.0), + 0, + ) + .unwrap() } /// Attempts to compute the Singular Value Decomposition of `matrix` using implicit shift. diff --git a/tests/linalg/svd.rs b/tests/linalg/svd.rs index 7eabe3d0..8e83df81 100644 --- a/tests/linalg/svd.rs +++ b/tests/linalg/svd.rs @@ -462,8 +462,8 @@ fn svd_sorted() { } #[test] -// Exercises bug reported in issue #983 of nalgebra -fn svd_consistent() { +// Exercises bug reported in issue #983 of nalgebra (https://github.com/dimforge/nalgebra/issues/983) +fn svd_regression_issue_983() { let m = nalgebra::dmatrix![ 10.74785316637712f64, -5.994983325167452, -6.064492921857296; -4.149751381521569, 20.654504205822462, -4.470436210703133; @@ -475,12 +475,12 @@ fn svd_consistent() { let svd3 = m.clone().svd(true, false); let svd4 = m.svd(false, false); - assert_relative_eq!(svd1.singular_values, svd2.singular_values, epsilon = 1e-5); - assert_relative_eq!(svd1.singular_values, svd3.singular_values, epsilon = 1e-5); - assert_relative_eq!(svd1.singular_values, svd4.singular_values, epsilon = 1e-5); + assert_relative_eq!(svd1.singular_values, svd2.singular_values, epsilon = 1e-9); + assert_relative_eq!(svd1.singular_values, svd3.singular_values, epsilon = 1e-9); + assert_relative_eq!(svd1.singular_values, svd4.singular_values, epsilon = 1e-9); assert_relative_eq!( svd1.singular_values, nalgebra::dvector![3.16188022e+01, 2.23811978e+01, 0.], - epsilon = 1e-5 + epsilon = 1e-6 ); } From a27d121a7a1783c822ff47c46aee0475bda15852 Mon Sep 17 00:00:00 2001 From: YuhanLiin Date: Wed, 9 Mar 2022 21:10:45 -0500 Subject: [PATCH 58/81] Add regression test for #1072 --- tests/linalg/svd.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/linalg/svd.rs b/tests/linalg/svd.rs index 8e83df81..900901ad 100644 --- a/tests/linalg/svd.rs +++ b/tests/linalg/svd.rs @@ -484,3 +484,18 @@ fn svd_regression_issue_983() { epsilon = 1e-6 ); } + +#[test] +// Exercises bug reported in issue #1072 of nalgebra (https://github.com/dimforge/nalgebra/issues/1072) +fn svd_regression_issue_1072() { + let x = nalgebra::dmatrix![-6.206610118536945f64, -3.67612186839874; -1.2755730783423473, 6.047238193479124]; + let mut x_svd = x.svd(true, true); + x_svd.singular_values = nalgebra::dvector![1.0, 0.0]; + let y = x_svd.recompose().unwrap(); + let y_svd = y.svd(true, true); + assert_relative_eq!( + y_svd.singular_values, + nalgebra::dvector![1.0, 0.0], + epsilon = 1e-9 + ); +} From 6d89e2aca6b53ba2271756dfe7547855867183a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 11 Mar 2022 17:17:34 +0100 Subject: [PATCH 59/81] Update to cust 0.3 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 15a24d41..0adc5729 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -98,7 +98,7 @@ glam019 = { package = "glam", version = "0.19", optional = true } glam020 = { package = "glam", version = "0.20", optional = true } [target.'cfg(not(target_os = "cuda"))'.dependencies] -cust = { version = "0.2", optional = true } +cust = { version = "0.3", optional = true } [dev-dependencies] From 1c4c3de92e0900bbd32d6f6e3d3a64a45dcd94e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 11 Mar 2022 17:43:01 +0100 Subject: [PATCH 60/81] CI: pin the version of Cuda --- .github/workflows/nalgebra-ci-build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/nalgebra-ci-build.yml b/.github/workflows/nalgebra-ci-build.yml index bc2f9ca6..707c4143 100644 --- a/.github/workflows/nalgebra-ci-build.yml +++ b/.github/workflows/nalgebra-ci-build.yml @@ -124,6 +124,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: Jimver/cuda-toolkit@v0.2.4 + with: + cuda: '11.2.2' - name: Install nightly-2021-12-04 uses: actions-rs/toolchain@v1 with: From 30c0450075290653ef14a9b6b6a4f387f9ef57f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 11 Mar 2022 18:06:40 +0100 Subject: [PATCH 61/81] CI: set the CUDA_ARCH env var when targetting nvptx --- .github/workflows/nalgebra-ci-build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/nalgebra-ci-build.yml b/.github/workflows/nalgebra-ci-build.yml index 707c4143..c00b6cbc 100644 --- a/.github/workflows/nalgebra-ci-build.yml +++ b/.github/workflows/nalgebra-ci-build.yml @@ -134,4 +134,6 @@ jobs: - uses: actions/checkout@v2 - run: rustup target add nvptx64-nvidia-cuda - run: cargo build --no-default-features --features cuda - - run: cargo build --no-default-features --features cuda --target=nvptx64-nvidia-cuda \ No newline at end of file + - run: cargo build --no-default-features --features cuda --target=nvptx64-nvidia-cuda + env: + CUDA_ARCH: "350" \ No newline at end of file From d7117e228a20aa93920ddd3c98c919710c9745aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Wed, 16 Mar 2022 18:07:29 +0100 Subject: [PATCH 62/81] Use cust_core instead of cust --- Cargo.toml | 6 ++---- src/base/array_storage.rs | 5 +---- src/base/dimension.rs | 10 ++-------- src/base/matrix.rs | 5 +---- src/base/unit.rs | 10 +++------- src/geometry/dual_quaternion.rs | 5 +---- src/geometry/isometry.rs | 5 +---- src/geometry/orthographic.rs | 5 +---- src/geometry/perspective.rs | 5 +---- src/geometry/point.rs | 7 +++---- src/geometry/quaternion.rs | 9 +++------ src/geometry/rotation.rs | 5 +---- src/geometry/scale.rs | 5 +---- src/geometry/similarity.rs | 5 +---- src/geometry/transform.rs | 23 +++++++---------------- src/geometry/translation.rs | 5 +---- src/geometry/unit_complex.rs | 4 ++-- 17 files changed, 32 insertions(+), 87 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0adc5729..8a3fea5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ compare = [ "matrixcompare-core" ] libm = [ "simba/libm" ] libm-force = [ "simba/libm_force" ] macros = [ "nalgebra-macros" ] -cuda = [ "cust", "simba/cuda" ] +cuda = [ "cust_core", "simba/cuda" ] # Conversion convert-mint = [ "mint" ] @@ -96,9 +96,7 @@ glam017 = { package = "glam", version = "0.17", optional = true } glam018 = { package = "glam", version = "0.18", optional = true } glam019 = { package = "glam", version = "0.19", optional = true } glam020 = { package = "glam", version = "0.20", optional = true } - -[target.'cfg(not(target_os = "cuda"))'.dependencies] -cust = { version = "0.3", optional = true } +cust_core = { version = "0.1", optional = true } [dev-dependencies] diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index 6851c381..b6bd236a 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -27,10 +27,7 @@ use std::mem; /// A array-based statically sized matrix data storage. #[repr(transparent)] #[derive(Copy, Clone, PartialEq, Eq, Hash)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct ArrayStorage(pub [[T; R]; C]); impl ArrayStorage { diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 86006f3d..e43cb734 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -13,10 +13,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// Dim of dynamically-sized algebraic entities. #[derive(Clone, Copy, Eq, PartialEq, Debug)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Dynamic { value: usize, } @@ -201,10 +198,7 @@ dim_ops!( ); #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Const; /// Trait implemented exclusively by type-level integers. diff --git a/src/base/matrix.rs b/src/base/matrix.rs index bdf3a8c7..cc69c9a1 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -150,10 +150,7 @@ pub type MatrixCross = /// some concrete types for `T` and a compatible data storage type `S`). #[repr(C)] #[derive(Clone, Copy)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Matrix { /// The data storage that contains all the matrix components. Disappointed? /// diff --git a/src/base/unit.rs b/src/base/unit.rs index 9336a5e5..bb8b56a1 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -21,10 +21,7 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF /// in their documentation, read their dedicated pages directly. #[repr(transparent)] #[derive(Clone, Hash, Copy)] -// #[cfg_attr( -// all(not(target_os = "cuda"), feature = "cuda"), -// derive(cust::DeviceCopy) -// )] +// #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Unit { pub(crate) value: T, } @@ -102,9 +99,8 @@ mod rkyv_impl { } } -#[cfg(all(not(target_os = "cuda"), feature = "cuda"))] -unsafe impl cust::memory::DeviceCopy - for Unit> +#[cfg(feature = "cuda")] +unsafe impl cust_core::DeviceCopy for Unit> where T: Scalar, R: Dim, diff --git a/src/geometry/dual_quaternion.rs b/src/geometry/dual_quaternion.rs index 4280668a..509b359a 100644 --- a/src/geometry/dual_quaternion.rs +++ b/src/geometry/dual_quaternion.rs @@ -39,10 +39,7 @@ use simba::scalar::{ClosedNeg, RealField}; /// See #[repr(C)] #[derive(Debug, Copy, Clone)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct DualQuaternion { /// The real component of the quaternion pub real: Quaternion, diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index 8cdd1bfc..0179f1ff 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -50,10 +50,7 @@ use crate::geometry::{AbstractRotation, Point, Translation}; /// #[repr(C)] #[derive(Debug, Copy, Clone)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "serde-serialize-no-std", derive(Serialize, Deserialize))] #[cfg_attr( feature = "serde-serialize-no-std", diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index 18a7852d..085ba61b 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -19,10 +19,7 @@ use crate::geometry::{Point3, Projective3}; /// A 3D orthographic projection stored as a homogeneous 4x4 matrix. #[repr(C)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[derive(Copy, Clone)] pub struct Orthographic3 { matrix: Matrix4, diff --git a/src/geometry/perspective.rs b/src/geometry/perspective.rs index 59b7f9f2..8ebab3e4 100644 --- a/src/geometry/perspective.rs +++ b/src/geometry/perspective.rs @@ -20,10 +20,7 @@ use crate::geometry::{Point3, Projective3}; /// A 3D perspective projection stored as a homogeneous 4x4 matrix. #[repr(C)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[derive(Copy, Clone)] pub struct Perspective3 { matrix: Matrix4, diff --git a/src/geometry/point.rs b/src/geometry/point.rs index b62998c3..a8d7684b 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -69,12 +69,11 @@ where { } -#[cfg(all(not(target_os = "cuda"), feature = "cuda"))] -unsafe impl cust::memory::DeviceCopy - for OPoint +#[cfg(feature = "cuda")] +unsafe impl cust_core::DeviceCopy for OPoint where DefaultAllocator: Allocator, - OVector: cust::memory::DeviceCopy, + OVector: cust_core::DeviceCopy, { } diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 0aa7f3d3..6d29f34f 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -23,10 +23,7 @@ use crate::geometry::{Point3, Rotation}; /// that may be used as a rotation. #[repr(C)] #[derive(Copy, Clone)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Quaternion { /// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. pub coords: Vector4, @@ -1045,8 +1042,8 @@ impl fmt::Display for Quaternion { /// A unit quaternions. May be used to represent a rotation. pub type UnitQuaternion = Unit>; -#[cfg(all(not(target_os = "cuda"), feature = "cuda"))] -unsafe impl cust::memory::DeviceCopy for UnitQuaternion {} +#[cfg(feature = "cuda")] +unsafe impl cust_core::DeviceCopy for UnitQuaternion {} impl PartialEq for UnitQuaternion { #[inline] diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index 69c4a355..4dbcfb43 100755 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -49,10 +49,7 @@ use crate::geometry::Point; /// * [Conversion to a matrix `matrix`, `to_homogeneous`…](#conversion-to-a-matrix) /// #[repr(C)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[derive(Copy, Clone)] pub struct Rotation { matrix: SMatrix, diff --git a/src/geometry/scale.rs b/src/geometry/scale.rs index 064e0075..abaeeccc 100755 --- a/src/geometry/scale.rs +++ b/src/geometry/scale.rs @@ -17,10 +17,7 @@ use crate::geometry::Point; /// A scale which supports non-uniform scaling. #[repr(C)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[derive(Copy, Clone)] pub struct Scale { /// The scale coordinates, i.e., how much is multiplied to a point's coordinates when it is diff --git a/src/geometry/similarity.rs b/src/geometry/similarity.rs index 46c86f5d..9658685e 100755 --- a/src/geometry/similarity.rs +++ b/src/geometry/similarity.rs @@ -18,10 +18,7 @@ use crate::geometry::{AbstractRotation, Isometry, Point, Translation}; /// A similarity, i.e., an uniform scaling, followed by a rotation, followed by a translation. #[repr(C)] #[derive(Debug, Copy, Clone)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[cfg_attr(feature = "serde-serialize-no-std", derive(Serialize, Deserialize))] #[cfg_attr( feature = "serde-serialize-no-std", diff --git a/src/geometry/transform.rs b/src/geometry/transform.rs index b0b5cced..2a7ca112 100755 --- a/src/geometry/transform.rs +++ b/src/geometry/transform.rs @@ -60,26 +60,17 @@ where /// Tag representing the most general (not necessarily inversible) `Transform` type. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub enum TGeneral {} /// Tag representing the most general inversible `Transform` type. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub enum TProjective {} /// Tag representing an affine `Transform`. Its bottom-row is equal to `(0, 0 ... 0, 1)`. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub enum TAffine {} impl TCategory for TGeneral { @@ -207,13 +198,13 @@ where { } -#[cfg(all(not(target_os = "cuda"), feature = "cuda"))] -unsafe impl - cust::memory::DeviceCopy for Transform +#[cfg(feature = "cuda")] +unsafe impl + cust_core::DeviceCopy for Transform where Const: DimNameAdd, DefaultAllocator: Allocator, U1>, DimNameSum, U1>>, - Owned, U1>, DimNameSum, U1>>: cust::memory::DeviceCopy, + Owned, U1>, DimNameSum, U1>>: cust_core::DeviceCopy, { } diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index b07cce20..5db46e82 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -17,10 +17,7 @@ use crate::geometry::Point; /// A translation. #[repr(C)] -#[cfg_attr( - all(not(target_os = "cuda"), feature = "cuda"), - derive(cust::DeviceCopy) -)] +#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[derive(Copy, Clone)] pub struct Translation { /// The translation coordinates, i.e., how much is added to a point's coordinates when it is diff --git a/src/geometry/unit_complex.rs b/src/geometry/unit_complex.rs index 48405dd4..caf25493 100755 --- a/src/geometry/unit_complex.rs +++ b/src/geometry/unit_complex.rs @@ -31,8 +31,8 @@ use std::cmp::{Eq, PartialEq}; /// * [Conversion to a matrix `to_rotation_matrix`, `to_homogeneous`…](#conversion-to-a-matrix) pub type UnitComplex = Unit>; -#[cfg(all(not(target_os = "cuda"), feature = "cuda"))] -unsafe impl cust::memory::DeviceCopy for UnitComplex {} +#[cfg(feature = "cuda")] +unsafe impl cust_core::DeviceCopy for UnitComplex {} impl PartialEq for UnitComplex { #[inline] From 27a349d282bbca65151c3b9fd544f7869b2f0edc Mon Sep 17 00:00:00 2001 From: Joshua Smith Date: Mon, 21 Mar 2022 16:08:53 -0500 Subject: [PATCH 63/81] fix for `UnitComplex::slerp()` #1093 --- src/geometry/unit_complex.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/geometry/unit_complex.rs b/src/geometry/unit_complex.rs index 48405dd4..5d3e45b2 100755 --- a/src/geometry/unit_complex.rs +++ b/src/geometry/unit_complex.rs @@ -410,7 +410,8 @@ where #[inline] #[must_use] pub fn slerp(&self, other: &Self, t: T) -> Self { - Self::new(self.angle() * (T::one() - t.clone()) + other.angle() * t) + let delta = other / self; + self * Self::new(delta.angle()*t) } } From 724117e5ad69ac2daba751b1091204bec097746f Mon Sep 17 00:00:00 2001 From: sterlingjensen <5555776+sterlingjensen@users.noreply.github.com> Date: Tue, 22 Mar 2022 11:53:46 -0500 Subject: [PATCH 64/81] Cleanup examples and doc links Close example code fences and normalize containing head line in touched files. Remove stale reference to `slice_assume_init` (commit 8c6ebf27), fix long dead internal links in deprecation notices. --- nalgebra-macros/src/lib.rs | 9 ++- src/base/blas.rs | 69 ++++++++------------ src/base/matrix.rs | 23 ++++--- src/geometry/dual_quaternion.rs | 11 ++++ src/geometry/dual_quaternion_construction.rs | 4 +- src/geometry/orthographic.rs | 15 +++++ src/geometry/point.rs | 2 + src/geometry/quaternion.rs | 11 +--- src/geometry/similarity_construction.rs | 10 +-- src/geometry/translation.rs | 2 + src/lib.rs | 4 +- 11 files changed, 82 insertions(+), 78 deletions(-) diff --git a/nalgebra-macros/src/lib.rs b/nalgebra-macros/src/lib.rs index 4bd791ae..0d7889ae 100644 --- a/nalgebra-macros/src/lib.rs +++ b/nalgebra-macros/src/lib.rs @@ -125,7 +125,6 @@ impl Parse for Matrix { /// (`;`) designates that a new row begins. /// /// # Examples -/// /// ``` /// use nalgebra::matrix; /// @@ -170,6 +169,7 @@ pub fn matrix(stream: TokenStream) -> TokenStream { /// `SMatrix`, it produces instances of `DMatrix`. At the moment it is not usable /// in `const fn` contexts. /// +/// # Example /// ``` /// use nalgebra::dmatrix; /// @@ -243,8 +243,7 @@ impl Parse for Vector { /// `vector!` is intended to be the most readable and performant way of constructing small, /// fixed-size vectors, and it is usable in `const fn` contexts. /// -/// ## Examples -/// +/// # Example /// ``` /// use nalgebra::vector; /// @@ -271,6 +270,7 @@ pub fn vector(stream: TokenStream) -> TokenStream { /// `SVector`, it produces instances of `DVector`. At the moment it is not usable /// in `const fn` contexts. /// +/// # Example /// ``` /// use nalgebra::dvector; /// @@ -301,8 +301,7 @@ pub fn dvector(stream: TokenStream) -> TokenStream { /// `point!` is intended to be the most readable and performant way of constructing small, /// points, and it is usable in `const fn` contexts. /// -/// ## Examples -/// +/// # Example /// ``` /// use nalgebra::point; /// diff --git a/src/base/blas.rs b/src/base/blas.rs index 4f56a70e..e65304b5 100644 --- a/src/base/blas.rs +++ b/src/base/blas.rs @@ -175,8 +175,7 @@ where /// Note that this is **not** the matrix multiplication as in, e.g., numpy. For matrix /// multiplication, use one of: `.gemm`, `.mul_to`, `.mul`, the `*` operator. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::{Vector3, Matrix2x3}; /// let vec1 = Vector3::new(1.0, 2.0, 3.0); @@ -207,8 +206,7 @@ where /// Note that this is **not** the matrix multiplication as in, e.g., numpy. For matrix /// multiplication, use one of: `.gemm`, `.mul_to`, `.mul`, the `*` operator. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::{Vector2, Complex}; /// let vec1 = Vector2::new(Complex::new(1.0, 2.0), Complex::new(3.0, 4.0)); @@ -232,8 +230,7 @@ where /// The dot product between the transpose of `self` and `rhs`. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::{Vector3, RowVector3, Matrix2x3, Matrix3x2}; /// let vec1 = Vector3::new(1.0, 2.0, 3.0); @@ -285,8 +282,7 @@ where /// /// If `b` is zero, `self` is never read from. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::Vector3; /// let mut vec1 = Vector3::new(1.0, 2.0, 3.0); @@ -308,8 +304,7 @@ where /// /// If `b` is zero, `self` is never read from. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::Vector3; /// let mut vec1 = Vector3::new(1.0, 2.0, 3.0); @@ -333,8 +328,7 @@ where /// /// If `beta` is zero, `self` is never read. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::{Matrix2, Vector2}; /// let mut vec1 = Vector2::new(1.0, 2.0); @@ -425,8 +419,7 @@ where /// If `beta` is zero, `self` is never read. If `self` is read, only its lower-triangular part /// (including the diagonal) is actually read. /// - /// # Examples: - /// + /// # Examples /// ``` /// # use nalgebra::{Matrix2, Vector2}; /// let mat = Matrix2::new(1.0, 2.0, @@ -468,8 +461,7 @@ where /// If `beta` is zero, `self` is never read. If `self` is read, only its lower-triangular part /// (including the diagonal) is actually read. /// - /// # Examples: - /// + /// # Examples /// ``` /// # use nalgebra::{Matrix2, Vector2, Complex}; /// let mat = Matrix2::new(Complex::new(1.0, 0.0), Complex::new(2.0, -0.1), @@ -552,8 +544,7 @@ where /// /// If `beta` is zero, `self` is never read. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::{Matrix2, Vector2}; /// let mat = Matrix2::new(1.0, 3.0, @@ -587,8 +578,7 @@ where /// For real matrices, this is the same as `.gemv_tr`. /// If `beta` is zero, `self` is never read. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::{Matrix2, Vector2, Complex}; /// let mat = Matrix2::new(Complex::new(1.0, 2.0), Complex::new(3.0, 4.0), @@ -656,8 +646,7 @@ where /// /// If `beta` is zero, `self` is never read. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::{Matrix2x3, Vector2, Vector3}; /// let mut mat = Matrix2x3::repeat(4.0); @@ -688,8 +677,7 @@ where /// /// If `beta` is zero, `self` is never read. /// - /// # Examples: - /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{Matrix2x3, Vector2, Vector3, Complex}; @@ -722,8 +710,7 @@ where /// /// If `beta` is zero, `self` is never read. /// - /// # Examples: - /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{Matrix2x3, Matrix3x4, Matrix2x4}; @@ -763,8 +750,7 @@ where /// /// If `beta` is zero, `self` is never read. /// - /// # Examples: - /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{Matrix3x2, Matrix3x4, Matrix2x4}; @@ -821,8 +807,7 @@ where /// /// If `beta` is zero, `self` is never read. /// - /// # Examples: - /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{Matrix3x2, Matrix3x4, Matrix2x4, Complex}; @@ -921,8 +906,7 @@ where /// If `beta` is zero, `self` is never read. The result is symmetric. Only the lower-triangular /// (including the diagonal) part of `self` is read/written. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::{Matrix2, Vector2}; /// let mut mat = Matrix2::identity(); @@ -934,6 +918,7 @@ where /// mat.ger_symm(10.0, &vec1, &vec2, 5.0); /// assert_eq!(mat.lower_triangle(), expected.lower_triangle()); /// assert_eq!(mat.m12, 99999.99999); // This was untouched. + /// ``` #[inline] #[deprecated(note = "This is renamed `syger` to match the original BLAS terminology.")] pub fn ger_symm( @@ -958,8 +943,7 @@ where /// If `beta` is zero, `self` is never read. The result is symmetric. Only the lower-triangular /// (including the diagonal) part of `self` is read/written. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::{Matrix2, Vector2}; /// let mut mat = Matrix2::identity(); @@ -971,6 +955,7 @@ where /// mat.syger(10.0, &vec1, &vec2, 5.0); /// assert_eq!(mat.lower_triangle(), expected.lower_triangle()); /// assert_eq!(mat.m12, 99999.99999); // This was untouched. + /// ``` #[inline] pub fn syger( &mut self, @@ -993,8 +978,7 @@ where /// If `beta` is zero, `self` is never read. The result is symmetric. Only the lower-triangular /// (including the diagonal) part of `self` is read/written. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::{Matrix2, Vector2, Complex}; /// let mut mat = Matrix2::identity(); @@ -1006,6 +990,7 @@ where /// mat.hegerc(Complex::new(10.0, 20.0), &vec1, &vec2, Complex::new(5.0, 15.0)); /// assert_eq!(mat.lower_triangle(), expected.lower_triangle()); /// assert_eq!(mat.m12, Complex::new(99999.99999, 88888.88888)); // This was untouched. + /// ``` #[inline] pub fn hegerc( &mut self, @@ -1031,8 +1016,7 @@ where /// /// This uses the provided workspace `work` to avoid allocations for intermediate results. /// - /// # Examples: - /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{DMatrix, DVector}; @@ -1053,6 +1037,7 @@ where /// /// mat.quadform_tr_with_workspace(&mut workspace, 10.0, &lhs, &mid, 5.0); /// assert_relative_eq!(mat, expected); + /// ``` pub fn quadform_tr_with_workspace( &mut self, work: &mut Vector, @@ -1085,8 +1070,7 @@ where /// If `D1` is a type-level integer, then the allocation is performed on the stack. /// Use `.quadform_tr_with_workspace(...)` instead to avoid allocations. /// - /// # Examples: - /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{Matrix2, Matrix3, Matrix2x3, Vector2}; @@ -1100,6 +1084,7 @@ where /// /// mat.quadform_tr(10.0, &lhs, &mid, 5.0); /// assert_relative_eq!(mat, expected); + /// ``` pub fn quadform_tr( &mut self, alpha: T, @@ -1124,6 +1109,7 @@ where /// /// This uses the provided workspace `work` to avoid allocations for intermediate results. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{DMatrix, DVector}; @@ -1145,6 +1131,7 @@ where /// /// mat.quadform_with_workspace(&mut workspace, 10.0, &mid, &rhs, 5.0); /// assert_relative_eq!(mat, expected); + /// ``` pub fn quadform_with_workspace( &mut self, work: &mut Vector, @@ -1180,6 +1167,7 @@ where /// If `D2` is a type-level integer, then the allocation is performed on the stack. /// Use `.quadform_with_workspace(...)` instead to avoid allocations. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{Matrix2, Matrix3x2, Matrix3}; @@ -1194,6 +1182,7 @@ where /// /// mat.quadform(10.0, &mid, &rhs, 5.0); /// assert_relative_eq!(mat, expected); + /// ``` pub fn quadform( &mut self, alpha: T, diff --git a/src/base/matrix.rs b/src/base/matrix.rs index bdf3a8c7..0f35c1ac 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -414,8 +414,6 @@ where { /// Assumes a matrix's entries to be initialized. This operation should be near zero-cost. /// - /// For the similar method that operates on matrix slices, see [`slice_assume_init`]. - /// /// # Safety /// The user must make sure that every single entry of the buffer has been initialized, /// or Undefined Behavior will immediately occur. @@ -436,12 +434,12 @@ impl> Matrix { /// The shape of this matrix returned as the tuple (number of rows, number of columns). /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::Matrix3x4; /// let mat = Matrix3x4::::zeros(); /// assert_eq!(mat.shape(), (3, 4)); + /// ``` #[inline] #[must_use] pub fn shape(&self) -> (usize, usize) { @@ -458,12 +456,12 @@ impl> Matrix { /// The number of rows of this matrix. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::Matrix3x4; /// let mat = Matrix3x4::::zeros(); /// assert_eq!(mat.nrows(), 3); + /// ``` #[inline] #[must_use] pub fn nrows(&self) -> usize { @@ -472,12 +470,12 @@ impl> Matrix { /// The number of columns of this matrix. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::Matrix3x4; /// let mat = Matrix3x4::::zeros(); /// assert_eq!(mat.ncols(), 4); + /// ``` #[inline] #[must_use] pub fn ncols(&self) -> usize { @@ -486,14 +484,14 @@ impl> Matrix { /// The strides (row stride, column stride) of this matrix. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::DMatrix; /// let mat = DMatrix::::zeros(10, 10); /// let slice = mat.slice_with_steps((0, 0), (5, 3), (1, 2)); /// // The column strides is the number of steps (here 2) multiplied by the corresponding dimension. /// assert_eq!(mat.strides(), (1, 10)); + /// ``` #[inline] #[must_use] pub fn strides(&self) -> (usize, usize) { @@ -1088,8 +1086,7 @@ impl> Matrix { impl> Matrix { /// Iterates through this matrix coordinates in column-major order. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::Matrix2x3; /// let mat = Matrix2x3::new(11, 12, 13, @@ -1102,6 +1099,7 @@ impl> Matrix { /// assert_eq!(*it.next().unwrap(), 13); /// assert_eq!(*it.next().unwrap(), 23); /// assert!(it.next().is_none()); + /// ``` #[inline] pub fn iter(&self) -> MatrixIter<'_, T, R, C, S> { MatrixIter::new(&self.data) @@ -1124,6 +1122,7 @@ impl> Matrix { } /// Iterate through the columns of this matrix. + /// /// # Example /// ``` /// # use nalgebra::Matrix2x3; diff --git a/src/geometry/dual_quaternion.rs b/src/geometry/dual_quaternion.rs index 4280668a..0621d600 100644 --- a/src/geometry/dual_quaternion.rs +++ b/src/geometry/dual_quaternion.rs @@ -19,6 +19,7 @@ use simba::scalar::{ClosedNeg, RealField}; /// `DualQuaternions` are stored as \[..real, ..dual\]. /// Both of the quaternion components are laid out in `i, j, k, w` order. /// +/// # Example /// ``` /// # use nalgebra::{DualQuaternion, Quaternion}; /// @@ -623,6 +624,7 @@ where /// dq.rotation().euler_angles().0, std::f32::consts::FRAC_PI_2, epsilon = 1.0e-6 /// ); /// assert_relative_eq!(dq.translation().vector.y, 3.0, epsilon = 1.0e-6); + /// ``` #[inline] #[must_use] pub fn sclerp(&self, other: &Self, t: T) -> Self @@ -713,6 +715,7 @@ where /// Return the rotation part of this unit dual quaternion. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3}; @@ -733,6 +736,7 @@ where /// Return the translation part of this unit dual quaternion. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3}; @@ -758,6 +762,7 @@ where /// Builds an isometry from this unit dual quaternion. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3}; @@ -783,6 +788,7 @@ where /// /// This is the same as the multiplication `self * pt`. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3, Point3}; @@ -807,6 +813,7 @@ where /// /// This is the same as the multiplication `self * v`. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3}; @@ -831,6 +838,7 @@ where /// This may be cheaper than inverting the unit dual quaternion and /// transforming the point. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3, Point3}; @@ -856,6 +864,7 @@ where /// This may be cheaper than inverting the unit dual quaternion and /// transforming the vector. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3}; @@ -880,6 +889,7 @@ where /// cheaper than inverting the unit dual quaternion and transforming the /// vector. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Unit, Vector3}; @@ -909,6 +919,7 @@ where /// Converts this unit dual quaternion interpreted as an isometry /// into its equivalent homogeneous transformation matrix. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{Matrix4, UnitDualQuaternion, UnitQuaternion, Vector3}; diff --git a/src/geometry/dual_quaternion_construction.rs b/src/geometry/dual_quaternion_construction.rs index 94bbc04f..ae7b5c97 100644 --- a/src/geometry/dual_quaternion_construction.rs +++ b/src/geometry/dual_quaternion_construction.rs @@ -27,7 +27,6 @@ impl DualQuaternion { /// The dual quaternion multiplicative identity. /// /// # Example - /// /// ``` /// # use nalgebra::{DualQuaternion, Quaternion}; /// @@ -134,6 +133,7 @@ impl UnitDualQuaternion { /// The unit dual quaternion multiplicative identity, which also represents /// the identity transformation as an isometry. /// + /// # Example /// ``` /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3, Point3}; /// let ident = UnitDualQuaternion::identity(); @@ -171,6 +171,7 @@ where /// Return a dual quaternion representing the translation and orientation /// given by the provided rotation quaternion and translation vector. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{UnitDualQuaternion, UnitQuaternion, Vector3, Point3}; @@ -196,6 +197,7 @@ where /// Return a unit dual quaternion representing the translation and orientation /// given by the provided isometry. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{Isometry3, UnitDualQuaternion, UnitQuaternion, Vector3, Point3}; diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index 18a7852d..a0c0cc29 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -319,6 +319,7 @@ impl Orthographic3 { /// The left offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -336,6 +337,7 @@ impl Orthographic3 { /// The right offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -353,6 +355,7 @@ impl Orthographic3 { /// The bottom offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -370,6 +373,7 @@ impl Orthographic3 { /// The top offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -387,6 +391,7 @@ impl Orthographic3 { /// The near plane offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -404,6 +409,7 @@ impl Orthographic3 { /// The far plane offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -526,6 +532,7 @@ impl Orthographic3 { /// Sets the left offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -545,6 +552,7 @@ impl Orthographic3 { /// Sets the right offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -564,6 +572,7 @@ impl Orthographic3 { /// Sets the bottom offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -583,6 +592,7 @@ impl Orthographic3 { /// Sets the top offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -602,6 +612,7 @@ impl Orthographic3 { /// Sets the near plane offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -621,6 +632,7 @@ impl Orthographic3 { /// Sets the far plane offset of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -640,6 +652,7 @@ impl Orthographic3 { /// Sets the view cuboid offsets along the `x` axis. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -665,6 +678,7 @@ impl Orthographic3 { /// Sets the view cuboid offsets along the `y` axis. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; @@ -690,6 +704,7 @@ impl Orthographic3 { /// Sets the near and far plane offsets of the view cuboid. /// + /// # Example /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Orthographic3; diff --git a/src/geometry/point.rs b/src/geometry/point.rs index b62998c3..7fede9b0 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -267,6 +267,7 @@ where /// assert_eq!(it.next(), Some(2.0)); /// assert_eq!(it.next(), Some(3.0)); /// assert_eq!(it.next(), None); + /// ``` #[inline] pub fn iter( &self, @@ -293,6 +294,7 @@ where /// } /// /// assert_eq!(p, Point3::new(10.0, 20.0, 30.0)); + /// ``` #[inline] pub fn iter_mut( &mut self, diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 0aa7f3d3..98ede5ee 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -405,6 +405,7 @@ where /// let expected = Quaternion::new(-20.0, 0.0, 0.0, 0.0); /// let result = a.inner(&b); /// assert_relative_eq!(expected, result, epsilon = 1.0e-5); + /// ``` #[inline] #[must_use] pub fn inner(&self, other: &Self) -> Self { @@ -1230,8 +1231,7 @@ where /// Panics if the angle between both quaternion is 180 degrees (in which case the interpolation /// is not well-defined). Use `.try_slerp` instead to avoid the panic. /// - /// # Examples: - /// + /// # Example /// ``` /// # use nalgebra::geometry::UnitQuaternion; /// @@ -1453,7 +1453,6 @@ where /// Builds a rotation matrix from this unit quaternion. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -1536,7 +1535,6 @@ where /// Converts this unit quaternion into its equivalent homogeneous transformation matrix. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -1560,7 +1558,6 @@ where /// This is the same as the multiplication `self * pt`. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -1581,7 +1578,6 @@ where /// This is the same as the multiplication `self * v`. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -1602,7 +1598,6 @@ where /// point. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -1625,7 +1620,6 @@ where /// vector. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -1646,7 +1640,6 @@ where /// vector. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; diff --git a/src/geometry/similarity_construction.rs b/src/geometry/similarity_construction.rs index 8d1d38b8..cabb1676 100644 --- a/src/geometry/similarity_construction.rs +++ b/src/geometry/similarity_construction.rs @@ -38,7 +38,6 @@ where /// Creates a new identity similarity. /// /// # Example - /// /// ``` /// # use nalgebra::{Similarity2, Point2, Similarity3, Point3}; /// @@ -95,7 +94,6 @@ where /// its axis passing through the point `p`. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -146,7 +144,6 @@ where /// Creates a new similarity from a translation, a rotation, and an uniform scaling factor. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -188,7 +185,6 @@ where /// Creates a new similarity from a translation and a rotation angle. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -232,7 +228,6 @@ macro_rules! similarity_construction_impl( /// factor. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -288,7 +283,6 @@ macro_rules! similarity_construction_impl( /// to `eye - at`. Non-collinearity is not checked. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -316,7 +310,7 @@ macro_rules! similarity_construction_impl( Self::from_isometry(Isometry::<_, $Rot, 3>::face_towards(eye, target, up), scaling) } - /// Deprecated: Use [`SimilarityMatrix3::face_towards`] instead. + /// Deprecated: Use [`SimilarityMatrix3::face_towards`](Self::face_towards) instead. #[deprecated(note="renamed to `face_towards`")] pub fn new_observer_frames(eye: &Point3, target: &Point3, @@ -338,7 +332,6 @@ macro_rules! similarity_construction_impl( /// requirement of this parameter is to not be collinear to `target - eye`. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; @@ -376,7 +369,6 @@ macro_rules! similarity_construction_impl( /// requirement of this parameter is to not be collinear to `target - eye`. /// /// # Example - /// /// ``` /// # #[macro_use] extern crate approx; /// # use std::f32; diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index b07cce20..b983f85d 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -231,6 +231,7 @@ impl Translation { /// let t = Translation3::new(1.0, 2.0, 3.0); /// let transformed_point = t.transform_point(&Point3::new(4.0, 5.0, 6.0)); /// assert_eq!(transformed_point, Point3::new(5.0, 7.0, 9.0)); + /// ``` #[inline] #[must_use] pub fn transform_point(&self, pt: &Point) -> Point { @@ -247,6 +248,7 @@ impl Translation { /// let t = Translation3::new(1.0, 2.0, 3.0); /// let transformed_point = t.inverse_transform_point(&Point3::new(4.0, 5.0, 6.0)); /// assert_eq!(transformed_point, Point3::new(3.0, 3.0, 3.0)); + /// ``` #[inline] #[must_use] pub fn inverse_transform_point(&self, pt: &Point) -> Point { diff --git a/src/lib.rs b/src/lib.rs index 28701cfa..92b28dcb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -246,8 +246,8 @@ pub fn min(a: T, b: T) -> T { /// The absolute value of `a`. /// -/// Deprecated: Use [`Matrix::abs`] or [`RealField::abs`] instead. -#[deprecated(note = "use the inherent method `Matrix::abs` or `RealField::abs` instead")] +/// Deprecated: Use [`Matrix::abs`] or [`ComplexField::abs`] instead. +#[deprecated(note = "use the inherent method `Matrix::abs` or `ComplexField::abs` instead")] #[inline] pub fn abs(a: &T) -> T { a.abs() From aa37f28ddac00820a188ac1f6e5d8433d78485cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Wed, 23 Mar 2022 22:55:16 +0100 Subject: [PATCH 65/81] Simplify the type definitions of Const aliases, to help rust-analyzer --- src/base/dimension.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/base/dimension.rs b/src/base/dimension.rs index e43cb734..de51339f 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -303,24 +303,24 @@ impl DimName for Const { pub type U1 = Const<1>; -impl ToTypenum for Const<{ typenum::U1::USIZE }> { +impl ToTypenum for Const<1> { type Typenum = typenum::U1; } impl ToConst for typenum::U1 { - type Const = Const<{ typenum::U1::USIZE }>; + type Const = Const<1>; } macro_rules! from_to_typenum ( - ($($D: ident),* $(,)*) => {$( - pub type $D = Const<{ typenum::$D::USIZE }>; + ($($D: ident, $VAL: expr);* $(;)*) => {$( + pub type $D = Const<$VAL>; - impl ToTypenum for Const<{ typenum::$D::USIZE }> { + impl ToTypenum for Const<$VAL> { type Typenum = typenum::$D; } impl ToConst for typenum::$D { - type Const = Const<{ typenum::$D::USIZE }>; + type Const = Const<$VAL>; } impl IsNotStaticOne for $D { } @@ -328,12 +328,12 @@ macro_rules! from_to_typenum ( ); from_to_typenum!( - U0, /*U1,*/ U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, - U19, U20, U21, U22, U23, U24, U25, U26, U27, U28, U29, U30, U31, U32, U33, U34, U35, U36, U37, - U38, U39, U40, U41, U42, U43, U44, U45, U46, U47, U48, U49, U50, U51, U52, U53, U54, U55, U56, - U57, U58, U59, U60, U61, U62, U63, U64, U65, U66, U67, U68, U69, U70, U71, U72, U73, U74, U75, - U76, U77, U78, U79, U80, U81, U82, U83, U84, U85, U86, U87, U88, U89, U90, U91, U92, U93, U94, - U95, U96, U97, U98, U99, U100, U101, U102, U103, U104, U105, U106, U107, U108, U109, U110, - U111, U112, U113, U114, U115, U116, U117, U118, U119, U120, U121, U122, U123, U124, U125, U126, - U127 + U0, 0; /*U1,1;*/ U2, 2; U3, 3; U4, 4; U5, 5; U6, 6; U7, 7; U8, 8; U9, 9; U10, 10; U11, 11; U12, 12; U13, 13; U14, 14; U15, 15; U16, 16; U17, 17; U18, 18; + U19, 19; U20, 20; U21, 21; U22, 22; U23, 23; U24, 24; U25, 25; U26, 26; U27, 27; U28, 28; U29, 29; U30, 30; U31, 31; U32, 32; U33, 33; U34, 34; U35, 35; U36, 36; U37, 37; + U38, 38; U39, 39; U40, 40; U41, 41; U42, 42; U43, 43; U44, 44; U45, 45; U46, 46; U47, 47; U48, 48; U49, 49; U50, 50; U51, 51; U52, 52; U53, 53; U54, 54; U55, 55; U56, 56; + U57, 57; U58, 58; U59, 59; U60, 60; U61, 61; U62, 62; U63, 63; U64, 64; U65, 65; U66, 66; U67, 67; U68, 68; U69, 69; U70, 70; U71, 71; U72, 72; U73, 73; U74, 74; U75, 75; + U76, 76; U77, 77; U78, 78; U79, 79; U80, 80; U81, 81; U82, 82; U83, 83; U84, 84; U85, 85; U86, 86; U87, 87; U88, 88; U89, 89; U90, 90; U91, 91; U92, 92; U93, 93; U94, 94; + U95, 95; U96, 96; U97, 97; U98, 98; U99, 99; U100, 100; U101, 101; U102, 102; U103, 103; U104, 104; U105, 105; U106, 106; U107, 107; U108, 108; U109, 109; U110, 110; + U111, 111; U112, 112; U113, 113; U114, 114; U115, 115; U116, 116; U117, 117; U118, 118; U119, 119; U120, 120; U121, 121; U122, 122; U123, 123; U124, 124; U125, 125; U126, 126; + U127, 127 ); From 44b700ecdfb1f7482a1737daa59d35e2a59e540c Mon Sep 17 00:00:00 2001 From: sterlingjensen <5555776+sterlingjensen@users.noreply.github.com> Date: Wed, 23 Mar 2022 22:08:20 -0500 Subject: [PATCH 66/81] run `cargo fmt` --- src/base/matrix.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 0f35c1ac..f5650701 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1122,7 +1122,7 @@ impl> Matrix { } /// Iterate through the columns of this matrix. - /// + /// /// # Example /// ``` /// # use nalgebra::Matrix2x3; From b02e4ec2a989ad2ddab9eff020e6bade8177c711 Mon Sep 17 00:00:00 2001 From: Joshua Smith Date: Sat, 26 Mar 2022 17:32:12 -0500 Subject: [PATCH 67/81] fixed cargo fmt error --- src/geometry/unit_complex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/geometry/unit_complex.rs b/src/geometry/unit_complex.rs index 5d3e45b2..b9aeaad4 100755 --- a/src/geometry/unit_complex.rs +++ b/src/geometry/unit_complex.rs @@ -411,7 +411,7 @@ where #[must_use] pub fn slerp(&self, other: &Self, t: T) -> Self { let delta = other / self; - self * Self::new(delta.angle()*t) + self * Self::new(delta.angle() * t) } } From baa320d7f31e80324ad5e60b51bfb34ca93ae7f1 Mon Sep 17 00:00:00 2001 From: Joshua Smith Date: Tue, 29 Mar 2022 13:38:10 -0500 Subject: [PATCH 68/81] added tests for complex and quaternion slerp pathing --- tests/geometry/rotation.rs | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/geometry/rotation.rs b/tests/geometry/rotation.rs index 9a29772e..bb7aacb6 100644 --- a/tests/geometry/rotation.rs +++ b/tests/geometry/rotation.rs @@ -33,7 +33,9 @@ fn quaternion_euler_angles_issue_494() { #[cfg(feature = "proptest-support")] mod proptest_tests { use na::{self, Rotation2, Rotation3, Unit}; + use na::{UnitComplex, UnitQuaternion}; use simba::scalar::RealField; + use approx::AbsDiffEq; use std::f64; use crate::proptest::*; @@ -229,5 +231,74 @@ mod proptest_tests { prop_assert_eq!(r, Rotation3::identity()) } } + + // + //In general, `slerp(a,b,t)` should equal `(b/a)^t * a` even though in practice, + //we may not use that formula directly for complex numbers or quaternions + // + + #[test] + fn slerp_powf_agree_2(a in unit_complex(), b in unit_complex(), t in PROPTEST_F64) { + let z1 = a.slerp(&b, t); + let z2 = (b/a).powf(t) * a; + prop_assert!(relative_eq!(z1,z2,epsilon=1e-10)); + } + + #[test] + fn slerp_powf_agree_3(a in unit_quaternion(), b in unit_quaternion(), t in PROPTEST_F64) { + if let Some(z1) = a.try_slerp(&b, t, f64::default_epsilon()) { + let z2 = (b/a).powf(t) * a; + prop_assert!(relative_eq!(z1,z2,epsilon=1e-10)); + } + } + + // + //when not antipodal, slerp should always take the shortest path between two orientations + // + + #[test] + fn slerp_takes_shortest_path_2( + z in unit_complex(), dtheta in -f64::pi()..f64::pi(), t in 0.0..1.0f64 + ) { + + //ambiguous when at ends of angle range, so we don't really care here + if dtheta.abs() != f64::pi() { + + //make two complex numbers separated by an angle between -pi and pi + let (z1, z2) = (z, z * UnitComplex::new(dtheta)); + let z3 = z1.slerp(&z2, t); + + //since the angle is no larger than a half-turn, and t is between 0 and 1, + //the shortest path just corresponds to adding the scaled angle + let a1 = z3.angle(); + let a2 = na::wrap(z1.angle() + dtheta*t, -f64::pi(), f64::pi()); + + prop_assert!(relative_eq!(a1, a2, epsilon=1e-10)); + } + + } + + #[test] + fn slerp_takes_shortest_path_3( + q in unit_quaternion(), dtheta in -f64::pi()..f64::pi(), t in 0.0..1.0f64 + ) { + + //ambiguous when at ends of angle range, so we don't really care here + if let Some(axis) = q.axis() { + + //make two quaternions separated by an angle between -pi and pi + let (q1, q2) = (q, q * UnitQuaternion::from_axis_angle(&axis, dtheta)); + let q3 = q1.slerp(&q2, t); + + //since the angle is no larger than a half-turn, and t is between 0 and 1, + //the shortest path just corresponds to adding the scaled angle + let q4 = q1 * UnitQuaternion::from_axis_angle(&axis, dtheta*t); + prop_assert!(relative_eq!(q3, q4, epsilon=1e-10)); + + } + + } + + } } From 96e2d1f69e390cd65928232d1222032e2e149f4b Mon Sep 17 00:00:00 2001 From: Joshua Smith Date: Tue, 29 Mar 2022 13:42:38 -0500 Subject: [PATCH 69/81] fixed cargo fmt --- tests/geometry/rotation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/geometry/rotation.rs b/tests/geometry/rotation.rs index bb7aacb6..84bba676 100644 --- a/tests/geometry/rotation.rs +++ b/tests/geometry/rotation.rs @@ -32,10 +32,10 @@ fn quaternion_euler_angles_issue_494() { #[cfg(feature = "proptest-support")] mod proptest_tests { + use approx::AbsDiffEq; use na::{self, Rotation2, Rotation3, Unit}; use na::{UnitComplex, UnitQuaternion}; use simba::scalar::RealField; - use approx::AbsDiffEq; use std::f64; use crate::proptest::*; From 80a844a3bf7a2fd72eb2272b57f6a451c2fabee7 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 16 Apr 2022 02:23:38 -0400 Subject: [PATCH 70/81] Update check for zero --- nalgebra-lapack/src/generalized_eigenvalues.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index db758332..69e5e465 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -234,10 +234,8 @@ where let mut c = 0; - let epsilon = T::RealField::default_epsilon(); - while c < n { - if eigenvalues[c].0.im.abs() > epsilon && c + 1 < n { + if eigenvalues[c].0.im.abs() != T::RealField::zero() && c + 1 < n { // taking care of the left eigenvector matrix l.column_mut(c).zip_apply(&self.vsl.column(c + 1), |r, i| { *r = Complex::new(r.re.clone(), i.clone()); From bc31012c08105b3c6a3fb2337851be51d527b349 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 16 Apr 2022 02:23:51 -0400 Subject: [PATCH 71/81] Add newline --- nalgebra-lapack/src/generalized_eigenvalues.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index 69e5e465..f4f3bc49 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -180,6 +180,7 @@ where } /// Calculates the generalized eigenvectors (left and right) associated with the generalized eigenvalues + /// /// Outputs two matrices. /// The first output matrix contains the left eigenvectors of the generalized eigenvalues /// as columns. From ff2d431ed00049fbbd4c3d9cf8d1a3506b35f808 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 16 Apr 2022 02:37:02 -0400 Subject: [PATCH 72/81] Remove repeated docs --- .../src/generalized_eigenvalues.rs | 39 +------------------ 1 file changed, 1 insertion(+), 38 deletions(-) diff --git a/nalgebra-lapack/src/generalized_eigenvalues.rs b/nalgebra-lapack/src/generalized_eigenvalues.rs index f4f3bc49..5d1e3ace 100644 --- a/nalgebra-lapack/src/generalized_eigenvalues.rs +++ b/nalgebra-lapack/src/generalized_eigenvalues.rs @@ -71,19 +71,6 @@ where /// Attempts to compute the generalized eigenvalues, and left and right associated eigenvectors /// via the raw returns from LAPACK's dggev and sggev routines /// - /// Each generalized eigenvalue (lambda) satisfies determinant(A - lambda*B) = 0 - /// - /// The right eigenvector v(j) corresponding to the eigenvalue lambda(j) - /// of (A,B) satisfies - /// - /// A * v(j) = lambda(j) * B * v(j). - /// - /// The left eigenvector u(j) corresponding to the eigenvalue lambda(j) - /// of (A,B) satisfies - /// - /// u(j)**H * A = lambda(j) * u(j)**H * B . - /// where u(j)**H is the conjugate-transpose of u(j). - /// /// Panics if the method did not converge. pub fn new(a: OMatrix, b: OMatrix) -> Self { Self::try_new(a, b).expect("Calculation of generalized eigenvalues failed.") @@ -92,19 +79,6 @@ where /// Attempts to compute the generalized eigenvalues (and eigenvectors) via the raw returns from LAPACK's /// dggev and sggev routines /// - /// Each generalized eigenvalue (lambda) satisfies determinant(A - lambda*B) = 0 - /// - /// The right eigenvector v(j) corresponding to the eigenvalue lambda(j) - /// of (A,B) satisfies - /// - /// A * v(j) = lambda(j) * B * v(j). - /// - /// The left eigenvector u(j) corresponding to the eigenvalue lambda(j) - /// of (A,B) satisfies - /// - /// u(j)**H * A = lambda(j) * u(j)**H * B . - /// where u(j)**H is the conjugate-transpose of u(j). - /// /// Returns `None` if the method did not converge. pub fn try_new(mut a: OMatrix, mut b: OMatrix) -> Option { assert!( @@ -186,17 +160,6 @@ where /// as columns. /// The second matrix contains the right eigenvectors of the generalized eigenvalues /// as columns. - /// - /// The right eigenvector v(j) corresponding to the eigenvalue lambda(j) - /// of (A,B) satisfies - /// - /// A * v(j) = lambda(j) * B * v(j) - /// - /// The left eigenvector u(j) corresponding to the eigenvalue lambda(j) - /// of (A,B) satisfies - /// - /// u(j)**H * A = lambda(j) * u(j)**H * B - /// where u(j)**H is the conjugate-transpose of u(j). pub fn eigenvectors(&self) -> (OMatrix, D, D>, OMatrix, D, D>) where DefaultAllocator: @@ -262,7 +225,7 @@ where (l, r) } - /// outputs the unprocessed (almost) version of generalized eigenvalues ((alphar, alphai), beta) + /// Outputs the unprocessed (almost) version of generalized eigenvalues ((alphar, alphai), beta) /// straight from LAPACK #[must_use] pub fn raw_eigenvalues(&self) -> OVector<(Complex, T), D> From ee7473cba8de71744e1e3216d8c3a6c40c663218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Mon, 18 Apr 2022 10:45:38 +0200 Subject: [PATCH 73/81] Readme: update sponsors --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fa1e0904..62ab4759 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,9 @@ And our gold sponsors:

- + + + +

\ No newline at end of file From adb38203051ebe50166bb1afb16cdf21ae4e26d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Thu, 28 Apr 2022 14:50:59 +0200 Subject: [PATCH 74/81] glam: add conversion from Vec2/3/4 to UnitVector2/3/4 + remove ambigous conversions --- src/third_party/glam/common/glam_isometry.rs | 64 ++------------- src/third_party/glam/common/glam_matrix.rs | 78 ++++++++++++++++++- .../glam/common/glam_unit_complex.rs | 2 +- src/third_party/glam/mod.rs | 2 - src/third_party/glam/v013/mod.rs | 18 ----- 5 files changed, 86 insertions(+), 78 deletions(-) delete mode 100644 src/third_party/glam/v013/mod.rs diff --git a/src/third_party/glam/common/glam_isometry.rs b/src/third_party/glam/common/glam_isometry.rs index 3a8d4961..5a528302 100644 --- a/src/third_party/glam/common/glam_isometry.rs +++ b/src/third_party/glam/common/glam_isometry.rs @@ -36,18 +36,18 @@ impl From> for (DVec3, DQuat) { } } -impl From> for (Vec3, Quat) { - fn from(iso: Isometry2) -> (Vec3, Quat) { - let tra = Vec3::new(iso.translation.x, iso.translation.y, 0.0); - let rot = Quat::from_axis_angle(Vec3::Z, iso.rotation.angle()); +impl From> for (Vec2, f32) { + fn from(iso: Isometry2) -> (Vec2, f32) { + let tra = Vec2::new(iso.translation.x, iso.translation.y); + let rot = iso.rotation.angle(); (tra, rot) } } -impl From> for (DVec3, DQuat) { - fn from(iso: Isometry2) -> (DVec3, DQuat) { - let tra = DVec3::new(iso.translation.x, iso.translation.y, 0.0); - let rot = DQuat::from_axis_angle(DVec3::Z, iso.rotation.angle()); +impl From> for (DVec2, f64) { + fn from(iso: Isometry2) -> (DVec2, f64) { + let tra = DVec2::new(iso.translation.x, iso.translation.y); + let rot = iso.rotation.angle(); (tra, rot) } } @@ -64,30 +64,6 @@ impl From<(DVec3, DQuat)> for Isometry3 { } } -impl From<(Vec3, Quat)> for Isometry2 { - fn from((tra, rot): (Vec3, Quat)) -> Self { - Isometry2::new([tra.x, tra.y].into(), rot.to_axis_angle().1) - } -} - -impl From<(DVec3, DQuat)> for Isometry2 { - fn from((tra, rot): (DVec3, DQuat)) -> Self { - Isometry2::new([tra.x, tra.y].into(), rot.to_axis_angle().1) - } -} - -impl From<(Vec2, Quat)> for Isometry2 { - fn from((tra, rot): (Vec2, Quat)) -> Self { - Isometry2::new(tra.into(), rot.to_axis_angle().1) - } -} - -impl From<(DVec2, DQuat)> for Isometry2 { - fn from((tra, rot): (DVec2, DQuat)) -> Self { - Isometry2::new(tra.into(), rot.to_axis_angle().1) - } -} - impl From<(Vec2, f32)> for Isometry2 { fn from((tra, rot): (Vec2, f32)) -> Self { Isometry2::new(tra.into(), rot) @@ -112,18 +88,6 @@ impl From for Isometry3 { } } -impl From for Isometry2 { - fn from(rot: Quat) -> Self { - Isometry2::new(Vector2::zeros(), rot.to_axis_angle().1) - } -} - -impl From for Isometry2 { - fn from(rot: DQuat) -> Self { - Isometry2::new(Vector2::zeros(), rot.to_axis_angle().1) - } -} - impl From for Isometry3 { fn from(tra: Vec3) -> Self { Isometry3::from_parts(tra.into(), UnitQuaternion::identity()) @@ -148,18 +112,6 @@ impl From for Isometry2 { } } -impl From for Isometry2 { - fn from(tra: Vec3) -> Self { - Isometry2::new([tra.x, tra.y].into(), 0.0) - } -} - -impl From for Isometry2 { - fn from(tra: DVec3) -> Self { - Isometry2::new([tra.x, tra.y].into(), 0.0) - } -} - impl TryFrom for Isometry2 { type Error = (); diff --git a/src/third_party/glam/common/glam_matrix.rs b/src/third_party/glam/common/glam_matrix.rs index 80f88054..fa9f713f 100644 --- a/src/third_party/glam/common/glam_matrix.rs +++ b/src/third_party/glam/common/glam_matrix.rs @@ -3,7 +3,11 @@ use super::glam::{ Mat4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4, }; use crate::storage::RawStorage; -use crate::{Matrix, Matrix2, Matrix3, Matrix4, Vector, Vector2, Vector3, Vector4, U2, U3, U4}; +use crate::{ + Matrix, Matrix2, Matrix3, Matrix4, Unit, UnitVector2, UnitVector3, UnitVector4, Vector, + Vector2, Vector3, Vector4, U2, U3, U4, +}; +use std::convert::TryFrom; macro_rules! impl_vec_conversion( ($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => { @@ -66,6 +70,63 @@ impl_vec_conversion!(i32, IVec2, IVec3, IVec4); impl_vec_conversion!(u32, UVec2, UVec3, UVec4); impl_vec_conversion!(bool, BVec2, BVec3, BVec4); +const ERR: &'static str = "Normalization failed."; + +macro_rules! impl_unit_vec_conversion( + ($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => { + impl TryFrom<$Vec2> for UnitVector2<$N> { + type Error = &'static str; + #[inline] + fn try_from(e: $Vec2) -> Result { + Unit::try_new(e.into(), 0.0).ok_or(ERR) + } + } + + impl From> for $Vec2 + { + #[inline] + fn from(e: UnitVector2<$N>) -> $Vec2 { + e.into_inner().into() + } + } + + impl TryFrom<$Vec3> for UnitVector3<$N> { + type Error = &'static str; + #[inline] + fn try_from(e: $Vec3) -> Result { + Unit::try_new(e.into(), 0.0).ok_or(ERR) + } + } + + impl From> for $Vec3 + { + #[inline] + fn from(e: UnitVector3<$N>) -> $Vec3 { + e.into_inner().into() + } + } + + impl TryFrom<$Vec4> for UnitVector4<$N> { + type Error = &'static str; + #[inline] + fn try_from(e: $Vec4) -> Result { + Unit::try_new(e.into(), 0.0).ok_or(ERR) + } + } + + impl From> for $Vec4 + { + #[inline] + fn from(e: UnitVector4<$N>) -> $Vec4 { + e.into_inner().into() + } + } + } +); + +impl_unit_vec_conversion!(f32, Vec2, Vec3, Vec4); +impl_unit_vec_conversion!(f64, DVec2, DVec3, DVec4); + impl From for Vector3 { #[inline] fn from(e: Vec3A) -> Vector3 { @@ -83,6 +144,21 @@ where } } +impl TryFrom for UnitVector3 { + type Error = &'static str; + #[inline] + fn try_from(e: Vec3A) -> Result { + Unit::try_new(e.into(), 0.0).ok_or(ERR) + } +} + +impl From> for Vec3A { + #[inline] + fn from(e: UnitVector3) -> Vec3A { + e.into_inner().into() + } +} + impl From for Matrix2 { #[inline] fn from(e: Mat2) -> Matrix2 { diff --git a/src/third_party/glam/common/glam_unit_complex.rs b/src/third_party/glam/common/glam_unit_complex.rs index 7ee6fc65..d44a2f09 100644 --- a/src/third_party/glam/common/glam_unit_complex.rs +++ b/src/third_party/glam/common/glam_unit_complex.rs @@ -1,4 +1,4 @@ -use super::glam::{DMat2, Mat2}; +use super::glam::{DMat2, DQuat, DVec3, Mat2, Quat, Vec3}; use crate::{Complex, UnitComplex}; impl From> for Mat2 { diff --git a/src/third_party/glam/mod.rs b/src/third_party/glam/mod.rs index d24ff7e5..ae2c4514 100644 --- a/src/third_party/glam/mod.rs +++ b/src/third_party/glam/mod.rs @@ -1,5 +1,3 @@ -#[cfg(feature = "glam013")] -mod v013; #[cfg(feature = "glam014")] mod v014; #[cfg(feature = "glam015")] diff --git a/src/third_party/glam/v013/mod.rs b/src/third_party/glam/v013/mod.rs deleted file mode 100644 index 4787fb21..00000000 --- a/src/third_party/glam/v013/mod.rs +++ /dev/null @@ -1,18 +0,0 @@ -#[path = "../common/glam_isometry.rs"] -mod glam_isometry; -#[path = "../common/glam_matrix.rs"] -mod glam_matrix; -#[path = "../common/glam_point.rs"] -mod glam_point; -#[path = "../common/glam_quaternion.rs"] -mod glam_quaternion; -#[path = "../common/glam_rotation.rs"] -mod glam_rotation; -#[path = "../common/glam_similarity.rs"] -mod glam_similarity; -#[path = "../common/glam_translation.rs"] -mod glam_translation; -#[path = "../common/glam_unit_complex.rs"] -mod glam_unit_complex; - -pub(self) use glam013 as glam; From c9bf6aa95f66f12d6cab7631d493594a944daf5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Thu, 28 Apr 2022 14:58:15 +0200 Subject: [PATCH 75/81] Fix warnings --- Cargo.toml | 2 -- nalgebra-glm/Cargo.toml | 1 - src/third_party/glam/common/glam_isometry.rs | 2 +- src/third_party/glam/common/glam_unit_complex.rs | 2 +- 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8a3fea5c..a629b554 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,6 @@ cuda = [ "cust_core", "simba/cuda" ] # Conversion convert-mint = [ "mint" ] convert-bytemuck = [ "bytemuck" ] -convert-glam013 = [ "glam013" ] convert-glam014 = [ "glam014" ] convert-glam015 = [ "glam015" ] convert-glam016 = [ "glam016" ] @@ -88,7 +87,6 @@ pest_derive = { version = "2", optional = true } bytemuck = { version = "1.5", optional = true } matrixcompare-core = { version = "0.1", optional = true } proptest = { version = "1", optional = true, default-features = false, features = ["std"] } -glam013 = { package = "glam", version = "0.13", optional = true } glam014 = { package = "glam", version = "0.14", optional = true } glam015 = { package = "glam", version = "0.15", optional = true } glam016 = { package = "glam", version = "0.16", optional = true } diff --git a/nalgebra-glm/Cargo.toml b/nalgebra-glm/Cargo.toml index f8087581..adf05fa3 100644 --- a/nalgebra-glm/Cargo.toml +++ b/nalgebra-glm/Cargo.toml @@ -26,7 +26,6 @@ cuda = [ "nalgebra/cuda" ] # Conversion convert-mint = [ "nalgebra/mint" ] convert-bytemuck = [ "nalgebra/bytemuck" ] -convert-glam013 = [ "nalgebra/glam013" ] convert-glam014 = [ "nalgebra/glam014" ] convert-glam015 = [ "nalgebra/glam015" ] convert-glam016 = [ "nalgebra/glam016" ] diff --git a/src/third_party/glam/common/glam_isometry.rs b/src/third_party/glam/common/glam_isometry.rs index 5a528302..7b188c39 100644 --- a/src/third_party/glam/common/glam_isometry.rs +++ b/src/third_party/glam/common/glam_isometry.rs @@ -1,5 +1,5 @@ use super::glam::{DMat3, DMat4, DQuat, DVec2, DVec3, Mat3, Mat4, Quat, Vec2, Vec3}; -use crate::{Isometry2, Isometry3, Matrix3, Matrix4, Translation3, UnitQuaternion, Vector2}; +use crate::{Isometry2, Isometry3, Matrix3, Matrix4, Translation3, UnitQuaternion}; use std::convert::TryFrom; impl From> for Mat3 { diff --git a/src/third_party/glam/common/glam_unit_complex.rs b/src/third_party/glam/common/glam_unit_complex.rs index d44a2f09..7ee6fc65 100644 --- a/src/third_party/glam/common/glam_unit_complex.rs +++ b/src/third_party/glam/common/glam_unit_complex.rs @@ -1,4 +1,4 @@ -use super::glam::{DMat2, DQuat, DVec3, Mat2, Quat, Vec3}; +use super::glam::{DMat2, Mat2}; use crate::{Complex, UnitComplex}; impl From> for Mat2 { From 90f2603de4790f7ac3a13096eae4e1cb3db9d7d4 Mon Sep 17 00:00:00 2001 From: zyansheep Date: Mon, 21 Mar 2022 12:08:46 -0400 Subject: [PATCH 76/81] upgrade rkyv to 0.7 --- Cargo.toml | 2 +- src/base/array_storage.rs | 16 ++++------------ src/base/dimension.rs | 8 +------- src/base/matrix.rs | 16 ++++------------ src/base/unit.rs | 16 ++++------------ src/geometry/isometry.rs | 23 ++++++----------------- src/geometry/quaternion.rs | 16 ++++------------ src/geometry/scale.rs | 16 ++++------------ src/geometry/translation.rs | 16 ++++------------ 9 files changed, 32 insertions(+), 97 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a629b554..907442cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -79,7 +79,7 @@ alga = { version = "0.9", default-features = false, optional = true } rand_distr = { version = "0.4", default-features = false, optional = true } matrixmultiply = { version = "0.3", optional = true } serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true } -rkyv = { version = "~0.6.4", default-features = false, features = ["const_generics"], optional = true } +rkyv = { version = "~0.7.1", optional = true } mint = { version = "0.5", optional = true } quickcheck = { version = "1", optional = true } pest = { version = "2", optional = true } diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index b6bd236a..d6dab40a 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -277,23 +277,15 @@ unsafe impl by #[cfg(feature = "rkyv-serialize-no-std")] mod rkyv_impl { use super::ArrayStorage; - use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; + use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; impl Archive for ArrayStorage { type Archived = ArrayStorage; type Resolver = <[[T; R]; C] as Archive>::Resolver; - fn resolve( - &self, - pos: usize, - resolver: Self::Resolver, - out: &mut core::mem::MaybeUninit, - ) { - self.0.resolve( - pos + offset_of!(Self::Archived, 0), - resolver, - project_struct!(out: Self::Archived => 0), - ); + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + let (fp, fo) = out_field!(out.0); + self.0.resolve(pos + fp, resolver, fo); } } diff --git a/src/base/dimension.rs b/src/base/dimension.rs index de51339f..39e0459d 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -242,13 +242,7 @@ mod rkyv_impl { type Archived = Self; type Resolver = (); - fn resolve( - &self, - _: usize, - _: Self::Resolver, - _: &mut core::mem::MaybeUninit, - ) { - } + unsafe fn resolve(&self, _: usize, _: Self::Resolver, _: *mut Self::Archived) {} } impl Serialize for Const { diff --git a/src/base/matrix.rs b/src/base/matrix.rs index f12cb3fa..1b598952 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -292,23 +292,15 @@ where mod rkyv_impl { use super::Matrix; use core::marker::PhantomData; - use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; + use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; impl Archive for Matrix { type Archived = Matrix; type Resolver = S::Resolver; - fn resolve( - &self, - pos: usize, - resolver: Self::Resolver, - out: &mut core::mem::MaybeUninit, - ) { - self.data.resolve( - pos + offset_of!(Self::Archived, data), - resolver, - project_struct!(out: Self::Archived => data), - ); + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + let (fp, fo) = out_field!(out.data); + self.data.resolve(pos + fp, resolver, fo); } } diff --git a/src/base/unit.rs b/src/base/unit.rs index bb8b56a1..d82c67ec 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -61,23 +61,15 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit { #[cfg(feature = "rkyv-serialize-no-std")] mod rkyv_impl { use super::Unit; - use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; + use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; impl Archive for Unit { type Archived = Unit; type Resolver = T::Resolver; - fn resolve( - &self, - pos: usize, - resolver: Self::Resolver, - out: &mut ::core::mem::MaybeUninit, - ) { - self.value.resolve( - pos + offset_of!(Self::Archived, value), - resolver, - project_struct!(out: Self::Archived => value), - ); + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + let (fp, fo) = out_field!(out.value); + self.value.resolve(pos + fp, resolver, fo); } } diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index 0179f1ff..1b4eb26f 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -77,7 +77,7 @@ pub struct Isometry { mod rkyv_impl { use super::Isometry; use crate::{base::Scalar, geometry::Translation}; - use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; + use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; impl Archive for Isometry where @@ -86,22 +86,11 @@ mod rkyv_impl { type Archived = Isometry; type Resolver = (R::Resolver, as Archive>::Resolver); - fn resolve( - &self, - pos: usize, - resolver: Self::Resolver, - out: &mut core::mem::MaybeUninit, - ) { - self.rotation.resolve( - pos + offset_of!(Self::Archived, rotation), - resolver.0, - project_struct!(out: Self::Archived => rotation), - ); - self.translation.resolve( - pos + offset_of!(Self::Archived, translation), - resolver.1, - project_struct!(out: Self::Archived => translation), - ); + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + let (fp, fo) = out_field!(out.rotation); + self.rotation.resolve(pos + fp, resolver.0, fo); + let (fp, fo) = out_field!(out.translation); + self.translation.resolve(pos + fp, resolver.1, fo); } } diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 987c9757..43f31e2e 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -101,23 +101,15 @@ where mod rkyv_impl { use super::Quaternion; use crate::base::Vector4; - use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; + use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; impl Archive for Quaternion { type Archived = Quaternion; type Resolver = as Archive>::Resolver; - fn resolve( - &self, - pos: usize, - resolver: Self::Resolver, - out: &mut core::mem::MaybeUninit, - ) { - self.coords.resolve( - pos + offset_of!(Self::Archived, coords), - resolver, - project_struct!(out: Self::Archived => coords), - ); + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + let (fp, fo) = out_field!(out.coords); + self.coords.resolve(pos + fp, resolver, fo); } } diff --git a/src/geometry/scale.rs b/src/geometry/scale.rs index abaeeccc..23265bba 100755 --- a/src/geometry/scale.rs +++ b/src/geometry/scale.rs @@ -88,23 +88,15 @@ where mod rkyv_impl { use super::Scale; use crate::base::SVector; - use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; + use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; impl Archive for Scale { type Archived = Scale; type Resolver = as Archive>::Resolver; - fn resolve( - &self, - pos: usize, - resolver: Self::Resolver, - out: &mut core::mem::MaybeUninit, - ) { - self.vector.resolve( - pos + offset_of!(Self::Archived, vector), - resolver, - project_struct!(out: Self::Archived => vector), - ); + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + let (fp, fo) = out_field!(out.vector); + self.vector.resolve(pos + fp, resolver, fo); } } diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index e1921d0a..e7dc5ee8 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -88,23 +88,15 @@ where mod rkyv_impl { use super::Translation; use crate::base::SVector; - use rkyv::{offset_of, project_struct, Archive, Deserialize, Fallible, Serialize}; + use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; impl Archive for Translation { type Archived = Translation; type Resolver = as Archive>::Resolver; - fn resolve( - &self, - pos: usize, - resolver: Self::Resolver, - out: &mut core::mem::MaybeUninit, - ) { - self.vector.resolve( - pos + offset_of!(Self::Archived, vector), - resolver, - project_struct!(out: Self::Archived => vector), - ); + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + let (fp, fo) = out_field!(out.vector); + self.vector.resolve(pos + fp, resolver, fo); } } From 1b6f2b83f7734708dd18dbf651084f632951329b Mon Sep 17 00:00:00 2001 From: zyansheep Date: Mon, 21 Mar 2022 12:47:03 -0400 Subject: [PATCH 77/81] add bytecheck for matrix --- Cargo.toml | 3 ++- src/base/matrix.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 907442cf..90869f44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,7 @@ convert-glam020 = [ "glam020" ] ## `serde-serialize`. serde-serialize-no-std = [ "serde", "num-complex/serde" ] serde-serialize = [ "serde-serialize-no-std", "serde/std" ] -rkyv-serialize-no-std = [ "rkyv" ] +rkyv-serialize-no-std = [ "rkyv", "bytecheck" ] rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std" ] # Randomness @@ -80,6 +80,7 @@ rand_distr = { version = "0.4", default-features = false, optional = true } matrixmultiply = { version = "0.3", optional = true } serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true } rkyv = { version = "~0.7.1", optional = true } +bytecheck = { version = "~0.6.1", optional = true } mint = { version = "0.5", optional = true } quickcheck = { version = "1", optional = true } pest = { version = "2", optional = true } diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 1b598952..63fbd0ce 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -293,6 +293,7 @@ mod rkyv_impl { use super::Matrix; use core::marker::PhantomData; use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; + use bytecheck::CheckBytes; impl Archive for Matrix { type Archived = Matrix; @@ -325,6 +326,22 @@ mod rkyv_impl { }) } } + + impl<__C: ?Sized, T, R, C, S: CheckBytes<__C>> + CheckBytes<__C> + for Matrix + where + S: CheckBytes<__C>, + { + type Error = >::Error; + unsafe fn check_bytes<'a>( + value: *const Matrix, + context: &mut __C, + ) -> Result<&'a Self, Self::Error> { + let _ = S::check_bytes(::core::ptr::addr_of!((*value).data), context)?; + Ok(&*value) + } + } } impl Matrix { From 24b97932e14a9d1728ca146009bf0a8f5d82da02 Mon Sep 17 00:00:00 2001 From: zyansheep Date: Mon, 21 Mar 2022 21:03:17 -0400 Subject: [PATCH 78/81] add bytecheck impls --- src/base/array_storage.rs | 22 +++++++++++++++++++ src/base/dimension.rs | 15 +++++++++++++ src/base/matrix.rs | 14 ++++++++----- src/base/unit.rs | 21 +++++++++++++++++++ src/geometry/isometry.rs | 42 +++++++++++++++++++++++++++++++++++++ src/geometry/quaternion.rs | 22 +++++++++++++++++++ src/geometry/scale.rs | 21 +++++++++++++++++++ src/geometry/translation.rs | 21 +++++++++++++++++++ 8 files changed, 173 insertions(+), 5 deletions(-) diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index d6dab40a..78cb6dcd 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -307,3 +307,25 @@ mod rkyv_impl { } } } +#[cfg(feature = "rkyv-serialize")] +mod bytecheck_impl { + use std::ptr::addr_of; + + use bytecheck::{ArrayCheckError, CheckBytes}; + + use super::ArrayStorage; + + impl<__C: ?Sized, T, const R: usize, const C: usize> CheckBytes<__C> for ArrayStorage + where + T: CheckBytes<__C>, + { + type Error = ArrayCheckError>::Error>>; + unsafe fn check_bytes<'a>( + value: *const ArrayStorage, + context: &mut __C, + ) -> Result<&'a Self, Self::Error> { + let _ = <[[T; R]; C] as CheckBytes<__C>>::check_bytes(addr_of!((*value).0), context)?; + Ok(&*value) + } + } +} diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 39e0459d..18c12803 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -257,6 +257,21 @@ mod rkyv_impl { } } } +#[cfg(feature = "rkyv-serialize")] +mod bytecheck_impl { + use bytecheck::CheckBytes; + + use super::Const; + impl<__C: ?Sized, const R: usize> CheckBytes<__C> for Const { + type Error = core::convert::Infallible; + unsafe fn check_bytes<'a>( + value: *const Const, + _context: &mut __C, + ) -> Result<&'a Self, Self::Error> { + Ok(&*value) + } + } +} pub trait ToConst { type Const: DimName; diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 63fbd0ce..2c481d37 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -293,7 +293,6 @@ mod rkyv_impl { use super::Matrix; use core::marker::PhantomData; use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - use bytecheck::CheckBytes; impl Archive for Matrix { type Archived = Matrix; @@ -326,10 +325,15 @@ mod rkyv_impl { }) } } +} +#[cfg(feature = "rkyv-serialize")] +mod bytecheck_impl { + use bytecheck::CheckBytes; + use std::ptr::addr_of; - impl<__C: ?Sized, T, R, C, S: CheckBytes<__C>> - CheckBytes<__C> - for Matrix + use super::Matrix; + + impl<__C: ?Sized, T, R, C, S: CheckBytes<__C>> CheckBytes<__C> for Matrix where S: CheckBytes<__C>, { @@ -338,7 +342,7 @@ mod rkyv_impl { value: *const Matrix, context: &mut __C, ) -> Result<&'a Self, Self::Error> { - let _ = S::check_bytes(::core::ptr::addr_of!((*value).data), context)?; + let _ = S::check_bytes(addr_of!((*value).data), context)?; Ok(&*value) } } diff --git a/src/base/unit.rs b/src/base/unit.rs index d82c67ec..6828aa02 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -90,6 +90,27 @@ mod rkyv_impl { } } } +#[cfg(feature = "rkyv-serialize")] +mod bytecheck_impl { + use std::ptr::addr_of; + + use bytecheck::CheckBytes; + + use super::Unit; + impl<__C: ?Sized, T: CheckBytes<__C>> CheckBytes<__C> for Unit + where + T: CheckBytes<__C>, + { + type Error = >::Error; + unsafe fn check_bytes<'a>( + value: *const Unit, + context: &mut __C, + ) -> Result<&'a Self, Self::Error> { + let _ = T::check_bytes(addr_of!((*value).value), context)?; + Ok(&*value) + } + } +} #[cfg(feature = "cuda")] unsafe impl cust_core::DeviceCopy for Unit> diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index 1b4eb26f..7f7175dd 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -121,6 +121,48 @@ mod rkyv_impl { } } } +#[cfg(feature = "rkyv-serialize")] +mod bytecheck_impl { + use crate::{Isometry, Scalar, Translation}; + use bytecheck::CheckBytes; + use std::{error::Error, fmt, ptr::addr_of}; + + #[derive(Debug)] + pub enum IsometryCheckBytesError { + Rotation(R), + Translation(T), + } + impl fmt::Display for IsometryCheckBytesError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Rotation(_) => write!(f, "failed to check bytes of isometry rotation"), + Self::Translation(_) => write!(f, "failed to check bytes of isometry translation"), + } + } + } + impl Error for IsometryCheckBytesError {} + + impl<__C: ?Sized, T: Scalar + CheckBytes<__C>, R: CheckBytes<__C>, const D: usize> + CheckBytes<__C> for Isometry + where + T: CheckBytes<__C>, + { + type Error = IsometryCheckBytesError< + as CheckBytes<__C>>::Error, + >::Error, + >; + unsafe fn check_bytes<'a>( + value: *const Isometry, + context: &mut __C, + ) -> Result<&'a Self, Self::Error> { + let _ = R::check_bytes(addr_of!((*value).rotation), context) + .map_err(|e| IsometryCheckBytesError::Rotation(e))?; + let _ = Translation::::check_bytes(addr_of!((*value).translation), context) + .map_err(|e| IsometryCheckBytesError::Translation(e))?; + Ok(&*value) + } + } +} impl hash::Hash for Isometry where diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 43f31e2e..71e38c4c 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -130,6 +130,28 @@ mod rkyv_impl { } } } +#[cfg(feature = "rkyv-serialize")] +mod bytecheck_impl { + use std::ptr::addr_of; + + use bytecheck::CheckBytes; + + use super::Quaternion; + use crate::Vector4; + impl<__C: ?Sized, T: CheckBytes<__C>> CheckBytes<__C> for Quaternion + where + T: CheckBytes<__C>, + { + type Error = as CheckBytes<__C>>::Error; + unsafe fn check_bytes<'a>( + value: *const Quaternion, + context: &mut __C, + ) -> Result<&'a Self, Self::Error> { + let _ = Vector4::check_bytes(addr_of!((*value).coords), context)?; + Ok(&*value) + } + } +} impl Quaternion where diff --git a/src/geometry/scale.rs b/src/geometry/scale.rs index 23265bba..333b63cb 100755 --- a/src/geometry/scale.rs +++ b/src/geometry/scale.rs @@ -118,6 +118,27 @@ mod rkyv_impl { } } } +#[cfg(feature = "rkyv-serialize")] +mod bytecheck_impl { + use std::ptr::addr_of; + + use bytecheck::CheckBytes; + + use crate::{SVector, Scale}; + impl<__C: ?Sized, T: CheckBytes<__C>, const D: usize> CheckBytes<__C> for Scale + where + T: CheckBytes<__C>, + { + type Error = as CheckBytes<__C>>::Error; + unsafe fn check_bytes<'a>( + value: *const Scale, + context: &mut __C, + ) -> Result<&'a Self, Self::Error> { + let _ = SVector::::check_bytes(addr_of!((*value).vector), context)?; + Ok(&*value) + } + } +} impl Scale { /// Inverts `self`. diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index e7dc5ee8..7af263bf 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -118,6 +118,27 @@ mod rkyv_impl { } } } +#[cfg(feature = "rkyv-serialize")] +mod bytecheck_impl { + use std::ptr::addr_of; + + use bytecheck::CheckBytes; + + use crate::{SVector, Translation}; + impl<__C: ?Sized, T: CheckBytes<__C>, const D: usize> CheckBytes<__C> for Translation + where + T: CheckBytes<__C>, + { + type Error = as CheckBytes<__C>>::Error; + unsafe fn check_bytes<'a>( + value: *const Translation, + context: &mut __C, + ) -> Result<&'a Self, Self::Error> { + let _ = SVector::::check_bytes(addr_of!((*value).vector), context)?; + Ok(&*value) + } + } +} impl Translation { /// Creates a new translation from the given vector. From ed41097d0c1dba439b1df9235e9de478845663c6 Mon Sep 17 00:00:00 2001 From: zyansheep Date: Tue, 22 Mar 2022 09:49:32 -0400 Subject: [PATCH 79/81] fix bytecheck feature in cargo.toml --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90869f44..ffd37598 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,8 +52,8 @@ convert-glam020 = [ "glam020" ] ## `serde-serialize`. serde-serialize-no-std = [ "serde", "num-complex/serde" ] serde-serialize = [ "serde-serialize-no-std", "serde/std" ] -rkyv-serialize-no-std = [ "rkyv", "bytecheck" ] -rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std" ] +rkyv-serialize-no-std = [ "rkyv" ] +rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std", "bytecheck" ] # Randomness ## To use rand in a #[no-std] environment, enable the From e913beca889dc278d1c0d6cadd2008d3f9bcc0af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 30 Apr 2022 10:32:10 +0200 Subject: [PATCH 80/81] Switch to derive macros for rkyv and bytecheck --- src/base/array_storage.rs | 61 ++------------------- src/base/dimension.rs | 50 ++++------------- src/base/matrix.rs | 65 ++-------------------- src/base/unit.rs | 59 ++------------------ src/geometry/dual_quaternion.rs | 5 ++ src/geometry/isometry.rs | 96 ++------------------------------- src/geometry/orthographic.rs | 5 ++ src/geometry/perspective.rs | 5 ++ src/geometry/point.rs | 5 ++ src/geometry/quaternion.rs | 61 ++------------------- src/geometry/rotation.rs | 5 ++ src/geometry/scale.rs | 61 ++------------------- src/geometry/similarity.rs | 5 ++ src/geometry/translation.rs | 61 ++------------------- src/lib.rs | 3 +- 15 files changed, 77 insertions(+), 470 deletions(-) diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index 78cb6dcd..3bc71e1a 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -27,6 +27,11 @@ use std::mem; /// A array-based statically sized matrix data storage. #[repr(transparent)] #[derive(Copy, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct ArrayStorage(pub [[T; R]; C]); @@ -273,59 +278,3 @@ unsafe impl by for ArrayStorage { } - -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::ArrayStorage; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for ArrayStorage { - type Archived = ArrayStorage; - type Resolver = <[[T; R]; C] as Archive>::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.0); - self.0.resolve(pos + fp, resolver, fo); - } - } - - impl, S: Fallible + ?Sized, const R: usize, const C: usize> Serialize - for ArrayStorage - { - fn serialize(&self, serializer: &mut S) -> Result { - self.0.serialize(serializer) - } - } - - impl - Deserialize, D> for ArrayStorage - where - T::Archived: Deserialize, - { - fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { - Ok(ArrayStorage(self.0.deserialize(deserializer)?)) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use std::ptr::addr_of; - - use bytecheck::{ArrayCheckError, CheckBytes}; - - use super::ArrayStorage; - - impl<__C: ?Sized, T, const R: usize, const C: usize> CheckBytes<__C> for ArrayStorage - where - T: CheckBytes<__C>, - { - type Error = ArrayCheckError>::Error>>; - unsafe fn check_bytes<'a>( - value: *const ArrayStorage, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = <[[T; R]; C] as CheckBytes<__C>>::check_bytes(addr_of!((*value).0), context)?; - Ok(&*value) - } - } -} diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 18c12803..4be97586 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -13,6 +13,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// Dim of dynamically-sized algebraic entities. #[derive(Clone, Copy, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Dynamic { value: usize, @@ -198,6 +203,11 @@ dim_ops!( ); #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Const; @@ -233,46 +243,6 @@ impl<'de, const D: usize> Deserialize<'de> for Const { } } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Const; - use rkyv::{Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Const { - type Archived = Self; - type Resolver = (); - - unsafe fn resolve(&self, _: usize, _: Self::Resolver, _: *mut Self::Archived) {} - } - - impl Serialize for Const { - fn serialize(&self, _: &mut S) -> Result { - Ok(()) - } - } - - impl Deserialize for Const { - fn deserialize(&self, _: &mut D) -> Result { - Ok(Const) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use bytecheck::CheckBytes; - - use super::Const; - impl<__C: ?Sized, const R: usize> CheckBytes<__C> for Const { - type Error = core::convert::Infallible; - unsafe fn check_bytes<'a>( - value: *const Const, - _context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - Ok(&*value) - } - } -} - pub trait ToConst { type Const: DimName; } diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 2c481d37..8f8786c1 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -150,6 +150,11 @@ pub type MatrixCross = /// some concrete types for `T` and a compatible data storage type `S`). #[repr(C)] #[derive(Clone, Copy)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Matrix { /// The data storage that contains all the matrix components. Disappointed? @@ -288,66 +293,6 @@ where { } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Matrix; - use core::marker::PhantomData; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Matrix { - type Archived = Matrix; - type Resolver = S::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.data); - self.data.resolve(pos + fp, resolver, fo); - } - } - - impl, _S: Fallible + ?Sized> Serialize<_S> - for Matrix - { - fn serialize(&self, serializer: &mut _S) -> Result { - self.data.serialize(serializer) - } - } - - impl - Deserialize, D> - for Matrix - where - S::Archived: Deserialize, - { - fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { - Ok(Matrix { - data: self.data.deserialize(deserializer)?, - _phantoms: PhantomData, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use bytecheck::CheckBytes; - use std::ptr::addr_of; - - use super::Matrix; - - impl<__C: ?Sized, T, R, C, S: CheckBytes<__C>> CheckBytes<__C> for Matrix - where - S: CheckBytes<__C>, - { - type Error = >::Error; - unsafe fn check_bytes<'a>( - value: *const Matrix, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = S::check_bytes(addr_of!((*value).data), context)?; - Ok(&*value) - } - } -} - impl Matrix { /// Creates a new matrix with the given data without statically checking that the matrix /// dimension matches the storage dimension. diff --git a/src/base/unit.rs b/src/base/unit.rs index 6828aa02..6fc00092 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -21,6 +21,11 @@ use crate::{Dim, Matrix, OMatrix, RealField, Scalar, SimdComplexField, SimdRealF /// in their documentation, read their dedicated pages directly. #[repr(transparent)] #[derive(Clone, Hash, Copy)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] // #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Unit { pub(crate) value: T, @@ -58,60 +63,6 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit { } } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Unit; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Unit { - type Archived = Unit; - type Resolver = T::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.value); - self.value.resolve(pos + fp, resolver, fo); - } - } - - impl, S: Fallible + ?Sized> Serialize for Unit { - fn serialize(&self, serializer: &mut S) -> Result { - self.value.serialize(serializer) - } - } - - impl Deserialize, D> for Unit - where - T::Archived: Deserialize, - { - fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { - Ok(Unit { - value: self.value.deserialize(deserializer)?, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use std::ptr::addr_of; - - use bytecheck::CheckBytes; - - use super::Unit; - impl<__C: ?Sized, T: CheckBytes<__C>> CheckBytes<__C> for Unit - where - T: CheckBytes<__C>, - { - type Error = >::Error; - unsafe fn check_bytes<'a>( - value: *const Unit, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = T::check_bytes(addr_of!((*value).value), context)?; - Ok(&*value) - } - } -} - #[cfg(feature = "cuda")] unsafe impl cust_core::DeviceCopy for Unit> where diff --git a/src/geometry/dual_quaternion.rs b/src/geometry/dual_quaternion.rs index 719ae13d..6f1b7053 100644 --- a/src/geometry/dual_quaternion.rs +++ b/src/geometry/dual_quaternion.rs @@ -40,6 +40,11 @@ use simba::scalar::{ClosedNeg, RealField}; /// See #[repr(C)] #[derive(Debug, Copy, Clone)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct DualQuaternion { /// The real component of the quaternion diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index 7f7175dd..92169742 100755 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -66,6 +66,11 @@ use crate::geometry::{AbstractRotation, Point, Translation}; Owned>: Deserialize<'de>, T: Scalar")) )] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] pub struct Isometry { /// The pure rotational part of this isometry. pub rotation: R, @@ -73,97 +78,6 @@ pub struct Isometry { pub translation: Translation, } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Isometry; - use crate::{base::Scalar, geometry::Translation}; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Isometry - where - T::Archived: Scalar, - { - type Archived = Isometry; - type Resolver = (R::Resolver, as Archive>::Resolver); - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.rotation); - self.rotation.resolve(pos + fp, resolver.0, fo); - let (fp, fo) = out_field!(out.translation); - self.translation.resolve(pos + fp, resolver.1, fo); - } - } - - impl, R: Serialize, S: Fallible + ?Sized, const D: usize> - Serialize for Isometry - where - T::Archived: Scalar, - { - fn serialize(&self, serializer: &mut S) -> Result { - Ok(( - self.rotation.serialize(serializer)?, - self.translation.serialize(serializer)?, - )) - } - } - - impl - Deserialize, _D> for Isometry - where - T::Archived: Scalar + Deserialize, - R::Archived: Scalar + Deserialize, - { - fn deserialize(&self, deserializer: &mut _D) -> Result, _D::Error> { - Ok(Isometry { - rotation: self.rotation.deserialize(deserializer)?, - translation: self.translation.deserialize(deserializer)?, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use crate::{Isometry, Scalar, Translation}; - use bytecheck::CheckBytes; - use std::{error::Error, fmt, ptr::addr_of}; - - #[derive(Debug)] - pub enum IsometryCheckBytesError { - Rotation(R), - Translation(T), - } - impl fmt::Display for IsometryCheckBytesError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Rotation(_) => write!(f, "failed to check bytes of isometry rotation"), - Self::Translation(_) => write!(f, "failed to check bytes of isometry translation"), - } - } - } - impl Error for IsometryCheckBytesError {} - - impl<__C: ?Sized, T: Scalar + CheckBytes<__C>, R: CheckBytes<__C>, const D: usize> - CheckBytes<__C> for Isometry - where - T: CheckBytes<__C>, - { - type Error = IsometryCheckBytesError< - as CheckBytes<__C>>::Error, - >::Error, - >; - unsafe fn check_bytes<'a>( - value: *const Isometry, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = R::check_bytes(addr_of!((*value).rotation), context) - .map_err(|e| IsometryCheckBytesError::Rotation(e))?; - let _ = Translation::::check_bytes(addr_of!((*value).translation), context) - .map_err(|e| IsometryCheckBytesError::Translation(e))?; - Ok(&*value) - } - } -} - impl hash::Hash for Isometry where Owned>: hash::Hash, diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index 1119d4e3..7348f676 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -19,6 +19,11 @@ use crate::geometry::{Point3, Projective3}; /// A 3D orthographic projection stored as a homogeneous 4x4 matrix. #[repr(C)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[derive(Copy, Clone)] pub struct Orthographic3 { diff --git a/src/geometry/perspective.rs b/src/geometry/perspective.rs index 8ebab3e4..351960bb 100644 --- a/src/geometry/perspective.rs +++ b/src/geometry/perspective.rs @@ -20,6 +20,11 @@ use crate::geometry::{Point3, Projective3}; /// A 3D perspective projection stored as a homogeneous 4x4 matrix. #[repr(C)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[derive(Copy, Clone)] pub struct Perspective3 { diff --git a/src/geometry/point.rs b/src/geometry/point.rs index cdc590fa..306c18e5 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -36,6 +36,11 @@ use std::mem::MaybeUninit; /// of said transformations for details. #[repr(C)] #[derive(Clone)] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] pub struct OPoint where DefaultAllocator: Allocator, diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 71e38c4c..f38dca6f 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -23,6 +23,11 @@ use crate::geometry::{Point3, Rotation}; /// that may be used as a rotation. #[repr(C)] #[derive(Copy, Clone)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] pub struct Quaternion { /// This quaternion as a 4D vector of coordinates in the `[ x, y, z, w ]` storage order. @@ -97,62 +102,6 @@ where } } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Quaternion; - use crate::base::Vector4; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Quaternion { - type Archived = Quaternion; - type Resolver = as Archive>::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.coords); - self.coords.resolve(pos + fp, resolver, fo); - } - } - - impl, S: Fallible + ?Sized> Serialize for Quaternion { - fn serialize(&self, serializer: &mut S) -> Result { - self.coords.serialize(serializer) - } - } - - impl Deserialize, D> for Quaternion - where - T::Archived: Deserialize, - { - fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { - Ok(Quaternion { - coords: self.coords.deserialize(deserializer)?, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use std::ptr::addr_of; - - use bytecheck::CheckBytes; - - use super::Quaternion; - use crate::Vector4; - impl<__C: ?Sized, T: CheckBytes<__C>> CheckBytes<__C> for Quaternion - where - T: CheckBytes<__C>, - { - type Error = as CheckBytes<__C>>::Error; - unsafe fn check_bytes<'a>( - value: *const Quaternion, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = Vector4::check_bytes(addr_of!((*value).coords), context)?; - Ok(&*value) - } - } -} - impl Quaternion where T::Element: SimdRealField, diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index 4dbcfb43..2a8bf427 100755 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -49,6 +49,11 @@ use crate::geometry::Point; /// * [Conversion to a matrix `matrix`, `to_homogeneous`…](#conversion-to-a-matrix) /// #[repr(C)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[derive(Copy, Clone)] pub struct Rotation { diff --git a/src/geometry/scale.rs b/src/geometry/scale.rs index 333b63cb..37da1ef0 100755 --- a/src/geometry/scale.rs +++ b/src/geometry/scale.rs @@ -17,6 +17,11 @@ use crate::geometry::Point; /// A scale which supports non-uniform scaling. #[repr(C)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[derive(Copy, Clone)] pub struct Scale { @@ -84,62 +89,6 @@ where } } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Scale; - use crate::base::SVector; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Scale { - type Archived = Scale; - type Resolver = as Archive>::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.vector); - self.vector.resolve(pos + fp, resolver, fo); - } - } - - impl, S: Fallible + ?Sized, const D: usize> Serialize for Scale { - fn serialize(&self, serializer: &mut S) -> Result { - self.vector.serialize(serializer) - } - } - - impl Deserialize, _D> - for Scale - where - T::Archived: Deserialize, - { - fn deserialize(&self, deserializer: &mut _D) -> Result, _D::Error> { - Ok(Scale { - vector: self.vector.deserialize(deserializer)?, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use std::ptr::addr_of; - - use bytecheck::CheckBytes; - - use crate::{SVector, Scale}; - impl<__C: ?Sized, T: CheckBytes<__C>, const D: usize> CheckBytes<__C> for Scale - where - T: CheckBytes<__C>, - { - type Error = as CheckBytes<__C>>::Error; - unsafe fn check_bytes<'a>( - value: *const Scale, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = SVector::::check_bytes(addr_of!((*value).vector), context)?; - Ok(&*value) - } - } -} - impl Scale { /// Inverts `self`. /// diff --git a/src/geometry/similarity.rs b/src/geometry/similarity.rs index 9658685e..8c38ff1e 100755 --- a/src/geometry/similarity.rs +++ b/src/geometry/similarity.rs @@ -34,6 +34,11 @@ use crate::geometry::{AbstractRotation, Isometry, Point, Translation}; DefaultAllocator: Allocator>, Owned>: Deserialize<'de>")) )] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] pub struct Similarity { /// The part of this similarity that does not include the scaling factor. pub isometry: Isometry, diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index 7af263bf..bef66f68 100755 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -17,6 +17,11 @@ use crate::geometry::Point; /// A translation. #[repr(C)] +#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-serialize-no-std", + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] #[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))] #[derive(Copy, Clone)] pub struct Translation { @@ -84,62 +89,6 @@ where } } -#[cfg(feature = "rkyv-serialize-no-std")] -mod rkyv_impl { - use super::Translation; - use crate::base::SVector; - use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; - - impl Archive for Translation { - type Archived = Translation; - type Resolver = as Archive>::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - let (fp, fo) = out_field!(out.vector); - self.vector.resolve(pos + fp, resolver, fo); - } - } - - impl, S: Fallible + ?Sized, const D: usize> Serialize for Translation { - fn serialize(&self, serializer: &mut S) -> Result { - self.vector.serialize(serializer) - } - } - - impl Deserialize, _D> - for Translation - where - T::Archived: Deserialize, - { - fn deserialize(&self, deserializer: &mut _D) -> Result, _D::Error> { - Ok(Translation { - vector: self.vector.deserialize(deserializer)?, - }) - } - } -} -#[cfg(feature = "rkyv-serialize")] -mod bytecheck_impl { - use std::ptr::addr_of; - - use bytecheck::CheckBytes; - - use crate::{SVector, Translation}; - impl<__C: ?Sized, T: CheckBytes<__C>, const D: usize> CheckBytes<__C> for Translation - where - T: CheckBytes<__C>, - { - type Error = as CheckBytes<__C>>::Error; - unsafe fn check_bytes<'a>( - value: *const Translation, - context: &mut __C, - ) -> Result<&'a Self, Self::Error> { - let _ = SVector::::check_bytes(addr_of!((*value).vector), context)?; - Ok(&*value) - } - } -} - impl Translation { /// Creates a new translation from the given vector. #[inline] diff --git a/src/lib.rs b/src/lib.rs index 92b28dcb..1ee1a3ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,12 +78,13 @@ an optimized set of tools for computer graphics and physics. Those features incl unused_mut, unused_parens, unused_qualifications, - unused_results, rust_2018_idioms, rust_2018_compatibility, future_incompatible, missing_copy_implementations )] +#![cfg_attr(feature = "rkyv-serialize-no-std", warn(unused_results))] // TODO: deny this once bytecheck stops generating warnings. +#![cfg_attr(not(feature = "rkyv-serialize-no-std"), deny(unused_results))] #![doc( html_favicon_url = "https://nalgebra.org/img/favicon.ico", html_root_url = "https://docs.rs/nalgebra/0.25.0" From b656faa233bccc7c201396dd1348d2bebd88b3b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 30 Apr 2022 10:59:26 +0200 Subject: [PATCH 81/81] Release v0.31.0 --- CHANGELOG.md | 22 ++++++++++++++++++++++ Cargo.toml | 2 +- examples/cargo/Cargo.toml | 2 +- nalgebra-glm/Cargo.toml | 4 ++-- nalgebra-lapack/Cargo.toml | 6 +++--- nalgebra-macros/Cargo.toml | 2 +- nalgebra-sparse/Cargo.toml | 6 +++--- 7 files changed, 33 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf6b6a0f..c00c01fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,29 @@ documented here. This project adheres to [Semantic Versioning](https://semver.org/). +## [0.31.0] (30 Apr. 2022) + +### Breaking changes +- Switch to `cust` 0.3 (for CUDA support). +- Switch to `rkyv` 0.7 +- Remove support for serialization based on `abomonation`. +- Remove support for conversions between `nalgebra` types and `glam` 0.13. + +### Modified +- The aliases for `Const` types have been simplified to help `rust-analyzer`. + +### Added +- Add `TryFrom` conversion between `UnitVector2/3/4` and `glam`’s `Vec2/3/4`. +- `nalgebra-sparse`: added support for serialization of sparse matrices with `serde`. +- `nalgebra-sparse`: add a CSC matrix constructor from unsorted (but valid) data. +- `nalgebra-lapack`: add generalized eigenvalues/eigenvectors calculation + QZ decomposition. + +### Fixed +- Improve stability of SVD. +- Fix slerp for `UnitComplex`. + ## [0.30.1] (09 Jan. 2022) + ### Added - Add conversion from/to types of `glam` 0.19 and 0.20. diff --git a/Cargo.toml b/Cargo.toml index ffd37598..732676ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nalgebra" -version = "0.30.1" +version = "0.31.0" authors = [ "Sébastien Crozet " ] description = "General-purpose linear algebra library with transformations and statically-sized or dynamically-sized matrices." diff --git a/examples/cargo/Cargo.toml b/examples/cargo/Cargo.toml index 501ae23e..63e70aab 100644 --- a/examples/cargo/Cargo.toml +++ b/examples/cargo/Cargo.toml @@ -4,7 +4,7 @@ version = "0.0.0" authors = [ "You" ] [dependencies] -nalgebra = "0.30.0" +nalgebra = "0.31.0" [[bin]] name = "example" diff --git a/nalgebra-glm/Cargo.toml b/nalgebra-glm/Cargo.toml index adf05fa3..e700af37 100644 --- a/nalgebra-glm/Cargo.toml +++ b/nalgebra-glm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nalgebra-glm" -version = "0.16.0" +version = "0.17.0" authors = ["sebcrozet "] description = "A computer-graphics oriented API for nalgebra, inspired by the C++ GLM library." @@ -36,4 +36,4 @@ convert-glam018 = [ "nalgebra/glam018" ] num-traits = { version = "0.2", default-features = false } approx = { version = "0.5", default-features = false } simba = { version = "0.7", default-features = false } -nalgebra = { path = "..", version = "0.30", default-features = false } +nalgebra = { path = "..", version = "0.31", default-features = false } diff --git a/nalgebra-lapack/Cargo.toml b/nalgebra-lapack/Cargo.toml index 7cdf9f78..91517a8d 100644 --- a/nalgebra-lapack/Cargo.toml +++ b/nalgebra-lapack/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nalgebra-lapack" -version = "0.21.0" +version = "0.22.0" authors = [ "Sébastien Crozet ", "Andrew Straw " ] description = "Matrix decompositions using nalgebra matrices and Lapack bindings." @@ -29,7 +29,7 @@ accelerate = ["lapack-src/accelerate"] intel-mkl = ["lapack-src/intel-mkl"] [dependencies] -nalgebra = { version = "0.30", path = ".." } +nalgebra = { version = "0.31", path = ".." } num-traits = "0.2" num-complex = { version = "0.4", default-features = false } simba = "0.7" @@ -39,7 +39,7 @@ lapack-src = { version = "0.8", default-features = false } # clippy = "*" [dev-dependencies] -nalgebra = { version = "0.30", features = [ "arbitrary", "rand" ], path = ".." } +nalgebra = { version = "0.31", features = [ "arbitrary", "rand" ], path = ".." } proptest = { version = "1", default-features = false, features = ["std"] } quickcheck = "1" approx = "0.5" diff --git a/nalgebra-macros/Cargo.toml b/nalgebra-macros/Cargo.toml index bd37b689..6e35f9e9 100644 --- a/nalgebra-macros/Cargo.toml +++ b/nalgebra-macros/Cargo.toml @@ -21,5 +21,5 @@ quote = "1.0" proc-macro2 = "1.0" [dev-dependencies] -nalgebra = { version = "0.30.0", path = ".." } +nalgebra = { version = "0.31.0", path = ".." } trybuild = "1.0.42" diff --git a/nalgebra-sparse/Cargo.toml b/nalgebra-sparse/Cargo.toml index eec7326d..7220373e 100644 --- a/nalgebra-sparse/Cargo.toml +++ b/nalgebra-sparse/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nalgebra-sparse" -version = "0.6.0" +version = "0.7.0" authors = [ "Andreas Longva", "Sébastien Crozet " ] edition = "2018" description = "Sparse matrix computation based on nalgebra." @@ -24,7 +24,7 @@ io = [ "pest", "pest_derive" ] slow-tests = [] [dependencies] -nalgebra = { version="0.30", path = "../" } +nalgebra = { version="0.31", path = "../" } num-traits = { version = "0.2", default-features = false } proptest = { version = "1.0", optional = true } matrixcompare-core = { version = "0.1.0", optional = true } @@ -35,7 +35,7 @@ serde = { version = "1.0", default-features = false, features = [ "derive" ], op [dev-dependencies] itertools = "0.10" matrixcompare = { version = "0.3.0", features = [ "proptest-support" ] } -nalgebra = { version="0.30", path = "../", features = ["compare"] } +nalgebra = { version="0.31", path = "../", features = ["compare"] } serde_json = "1.0" [package.metadata.docs.rs]