Skip to content

Commit

Permalink
Implement the single-trait approach.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebcrozet committed Aug 2, 2021
1 parent 2243a11 commit b8b19b7
Show file tree
Hide file tree
Showing 133 changed files with 3,292 additions and 4,035 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ documented here.

This project adheres to [Semantic Versioning](https://semver.org/).

## [0.29.0]
### Modified
- The closure given to `apply`, `zip_apply`, `zip_zip_apply` must now modify the
first argument inplace, instead of returning a new value. This makes these
methods more versatile, and avoid useless clones when using non-Copy scalar
types.

## [0.28.0]
### Added
- Implement `Hash` for `Transform`.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ lto = true

[package.metadata.docs.rs]
# Enable certain features when building docs for docs.rs
features = [ "proptest-support", "compare", "macros" ]
features = [ "proptest-support", "compare", "macros", "rand" ]
9 changes: 3 additions & 6 deletions benches/core/matrix.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use na::{
Const, DMatrix, DVector, Dynamic, Matrix2, Matrix3, Matrix4, OMatrix, Vector2, Vector3,
Vector4, U10,
};
use na::{DMatrix, DVector, Matrix2, Matrix3, Matrix4, OMatrix, Vector2, Vector3, Vector4, U10};
use rand::Rng;
use rand_isaac::IsaacRng;
use std::ops::{Add, Div, Mul, Sub};
Expand Down Expand Up @@ -189,15 +186,15 @@ fn axpy(bench: &mut criterion::Criterion) {
fn tr_mul_to(bench: &mut criterion::Criterion) {
let a = DMatrix::<f64>::new_random(1000, 1000);
let b = DVector::<f64>::new_random(1000);
let mut c = DVector::new_uninitialized_generic(Dynamic::new(1000), Const::<1>);
let mut c = DVector::from_element(1000, 0.0);

bench.bench_function("tr_mul_to", move |bh| bh.iter(|| a.tr_mul_to(&b, &mut c)));
}

fn mat_mul_mat(bench: &mut criterion::Criterion) {
let a = DMatrix::<f64>::new_random(100, 100);
let b = DMatrix::<f64>::new_random(100, 100);
let mut ab = DMatrix::new_uninitialized_generic(Dynamic::new(100), Dynamic::new(100));
let mut ab = DMatrix::<f64>::from_element(100, 100, 0.0);

bench.bench_function("mat_mul_mat", move |bh| {
bh.iter(|| {
Expand Down
2 changes: 2 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
too-many-arguments-threshold = 8
type-complexity-threshold = 675
2 changes: 1 addition & 1 deletion nalgebra-glm/src/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ pub type DMat4x4 = Matrix4<f64>;
pub type Mat2 = Matrix2<f32>;
/// A 2x2 matrix with `f32` components.
pub type Mat2x2 = Matrix2<f32>;
/// A 2x2 matrix with `f32` components.
/// A 2x3 matrix with `f32` components.
pub type Mat2x3 = Matrix2x3<f32>;
/// A 2x4 matrix with `f32` components.
pub type Mat2x4 = Matrix2x4<f32>;
Expand Down
10 changes: 10 additions & 0 deletions nalgebra-glm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@
and keep in mind it is possible to convert, e.g., an `Isometry3` to a `Mat4` and vice-versa (see the [conversions section](#conversions)).
*/

#![deny(
nonstandard_style,
unused,
missing_docs,
rust_2018_idioms,
rust_2018_compatibility,
future_incompatible,
missing_copy_implementations,
missing_debug_implementations
)]
#![doc(html_favicon_url = "https://nalgebra.org/img/favicon.ico")]
#![cfg_attr(not(feature = "std"), no_std)]

Expand Down
10 changes: 5 additions & 5 deletions nalgebra-lapack/src/cholesky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use num_complex::Complex;

use na::allocator::Allocator;
use na::dimension::Dim;
use na::storage::Storage;
use na::storage::RawStorage;
use na::{DefaultAllocator, Matrix, OMatrix, Scalar};

use lapack;
Expand All @@ -24,17 +24,17 @@ use lapack;
OMatrix<T, D, D>: Deserialize<'de>"))
)]
#[derive(Clone, Debug)]
pub struct Cholesky<T, D: Dim>
pub struct Cholesky<T: Scalar, D: Dim>
where
DefaultAllocator: Allocator<T, D, D>,
{
l: OMatrix<T, D, D>,
}

impl<T: Copy, D: Dim> Copy for Cholesky<T, D>
impl<T: Scalar + Copy, D: Dim> Copy for Cholesky<T, D>
where
DefaultAllocator: Allocator<T, D, D>,
Owned<T, D, D>: Copy,
OMatrix<T, D, D>: Copy,
{
}

Expand Down Expand Up @@ -104,7 +104,7 @@ where
b: &Matrix<T, R2, C2, S2>,
) -> Option<OMatrix<T, R2, C2>>
where
S2: Storage<T, R2, C2>,
S2: RawStorage<T, R2, C2>,
DefaultAllocator: Allocator<T, R2, C2>,
{
let mut res = b.clone_owned();
Expand Down
45 changes: 6 additions & 39 deletions nalgebra-lapack/src/eigen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt;

#[cfg(feature = "serde-serialize")]
use serde::{Deserialize, Serialize};

Expand All @@ -11,7 +9,7 @@ use simba::scalar::RealField;
use crate::ComplexHelper;
use na::allocator::Allocator;
use na::dimension::{Const, Dim};
use na::storage::Storage;
use na::storage::RawStorage;
use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};

use lapack;
Expand All @@ -34,7 +32,8 @@ use lapack;
OMatrix<T, D, D>: Deserialize<'de>")
)
)]
pub struct Eigen<T, D: Dim>
#[derive(Clone, Debug)]
pub struct Eigen<T: Scalar, D: Dim>
where
DefaultAllocator: Allocator<T, D> + Allocator<T, D, D>,
{
Expand All @@ -46,44 +45,14 @@ where
pub left_eigenvectors: Option<OMatrix<T, D, D>>,
}

impl<T: Copy, D: Dim> Copy for Eigen<T, D>
impl<T: Scalar + Copy, D: Dim> Copy for Eigen<T, D>
where
DefaultAllocator: Allocator<T, D> + Allocator<T, D, D>,
OVector<T, D>: Copy,
OMatrix<T, D, D>: Copy,
{
}

impl<T: Clone, D: Dim> Clone for Eigen<T, D>
where
DefaultAllocator: Allocator<T, D> + Allocator<T, D, D>,
OVector<T, D>: Clone,
OMatrix<T, D, D>: Clone,
{
fn clone(&self) -> Self {
Self {
eigenvalues: self.eigenvalues.clone(),
eigenvectors: self.eigenvectors.clone(),
left_eigenvectors: self.left_eigenvectors.clone(),
}
}
}

impl<T: fmt::Debug, D: Dim> fmt::Debug for Eigen<T, D>
where
DefaultAllocator: Allocator<T, D> + Allocator<T, D, D>,
OVector<T, D>: fmt::Debug,
OMatrix<T, D, D>: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Eigen")
.field("eigenvalues", &self.eigenvalues)
.field("eigenvectors", &self.eigenvectors)
.field("left_eigenvectors", &self.left_eigenvectors)
.finish()
}
}

impl<T: EigenScalar + RealField, D: Dim> Eigen<T, D>
where
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
Expand All @@ -104,13 +73,11 @@ where
let ljob = if left_eigenvectors { b'V' } else { b'T' };
let rjob = if eigenvectors { b'V' } else { b'T' };

let (nrows, ncols) = m.data.shape();
let (nrows, ncols) = m.shape_generic();
let n = nrows.value();

let lda = n as i32;

// IMPORTANT TODO: this is still UB.

let mut wr = unsafe { Matrix::new_uninitialized_generic(nrows, Const::<1>).assume_init() };
// TODO: Tap into the workspace.
let mut wi = unsafe { Matrix::new_uninitialized_generic(nrows, Const::<1>).assume_init() };
Expand Down Expand Up @@ -275,7 +242,7 @@ where
"Unable to compute the eigenvalue decomposition of a non-square matrix."
);

let nrows = m.data.shape().0;
let nrows = m.shape_generic().0;
let n = nrows.value();

let lda = n as i32;
Expand Down
7 changes: 3 additions & 4 deletions nalgebra-lapack/src/hessenberg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use num_complex::Complex;
use crate::ComplexHelper;
use na::allocator::Allocator;
use na::dimension::{Const, DimDiff, DimSub, U1};
use na::storage::Storage;
use na::storage::RawStorage;
use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};

use lapack;
Expand Down Expand Up @@ -48,7 +48,7 @@ where
{
/// Computes the hessenberg decomposition of the matrix `m`.
pub fn new(mut m: OMatrix<T, D, D>) -> Self {
let nrows = m.data.shape().0;
let nrows = m.shape_generic().0;
let n = nrows.value() as i32;

assert!(
Expand All @@ -60,7 +60,6 @@ where
"Unable to compute the hessenberg decomposition of an empty matrix."
);

// IMPORTANT TODO: this is still UB.
let mut tau = unsafe {
Matrix::new_uninitialized_generic(nrows.sub(Const::<1>), Const::<1>).assume_init()
};
Expand All @@ -85,7 +84,7 @@ where
);
lapack_panic!(info);

Self { h: m, tau: tau }
Self { h: m, tau }
}

/// Computes the hessenberg matrix of this decomposition.
Expand Down
1 change: 0 additions & 1 deletion nalgebra-lapack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ impl ComplexHelper for Complex<f64> {
}
}

// This is UB.
unsafe fn uninitialized_vec<T: Copy>(n: usize) -> Vec<T> {
let mut res = Vec::new();
res.reserve_exact(n);
Expand Down
16 changes: 8 additions & 8 deletions nalgebra-lapack/src/lu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use num_complex::Complex;
use crate::ComplexHelper;
use na::allocator::Allocator;
use na::dimension::{Const, Dim, DimMin, DimMinimum};
use na::storage::Storage;
use na::storage::RawStorage;
use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};

use lapack;
Expand Down Expand Up @@ -61,7 +61,7 @@ where
{
/// Computes the LU decomposition with partial (row) pivoting of `matrix`.
pub fn new(mut m: OMatrix<T, R, C>) -> Self {
let (nrows, ncols) = m.data.shape();
let (nrows, ncols) = m.shape_generic();
let min_nrows_ncols = nrows.min(ncols);
let nrows = nrows.value() as i32;
let ncols = ncols.value() as i32;
Expand All @@ -87,7 +87,7 @@ where
#[inline]
#[must_use]
pub fn l(&self) -> OMatrix<T, R, DimMinimum<R, C>> {
let (nrows, ncols) = self.lu.data.shape();
let (nrows, ncols) = self.lu.shape_generic();
let mut res = self.lu.columns_generic(0, nrows.min(ncols)).into_owned();

res.fill_upper_triangle(Zero::zero(), 1);
Expand All @@ -100,7 +100,7 @@ where
#[inline]
#[must_use]
pub fn u(&self) -> OMatrix<T, DimMinimum<R, C>, C> {
let (nrows, ncols) = self.lu.data.shape();
let (nrows, ncols) = self.lu.shape_generic();
let mut res = self.lu.rows_generic(0, nrows.min(ncols)).into_owned();

res.fill_lower_triangle(Zero::zero(), 1);
Expand All @@ -115,7 +115,7 @@ where
#[inline]
#[must_use]
pub fn p(&self) -> OMatrix<T, R, R> {
let (dim, _) = self.lu.data.shape();
let (dim, _) = self.lu.shape_generic();
let mut id = Matrix::identity_generic(dim, dim);
self.permute(&mut id);

Expand Down Expand Up @@ -191,7 +191,7 @@ where
b: &Matrix<T, R2, C2, S2>,
) -> Option<OMatrix<T, R2, C2>>
where
S2: Storage<T, R2, C2>,
S2: RawStorage<T, R2, C2>,
DefaultAllocator: Allocator<T, R2, C2> + Allocator<i32, R2>,
{
let mut res = b.clone_owned();
Expand All @@ -209,7 +209,7 @@ where
b: &Matrix<T, R2, C2, S2>,
) -> Option<OMatrix<T, R2, C2>>
where
S2: Storage<T, R2, C2>,
S2: RawStorage<T, R2, C2>,
DefaultAllocator: Allocator<T, R2, C2> + Allocator<i32, R2>,
{
let mut res = b.clone_owned();
Expand All @@ -227,7 +227,7 @@ where
b: &Matrix<T, R2, C2, S2>,
) -> Option<OMatrix<T, R2, C2>>
where
S2: Storage<T, R2, C2>,
S2: RawStorage<T, R2, C2>,
DefaultAllocator: Allocator<T, R2, C2> + Allocator<i32, R2>,
{
let mut res = b.clone_owned();
Expand Down
17 changes: 9 additions & 8 deletions nalgebra-lapack/src/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use num_complex::Complex;
use crate::ComplexHelper;
use na::allocator::Allocator;
use na::dimension::{Const, Dim, DimMin, DimMinimum};
use na::storage::Storage;
use na::storage::RawStorage;
use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};

use lapack;
Expand Down Expand Up @@ -54,14 +54,15 @@ where
{
/// Computes the QR decomposition of the matrix `m`.
pub fn new(mut m: OMatrix<T, R, C>) -> Self {
let (nrows, ncols) = m.data.shape();
let (nrows, ncols) = m.shape_generic();

let mut info = 0;
let mut tau =
unsafe { Matrix::new_uninitialized_generic(nrows.min(ncols), U1).assume_init() };
let mut tau = unsafe {
Matrix::new_uninitialized_generic(nrows.min(ncols), Const::<1>).assume_init()
};

if nrows.value() == 0 || ncols.value() == 0 {
return Self { qr: m, tau: tau };
return Self { qr: m, tau };
}

let lwork = T::xgeqrf_work_size(
Expand All @@ -86,14 +87,14 @@ where
&mut info,
);

Self { qr: m, tau: tau }
Self { qr: m, tau }
}

/// Retrieves the upper trapezoidal submatrix `R` of this decomposition.
#[inline]
#[must_use]
pub fn r(&self) -> OMatrix<T, DimMinimum<R, C>, C> {
let (nrows, ncols) = self.qr.data.shape();
let (nrows, ncols) = self.qr.shape_generic();
self.qr.rows_generic(0, nrows.min(ncols)).upper_triangle()
}
}
Expand All @@ -119,7 +120,7 @@ where
#[inline]
#[must_use]
pub fn q(&self) -> OMatrix<T, R, DimMinimum<R, C>> {
let (nrows, ncols) = self.qr.data.shape();
let (nrows, ncols) = self.qr.shape_generic();
let min_nrows_ncols = nrows.min(ncols);

if min_nrows_ncols.value() == 0 {
Expand Down
Loading

0 comments on commit b8b19b7

Please sign in to comment.