From 7230ae1e638893d2aa87b30f96dfd3cfa1e73f46 Mon Sep 17 00:00:00 2001 From: metric-space Date: Tue, 18 Jan 2022 22:35:11 -0500 Subject: [PATCH 01/53] 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 02/53] 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 03/53] 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 04/53] 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 05/53] 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 06/53] 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 07/53] 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 08/53] 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 09/53] 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 10/53] 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 11/53] 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 12/53] 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 13/53] 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 14/53] 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 d5069f318eb77f411090eda63c5b053f9dafd69d Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 5 Feb 2022 23:44:05 -0500 Subject: [PATCH 15/53] 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 a4de6a83cca2a0562418e5feeaffef4756468503 Mon Sep 17 00:00:00 2001 From: metric-space Date: Wed, 9 Feb 2022 08:48:38 -0500 Subject: [PATCH 16/53] 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 17/53] 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 18/53] 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 19/53] 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 20/53] 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 21/53] 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 22/53] 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 5e10ca46cbbf9e6b5c68b26d4b3570669650db7b Mon Sep 17 00:00:00 2001 From: metric-space Date: Tue, 15 Feb 2022 01:45:33 -0500 Subject: [PATCH 23/53] 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 24/53] 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 3413ab7da82cc4fb7662952c6687817785d80f22 Mon Sep 17 00:00:00 2001 From: metric-space Date: Sat, 5 Mar 2022 13:52:42 -0500 Subject: [PATCH 25/53] 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 26/53] 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 27/53] 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 28/53] 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 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 29/53] 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 30/53] 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 31/53] 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 32/53] 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 33/53] 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 34/53] 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 35/53] 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 36/53] 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 37/53] 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 38/53] 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 39/53] 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 40/53] 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 41/53] 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 42/53] 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 43/53] 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 44/53] 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 45/53] 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 46/53] 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 47/53] 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 48/53] 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 49/53] 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 50/53] 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 51/53] 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] From b38ceb4a1dda2243ba56ae6ec1513f11bce1bb53 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Tue, 3 May 2022 14:19:24 -0600 Subject: [PATCH 52/53] fix failing test --- nalgebra-sparse/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/nalgebra-sparse/Cargo.toml b/nalgebra-sparse/Cargo.toml index c5ab9614..7220373e 100644 --- a/nalgebra-sparse/Cargo.toml +++ b/nalgebra-sparse/Cargo.toml @@ -33,6 +33,7 @@ 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.31", path = "../", features = ["compare"] } serde_json = "1.0" From 3195fba72763790f156c3179ee67a5dcf3345597 Mon Sep 17 00:00:00 2001 From: Saurabh Raje Date: Tue, 3 May 2022 14:21:34 -0600 Subject: [PATCH 53/53] Create rust.yml --- .github/workflows/rust.yml | 139 +++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 .github/workflows/rust.yml diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 00000000..304ecf5f --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,139 @@ +name: nalgebra CI build + +on: + push: + branches: [ dev, master ] + pull_request: + branches: [ dev, master ] + +env: + CARGO_TERM_COLOR: always + +jobs: + check-fmt: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Check formatting + run: cargo fmt -- --check + clippy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install clippy + run: rustup component add clippy + - name: Run clippy + run: cargo clippy + build-nalgebra: + runs-on: ubuntu-latest +# env: +# RUSTFLAGS: -D warnings + steps: + - uses: actions/checkout@v2 + - name: Build --no-default-feature + run: cargo build --no-default-features; + - name: Build (default features) + run: cargo build; + - name: Build --features serde-serialize + run: cargo build --features serde-serialize + - name: Build nalgebra-lapack + run: cd nalgebra-lapack; cargo build; + - 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: + runs-on: ubuntu-latest + steps: + # Needed because the --all-features build which enables cuda support. + - uses: Jimver/cuda-toolkit@v0.2.4 + - uses: actions/checkout@v2 + - run: cargo build --all-features; + - run: cargo build -p nalgebra-glm --all-features; + test-nalgebra: + runs-on: ubuntu-latest +# env: +# RUSTFLAGS: -D warnings + steps: + - uses: actions/checkout@v2 + - name: test + 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; + test-nalgebra-sparse: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - 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,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,serde-serialize,slow-tests slow + test-nalgebra-macros: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: test nalgebra-macros + run: cargo test -p nalgebra-macros + build-wasm: + runs-on: ubuntu-latest +# env: +# RUSTFLAGS: -D warnings + steps: + - uses: actions/checkout@v2 + - run: rustup target add wasm32-unknown-unknown + - name: build nalgebra + run: cargo build --verbose --target wasm32-unknown-unknown; + - name: build nalgebra-glm + run: cargo build -p nalgebra-glm --verbose --target wasm32-unknown-unknown; + build-no-std: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install latest nightly + uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + components: rustfmt + - name: install xargo + run: cp .github/Xargo.toml .; rustup component add rust-src; cargo install -f xargo; + - name: build x86_64-unknown-linux-gnu + run: xargo build --verbose --no-default-features --target=x86_64-unknown-linux-gnu; + - name: build x86_64-unknown-linux-gnu --features rand-no-std + run: xargo build --verbose --no-default-features --target=x86_64-unknown-linux-gnu; + - name: build x86_64-unknown-linux-gnu --features alloc + run: xargo build --verbose --no-default-features --features alloc --target=x86_64-unknown-linux-gnu; + - name: build thumbv7em-none-eabihf + run: xargo build --verbose --no-default-features --target=thumbv7em-none-eabihf; + - name: build x86_64-unknown-linux-gnu nalgebra-glm + run: xargo build --verbose --no-default-features -p nalgebra-glm --target=x86_64-unknown-linux-gnu; + - name: build thumbv7em-none-eabihf nalgebra-glm + run: xargo build --verbose --no-default-features -p nalgebra-glm --target=thumbv7em-none-eabihf; + build-cuda: + 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: + toolchain: nightly-2021-12-04 + override: true + - 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 + env: + CUDA_ARCH: "350"