pub struct AnyScalar { /* private fields */ }Expand description
Dynamic scalar compatibility wrapper for tensor4all-core.
This owns a rank-0 TensorDynLen so that scalar values can participate in
the same eager autodiff graph as tensors while preserving the existing
dynamic scalar API shape.
Implementations§
Source§impl AnyScalar
impl AnyScalar
Sourcepub fn from_value<T: ScalarTensorElement>(value: T) -> Self
pub fn from_value<T: ScalarTensorElement>(value: T) -> Self
Creates an AnyScalar from a tensor element.
Use this when you already have a scalar value that implements
TensorElement and want to lift it into the dynamic scalar wrapper.
§Arguments
value- The scalar value to wrap.
§Returns
A rank-0 AnyScalar containing value.
§Examples
use tensor4all_core::AnyScalar;
let scalar = AnyScalar::from_value(3.0f64);
assert_eq!(scalar.real(), 3.0);
assert!(scalar.is_real());Sourcepub fn from_real(x: f64) -> Self
pub fn from_real(x: f64) -> Self
Creates a real-valued AnyScalar.
This is a convenience wrapper around AnyScalar::from_value.
§Arguments
x- The real scalar value to wrap.
§Returns
A rank-0 AnyScalar with real dtype.
§Examples
use tensor4all_core::AnyScalar;
let scalar = AnyScalar::from_real(1.25);
assert_eq!(scalar.as_f64(), Some(1.25));
assert!(scalar.is_real());Sourcepub fn from_complex(re: f64, im: f64) -> Self
pub fn from_complex(re: f64, im: f64) -> Self
Creates a complex-valued AnyScalar.
This is a convenience wrapper around AnyScalar::from_value.
§Arguments
re- The real part of the complex value.im- The imaginary part of the complex value.
§Returns
A rank-0 AnyScalar containing the requested complex number.
§Examples
use tensor4all_core::AnyScalar;
let scalar = AnyScalar::from_complex(1.0, -2.0);
assert_eq!(scalar.as_c64().map(|z| (z.re, z.im)), Some((1.0, -2.0)));
assert!(scalar.is_complex());Sourcepub fn new_real(x: f64) -> Self
pub fn new_real(x: f64) -> Self
Creates a real-valued AnyScalar.
This is an alias for AnyScalar::from_real.
§Arguments
x- The real scalar value to wrap.
§Returns
A rank-0 AnyScalar with real dtype.
§Examples
use tensor4all_core::AnyScalar;
let scalar = AnyScalar::new_real(2.5);
assert_eq!(scalar.real(), 2.5);
assert!(scalar.is_real());Sourcepub fn new_complex(re: f64, im: f64) -> Self
pub fn new_complex(re: f64, im: f64) -> Self
Creates a complex-valued AnyScalar.
This is an alias for AnyScalar::from_complex.
§Arguments
re- The real part of the complex value.im- The imaginary part of the complex value.
§Returns
A rank-0 AnyScalar containing the requested complex number.
§Examples
use tensor4all_core::AnyScalar;
let scalar = AnyScalar::new_complex(2.0, 3.0);
assert_eq!(scalar.as_c64().map(|z| (z.re, z.im)), Some((2.0, 3.0)));
assert!(scalar.is_complex());Sourcepub fn primal(&self) -> Result<Self>
pub fn primal(&self) -> Result<Self>
Returns the detached primal value of this scalar.
This is an alias for AnyScalar::detach.
§Returns
A scalar with the same value and no gradient tracking.
§Examples
use tensor4all_core::AnyScalar;
let primal = AnyScalar::new_real(5.0).enable_grad().unwrap().primal().unwrap();
assert_eq!(primal.real(), 5.0);
assert!(!primal.tracks_grad());Sourcepub fn enable_grad(self) -> Result<Self>
pub fn enable_grad(self) -> Result<Self>
Sourcepub fn tracks_grad(&self) -> bool
pub fn tracks_grad(&self) -> bool
Sourcepub fn grad(&self) -> Result<Option<Self>>
pub fn grad(&self) -> Result<Option<Self>>
Returns the stored gradient, if any.
§Returns
Ok(Some(_)) when a gradient is available, Ok(None) when no gradient
has been recorded, or an error if the backend cannot read it.
§Errors
Propagates autodiff or tensor access failures from the underlying tensor runtime.
§Examples
use tensor4all_core::AnyScalar;
let x = AnyScalar::new_real(2.0).enable_grad().unwrap();
let y = &x * &x;
y.backward().unwrap();
let grad = x.grad().unwrap().unwrap();
assert_eq!(grad.real(), 4.0);Sourcepub fn clear_grad(&self) -> Result<()>
pub fn clear_grad(&self) -> Result<()>
Clears the stored gradient for this scalar.
§Returns
Ok(()) when the gradient buffer was cleared successfully.
§Errors
Propagates tensor runtime failures from the underlying autodiff state.
§Examples
use tensor4all_core::AnyScalar;
let x = AnyScalar::new_real(2.0).enable_grad().unwrap();
let y = &x * &x;
y.backward().unwrap();
assert!(x.grad().unwrap().is_some());
x.clear_grad().unwrap();
assert!(x.grad().unwrap().is_none());Sourcepub fn backward(&self) -> Result<()>
pub fn backward(&self) -> Result<()>
Runs reverse-mode autodiff starting from this scalar.
§Returns
Ok(()) when gradients were accumulated successfully.
§Errors
Propagates failures from the underlying tensor autodiff engine.
§Examples
use tensor4all_core::AnyScalar;
let x = AnyScalar::new_real(2.0).enable_grad().unwrap();
let y = &x * &x;
y.backward().unwrap();
let grad = x.grad().unwrap().unwrap();
assert_eq!(grad.real(), 4.0);Sourcepub fn detach(&self) -> Result<Self>
pub fn detach(&self) -> Result<Self>
Returns a detached copy of this scalar.
§Returns
A scalar with the same value but without gradient tracking.
§Examples
use tensor4all_core::AnyScalar;
let detached = AnyScalar::new_real(7.0)
.enable_grad()
.unwrap()
.detach()
.unwrap();
assert_eq!(detached.real(), 7.0);
assert!(!detached.tracks_grad());Sourcepub fn is_complex(&self) -> bool
pub fn is_complex(&self) -> bool
Sourcepub fn as_c64(&self) -> Option<Complex64>
pub fn as_c64(&self) -> Option<Complex64>
Returns this scalar as a Complex64 when it is complex-valued.
§Returns
Some(value) for complex scalars or None for real and integer
scalars.
§Examples
use tensor4all_core::AnyScalar;
let scalar = AnyScalar::new_complex(2.5, 1.0);
assert_eq!(scalar.as_c64().map(|z| (z.re, z.im)), Some((2.5, 1.0)));
assert_eq!(AnyScalar::new_real(2.5).as_c64(), None);Sourcepub fn compose_complex(real: Self, imag: Self) -> Result<Self>
pub fn compose_complex(real: Self, imag: Self) -> Result<Self>
Combines two real-valued scalars into a complex scalar.
§Arguments
real- The real component.imag- The imaginary component.
§Returns
A complex AnyScalar whose real and imaginary parts come from the
inputs.
§Errors
Returns an error if either input is not real-valued.
§Examples
use tensor4all_core::AnyScalar;
let scalar = AnyScalar::compose_complex(
AnyScalar::new_real(3.0),
AnyScalar::new_real(-4.0),
)
.unwrap();
assert_eq!(scalar.as_c64().map(|z| (z.re, z.im)), Some((3.0, -4.0)));Sourcepub fn powi(&self, exponent: i32) -> Self
pub fn powi(&self, exponent: i32) -> Self
Raises this scalar to an integer power.
§Arguments
exponent- The integer exponent to apply. Negative exponents return the reciprocal power.
§Returns
The value of self^exponent. Zero exponents return 1.
§Examples
use tensor4all_core::AnyScalar;
assert_eq!(AnyScalar::new_real(2.0).powi(3).real(), 8.0);
assert_eq!(AnyScalar::new_real(2.0).powi(-1).real(), 0.5);Trait Implementations§
Source§impl PartialOrd for AnyScalar
impl PartialOrd for AnyScalar
Source§impl SumFromStorage for AnyScalar
impl SumFromStorage for AnyScalar
Source§fn sum_from_storage(storage: &Storage) -> Self
fn sum_from_storage(storage: &Storage) -> Self
Auto Trait Implementations§
impl Freeze for AnyScalar
impl !RefUnwindSafe for AnyScalar
impl Send for AnyScalar
impl Sync for AnyScalar
impl Unpin for AnyScalar
impl UnsafeUnpin for AnyScalar
impl !UnwindSafe for AnyScalar
Blanket Implementations§
§impl<Rhs, Lhs, Output> AddByRef<Rhs> for Lhs
impl<Rhs, Lhs, Output> AddByRef<Rhs> for Lhs
type Output = Output
fn add_by_ref(&self, rhs: &Rhs) -> <Lhs as AddByRef<Rhs>>::Output
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
§impl<Rhs, Lhs, Output> DivByRef<Rhs> for Lhs
impl<Rhs, Lhs, Output> DivByRef<Rhs> for Lhs
type Output = Output
fn div_by_ref(&self, rhs: &Rhs) -> <Lhs as DivByRef<Rhs>>::Output
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more