pub struct Quantity<F, U> { /* private fields */ }
Expand description
Representation of a value with a corresponding unit.
§Contents
Implementations§
source§impl<T> Quantity<T, SIUnit>
impl<T> Quantity<T, SIUnit>
sourcepub fn into_raw_parts(self) -> (T, [i8; 7])
pub fn into_raw_parts(self) -> (T, [i8; 7])
Split an SI quantity into its value and unit vector.
sourcepub fn from_raw_parts(value: T, unit: [i8; 7]) -> Self
pub fn from_raw_parts(value: T, unit: [i8; 7]) -> Self
Create an SI quantity from its value and unit vector.
source§impl<F, U: Unit> Quantity<F, U>
impl<F, U: Unit> Quantity<F, U>
§Methods for All Quantities
sourcepub fn has_unit<F2>(&self, other: &Quantity<F2, U>) -> bool
pub fn has_unit<F2>(&self, other: &Quantity<F2, U>) -> bool
Check if the quantity has the same unit as the argument.
§Example
let p = 5.0 * NEWTON/METER.powi(2);
assert!(p.has_unit(&BAR));
sourcepub fn value(&self) -> Result<&F, QuantityError>
pub fn value(&self) -> Result<&F, QuantityError>
Return a reference to its value if the quantity is dimensionless.
§Example
let p1 = 5.0 * PASCAL;
let p2 = 2.5 * PASCAL;
let ratio = p1 / p2;
assert_relative_eq!(ratio.value()?, &2.0);
sourcepub fn into_value(self) -> Result<F, QuantityError>
pub fn into_value(self) -> Result<F, QuantityError>
Converts to its value if the quantity is dimensionless.
§Example
let p1 = 5.0 * PASCAL;
let p2 = 2.5 * PASCAL;
let ratio = p1 / p2;
assert_relative_eq!(ratio.into_value()?, 2.0);
sourcepub fn to_reduced<'a>(
&'a self,
reference: QuantityScalar<U>
) -> Result<F, QuantityError>
pub fn to_reduced<'a>( &'a self, reference: QuantityScalar<U> ) -> Result<F, QuantityError>
Returns the value of self in a given unit if possible.
§Example
let p = 5.0 * BAR;
assert_relative_eq!(p.to_reduced(PASCAL)?, 500000.0);
source§impl<U: Unit> Quantity<f64, U>
impl<U: Unit> Quantity<f64, U>
§Methods for Scalar Quantities
sourcepub fn powi(&self, i: i32) -> Self
pub fn powi(&self, i: i32) -> Self
Calculate the integer power of self.
§Example
let x = 5.0 * METER;
assert_relative_eq!(x.powi(2), &(25.0 * METER * METER));
sourcepub fn sqrt(&self) -> Result<Self, QuantityError>
pub fn sqrt(&self) -> Result<Self, QuantityError>
Try to calculate the square root of self.
§Example
let x = 25.0 * METER * METER;
assert_relative_eq!(x.sqrt()?, &(5.0 * METER));
assert!(METER.sqrt().is_err());
sourcepub fn cbrt(&self) -> Result<Self, QuantityError>
pub fn cbrt(&self) -> Result<Self, QuantityError>
Try to calculate the cubic root of self.
§Example
let x = 125.0 * METER * METER * METER;
assert_relative_eq!(x.cbrt()?, &(5.0 * METER));
assert!(METER.cbrt().is_err());
sourcepub fn root(&self, i: i32) -> Result<Self, QuantityError>
pub fn root(&self, i: i32) -> Result<Self, QuantityError>
Try to calculate the integer root of self.
§Example
let x = 625.0 * METER * METER * METER * METER;
assert_relative_eq!(x.root(4)?, &(5.0 * METER));
assert!(METER.root(4).is_err());
sourcepub fn max(&self, other: Self) -> Result<Self, QuantityError>
pub fn max(&self, other: Self) -> Result<Self, QuantityError>
Return the maximum of self
and other
if they have the same unit.
§Example
let p1 = 110.0 * KILO * PASCAL;
let p2 = BAR;
assert_relative_eq!(p1.max(p2)?, &p1);
assert!(BAR.max(KELVIN).is_err());
sourcepub fn min(&self, other: Self) -> Result<Self, QuantityError>
pub fn min(&self, other: Self) -> Result<Self, QuantityError>
Return the minimum of self
and other
if they have the same unit.
§Example
let p1 = 110.0 * KILO * PASCAL;
let p2 = BAR;
assert_relative_eq!(p1.min(p2)?, &p2);
assert!(BAR.min(KELVIN).is_err());
sourcepub fn abs(&self) -> Selfwhere
U: Clone,
pub fn abs(&self) -> Selfwhere
U: Clone,
Return the absolute value of self
.
§Example
let t = -50.0 * KELVIN;
assert_relative_eq!(t.abs(), &(50.0 * KELVIN));
sourcepub fn signum(&self) -> f64
pub fn signum(&self) -> f64
Returns a number that represents the sign of self
.
1.0
if the number is positive,+0.0
orINFINITY
-1.0
if the number is negative,-0.0
orNEG_INFINITY
NAN
if the number isNAN
sourcepub fn is_sign_positive(&self) -> bool
pub fn is_sign_positive(&self) -> bool
Returns true
if self
has a positive sign, including +0.0
, NaN
s with
positive sign bit and positive infinity.
sourcepub fn is_sign_negative(&self) -> bool
pub fn is_sign_negative(&self) -> bool
Returns true
if self
has a negative sign, including -0.0
, NaN
s with
negative sign bit and negative infinity.
source§impl<U: Unit, D: Dimension, S: Data<Elem = f64>> Quantity<ArrayBase<S, D>, U>
impl<U: Unit, D: Dimension, S: Data<Elem = f64>> Quantity<ArrayBase<S, D>, U>
§Methods for n-Dimensional Array Quantities
sourcepub fn sum(&self) -> QuantityScalar<U>
pub fn sum(&self) -> QuantityScalar<U>
Return the sum of all elements in the array.
§Example
let x = arr1(&[1.5, 2.5]) * BAR;
assert_relative_eq!(x.sum(), &(4.0 * BAR));
sourcepub fn to_owned(&self) -> QuantityArray<U, D>
pub fn to_owned(&self) -> QuantityArray<U, D>
Return an uniquely owned copy of the array.
sourcepub fn get<I: NdIndex<D>>(&self, index: I) -> QuantityScalar<U>
pub fn get<I: NdIndex<D>>(&self, index: I) -> QuantityScalar<U>
Return the element at index
.
The Index
trait can not be implemented, because a new instance has to be created,
when indexing a quantity array. This serves as replacement for it.
sourcepub fn try_set<I: NdIndex<D>>(
&mut self,
index: I,
scalar: QuantityScalar<U>
) -> Result<(), QuantityError>where
S: DataMut,
pub fn try_set<I: NdIndex<D>>(
&mut self,
index: I,
scalar: QuantityScalar<U>
) -> Result<(), QuantityError>where
S: DataMut,
Set the element at index
to scalar
if scalar has the same unit as self
.
sourcepub fn index_axis(
&self,
axis: Axis,
index: usize
) -> Quantity<ArrayView<'_, f64, D::Smaller>, U>where
D: RemoveAxis,
pub fn index_axis(
&self,
axis: Axis,
index: usize
) -> Quantity<ArrayView<'_, f64, D::Smaller>, U>where
D: RemoveAxis,
Returns a view restricted to index along the axis, with the axis removed.
sourcepub fn insert_axis(self, axis: Axis) -> Quantity<ArrayBase<S, D::Larger>, U>
pub fn insert_axis(self, axis: Axis) -> Quantity<ArrayBase<S, D::Larger>, U>
Insert new array axis at axis and return the result.
sourcepub fn sum_axis(&self, axis: Axis) -> Quantity<Array<f64, D::Smaller>, U>where
D: RemoveAxis,
pub fn sum_axis(&self, axis: Axis) -> Quantity<Array<f64, D::Smaller>, U>where
D: RemoveAxis,
Return sum along axis.
sourcepub fn to_vec(&self) -> Vec<QuantityScalar<U>>
pub fn to_vec(&self) -> Vec<QuantityScalar<U>>
Return a vector of scalar quantities for each element of self
.
sourcepub fn from_shape_fn<Sh, F>(shape: Sh, f: F) -> QuantityArray<U, D>
pub fn from_shape_fn<Sh, F>(shape: Sh, f: F) -> QuantityArray<U, D>
Create an array with values created by the function f.
sourcepub fn powi(&self, i: i32) -> QuantityArray<U, D>
pub fn powi(&self, i: i32) -> QuantityArray<U, D>
Calculate the integer power of self.
§Example
let x = arr1(&[3.0, 5.0]) * METER;
assert_relative_eq!(x.powi(2), &(arr1(&[9.0, 25.0]) * METER * METER));
sourcepub fn sqrt(&self) -> Result<QuantityArray<U, D>, QuantityError>
pub fn sqrt(&self) -> Result<QuantityArray<U, D>, QuantityError>
Try to calculate the square root of self.
§Example
let x = arr1(&[9.0, 25.0]) * METER * METER;
assert_relative_eq!(x.sqrt()?, &(arr1(&[3.0, 5.0]) * METER));
sourcepub fn cbrt(&self) -> Result<QuantityArray<U, D>, QuantityError>
pub fn cbrt(&self) -> Result<QuantityArray<U, D>, QuantityError>
Try to calculate the cubic root of self.
§Example
let x = arr1(&[27.0, 125.0]) * METER * METER * METER;
assert_relative_eq!(x.cbrt()?, &(arr1(&[3.0, 5.0]) * METER));
sourcepub fn root(&self, i: i32) -> Result<QuantityArray<U, D>, QuantityError>
pub fn root(&self, i: i32) -> Result<QuantityArray<U, D>, QuantityError>
Try to calculate the integer root of self.
§Example
let x = arr1(&[81.0, 625.0]) * METER * METER * METER * METER;
assert_relative_eq!(x.root(4)?, &(arr1(&[3.0, 5.0]) * METER));
source§impl<U: Unit> Quantity<ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>, U>
impl<U: Unit> Quantity<ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>, U>
§Methods for 1-Dimensional Array Quantities
sourcepub fn linspace(
start: QuantityScalar<U>,
end: QuantityScalar<U>,
n: usize
) -> Result<Self, QuantityError>
pub fn linspace( start: QuantityScalar<U>, end: QuantityScalar<U>, n: usize ) -> Result<Self, QuantityError>
Create a one-dimensional array with n evenly spaced elements from start
to end
(inclusive) if start
and end
have the same unit.
§Example
let x = SIArray1::linspace(1.0 * METER, 3.0 * METER, 5)?;
assert_relative_eq!(x, &(arr1(&[1.0, 1.5, 2.0, 2.5, 3.0]) * METER));
sourcepub fn logspace(
start: QuantityScalar<U>,
end: QuantityScalar<U>,
n: usize
) -> Result<Self, QuantityError>
pub fn logspace( start: QuantityScalar<U>, end: QuantityScalar<U>, n: usize ) -> Result<Self, QuantityError>
Create a one-dimensional array with n logarithmically spaced elements from start
to end
(inclusive) if start
and end
have the same unit.
§Example
let x = SIArray1::logspace(1.0 * METER, 16.0 * METER, 5)?;
assert_relative_eq!(x, &(arr1(&[1.0, 2.0, 4.0, 8.0, 16.0]) * METER));
sourcepub fn from_vec(vec: Vec<QuantityScalar<U>>) -> Self
pub fn from_vec(vec: Vec<QuantityScalar<U>>) -> Self
Create a one-dimensional array from a vector of scalar quantities.
Trait Implementations§
source§impl<F: AbsDiffEq, U: PartialEq + Display> AbsDiffEq for Quantity<F, U>
impl<F: AbsDiffEq, U: PartialEq + Display> AbsDiffEq for Quantity<F, U>
source§fn default_epsilon() -> Self::Epsilon
fn default_epsilon() -> Self::Epsilon
source§fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
source§fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq
.source§impl<'a, F, F2, U> AddAssign<&'a Quantity<F2, U>> for Quantity<F, U>
impl<'a, F, F2, U> AddAssign<&'a Quantity<F2, U>> for Quantity<F, U>
source§fn add_assign(&mut self, other: &'a Quantity<F2, U>)
fn add_assign(&mut self, other: &'a Quantity<F2, U>)
+=
operation. Read moresource§impl<F, F2, U> AddAssign<Quantity<F2, U>> for Quantity<F, U>
impl<F, F2, U> AddAssign<Quantity<F2, U>> for Quantity<F, U>
source§fn add_assign(&mut self, other: Quantity<F2, U>)
fn add_assign(&mut self, other: Quantity<F2, U>)
+=
operation. Read moresource§impl<'de, F, U> Deserialize<'de> for Quantity<F, U>where
F: Deserialize<'de>,
U: Deserialize<'de>,
impl<'de, F, U> Deserialize<'de> for Quantity<F, U>where
F: Deserialize<'de>,
U: Deserialize<'de>,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<'a, F, F2, U> DivAssign<&'a Quantity<F2, U>> for Quantity<F, U>
impl<'a, F, F2, U> DivAssign<&'a Quantity<F2, U>> for Quantity<F, U>
source§fn div_assign(&mut self, other: &'a Quantity<F2, U>)
fn div_assign(&mut self, other: &'a Quantity<F2, U>)
/=
operation. Read moresource§impl<F, F2, U> DivAssign<Quantity<F2, U>> for Quantity<F, U>
impl<F, F2, U> DivAssign<Quantity<F2, U>> for Quantity<F, U>
source§fn div_assign(&mut self, other: Quantity<F2, U>)
fn div_assign(&mut self, other: Quantity<F2, U>)
/=
operation. Read moresource§impl<U: Unit> FromIterator<Quantity<f64, U>> for QuantityArray1<U>
impl<U: Unit> FromIterator<Quantity<f64, U>> for QuantityArray1<U>
source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = QuantityScalar<U>>,
fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = QuantityScalar<U>>,
source§impl<'a, F, U: Copy> IntoIterator for &'a Quantity<F, U>
impl<'a, F, U: Copy> IntoIterator for &'a Quantity<F, U>
source§impl<'a, F, F2, U> MulAssign<&'a Quantity<F2, U>> for Quantity<F, U>
impl<'a, F, F2, U> MulAssign<&'a Quantity<F2, U>> for Quantity<F, U>
source§fn mul_assign(&mut self, other: &'a Quantity<F2, U>)
fn mul_assign(&mut self, other: &'a Quantity<F2, U>)
*=
operation. Read moresource§impl<F, F2, U> MulAssign<Quantity<F2, U>> for Quantity<F, U>
impl<F, F2, U> MulAssign<Quantity<F2, U>> for Quantity<F, U>
source§fn mul_assign(&mut self, other: Quantity<F2, U>)
fn mul_assign(&mut self, other: Quantity<F2, U>)
*=
operation. Read moresource§impl<F: PartialEq, U: PartialEq + Display> PartialEq for Quantity<F, U>
impl<F: PartialEq, U: PartialEq + Display> PartialEq for Quantity<F, U>
source§impl<F: PartialOrd, U: PartialEq + Display> PartialOrd for Quantity<F, U>
impl<F: PartialOrd, U: PartialEq + Display> PartialOrd for Quantity<F, U>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<F: RelativeEq, U: PartialEq + Display> RelativeEq for Quantity<F, U>
impl<F: RelativeEq, U: PartialEq + Display> RelativeEq for Quantity<F, U>
source§fn default_max_relative() -> Self::Epsilon
fn default_max_relative() -> Self::Epsilon
source§fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon
) -> bool
fn relative_eq( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool
source§fn relative_ne(
&self,
other: &Rhs,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon
) -> bool
fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool
RelativeEq::relative_eq
.source§impl<'a, F, F2, U> SubAssign<&'a Quantity<F2, U>> for Quantity<F, U>
impl<'a, F, F2, U> SubAssign<&'a Quantity<F2, U>> for Quantity<F, U>
source§fn sub_assign(&mut self, other: &'a Quantity<F2, U>)
fn sub_assign(&mut self, other: &'a Quantity<F2, U>)
-=
operation. Read moresource§impl<F, F2, U> SubAssign<Quantity<F2, U>> for Quantity<F, U>
impl<F, F2, U> SubAssign<Quantity<F2, U>> for Quantity<F, U>
source§fn sub_assign(&mut self, other: Quantity<F2, U>)
fn sub_assign(&mut self, other: Quantity<F2, U>)
-=
operation. Read more