diff --git a/CHANGELOG.md b/CHANGELOG.md index 2950a1df..e035e084 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ documented here. This project adheres to [Semantic Versioning](https://semver.org/). +## [0.31.2] (09 Oct. 2022) + +### Modified +- Use `#[inline]` on the `Dim` implementation for `Const` to improve opt-level 1 performance. +- Make the `Point::new` constructions const-fn. + +### Added +- Add `UnitVector::cast` to change the underlying scalar type. + + ## [0.31.1] (31 July 2022) ### Modified diff --git a/Cargo.toml b/Cargo.toml index 5b9940df..89de73e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nalgebra" -version = "0.31.1" +version = "0.31.2" authors = [ "Sébastien Crozet " ] description = "General-purpose linear algebra library with transformations and statically-sized or dynamically-sized matrices." diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 2b302e37..25dc07fb 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -211,6 +211,13 @@ impl CooMatrix { self.values.push(v); } + /// Clear all triplets from the matrix. + pub fn clear_triplets(&mut self) { + self.col_indices.clear(); + self.row_indices.clear(); + self.values.clear(); + } + /// The number of rows in the matrix. #[inline] #[must_use] diff --git a/nalgebra-sparse/tests/unit_tests/coo.rs b/nalgebra-sparse/tests/unit_tests/coo.rs index c70c5f97..d232b041 100644 --- a/nalgebra-sparse/tests/unit_tests/coo.rs +++ b/nalgebra-sparse/tests/unit_tests/coo.rs @@ -226,6 +226,29 @@ fn coo_push_valid_entries() { ); } +#[test] +fn coo_clear_triplets_valid_entries() { + let mut coo = CooMatrix::new(3, 3); + + coo.push(0, 0, 1); + coo.push(0, 0, 2); + coo.push(2, 2, 3); + assert_eq!( + coo.triplet_iter().collect::>(), + vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)] + ); + coo.clear_triplets(); + assert_eq!(coo.triplet_iter().collect::>(), vec![]); + // making sure everyhting works after clearing + coo.push(0, 0, 1); + coo.push(0, 0, 2); + coo.push(2, 2, 3); + assert_eq!( + coo.triplet_iter().collect::>(), + vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)] + ); +} + #[test] fn coo_push_out_of_bounds_entries() { { diff --git a/src/base/default_allocator.rs b/src/base/default_allocator.rs index 09197bbd..6aaadfc7 100644 --- a/src/base/default_allocator.rs +++ b/src/base/default_allocator.rs @@ -26,7 +26,7 @@ use std::mem::{ManuallyDrop, MaybeUninit}; * Allocator. * */ -/// An allocator based on `GenericArray` and `VecStorage` for statically-sized and dynamically-sized +/// An allocator based on [`ArrayStorage`] and [`VecStorage`] for statically-sized and dynamically-sized /// matrices respectively. #[derive(Copy, Clone, Debug)] pub struct DefaultAllocator; diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 4be97586..b1b700d7 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -252,14 +252,17 @@ pub trait ToTypenum { } unsafe impl Dim for Const { + #[inline] fn try_to_usize() -> Option { Some(T) } + #[inline] fn value(&self) -> usize { T } + #[inline] fn from_usize(dim: usize) -> Self { assert_eq!(dim, T); Self diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 8f8786c1..d9294c9e 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -2186,3 +2186,28 @@ where } } } + +impl Unit> +where + T: Scalar, + D: Dim, + S: RawStorage, +{ + /// Cast the components of `self` to another type. + /// + /// # Example + /// ``` + /// # use nalgebra::Vector3; + /// let v = Vector3::::y_axis(); + /// let v2 = v.cast::(); + /// assert_eq!(v2, Vector3::::y_axis()); + /// ``` + pub fn cast(self) -> Unit> + where + T: Scalar, + OVector: SupersetOf>, + DefaultAllocator: Allocator, + { + Unit::new_unchecked(crate::convert_ref(self.as_ref())) + } +} diff --git a/src/geometry/point_construction.rs b/src/geometry/point_construction.rs index 2136080a..598cf4ed 100644 --- a/src/geometry/point_construction.rs +++ b/src/geometry/point_construction.rs @@ -202,6 +202,24 @@ impl Point1 { /// assert_eq!(p.x, 1.0); /// ``` #[inline] + #[cfg(not(feature = "cuda"))] + pub const fn new(x: T) -> Self { + Point { + coords: Vector1::new(x), + } + } + + /// Initializes this point from its components. + /// + /// # Example + /// + /// ``` + /// # use nalgebra::Point1; + /// let p = Point1::new(1.0); + /// assert_eq!(p.x, 1.0); + /// ``` + #[inline] + #[cfg(feature = "cuda")] pub fn new(x: T) -> Self { Point { coords: Vector1::new(x), @@ -216,6 +234,19 @@ macro_rules! componentwise_constructors_impl( #[doc = $doc] #[doc = "```"] #[inline] + #[cfg(not(feature = "cuda"))] + pub const fn new($($args: T),*) -> Self { + Point { coords: $Vector::new($($args),*) } + } + + // TODO: always let new be const once CUDA updates its supported + // nightly version to something more recent. + #[doc = "Initializes this point from its components."] + #[doc = "# Example\n```"] + #[doc = $doc] + #[doc = "```"] + #[inline] + #[cfg(feature = "cuda")] pub fn new($($args: T),*) -> Self { Point { coords: $Vector::new($($args),*) } } diff --git a/src/lib.rs b/src/lib.rs index 8da86531..86a5dc6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,28 +46,34 @@ fn main() { **nalgebra** is meant to be a general-purpose, low-dimensional, linear algebra library, with an optimized set of tools for computer graphics and physics. Those features include: -* A single parametrizable type `Matrix` for vectors, (square or rectangular) matrices, and slices - with dimensions known either at compile-time (using type-level integers) or at runtime. +* A single parametrizable type [`Matrix`](Matrix) for vectors, (square or rectangular) matrices, and + slices with dimensions known either at compile-time (using type-level integers) or at runtime. * Matrices and vectors with compile-time sizes are statically allocated while dynamic ones are allocated on the heap. -* Convenient aliases for low-dimensional matrices and vectors: `Vector1` to `Vector6` and - `Matrix1x1` to `Matrix6x6`, including rectangular matrices like `Matrix2x5`. -* Points sizes known at compile time, and convenience aliases: `Point1` to `Point6`. -* Translation (seen as a transformation that composes by multiplication): `Translation2`, - `Translation3`. -* Rotation matrices: `Rotation2`, `Rotation3`. -* Quaternions: `Quaternion`, `UnitQuaternion` (for 3D rotation). -* Unit complex numbers can be used for 2D rotation: `UnitComplex`. -* Algebraic entities with a norm equal to one: `Unit`, e.g., `Unit>`. -* Isometries (translation ⨯ rotation): `Isometry2`, `Isometry3` -* Similarity transformations (translation ⨯ rotation ⨯ uniform scale): `Similarity2`, `Similarity3`. -* Affine transformations stored as a homogeneous matrix: `Affine2`, `Affine3`. -* Projective (i.e. invertible) transformations stored as a homogeneous matrix: `Projective2`, - `Projective3`. +* Convenient aliases for low-dimensional matrices and vectors: [`Vector1`](Vector1) to + [`Vector6`](Vector6) and [`Matrix1x1`](Matrix1) to [`Matrix6x6`](Matrix6), including rectangular + matrices like [`Matrix2x5`](Matrix2x5). +* Points sizes known at compile time, and convenience aliases: [`Point1`](Point1) to + [`Point6`](Point6). +* Translation (seen as a transformation that composes by multiplication): + [`Translation2`](Translation2), [`Translation3`](Translation3). +* Rotation matrices: [`Rotation2`](Rotation2), [`Rotation3`](Rotation3). +* Quaternions: [`Quaternion`](Quaternion), [`UnitQuaternion`](UnitQuaternion) (for 3D rotation). +* Unit complex numbers can be used for 2D rotation: [`UnitComplex`](UnitComplex). +* Algebraic entities with a norm equal to one: [`Unit`](Unit), e.g., `Unit>`. +* Isometries (translation ⨯ rotation): [`Isometry2`](Isometry2), [`Isometry3`](Isometry3) +* Similarity transformations (translation ⨯ rotation ⨯ uniform scale): + [`Similarity2`](Similarity2), [`Similarity3`](Similarity3). +* Affine transformations stored as a homogeneous matrix: + [`Affine2`](Affine2), [`Affine3`](Affine3). +* Projective (i.e. invertible) transformations stored as a homogeneous matrix: + [`Projective2`](Projective2), [`Projective3`](Projective3). * General transformations that does not have to be invertible, stored as a homogeneous matrix: - `Transform2`, `Transform3`. -* 3D projections for computer graphics: `Perspective3`, `Orthographic3`. -* Matrix factorizations: `Cholesky`, `QR`, `LU`, `FullPivLU`, `SVD`, `Schur`, `Hessenberg`, `SymmetricEigen`. + [`Transform2`](Transform2), [`Transform3`](Transform3). +* 3D projections for computer graphics: [`Perspective3`](Perspective3), + [`Orthographic3`](Orthographic3). +* Matrix factorizations: [`Cholesky`](Cholesky), [`QR`](QR), [`LU`](LU), [`FullPivLU`](FullPivLU), + [`SVD`](SVD), [`Schur`](Schur), [`Hessenberg`](Hessenberg), [`SymmetricEigen`](SymmetricEigen). * Insertion and removal of rows of columns of a matrix. */