From 59fe5f3e7a70fcf3d63350c9cc54c0f98d05ee16 Mon Sep 17 00:00:00 2001 From: Philipp Rehner Date: Fri, 24 Jul 2026 18:07:26 +0200 Subject: [PATCH] Add into_dimensionality and into_dyn --- CHANGELOG.md | 4 ++++ Cargo.toml | 2 +- si-units/src/fmt.rs | 4 ++-- src/array.rs | 19 +++++++++++++++++-- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4d742e..979ca81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.14.1] - 2026-07-24 +### Added +- Added `into_dimensionality` and `into_dyn` for quantities of arrays. [#119](https://github.com/itt-ustutt/quantity/pull/119) + ## [0.14.0] - 2026-06-14 ### Packaging - Updated `nalgebra` dependency to 0.35. diff --git a/Cargo.toml b/Cargo.toml index d8e3138..5b0f580 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "quantity" -version = "0.14.0" +version = "0.14.1" authors = [ "Philipp Rehner ", "Gernot Bauer ", diff --git a/si-units/src/fmt.rs b/si-units/src/fmt.rs index bce1152..1f0b817 100644 --- a/si-units/src/fmt.rs +++ b/si-units/src/fmt.rs @@ -67,10 +67,10 @@ impl SINumber { format!( "{}\\,{}", float_to_latex(value), - &unit_to_latex(symbols, exponents, Some(prefix)) + unit_to_latex(symbols, exponents, Some(prefix)) ) } else { - format!("{}\\,{}", float_to_latex(self.value), &self.unit.to_latex()) + format!("{}\\,{}", float_to_latex(self.value), self.unit.to_latex()) } } } diff --git a/src/array.rs b/src/array.rs index f924363..041a620 100644 --- a/src/array.rs +++ b/src/array.rs @@ -1,8 +1,8 @@ use super::Quantity; use ndarray::iter::LanesMut; use ndarray::{ - Array, Array1, ArrayBase, ArrayView, Axis, Data, DataMut, Dimension, NdIndex, RemoveAxis, - ShapeBuilder, + Array, Array1, ArrayBase, ArrayView, Axis, Data, DataMut, Dimension, IxDyn, NdIndex, + RemoveAxis, ShapeBuilder, ShapeError, }; use num_traits::Zero; use std::iter::FromIterator; @@ -159,6 +159,21 @@ impl, U, D: Dimension> Quantity, U> { Quantity::new(self.0.insert_axis(axis)) } + /// Convert an array or array view to another with the same type, but different dimensionality + /// type. Errors if the dimensions don't agree (the number of axes must match). + pub fn into_dimensionality(self) -> Result, U>, ShapeError> + where + D2: Dimension, + { + self.0.into_dimensionality().map(Quantity::new) + } + + /// Convert any array or array view to a dynamic dimensional array or + /// array view (respectively). + pub fn into_dyn(self) -> Quantity, U> { + Quantity::new(self.0.into_dyn()) + } + /// Return the element at `index`. /// /// The `Index` trait can not be implemented, because a new instance has to be created,