use crate::tensor::*;
use crate::units::*;
use crate::tensor::element::*;
use std::marker::PhantomData;
pub type Scalar<E: TensorElement, D> = Tensor<E, D, 1, 1, 1>;
impl<E: TensorElement, D> Scalar<E, D> {
pub const EPSILON: Scalar<E, D> = Scalar {
data: [E::EPSILON],
_phantom: PhantomData,
};
pub fn from<U: Unit<Dimension = D>>(value: E) -> Self {
Scalar {
data: [U::to_base(value.into()).into()],
_phantom: PhantomData,
}
}
pub fn raw(&self) -> E {
self.data[0]
}
pub fn raw_as<T: From<E>>(&self) -> T {
T::from(self.data[0])
}
pub fn mag(&self) -> Scalar<f64, D> {
Scalar {
data: [self.data[0].mag()],
_phantom: PhantomData,
}
}
}
impl<E: TensorElement, D> Scalar<E, D> {
pub fn epsilon(&self) -> Self {
Self::EPSILON
}
}
pub trait ToScalar<E: TensorElement> {
fn scalar<U: Unit>(&self) -> Scalar<E, U::Dimension>;
}
impl<E: TensorElement> ToScalar<E> for E {
fn scalar<U: Unit>(&self) -> Scalar<E, U::Dimension> {
Scalar {
data: [*self],
_phantom: PhantomData,
}
}
}