pub struct BackendScalar { /* private fields */ }Expand description
Dynamic scalar used across tensor4all backends.
This is a tensor4all-owned rank-0 wrapper over tenferro’s dynamic tensor.
§Examples
use tensor4all_tensorbackend::BackendScalar;
// Real scalar
let a = BackendScalar::new_real(3.14);
assert!((a.real() - 3.14).abs() < 1e-10);
assert_eq!(a.imag(), 0.0);
assert!(a.is_real());
assert!(!a.is_complex());
// Complex scalar
let b = BackendScalar::new_complex(1.0, 2.0);
assert!((b.real() - 1.0).abs() < 1e-10);
assert!((b.imag() - 2.0).abs() < 1e-10);
assert!(b.is_complex());
// Arithmetic
let c = BackendScalar::new_real(2.0);
let d = a + c;
assert!((d.real() - 5.14).abs() < 1e-10);Implementations§
Source§impl BackendScalar
impl BackendScalar
Sourcepub fn from_value<T: ScalarTensorElement>(value: T) -> Self
pub fn from_value<T: ScalarTensorElement>(value: T) -> Self
Creates a scalar from any supported public tensor element type.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::from_value(3.14_f64);
assert!((s.real() - 3.14).abs() < 1e-10);
use num_complex::Complex64;
let z = BackendScalar::from_value(Complex64::new(1.0, 2.0));
assert!(z.is_complex());
assert!((z.real() - 1.0).abs() < 1e-10);
assert!((z.imag() - 2.0).abs() < 1e-10);Sourcepub fn from_real(x: f64) -> Self
pub fn from_real(x: f64) -> Self
Creates a real scalar from an f64 value.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::from_real(2.5);
assert!((s.real() - 2.5).abs() < 1e-10);
assert!(s.is_real());Sourcepub fn from_complex(re: f64, im: f64) -> Self
pub fn from_complex(re: f64, im: f64) -> Self
Creates a complex scalar from real and imaginary parts.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::from_complex(1.0, -1.0);
assert!((s.real() - 1.0).abs() < 1e-10);
assert!((s.imag() - (-1.0)).abs() < 1e-10);
assert!(s.is_complex());Sourcepub fn new_real(x: f64) -> Self
pub fn new_real(x: f64) -> Self
Backward-compatible constructor for a real scalar.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::new_real(42.0);
assert!((s.real() - 42.0).abs() < 1e-10);Sourcepub fn new_complex(re: f64, im: f64) -> Self
pub fn new_complex(re: f64, im: f64) -> Self
Backward-compatible constructor for a complex scalar.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::new_complex(3.0, 4.0);
assert!((s.abs() - 5.0).abs() < 1e-10); // |3 + 4i| = 5Sourcepub fn primal(&self) -> Self
pub fn primal(&self) -> Self
Returns the scalar’s plain rank-0 tensor value.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::new_real(5.0);
let p = s.primal();
assert!((p.real() - 5.0).abs() < 1e-10);Sourcepub fn real(&self) -> f64
pub fn real(&self) -> f64
Returns the real part while intentionally dropping AD metadata.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::new_complex(3.0, 4.0);
assert!((s.real() - 3.0).abs() < 1e-10);Sourcepub fn imag(&self) -> f64
pub fn imag(&self) -> f64
Returns the imaginary part while intentionally dropping AD metadata.
Returns 0.0 for real scalars.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::new_complex(3.0, 4.0);
assert!((s.imag() - 4.0).abs() < 1e-10);
let r = BackendScalar::new_real(5.0);
assert_eq!(r.imag(), 0.0);Sourcepub fn abs(&self) -> f64
pub fn abs(&self) -> f64
Returns the absolute value while intentionally dropping AD metadata.
For complex scalars, returns the complex modulus (sqrt(re^2 + im^2)).
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::new_complex(3.0, 4.0);
assert!((s.abs() - 5.0).abs() < 1e-10);
let r = BackendScalar::new_real(-7.0);
assert!((r.abs() - 7.0).abs() < 1e-10);Sourcepub fn is_complex(&self) -> bool
pub fn is_complex(&self) -> bool
Returns true when the scalar is complex.
§Examples
use tensor4all_tensorbackend::BackendScalar;
assert!(BackendScalar::new_complex(1.0, 0.0).is_complex());
assert!(!BackendScalar::new_real(1.0).is_complex());Sourcepub fn is_real(&self) -> bool
pub fn is_real(&self) -> bool
Returns true when the scalar is real.
§Examples
use tensor4all_tensorbackend::BackendScalar;
assert!(BackendScalar::new_real(1.0).is_real());
assert!(!BackendScalar::new_complex(1.0, 2.0).is_real());Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Returns true when the scalar is zero.
§Examples
use tensor4all_tensorbackend::BackendScalar;
assert!(BackendScalar::new_real(0.0).is_zero());
assert!(!BackendScalar::new_real(1.0).is_zero());Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Returns the underlying value as f64 when real.
Returns None for complex scalars.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let r = BackendScalar::new_real(2.5);
assert_eq!(r.as_f64(), Some(2.5));
let c = BackendScalar::new_complex(1.0, 1.0);
assert_eq!(c.as_f64(), None);Sourcepub fn as_c64(&self) -> Option<Complex64>
pub fn as_c64(&self) -> Option<Complex64>
Returns the underlying value as Complex64 when complex.
Returns None for real scalars.
§Examples
use tensor4all_tensorbackend::BackendScalar;
use num_complex::Complex64;
let c = BackendScalar::new_complex(1.0, 2.0);
assert_eq!(c.as_c64(), Some(Complex64::new(1.0, 2.0)));
let r = BackendScalar::new_real(1.0);
assert_eq!(r.as_c64(), None);Sourcepub fn conj(&self) -> Self
pub fn conj(&self) -> Self
Returns the complex conjugate.
For real scalars, returns a copy (conjugate of a real number is itself).
§Examples
use tensor4all_tensorbackend::BackendScalar;
let c = BackendScalar::new_complex(3.0, 4.0);
let cc = c.conj();
assert!((cc.real() - 3.0).abs() < 1e-10);
assert!((cc.imag() - (-4.0)).abs() < 1e-10);
let r = BackendScalar::new_real(5.0);
assert!((r.conj().real() - 5.0).abs() < 1e-10);Sourcepub fn real_part(&self) -> Self
pub fn real_part(&self) -> Self
Returns the real part as a scalar, preserving scalar semantics.
Unlike real, this returns an BackendScalar rather than raw f64.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let c = BackendScalar::new_complex(3.0, 4.0);
let re = c.real_part();
assert!(re.is_real());
assert!((re.real() - 3.0).abs() < 1e-10);Sourcepub fn imag_part(&self) -> Self
pub fn imag_part(&self) -> Self
Returns the imaginary part as a scalar, preserving scalar semantics.
Unlike imag, this returns an BackendScalar rather than raw f64.
The result is always a real scalar.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let c = BackendScalar::new_complex(3.0, 4.0);
let im = c.imag_part();
assert!(im.is_real());
assert!((im.real() - 4.0).abs() < 1e-10);Sourcepub fn compose_complex(real: Self, imag: Self) -> Result<Self>
pub fn compose_complex(real: Self, imag: Self) -> Result<Self>
Compose a complex scalar from real-valued parts.
§Errors
Returns an error when either input is not a real scalar (a dtype mismatch failure).
§Examples
use tensor4all_tensorbackend::BackendScalar;
let re = BackendScalar::new_real(3.0);
let im = BackendScalar::new_real(4.0);
let c = BackendScalar::compose_complex(re, im).unwrap();
assert!(c.is_complex());
assert!((c.real() - 3.0).abs() < 1e-10);
assert!((c.imag() - 4.0).abs() < 1e-10);Sourcepub fn sqrt(&self) -> Self
pub fn sqrt(&self) -> Self
Square root, preserving AD metadata.
Automatically promotes to complex if the value is negative.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::new_real(9.0);
assert!((s.sqrt().real() - 3.0).abs() < 1e-10);Sourcepub fn powf(&self, exponent: f64) -> Self
pub fn powf(&self, exponent: f64) -> Self
Real exponent power, preserving AD metadata.
Automatically promotes to complex when the base is negative and the exponent is non-integer.
§Examples
use tensor4all_tensorbackend::BackendScalar;
let s = BackendScalar::new_real(2.0);
assert!((s.powf(3.0).real() - 8.0).abs() < 1e-10);Trait Implementations§
Source§impl Add for BackendScalar
impl Add for BackendScalar
Source§impl Clone for BackendScalar
impl Clone for BackendScalar
Source§impl Debug for BackendScalar
impl Debug for BackendScalar
Source§impl Default for BackendScalar
impl Default for BackendScalar
Source§impl Display for BackendScalar
impl Display for BackendScalar
Source§impl Div for BackendScalar
impl Div for BackendScalar
Source§impl Div<BackendScalar> for Complex64
impl Div<BackendScalar> for Complex64
Source§type Output = BackendScalar
type Output = BackendScalar
/ operator.Source§impl From<BackendScalar> for Complex64
impl From<BackendScalar> for Complex64
Source§fn from(value: BackendScalar) -> Self
fn from(value: BackendScalar) -> Self
Source§impl From<f32> for BackendScalar
impl From<f32> for BackendScalar
Source§impl From<f64> for BackendScalar
impl From<f64> for BackendScalar
Source§impl Mul for BackendScalar
impl Mul for BackendScalar
Source§impl Mul<BackendScalar> for f64
impl Mul<BackendScalar> for f64
Source§type Output = BackendScalar
type Output = BackendScalar
* operator.Source§impl Mul<BackendScalar> for Complex64
impl Mul<BackendScalar> for Complex64
Source§type Output = BackendScalar
type Output = BackendScalar
* operator.Source§impl Mul<BackendScalar> for &Storage
Multiply storage by a scalar (BackendScalar).
May promote f64 storage to Complex64 when scalar is complex.
impl Mul<BackendScalar> for &Storage
Multiply storage by a scalar (BackendScalar). May promote f64 storage to Complex64 when scalar is complex.
Source§impl Neg for BackendScalar
impl Neg for BackendScalar
Source§impl One for BackendScalar
impl One for BackendScalar
Source§impl PartialEq for BackendScalar
impl PartialEq for BackendScalar
Source§impl PartialOrd for BackendScalar
impl PartialOrd for BackendScalar
Source§impl Sub for BackendScalar
impl Sub for BackendScalar
Source§impl SumFromStorage for BackendScalar
impl SumFromStorage for BackendScalar
Source§fn sum_from_storage(storage: &Storage) -> Self
fn sum_from_storage(storage: &Storage) -> Self
Source§impl TryFrom<BackendScalar> for f64
impl TryFrom<BackendScalar> for f64
Auto Trait Implementations§
impl !Freeze for BackendScalar
impl !RefUnwindSafe for BackendScalar
impl !UnwindSafe for BackendScalar
impl Send for BackendScalar
impl Sync for BackendScalar
impl Unpin for BackendScalar
impl UnsafeUnpin for BackendScalar
Blanket Implementations§
Source§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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T, U> Imply<T> for U
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