Skip to main content

BackendScalar

Struct BackendScalar 

Source
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

Source

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);
Source

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());
Source

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());
Source

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);
Source

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| = 5
Source

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);
Source

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);
Source

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);
Source

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);
Source

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());
Source

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());
Source

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());
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

pub fn powi(&self, exponent: i32) -> Self

Integer exponent power, preserving AD metadata.

§Examples
use tensor4all_tensorbackend::BackendScalar;

let s = BackendScalar::new_real(3.0);
assert!((s.powi(2).real() - 9.0).abs() < 1e-10);

Trait Implementations§

Source§

impl Add for BackendScalar

Source§

type Output = BackendScalar

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for BackendScalar

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BackendScalar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BackendScalar

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for BackendScalar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div for BackendScalar

Source§

type Output = BackendScalar

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<BackendScalar> for Complex64

Source§

type Output = BackendScalar

The resulting type after applying the / operator.
Source§

fn div(self, rhs: BackendScalar) -> Self::Output

Performs the / operation. Read more
Source§

impl From<BackendScalar> for Complex64

Source§

fn from(value: BackendScalar) -> Self

Converts to this type from the input type.
Source§

impl From<Complex<f32>> for BackendScalar

Source§

fn from(value: Complex32) -> Self

Converts to this type from the input type.
Source§

impl From<Complex<f64>> for BackendScalar

Source§

fn from(value: Complex64) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for BackendScalar

Source§

fn from(value: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for BackendScalar

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl Mul for BackendScalar

Source§

type Output = BackendScalar

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<BackendScalar> for f64

Source§

type Output = BackendScalar

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: BackendScalar) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<BackendScalar> for Complex64

Source§

type Output = BackendScalar

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: BackendScalar) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<BackendScalar> for &Storage

Multiply storage by a scalar (BackendScalar). May promote f64 storage to Complex64 when scalar is complex.

Source§

type Output = Storage

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: BackendScalar) -> Self::Output

Performs the * operation. Read more
Source§

impl Neg for BackendScalar

Source§

type Output = BackendScalar

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl One for BackendScalar

Source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
Source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Returns true if self is equal to the multiplicative identity. Read more
Source§

impl PartialEq for BackendScalar

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for BackendScalar

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for BackendScalar

Source§

type Output = BackendScalar

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl SumFromStorage for BackendScalar

Source§

fn sum_from_storage(storage: &Storage) -> Self

Compute the sum of all elements in the storage.
Source§

impl TryFrom<BackendScalar> for f64

Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

fn try_from(value: BackendScalar) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Zero for BackendScalar

Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,

§

impl<T> MaybeSync for T
where T: Sync,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V