Merge branch 'dev'

This commit is contained in:
Sébastien Crozet 2022-10-09 22:16:43 +02:00
commit 2adb4f0b26
9 changed files with 126 additions and 21 deletions

View File

@ -4,6 +4,16 @@ documented here.
This project adheres to [Semantic Versioning](https://semver.org/). 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) ## [0.31.1] (31 July 2022)
### Modified ### Modified

View File

@ -1,6 +1,6 @@
[package] [package]
name = "nalgebra" name = "nalgebra"
version = "0.31.1" version = "0.31.2"
authors = [ "Sébastien Crozet <developer@crozet.re>" ] authors = [ "Sébastien Crozet <developer@crozet.re>" ]
description = "General-purpose linear algebra library with transformations and statically-sized or dynamically-sized matrices." description = "General-purpose linear algebra library with transformations and statically-sized or dynamically-sized matrices."

View File

@ -211,6 +211,13 @@ impl<T> CooMatrix<T> {
self.values.push(v); 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. /// The number of rows in the matrix.
#[inline] #[inline]
#[must_use] #[must_use]

View File

@ -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<_>>(),
vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)]
);
coo.clear_triplets();
assert_eq!(coo.triplet_iter().collect::<Vec<_>>(), 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<_>>(),
vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)]
);
}
#[test] #[test]
fn coo_push_out_of_bounds_entries() { fn coo_push_out_of_bounds_entries() {
{ {

View File

@ -26,7 +26,7 @@ use std::mem::{ManuallyDrop, MaybeUninit};
* Allocator. * 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. /// matrices respectively.
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct DefaultAllocator; pub struct DefaultAllocator;

View File

@ -252,14 +252,17 @@ pub trait ToTypenum {
} }
unsafe impl<const T: usize> Dim for Const<T> { unsafe impl<const T: usize> Dim for Const<T> {
#[inline]
fn try_to_usize() -> Option<usize> { fn try_to_usize() -> Option<usize> {
Some(T) Some(T)
} }
#[inline]
fn value(&self) -> usize { fn value(&self) -> usize {
T T
} }
#[inline]
fn from_usize(dim: usize) -> Self { fn from_usize(dim: usize) -> Self {
assert_eq!(dim, T); assert_eq!(dim, T);
Self Self

View File

@ -2186,3 +2186,28 @@ where
} }
} }
} }
impl<T, D, S> Unit<Vector<T, D, S>>
where
T: Scalar,
D: Dim,
S: RawStorage<T, D, U1>,
{
/// Cast the components of `self` to another type.
///
/// # Example
/// ```
/// # use nalgebra::Vector3;
/// let v = Vector3::<f64>::y_axis();
/// let v2 = v.cast::<f32>();
/// assert_eq!(v2, Vector3::<f32>::y_axis());
/// ```
pub fn cast<T2: Scalar>(self) -> Unit<OVector<T2, D>>
where
T: Scalar,
OVector<T2, D>: SupersetOf<Vector<T, D, S>>,
DefaultAllocator: Allocator<T2, D, U1>,
{
Unit::new_unchecked(crate::convert_ref(self.as_ref()))
}
}

View File

@ -202,6 +202,24 @@ impl<T: Scalar> Point1<T> {
/// assert_eq!(p.x, 1.0); /// assert_eq!(p.x, 1.0);
/// ``` /// ```
#[inline] #[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 { pub fn new(x: T) -> Self {
Point { Point {
coords: Vector1::new(x), coords: Vector1::new(x),
@ -216,6 +234,19 @@ macro_rules! componentwise_constructors_impl(
#[doc = $doc] #[doc = $doc]
#[doc = "```"] #[doc = "```"]
#[inline] #[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 { pub fn new($($args: T),*) -> Self {
Point { coords: $Vector::new($($args),*) } Point { coords: $Vector::new($($args),*) }
} }

View File

@ -46,28 +46,34 @@ fn main() {
**nalgebra** is meant to be a general-purpose, low-dimensional, linear algebra library, with **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: 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 * A single parametrizable type [`Matrix`](Matrix) for vectors, (square or rectangular) matrices, and
with dimensions known either at compile-time (using type-level integers) or at runtime. 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 * Matrices and vectors with compile-time sizes are statically allocated while dynamic ones are
allocated on the heap. allocated on the heap.
* Convenient aliases for low-dimensional matrices and vectors: `Vector1` to `Vector6` and * Convenient aliases for low-dimensional matrices and vectors: [`Vector1`](Vector1) to
`Matrix1x1` to `Matrix6x6`, including rectangular matrices like `Matrix2x5`. [`Vector6`](Vector6) and [`Matrix1x1`](Matrix1) to [`Matrix6x6`](Matrix6), including rectangular
* Points sizes known at compile time, and convenience aliases: `Point1` to `Point6`. matrices like [`Matrix2x5`](Matrix2x5).
* Translation (seen as a transformation that composes by multiplication): `Translation2`, * Points sizes known at compile time, and convenience aliases: [`Point1`](Point1) to
`Translation3`. [`Point6`](Point6).
* Rotation matrices: `Rotation2`, `Rotation3`. * Translation (seen as a transformation that composes by multiplication):
* Quaternions: `Quaternion`, `UnitQuaternion` (for 3D rotation). [`Translation2`](Translation2), [`Translation3`](Translation3).
* Unit complex numbers can be used for 2D rotation: `UnitComplex`. * Rotation matrices: [`Rotation2`](Rotation2), [`Rotation3`](Rotation3).
* Algebraic entities with a norm equal to one: `Unit<T>`, e.g., `Unit<Vector3<f32>>`. * Quaternions: [`Quaternion`](Quaternion), [`UnitQuaternion`](UnitQuaternion) (for 3D rotation).
* Isometries (translation rotation): `Isometry2`, `Isometry3` * Unit complex numbers can be used for 2D rotation: [`UnitComplex`](UnitComplex).
* Similarity transformations (translation rotation uniform scale): `Similarity2`, `Similarity3`. * Algebraic entities with a norm equal to one: [`Unit<T>`](Unit), e.g., `Unit<Vector3<f32>>`.
* Affine transformations stored as a homogeneous matrix: `Affine2`, `Affine3`. * Isometries (translation rotation): [`Isometry2`](Isometry2), [`Isometry3`](Isometry3)
* Projective (i.e. invertible) transformations stored as a homogeneous matrix: `Projective2`, * Similarity transformations (translation rotation uniform scale):
`Projective3`. [`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: * General transformations that does not have to be invertible, stored as a homogeneous matrix:
`Transform2`, `Transform3`. [`Transform2`](Transform2), [`Transform3`](Transform3).
* 3D projections for computer graphics: `Perspective3`, `Orthographic3`. * 3D projections for computer graphics: [`Perspective3`](Perspective3),
* Matrix factorizations: `Cholesky`, `QR`, `LU`, `FullPivLU`, `SVD`, `Schur`, `Hessenberg`, `SymmetricEigen`. [`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. * Insertion and removal of rows of columns of a matrix.
*/ */