Skip to main content

tensor4all_core/
any_scalar.rs

1#[cfg(test)]
2use std::cell::Cell;
3use std::cmp::Ordering;
4use std::fmt;
5use std::ops::{Add, Div, Mul, Neg, Sub};
6use std::sync::Arc;
7
8use anyhow::{anyhow, Result};
9use num_complex::{Complex32, Complex64};
10use num_traits::{One, Zero};
11use tenferro::DType;
12use tensor4all_tensorbackend::{
13    BackendScalar, ScalarTensorElement, ScalarValue, Storage, SumFromStorage,
14};
15
16use crate::defaults::eager_session::{
17    with_eager_session_typed, with_engine, with_execution_engine,
18};
19use crate::defaults::idx_tensor::IdxTensor;
20
21#[cfg(test)]
22thread_local! {
23    static FORCE_ANY_SCALAR_TENSOR_INITIALIZATION_FAILURE: Cell<bool> = const { Cell::new(false) };
24}
25
26#[derive(Debug, Clone, thiserror::Error)]
27enum AnyScalarTensorError {
28    #[error("AnyScalar tensor initialization failed: {source}")]
29    Initialization {
30        #[source]
31        source: Arc<dyn std::error::Error + Send + Sync + 'static>,
32    },
33    #[error("AnyScalar::{op} failed: {source}")]
34    Operation {
35        op: &'static str,
36        #[source]
37        source: Arc<dyn std::error::Error + Send + Sync + 'static>,
38    },
39}
40
41fn initialize_tensor<T: ScalarTensorElement>(
42    value: T,
43) -> std::result::Result<IdxTensor, AnyScalarTensorError> {
44    #[cfg(test)]
45    if FORCE_ANY_SCALAR_TENSOR_INITIALIZATION_FAILURE.with(Cell::get) {
46        return Err(AnyScalarTensorError::Initialization {
47            source: Arc::new(std::io::Error::other(
48                "forced AnyScalar eager initialization failure",
49            )),
50        });
51    }
52
53    IdxTensor::scalar(value).map_err(|source| AnyScalarTensorError::Initialization {
54        source: Arc::from(anyhow::Error::new(source).into_boxed_dyn_error()),
55    })
56}
57
58/// Error returned by eager-tensor `AnyScalar` operations (autodiff, conjugation,
59/// and complex composition).
60///
61/// The full original diagnostic is preserved in [`AnyScalarError::source`], so
62/// callers can inspect the underlying tensor, AD-runtime, or configuration
63/// failure without losing context.
64///
65/// # Examples
66///
67/// ```
68/// use tensor4all_core::{AnyScalar, AnyScalarError};
69///
70/// let result: Result<AnyScalar, AnyScalarError> =
71///     AnyScalar::compose_complex(AnyScalar::new_real(1.0), AnyScalar::new_complex(0.0, 1.0));
72/// let err = result.unwrap_err();
73/// assert!(err.source.to_string().contains("real-valued"));
74/// ```
75#[derive(Debug, thiserror::Error)]
76#[error("AnyScalar eager-tensor operation failed: {source}")]
77pub struct AnyScalarError {
78    /// Original tensor, AD-runtime, or configuration diagnostic, including any
79    /// operation-specific context added by the failing call.
80    #[source]
81    pub source: anyhow::Error,
82}
83
84impl From<anyhow::Error> for AnyScalarError {
85    fn from(source: anyhow::Error) -> Self {
86        Self { source }
87    }
88}
89
90fn operation_error<E>(op: &'static str, source: E) -> anyhow::Error
91where
92    E: std::error::Error + Send + Sync + 'static,
93{
94    anyhow::Error::new(AnyScalarTensorError::Operation {
95        op,
96        source: Arc::new(source),
97    })
98}
99
100fn operation_error_from_anyhow(op: &'static str, source: anyhow::Error) -> anyhow::Error {
101    match source.downcast::<AnyScalarTensorError>() {
102        Ok(source) => anyhow::Error::new(source),
103        Err(source) => anyhow::Error::new(AnyScalarTensorError::Operation {
104            op,
105            source: Arc::from(source.into_boxed_dyn_error()),
106        }),
107    }
108}
109
110/// Dynamic scalar compatibility wrapper for tensor4all-core.
111/// This owns a rank-0 [`IdxTensor`] so that scalar values can participate in
112/// the same eager autodiff graph as tensors while preserving the existing
113/// dynamic scalar API shape. The infallible scalar constructors retain a
114/// tensor-initialization failure for later fallible tensor or AD operations.
115/// Infallible arithmetic also retains typed backend diagnostics and the
116/// tracked-state marker when an eager operation fails.
117#[derive(Clone)]
118pub struct AnyScalar {
119    tensor: std::result::Result<IdxTensor, AnyScalarTensorError>,
120    /// Holds only the four [`ScalarTensorElement`] kinds. Values arrive through
121    /// [`AnyScalar::from_value`] and [`AnyScalar::from_published`] (both bound to
122    /// that trait), [`AnyScalar::scalar_value_from_tensor`] (which rejects every
123    /// other dtype), or [`AnyScalar::from_backend_scalar`] (which normalises to
124    /// `f64`/`c64`), so the integer and bool variants are matched but never
125    /// stored.
126    value: ScalarValue,
127    tracks_grad: bool,
128}
129
130impl AnyScalar {
131    fn wrap_tensor(tensor: IdxTensor) -> Result<Self> {
132        let dims = tensor.dims();
133        anyhow::ensure!(
134            dims.is_empty(),
135            "AnyScalar requires a rank-0 tensor, got dims {:?}",
136            dims
137        );
138        let value = Self::scalar_value_from_tensor(&tensor)?;
139        let tracks_grad = tensor.tracks_grad();
140        Ok(Self {
141            tensor: Ok(tensor),
142            value,
143            tracks_grad,
144        })
145    }
146
147    fn from_tensor_result(tensor: Result<IdxTensor>, op: &'static str) -> Result<Self> {
148        let tensor = tensor.map_err(|error| operation_error_from_anyhow(op, error))?;
149        Self::wrap_tensor(tensor).map_err(|error| operation_error_from_anyhow(op, error))
150    }
151
152    fn fallback_result(
153        result: Result<Self>,
154        op: &'static str,
155        fallback: impl FnOnce() -> ScalarValue,
156        tracks_grad: bool,
157    ) -> Self {
158        match result {
159            Ok(result) => result,
160            Err(error) => {
161                let error = match error.downcast::<AnyScalarTensorError>() {
162                    Ok(error) => error,
163                    Err(error) => AnyScalarTensorError::Operation {
164                        op,
165                        source: Arc::from(error.into_boxed_dyn_error()),
166                    },
167                };
168                Self {
169                    tensor: Err(error),
170                    value: fallback(),
171                    tracks_grad,
172                }
173            }
174        }
175    }
176
177    fn scalar_value_from_backend(value: BackendScalar) -> ScalarValue {
178        value
179            .as_c64()
180            .map(ScalarValue::C64)
181            .unwrap_or_else(|| ScalarValue::F64(value.real()))
182    }
183
184    fn zero_like(&self) -> Self {
185        match self.value() {
186            ScalarValue::F32(_) => Self::from_value(0.0_f32),
187            ScalarValue::F64(_) => Self::from_value(0.0_f64),
188            ScalarValue::C32(_) => Self::from_value(Complex32::new(0.0, 0.0)),
189            ScalarValue::C64(_) => Self::from_value(Complex64::new(0.0, 0.0)),
190            // Unreachable: see the invariant on `AnyScalar::value`.
191            ScalarValue::I32(_) | ScalarValue::I64(_) | ScalarValue::Bool(_) => {
192                Self::from_real(0.0)
193            }
194        }
195    }
196
197    fn one_like(&self) -> Self {
198        match self.value() {
199            ScalarValue::F32(_) => Self::from_value(1.0_f32),
200            ScalarValue::F64(_) => Self::from_value(1.0_f64),
201            ScalarValue::C32(_) => Self::from_value(Complex32::new(1.0, 0.0)),
202            ScalarValue::C64(_) => Self::from_value(Complex64::new(1.0, 0.0)),
203            // Unreachable: see the invariant on `AnyScalar::value`.
204            ScalarValue::I32(_) | ScalarValue::I64(_) | ScalarValue::Bool(_) => {
205                Self::from_real(1.0)
206            }
207        }
208    }
209
210    fn from_eager_binary<E>(
211        lhs: &Self,
212        rhs: &Self,
213        op: &'static str,
214        f: impl FnOnce(
215            &mut tenferro_ad::EagerSession<'_>,
216            &tenferro_ad::EagerTensor,
217            &tenferro_ad::EagerTensor,
218        ) -> std::result::Result<tenferro_ad::EagerTensor, E>,
219    ) -> Result<Self>
220    where
221        E: From<tenferro_ad::Error> + std::error::Error + Send + Sync + 'static,
222    {
223        let lhs_inner = lhs.as_tensor()?.as_inner()?;
224        let rhs_inner = rhs.as_tensor()?.as_inner()?;
225        let result = with_eager_session_typed(lhs_inner.runtime(), None, |session| {
226            f(session, lhs_inner, rhs_inner)
227        })
228        .map_err(|error| operation_error(op, error))?
229        .map_err(|error| operation_error(op, error))?;
230        Self::from_tensor_result(IdxTensor::from_inner(vec![], result), op)
231    }
232
233    fn from_eager_unary<E>(
234        input: &Self,
235        op: &'static str,
236        f: impl FnOnce(
237            &mut tenferro_ad::EagerSession<'_>,
238            &tenferro_ad::EagerTensor,
239        ) -> std::result::Result<tenferro_ad::EagerTensor, E>,
240    ) -> Result<Self>
241    where
242        E: From<tenferro_ad::Error> + std::error::Error + Send + Sync + 'static,
243    {
244        let inner = input.as_tensor()?.as_inner()?;
245        let result = with_eager_session_typed(inner.runtime(), None, |session| f(session, inner))
246            .map_err(|error| operation_error(op, error))?
247            .map_err(|error| operation_error(op, error))?;
248        Self::from_tensor_result(IdxTensor::from_inner(vec![], result), op)
249    }
250
251    fn scalar_value_from_tensor(tensor: &IdxTensor) -> Result<ScalarValue> {
252        let inner = tensor.as_inner()?;
253        match inner.dtype() {
254            DType::F32 => inner
255                .value()?
256                .as_slice::<f32>()?
257                .first()
258                .copied()
259                .map(ScalarValue::F32)
260                .ok_or_else(|| anyhow!("rank-0 f32 scalar tensor is empty")),
261            DType::F64 => inner
262                .value()?
263                .as_slice::<f64>()?
264                .first()
265                .copied()
266                .map(ScalarValue::F64)
267                .ok_or_else(|| anyhow!("rank-0 f64 scalar tensor is empty")),
268            DType::C32 => inner
269                .value()?
270                .as_slice::<Complex32>()?
271                .first()
272                .copied()
273                .map(ScalarValue::C32)
274                .ok_or_else(|| anyhow!("rank-0 c32 scalar tensor is empty")),
275            DType::C64 => inner
276                .value()?
277                .as_slice::<Complex64>()?
278                .first()
279                .copied()
280                .map(ScalarValue::C64)
281                .ok_or_else(|| anyhow!("rank-0 c64 scalar tensor is empty")),
282            dtype => Err(anyhow!("unsupported scalar tensor dtype {dtype:?}")),
283        }
284    }
285
286    fn value(&self) -> ScalarValue {
287        self.value
288    }
289
290    fn from_backend_scalar(value: BackendScalar) -> Self {
291        // `scalar_value_from_backend` normalises to exactly these two kinds.
292        match Self::scalar_value_from_backend(value) {
293            ScalarValue::C64(value) => Self::from_value(value),
294            other => Self::from_real(other.real()),
295        }
296    }
297
298    pub(crate) fn from_tensor(tensor: IdxTensor) -> Result<Self> {
299        Self::wrap_tensor(tensor)
300    }
301
302    /// Wrap a scalar whose rank-0 value is already published as plain host storage.
303    ///
304    /// [`AnyScalar::from_tensor`] extracts its value through the tensor's eager representation, which for a
305    /// freshly materialized tensor populates an eager cache from the process-global eager runtime. An
306    /// explicit-session route must not do that, so this constructor takes the value directly; the tensor is
307    /// carried only so the scalar still behaves like one.
308    pub(crate) fn from_published<T: ScalarTensorElement>(value: T, tensor: IdxTensor) -> Self {
309        Self {
310            tensor: Ok(tensor),
311            value: T::scalar_value(value),
312            tracks_grad: false,
313        }
314    }
315
316    pub(crate) fn as_tensor(&self) -> Result<&IdxTensor> {
317        self.tensor
318            .as_ref()
319            .map_err(|error| anyhow::Error::new(error.clone()))
320    }
321
322    /// Creates an `AnyScalar` from a tensor element.
323    ///
324    /// Use this when you already have a scalar value that implements
325    /// [`TensorElement`] and want to lift it into the dynamic scalar wrapper.
326    ///
327    /// # Arguments
328    ///
329    /// * `value` - The scalar value to wrap.
330    ///
331    /// # Returns
332    ///
333    /// A rank-0 `AnyScalar` containing `value`. The supported scalar types are
334    /// `f32`, `f64`, `Complex32`, and `Complex64`; the dtype is retained without
335    /// promotion.
336    ///
337    /// Tensor initialization is attempted eagerly. Because this constructor is
338    /// infallible, an initialization failure is retained and returned by later
339    /// tensor- or AD-dependent operations such as [`AnyScalar::enable_grad`].
340    /// Value-only accessors and non-AD arithmetic remain available.
341    ///
342    /// # Examples
343    ///
344    /// ```
345    /// use tensor4all_core::AnyScalar;
346    ///
347    /// let scalar = AnyScalar::from_value(3.0f64);
348    /// assert_eq!(scalar.real(), 3.0);
349    /// assert!(scalar.is_real());
350    /// ```
351    pub fn from_value<T: ScalarTensorElement>(value: T) -> Self {
352        Self {
353            tensor: initialize_tensor(value),
354            value: T::scalar_value(value),
355            tracks_grad: false,
356        }
357    }
358
359    /// Creates a real-valued `AnyScalar`.
360    ///
361    /// This is a convenience wrapper around [`AnyScalar::from_value`].
362    ///
363    /// # Arguments
364    ///
365    /// * `x` - The real scalar value to wrap.
366    ///
367    /// # Returns
368    ///
369    /// A rank-0 `AnyScalar` with real dtype.
370    ///
371    /// # Examples
372    ///
373    /// ```
374    /// use tensor4all_core::AnyScalar;
375    ///
376    /// let scalar = AnyScalar::from_real(1.25);
377    /// assert_eq!(scalar.as_f64(), Some(1.25));
378    /// assert!(scalar.is_real());
379    /// ```
380    pub fn from_real(x: f64) -> Self {
381        Self::from_value(x)
382    }
383
384    /// Creates a complex-valued `AnyScalar`.
385    ///
386    /// This is a convenience wrapper around [`AnyScalar::from_value`].
387    ///
388    /// # Arguments
389    ///
390    /// * `re` - The real part of the complex value.
391    /// * `im` - The imaginary part of the complex value.
392    ///
393    /// # Returns
394    ///
395    /// A rank-0 `AnyScalar` containing the requested complex number.
396    ///
397    /// # Examples
398    ///
399    /// ```
400    /// use tensor4all_core::AnyScalar;
401    ///
402    /// let scalar = AnyScalar::from_complex(1.0, -2.0);
403    /// assert_eq!(scalar.as_c64().map(|z| (z.re, z.im)), Some((1.0, -2.0)));
404    /// assert!(scalar.is_complex());
405    /// ```
406    pub fn from_complex(re: f64, im: f64) -> Self {
407        Self::from_value(Complex64::new(re, im))
408    }
409
410    /// Creates a real-valued `AnyScalar`.
411    ///
412    /// This is an alias for [`AnyScalar::from_real`].
413    ///
414    /// # Arguments
415    ///
416    /// * `x` - The real scalar value to wrap.
417    ///
418    /// # Returns
419    ///
420    /// A rank-0 `AnyScalar` with real dtype.
421    ///
422    /// # Examples
423    ///
424    /// ```
425    /// use tensor4all_core::AnyScalar;
426    ///
427    /// let scalar = AnyScalar::new_real(2.5);
428    /// assert_eq!(scalar.real(), 2.5);
429    /// assert!(scalar.is_real());
430    /// ```
431    pub fn new_real(x: f64) -> Self {
432        Self::from_real(x)
433    }
434
435    /// Creates a complex-valued `AnyScalar`.
436    ///
437    /// This is an alias for [`AnyScalar::from_complex`].
438    ///
439    /// # Arguments
440    ///
441    /// * `re` - The real part of the complex value.
442    /// * `im` - The imaginary part of the complex value.
443    ///
444    /// # Returns
445    ///
446    /// A rank-0 `AnyScalar` containing the requested complex number.
447    ///
448    /// # Examples
449    ///
450    /// ```
451    /// use tensor4all_core::AnyScalar;
452    ///
453    /// let scalar = AnyScalar::new_complex(2.0, 3.0);
454    /// assert_eq!(scalar.as_c64().map(|z| (z.re, z.im)), Some((2.0, 3.0)));
455    /// assert!(scalar.is_complex());
456    /// ```
457    pub fn new_complex(re: f64, im: f64) -> Self {
458        Self::from_complex(re, im)
459    }
460
461    /// Returns the detached primal value of this scalar.
462    ///
463    /// This is an alias for [`AnyScalar::detach`].
464    ///
465    /// # Returns
466    ///
467    /// A scalar with the same value and no gradient tracking.
468    ///
469    /// # Errors
470    ///
471    /// Returns an [`AnyScalarError`] when this scalar's eager backend tensor
472    /// could not be created, or when the AD backend cannot detach it.
473    ///
474    /// # Examples
475    ///
476    /// ```
477    /// use tensor4all_core::AnyScalar;
478    ///
479    /// let primal = AnyScalar::new_real(5.0).enable_grad().unwrap().primal().unwrap();
480    /// assert_eq!(primal.real(), 5.0);
481    /// assert!(!primal.tracks_grad());
482    /// ```
483    pub fn primal(&self) -> std::result::Result<Self, AnyScalarError> {
484        self.detach()
485    }
486
487    /// Enables gradient tracking for this scalar.
488    ///
489    /// # Returns
490    ///
491    /// A new scalar that shares the same value but participates in autodiff.
492    ///
493    /// # Errors
494    ///
495    /// Returns the original tensor-initialization diagnostic if this scalar's
496    /// eager backend tensor could not be created, or propagates an AD runtime
497    /// failure while enabling gradients.
498    ///
499    /// # Examples
500    ///
501    /// ```
502    /// use tensor4all_core::AnyScalar;
503    ///
504    /// let scalar = AnyScalar::new_real(2.0).enable_grad().unwrap();
505    /// assert!(scalar.tracks_grad());
506    /// ```
507    pub fn enable_grad(self) -> std::result::Result<Self, AnyScalarError> {
508        let tensor = self.tensor.map_err(anyhow::Error::new)?;
509        Self::from_tensor(tensor.enable_grad().map_err(anyhow::Error::from)?)
510            .map_err(AnyScalarError::from)
511    }
512
513    /// Returns whether this scalar tracks gradients.
514    ///
515    /// # Returns
516    ///
517    /// `true` when the scalar participates in autodiff or retains a failed
518    /// tracked operation, otherwise `false`.
519    ///
520    /// # Examples
521    ///
522    /// ```
523    /// use tensor4all_core::AnyScalar;
524    ///
525    /// let scalar = AnyScalar::new_real(1.0);
526    /// assert!(!scalar.tracks_grad());
527    /// ```
528    pub fn tracks_grad(&self) -> bool {
529        self.tracks_grad || self.tensor.as_ref().is_ok_and(IdxTensor::tracks_grad)
530    }
531
532    /// Returns the stored gradient, if any.
533    ///
534    /// # Returns
535    ///
536    /// `Ok(Some(_))` when a gradient is available, `Ok(None)` when no gradient
537    /// has been recorded, or an error if the backend cannot read it.
538    ///
539    /// # Errors
540    ///
541    /// Returns an [`AnyScalarError`] when this scalar's eager backend tensor
542    /// could not be created, or when the AD backend cannot read the stored
543    /// gradient. A scalar with no recorded gradient returns `Ok(None)` rather
544    /// than an error.
545    ///
546    /// # Examples
547    ///
548    /// ```
549    /// use tensor4all_core::AnyScalar;
550    ///
551    /// let x = AnyScalar::new_real(2.0).enable_grad().unwrap();
552    /// let y = &x * &x;
553    /// y.backward().unwrap();
554    ///
555    /// let grad = x.grad().unwrap().unwrap();
556    /// assert_eq!(grad.real(), 4.0);
557    /// ```
558    pub fn grad(&self) -> std::result::Result<Option<Self>, AnyScalarError> {
559        self.as_tensor()?
560            .grad()
561            .map_err(anyhow::Error::from)
562            .and_then(|maybe_grad| maybe_grad.map(Self::from_tensor).transpose())
563            .map_err(AnyScalarError::from)
564    }
565
566    /// Clears the stored gradient for this scalar.
567    ///
568    /// # Returns
569    ///
570    /// `Ok(())` when the gradient buffer was cleared successfully.
571    ///
572    /// # Errors
573    ///
574    /// Returns an [`AnyScalarError`] when this scalar's eager backend tensor
575    /// could not be created, or when the AD backend cannot clear the stored
576    /// gradient.
577    ///
578    /// # Examples
579    ///
580    /// ```
581    /// use tensor4all_core::AnyScalar;
582    ///
583    /// let x = AnyScalar::new_real(2.0).enable_grad().unwrap();
584    /// let y = &x * &x;
585    /// y.backward().unwrap();
586    /// assert!(x.grad().unwrap().is_some());
587    ///
588    /// x.clear_grad().unwrap();
589    /// assert!(x.grad().unwrap().is_none());
590    /// ```
591    pub fn clear_grad(&self) -> std::result::Result<(), AnyScalarError> {
592        self.as_tensor()?
593            .clear_grad()
594            .map_err(anyhow::Error::from)
595            .map_err(AnyScalarError::from)
596    }
597
598    /// Runs reverse-mode autodiff starting from this scalar.
599    ///
600    /// # Returns
601    ///
602    /// `Ok(())` when gradients were accumulated successfully.
603    ///
604    /// # Errors
605    ///
606    /// Returns an [`AnyScalarError`] when this scalar's eager backend tensor
607    /// could not be created, when it is not a scalar-valued tracked leaf, or
608    /// when the reverse pass fails.
609    ///
610    /// # Examples
611    ///
612    /// ```
613    /// use tensor4all_core::AnyScalar;
614    ///
615    /// let x = AnyScalar::new_real(2.0).enable_grad().unwrap();
616    /// let y = &x * &x;
617    /// y.backward().unwrap();
618    ///
619    /// let grad = x.grad().unwrap().unwrap();
620    /// assert_eq!(grad.real(), 4.0);
621    /// ```
622    pub fn backward(&self) -> std::result::Result<(), AnyScalarError> {
623        let inner = self.as_tensor()?.as_inner()?;
624        with_execution_engine(inner.runtime(), None, |engine| inner.backward(engine))
625            .map(|_| ())
626            .map_err(|error| AnyScalarError::from(anyhow::Error::new(error)))
627    }
628
629    /// Returns a detached copy of this scalar.
630    ///
631    /// # Returns
632    ///
633    /// A scalar with the same value but without gradient tracking.
634    ///
635    /// # Errors
636    ///
637    /// Returns an [`AnyScalarError`] when this scalar's eager backend tensor
638    /// could not be created, or when the AD backend cannot detach it.
639    ///
640    /// # Examples
641    ///
642    /// ```
643    /// use tensor4all_core::AnyScalar;
644    ///
645    /// let detached = AnyScalar::new_real(7.0)
646    ///     .enable_grad()
647    ///     .unwrap()
648    ///     .detach()
649    ///     .unwrap();
650    /// assert_eq!(detached.real(), 7.0);
651    /// assert!(!detached.tracks_grad());
652    /// ```
653    pub fn detach(&self) -> std::result::Result<Self, AnyScalarError> {
654        let tensor = self.as_tensor()?;
655        let inner = tensor.as_inner()?;
656        let detached = with_engine(inner.runtime(), None, |engine| inner.detach(engine))
657            .map_err(|error| AnyScalarError::from(anyhow::Error::new(error)))?;
658        Self::from_tensor(IdxTensor::from_inner(vec![], detached)?).map_err(AnyScalarError::from)
659    }
660
661    /// Returns the real part of this scalar.
662    ///
663    /// # Returns
664    ///
665    /// The real component as an `f64`, regardless of the underlying storage
666    /// type.
667    ///
668    /// # Examples
669    ///
670    /// ```
671    /// use tensor4all_core::AnyScalar;
672    ///
673    /// let scalar = AnyScalar::new_complex(3.0, -4.0);
674    /// assert_eq!(scalar.real(), 3.0);
675    /// ```
676    pub fn real(&self) -> f64 {
677        self.value().real()
678    }
679
680    /// Returns the imaginary part of this scalar.
681    ///
682    /// # Returns
683    ///
684    /// The imaginary component as an `f64`. Real-valued scalars return `0.0`.
685    ///
686    /// # Examples
687    ///
688    /// ```
689    /// use tensor4all_core::AnyScalar;
690    ///
691    /// let scalar = AnyScalar::new_complex(3.0, -4.0);
692    /// assert_eq!(scalar.imag(), -4.0);
693    /// ```
694    pub fn imag(&self) -> f64 {
695        self.value().imag()
696    }
697
698    /// Returns the magnitude of this scalar.
699    ///
700    /// # Returns
701    ///
702    /// The absolute value for real scalars or the complex norm for complex
703    /// scalars.
704    ///
705    /// # Examples
706    ///
707    /// ```
708    /// use tensor4all_core::AnyScalar;
709    ///
710    /// let scalar = AnyScalar::new_complex(3.0, -4.0);
711    /// assert_eq!(scalar.abs(), 5.0);
712    /// ```
713    pub fn abs(&self) -> f64 {
714        self.value().abs()
715    }
716
717    /// Returns whether this scalar is complex-valued.
718    ///
719    /// # Returns
720    ///
721    /// `true` for complex dtypes and `false` for real or integer dtypes.
722    ///
723    /// # Examples
724    ///
725    /// ```
726    /// use tensor4all_core::AnyScalar;
727    ///
728    /// assert!(AnyScalar::new_complex(1.0, 2.0).is_complex());
729    /// assert!(!AnyScalar::new_real(1.0).is_complex());
730    /// ```
731    pub fn is_complex(&self) -> bool {
732        self.value().is_complex()
733    }
734
735    /// Returns whether this scalar is real-valued.
736    ///
737    /// # Returns
738    ///
739    /// `true` when the scalar is not complex-valued.
740    ///
741    /// # Examples
742    ///
743    /// ```
744    /// use tensor4all_core::AnyScalar;
745    ///
746    /// assert!(AnyScalar::new_real(1.0).is_real());
747    /// assert!(!AnyScalar::new_complex(1.0, 2.0).is_real());
748    /// ```
749    pub fn is_real(&self) -> bool {
750        !self.is_complex()
751    }
752
753    /// Returns whether this scalar is exactly zero.
754    ///
755    /// # Returns
756    ///
757    /// `true` for exact zeros and `false` for any nonzero value.
758    ///
759    /// # Examples
760    ///
761    /// ```
762    /// use tensor4all_core::AnyScalar;
763    ///
764    /// assert!(AnyScalar::new_real(0.0).is_zero());
765    /// assert!(!AnyScalar::new_complex(0.0, 1.0).is_zero());
766    /// ```
767    pub fn is_zero(&self) -> bool {
768        self.value().is_zero()
769    }
770
771    /// Returns this scalar as an `f64` when it is real-valued.
772    ///
773    /// # Returns
774    ///
775    /// `Some(value)` for real and integer scalars, or `None` for complex
776    /// scalars.
777    ///
778    /// # Examples
779    ///
780    /// ```
781    /// use tensor4all_core::AnyScalar;
782    ///
783    /// assert_eq!(AnyScalar::new_real(2.5).as_f64(), Some(2.5));
784    /// assert_eq!(AnyScalar::new_complex(2.5, 1.0).as_f64(), None);
785    /// ```
786    pub fn as_f64(&self) -> Option<f64> {
787        match self.value() {
788            ScalarValue::F32(value) => Some(value as f64),
789            ScalarValue::F64(value) => Some(value),
790            ScalarValue::I32(value) => Some(value as f64),
791            ScalarValue::I64(value) => Some(value as f64),
792            ScalarValue::Bool(value) => Some(if value { 1.0 } else { 0.0 }),
793            ScalarValue::C32(_) | ScalarValue::C64(_) => None,
794        }
795    }
796
797    /// Returns this scalar as a `Complex64` when it is complex-valued.
798    ///
799    /// # Returns
800    ///
801    /// `Some(value)` for complex scalars or `None` for real and integer
802    /// scalars.
803    ///
804    /// # Examples
805    ///
806    /// ```
807    /// use tensor4all_core::AnyScalar;
808    ///
809    /// let scalar = AnyScalar::new_complex(2.5, 1.0);
810    /// assert_eq!(scalar.as_c64().map(|z| (z.re, z.im)), Some((2.5, 1.0)));
811    /// assert_eq!(AnyScalar::new_real(2.5).as_c64(), None);
812    /// ```
813    pub fn as_c64(&self) -> Option<Complex64> {
814        match self.value() {
815            ScalarValue::F32(_)
816            | ScalarValue::F64(_)
817            | ScalarValue::I32(_)
818            | ScalarValue::I64(_)
819            | ScalarValue::Bool(_) => None,
820            ScalarValue::C32(value) => Some(Complex64::new(value.re as f64, value.im as f64)),
821            ScalarValue::C64(value) => Some(value),
822        }
823    }
824
825    /// Returns the complex conjugate of this scalar.
826    ///
827    /// # Returns
828    ///
829    /// The conjugated scalar. Real-valued inputs are returned unchanged.
830    ///
831    /// # Errors
832    ///
833    /// Returns an [`AnyScalarError`] when this scalar's eager backend tensor
834    /// could not be created, or when the AD backend's conjugation fails (for
835    /// example for a dtype it cannot track). Real-valued scalars are returned
836    /// unchanged.
837    ///
838    /// # Examples
839    ///
840    /// ```
841    /// use tensor4all_core::AnyScalar;
842    ///
843    /// let scalar = AnyScalar::new_complex(3.0, -4.0).conj();
844    /// assert_eq!(scalar.as_c64().map(|z| (z.re, z.im)), Some((3.0, 4.0)));
845    /// ```
846    pub fn try_conj(&self) -> std::result::Result<Self, AnyScalarError> {
847        self.as_tensor()?;
848        if !self.tracks_grad() {
849            return Ok(Self::from_backend_scalar(self.to_backend_scalar().conj()));
850        }
851        Self::from_eager_unary(self, "conj", |session, tensor| session.conj(tensor))
852            .map_err(AnyScalarError::from)
853    }
854
855    /// Returns the complex conjugate of this scalar.
856    pub fn conj(&self) -> Self {
857        Self::fallback_result(
858            self.try_conj().map_err(|error| error.source),
859            "conj",
860            || Self::scalar_value_from_backend(self.to_backend_scalar().conj()),
861            self.tracks_grad(),
862        )
863    }
864
865    /// Returns the real part as a real-valued scalar.
866    ///
867    /// # Returns
868    ///
869    /// A real-valued scalar containing the real component of `self`.
870    ///
871    /// # Examples
872    ///
873    /// ```
874    /// use tensor4all_core::AnyScalar;
875    ///
876    /// let scalar = AnyScalar::new_complex(3.0, -4.0).real_part();
877    /// assert_eq!(scalar.real(), 3.0);
878    /// assert!(scalar.is_real());
879    /// ```
880    pub fn real_part(&self) -> Self {
881        Self::fallback_result(
882            self.try_real_part(),
883            "real_part",
884            || Self::from_real(self.real()).value(),
885            self.tracks_grad(),
886        )
887    }
888
889    /// Returns the imaginary part as a real-valued scalar.
890    ///
891    /// # Returns
892    ///
893    /// A real-valued scalar containing the imaginary component of `self`.
894    ///
895    /// # Examples
896    ///
897    /// ```
898    /// use tensor4all_core::AnyScalar;
899    ///
900    /// let scalar = AnyScalar::new_complex(3.0, -4.0).imag_part();
901    /// assert_eq!(scalar.real(), -4.0);
902    /// assert!(scalar.is_real());
903    /// ```
904    pub fn imag_part(&self) -> Self {
905        Self::fallback_result(
906            self.try_imag_part(),
907            "imag_part",
908            || Self::from_real(self.imag()).value(),
909            self.tracks_grad(),
910        )
911    }
912
913    /// Combines two real-valued scalars into a complex scalar.
914    ///
915    /// # Arguments
916    ///
917    /// * `real` - The real component.
918    /// * `imag` - The imaginary component.
919    ///
920    /// # Returns
921    ///
922    /// A complex `AnyScalar` whose real and imaginary parts come from the
923    /// inputs.
924    ///
925    /// # Errors
926    ///
927    /// Returns an [`AnyScalarError`] when either input is not real-valued, or
928    /// when the multiplication by the imaginary unit or the addition fails in
929    /// the eager backend.
930    ///
931    /// # Examples
932    ///
933    /// ```
934    /// use tensor4all_core::AnyScalar;
935    ///
936    /// let scalar = AnyScalar::compose_complex(
937    ///     AnyScalar::new_real(3.0),
938    ///     AnyScalar::new_real(-4.0),
939    /// )
940    /// .unwrap();
941    /// assert_eq!(scalar.as_c64().map(|z| (z.re, z.im)), Some((3.0, -4.0)));
942    /// ```
943    pub fn compose_complex(real: Self, imag: Self) -> std::result::Result<Self, AnyScalarError> {
944        if !real.is_real() || !imag.is_real() {
945            return Err(anyhow!("compose_complex requires real-valued inputs").into());
946        }
947        let imag_term = imag.try_mul(&Self::new_complex(0.0, 1.0))?;
948        real.try_add(&imag_term).map_err(AnyScalarError::from)
949    }
950
951    /// Returns the square root of this scalar.
952    ///
953    /// # Returns
954    ///
955    /// The principal square root. Negative real inputs and complex inputs use
956    /// complex arithmetic.
957    ///
958    /// # Examples
959    ///
960    /// ```
961    /// use tensor4all_core::AnyScalar;
962    ///
963    /// let scalar = AnyScalar::new_real(9.0).sqrt();
964    /// assert_eq!(scalar.real(), 3.0);
965    /// assert!(scalar.is_real());
966    /// ```
967    pub fn sqrt(&self) -> Self {
968        Self::fallback_result(
969            self.try_sqrt(),
970            "sqrt",
971            || Self::scalar_value_from_backend(self.to_backend_scalar().sqrt()),
972            self.tracks_grad(),
973        )
974    }
975
976    /// Raises this scalar to a floating-point power.
977    ///
978    /// # Arguments
979    ///
980    /// * `exponent` - The exponent to apply.
981    ///
982    /// # Returns
983    ///
984    /// The value of `self^exponent`.
985    ///
986    /// # Examples
987    ///
988    /// ```
989    /// use tensor4all_core::AnyScalar;
990    ///
991    /// let scalar = AnyScalar::new_real(2.0).powf(3.0);
992    /// assert_eq!(scalar.real(), 8.0);
993    /// ```
994    pub fn powf(&self, exponent: f64) -> Self {
995        Self::fallback_result(
996            self.try_powf(exponent),
997            "powf",
998            || Self::scalar_value_from_backend(self.to_backend_scalar().powf(exponent)),
999            self.tracks_grad(),
1000        )
1001    }
1002
1003    /// Raises this scalar to an integer power.
1004    ///
1005    /// # Arguments
1006    ///
1007    /// * `exponent` - The integer exponent to apply. Negative exponents return
1008    ///
1009    ///   the reciprocal power.
1010    ///
1011    /// # Returns
1012    ///
1013    /// The value of `self^exponent`. Zero exponents return `1`.
1014    ///
1015    /// # Examples
1016    ///
1017    /// ```
1018    /// use tensor4all_core::AnyScalar;
1019    ///
1020    /// assert_eq!(AnyScalar::new_real(2.0).powi(3).real(), 8.0);
1021    /// assert_eq!(AnyScalar::new_real(2.0).powi(-1).real(), 0.5);
1022    /// ```
1023    pub fn powi(&self, exponent: i32) -> Self {
1024        Self::fallback_result(
1025            self.try_powi(exponent),
1026            "powi",
1027            || Self::scalar_value_from_backend(self.to_backend_scalar().powi(exponent)),
1028            self.tracks_grad(),
1029        )
1030    }
1031
1032    pub(crate) fn to_backend_scalar(&self) -> BackendScalar {
1033        BackendScalar::from(self.value())
1034    }
1035
1036    pub(crate) fn try_add(&self, rhs: &Self) -> Result<Self> {
1037        self.as_tensor()?;
1038        rhs.as_tensor()?;
1039        if !self.tracks_grad() && !rhs.tracks_grad() {
1040            return Ok(Self::from_backend_scalar(
1041                self.to_backend_scalar() + rhs.to_backend_scalar(),
1042            ));
1043        }
1044        Self::from_eager_binary(self, rhs, "add", |session, lhs, rhs| session.add(lhs, rhs))
1045    }
1046
1047    pub(crate) fn try_mul(&self, rhs: &Self) -> Result<Self> {
1048        self.as_tensor()?;
1049        rhs.as_tensor()?;
1050        if !self.tracks_grad() && !rhs.tracks_grad() {
1051            return Ok(Self::from_backend_scalar(
1052                self.to_backend_scalar() * rhs.to_backend_scalar(),
1053            ));
1054        }
1055        Self::from_eager_binary(self, rhs, "mul", |session, lhs, rhs| session.mul(lhs, rhs))
1056    }
1057
1058    pub(crate) fn try_div(&self, rhs: &Self) -> Result<Self> {
1059        self.as_tensor()?;
1060        rhs.as_tensor()?;
1061        if !self.tracks_grad() && !rhs.tracks_grad() {
1062            return Ok(Self::from_backend_scalar(
1063                self.to_backend_scalar() / rhs.to_backend_scalar(),
1064            ));
1065        }
1066        Self::from_eager_binary(self, rhs, "div", |session, lhs, rhs| session.div(lhs, rhs))
1067    }
1068
1069    pub(crate) fn try_neg(&self) -> Result<Self> {
1070        self.as_tensor()?;
1071        if !self.tracks_grad() {
1072            return Ok(Self::from_backend_scalar(-self.to_backend_scalar()));
1073        }
1074        Self::from_eager_unary(self, "neg", |session, tensor| session.neg(tensor))
1075    }
1076
1077    fn try_real_part(&self) -> Result<Self> {
1078        self.as_tensor()?;
1079        if !self.tracks_grad() {
1080            return Ok(Self::from_real(self.real()));
1081        }
1082        if self.is_complex() {
1083            Self::from_eager_unary(self, "real_part", |session, tensor| {
1084                session.cast(tensor, DType::F64)
1085            })
1086        } else {
1087            self.try_mul(&Self::new_real(1.0))
1088        }
1089    }
1090
1091    fn try_imag_part(&self) -> Result<Self> {
1092        self.as_tensor()?;
1093        if !self.tracks_grad() {
1094            return Ok(Self::from_real(self.imag()));
1095        }
1096        if self.is_complex() {
1097            let factor = Self::new_complex(0.0, -1.0);
1098            let imaginary =
1099                Self::from_eager_binary(self, &factor, "imag_part", |session, value, factor| {
1100                    session.mul(value, factor)
1101                })?;
1102            Self::from_eager_unary(&imaginary, "imag_part", |session, tensor| {
1103                session.cast(tensor, DType::F64)
1104            })
1105        } else {
1106            self.try_mul(&Self::new_real(0.0))
1107        }
1108    }
1109
1110    fn try_sqrt(&self) -> Result<Self> {
1111        self.as_tensor()?;
1112        if !self.tracks_grad() {
1113            return Ok(Self::from_backend_scalar(self.to_backend_scalar().sqrt()));
1114        }
1115        if self.is_real() && self.real() < 0.0 {
1116            let magnitude_input =
1117                Self::from_eager_unary(self, "sqrt", |session, tensor| session.neg(tensor))?;
1118            let magnitude = magnitude_input.try_sqrt()?;
1119            let factor = Self::new_complex(0.0, 1.0);
1120            return Self::from_eager_binary(
1121                &magnitude,
1122                &factor,
1123                "sqrt",
1124                |session, value, factor| session.mul(value, factor),
1125            );
1126        }
1127        Self::from_eager_unary(self, "sqrt", |session, tensor| session.sqrt(tensor))
1128    }
1129
1130    fn try_powf(&self, exponent: f64) -> Result<Self> {
1131        self.as_tensor()?;
1132        if !self.tracks_grad() {
1133            return Ok(Self::from_backend_scalar(
1134                self.to_backend_scalar().powf(exponent),
1135            ));
1136        }
1137        if self.is_real() && self.real() < 0.0 && exponent.fract() != 0.0 {
1138            let magnitude_input =
1139                Self::from_eager_unary(self, "powf", |session, tensor| session.neg(tensor))?;
1140            let magnitude = magnitude_input.try_powf(exponent)?;
1141            let phase = std::f64::consts::PI * exponent;
1142            let factor = Self::new_complex(phase.cos(), phase.sin());
1143            return Self::from_eager_binary(
1144                &magnitude,
1145                &factor,
1146                "powf",
1147                |session, value, factor| session.mul(value, factor),
1148            );
1149        }
1150        let exponent = if self.is_complex() {
1151            Self::new_complex(exponent, 0.0)
1152        } else {
1153            Self::new_real(exponent)
1154        };
1155        Self::from_eager_binary(self, &exponent, "powf", |session, base, exponent| {
1156            session.pow(base, exponent)
1157        })
1158    }
1159
1160    fn try_powi(&self, exponent: i32) -> Result<Self> {
1161        self.as_tensor()?;
1162        if exponent == 0 {
1163            if self.tracks_grad() {
1164                // Build 1 as `self * 0 + 1`, rather than evaluating x^0.
1165                // This keeps the result in the graph and has an exact zero
1166                // derivative even when the input is zero.
1167                let zeroed = self.try_mul(&self.zero_like())?;
1168                return zeroed.try_add(&self.one_like());
1169            }
1170            return Ok(Self::one());
1171        }
1172        if self.tracks_grad() {
1173            return self.try_powf(exponent as f64);
1174        }
1175        Ok(Self::from_backend_scalar(
1176            self.to_backend_scalar().powi(exponent),
1177        ))
1178    }
1179}
1180
1181impl SumFromStorage for AnyScalar {
1182    fn sum_from_storage(storage: &Storage) -> Self {
1183        Self::from_backend_scalar(BackendScalar::sum_from_storage(storage))
1184    }
1185}
1186
1187impl From<f32> for AnyScalar {
1188    fn from(value: f32) -> Self {
1189        Self::from_value(value)
1190    }
1191}
1192
1193impl From<f64> for AnyScalar {
1194    fn from(value: f64) -> Self {
1195        Self::from_value(value)
1196    }
1197}
1198
1199impl From<Complex32> for AnyScalar {
1200    fn from(value: Complex32) -> Self {
1201        Self::from_value(value)
1202    }
1203}
1204
1205impl From<Complex64> for AnyScalar {
1206    fn from(value: Complex64) -> Self {
1207        Self::from_value(value)
1208    }
1209}
1210
1211impl TryFrom<AnyScalar> for f64 {
1212    type Error = &'static str;
1213
1214    fn try_from(value: AnyScalar) -> std::result::Result<Self, Self::Error> {
1215        value.as_f64().ok_or("cannot convert complex scalar to f64")
1216    }
1217}
1218
1219impl From<AnyScalar> for Complex64 {
1220    fn from(value: AnyScalar) -> Self {
1221        value.value().into_complex()
1222    }
1223}
1224
1225impl Add<&AnyScalar> for &AnyScalar {
1226    type Output = AnyScalar;
1227
1228    fn add(self, rhs: &AnyScalar) -> Self::Output {
1229        AnyScalar::fallback_result(
1230            self.try_add(rhs),
1231            "add",
1232            || {
1233                AnyScalar::scalar_value_from_backend(
1234                    self.to_backend_scalar() + rhs.to_backend_scalar(),
1235                )
1236            },
1237            self.tracks_grad() || rhs.tracks_grad(),
1238        )
1239    }
1240}
1241
1242impl Add<AnyScalar> for AnyScalar {
1243    type Output = AnyScalar;
1244
1245    fn add(self, rhs: AnyScalar) -> Self::Output {
1246        Add::add(&self, &rhs)
1247    }
1248}
1249
1250impl Add<AnyScalar> for &AnyScalar {
1251    type Output = AnyScalar;
1252
1253    fn add(self, rhs: AnyScalar) -> Self::Output {
1254        Add::add(self, &rhs)
1255    }
1256}
1257
1258impl Add<&AnyScalar> for AnyScalar {
1259    type Output = AnyScalar;
1260
1261    fn add(self, rhs: &AnyScalar) -> Self::Output {
1262        Add::add(&self, rhs)
1263    }
1264}
1265
1266impl Sub<&AnyScalar> for &AnyScalar {
1267    type Output = AnyScalar;
1268
1269    fn sub(self, rhs: &AnyScalar) -> Self::Output {
1270        Add::add(self, &Neg::neg(rhs))
1271    }
1272}
1273
1274impl Sub<AnyScalar> for AnyScalar {
1275    type Output = AnyScalar;
1276
1277    fn sub(self, rhs: AnyScalar) -> Self::Output {
1278        Sub::sub(&self, &rhs)
1279    }
1280}
1281
1282impl Sub<AnyScalar> for &AnyScalar {
1283    type Output = AnyScalar;
1284
1285    fn sub(self, rhs: AnyScalar) -> Self::Output {
1286        Sub::sub(self, &rhs)
1287    }
1288}
1289
1290impl Sub<&AnyScalar> for AnyScalar {
1291    type Output = AnyScalar;
1292
1293    fn sub(self, rhs: &AnyScalar) -> Self::Output {
1294        Sub::sub(&self, rhs)
1295    }
1296}
1297
1298impl Mul<&AnyScalar> for &AnyScalar {
1299    type Output = AnyScalar;
1300
1301    fn mul(self, rhs: &AnyScalar) -> Self::Output {
1302        AnyScalar::fallback_result(
1303            self.try_mul(rhs),
1304            "mul",
1305            || {
1306                AnyScalar::scalar_value_from_backend(
1307                    self.to_backend_scalar() * rhs.to_backend_scalar(),
1308                )
1309            },
1310            self.tracks_grad() || rhs.tracks_grad(),
1311        )
1312    }
1313}
1314
1315impl Mul<AnyScalar> for AnyScalar {
1316    type Output = AnyScalar;
1317
1318    fn mul(self, rhs: AnyScalar) -> Self::Output {
1319        Mul::mul(&self, &rhs)
1320    }
1321}
1322
1323impl Mul<AnyScalar> for &AnyScalar {
1324    type Output = AnyScalar;
1325
1326    fn mul(self, rhs: AnyScalar) -> Self::Output {
1327        Mul::mul(self, &rhs)
1328    }
1329}
1330
1331impl Mul<&AnyScalar> for AnyScalar {
1332    type Output = AnyScalar;
1333
1334    fn mul(self, rhs: &AnyScalar) -> Self::Output {
1335        Mul::mul(&self, rhs)
1336    }
1337}
1338
1339impl Div<&AnyScalar> for &AnyScalar {
1340    type Output = AnyScalar;
1341
1342    fn div(self, rhs: &AnyScalar) -> Self::Output {
1343        AnyScalar::fallback_result(
1344            self.try_div(rhs),
1345            "div",
1346            || {
1347                AnyScalar::scalar_value_from_backend(
1348                    self.to_backend_scalar() / rhs.to_backend_scalar(),
1349                )
1350            },
1351            self.tracks_grad() || rhs.tracks_grad(),
1352        )
1353    }
1354}
1355
1356impl Div<AnyScalar> for AnyScalar {
1357    type Output = AnyScalar;
1358
1359    fn div(self, rhs: AnyScalar) -> Self::Output {
1360        Div::div(&self, &rhs)
1361    }
1362}
1363
1364impl Div<AnyScalar> for &AnyScalar {
1365    type Output = AnyScalar;
1366
1367    fn div(self, rhs: AnyScalar) -> Self::Output {
1368        Div::div(self, &rhs)
1369    }
1370}
1371
1372impl Div<&AnyScalar> for AnyScalar {
1373    type Output = AnyScalar;
1374
1375    fn div(self, rhs: &AnyScalar) -> Self::Output {
1376        Div::div(&self, rhs)
1377    }
1378}
1379
1380impl Neg for &AnyScalar {
1381    type Output = AnyScalar;
1382
1383    fn neg(self) -> Self::Output {
1384        AnyScalar::fallback_result(
1385            self.try_neg(),
1386            "neg",
1387            || AnyScalar::scalar_value_from_backend(-self.to_backend_scalar()),
1388            self.tracks_grad(),
1389        )
1390    }
1391}
1392
1393impl Neg for AnyScalar {
1394    type Output = AnyScalar;
1395
1396    fn neg(self) -> Self::Output {
1397        Neg::neg(&self)
1398    }
1399}
1400
1401impl Mul<AnyScalar> for f64 {
1402    type Output = AnyScalar;
1403
1404    fn mul(self, rhs: AnyScalar) -> Self::Output {
1405        AnyScalar::from_real(self) * rhs
1406    }
1407}
1408
1409impl Mul<AnyScalar> for Complex64 {
1410    type Output = AnyScalar;
1411
1412    fn mul(self, rhs: AnyScalar) -> Self::Output {
1413        AnyScalar::from(self) * rhs
1414    }
1415}
1416
1417impl Div<AnyScalar> for Complex64 {
1418    type Output = AnyScalar;
1419
1420    fn div(self, rhs: AnyScalar) -> Self::Output {
1421        AnyScalar::from(self) / rhs
1422    }
1423}
1424
1425impl Default for AnyScalar {
1426    fn default() -> Self {
1427        Self::zero()
1428    }
1429}
1430
1431impl Zero for AnyScalar {
1432    fn zero() -> Self {
1433        Self::from_real(0.0)
1434    }
1435
1436    fn is_zero(&self) -> bool {
1437        AnyScalar::is_zero(self)
1438    }
1439}
1440
1441impl One for AnyScalar {
1442    fn one() -> Self {
1443        Self::from_real(1.0)
1444    }
1445}
1446
1447impl PartialEq for AnyScalar {
1448    fn eq(&self, other: &Self) -> bool {
1449        self.value() == other.value()
1450    }
1451}
1452
1453impl PartialOrd for AnyScalar {
1454    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1455        match (self.value(), other.value()) {
1456            (ScalarValue::F32(lhs), ScalarValue::F32(rhs)) => lhs.partial_cmp(&rhs),
1457            (ScalarValue::F32(lhs), ScalarValue::F64(rhs)) => (lhs as f64).partial_cmp(&rhs),
1458            (ScalarValue::F64(lhs), ScalarValue::F32(rhs)) => lhs.partial_cmp(&(rhs as f64)),
1459            (ScalarValue::F64(lhs), ScalarValue::F64(rhs)) => lhs.partial_cmp(&rhs),
1460            _ => None,
1461        }
1462    }
1463}
1464
1465impl fmt::Display for AnyScalar {
1466    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1467        match self.value() {
1468            ScalarValue::F32(value) => value.fmt(f),
1469            ScalarValue::F64(value) => value.fmt(f),
1470            ScalarValue::I32(value) => value.fmt(f),
1471            ScalarValue::I64(value) => value.fmt(f),
1472            ScalarValue::Bool(value) => value.fmt(f),
1473            ScalarValue::C32(value) => value.fmt(f),
1474            ScalarValue::C64(value) => value.fmt(f),
1475        }
1476    }
1477}
1478
1479impl fmt::Debug for AnyScalar {
1480    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1481        let dtype = match self.value {
1482            ScalarValue::F32(_) => "f32",
1483            ScalarValue::F64(_) => "f64",
1484            ScalarValue::I32(_) => "i32",
1485            ScalarValue::I64(_) => "i64",
1486            ScalarValue::Bool(_) => "bool",
1487            ScalarValue::C32(_) => "c32",
1488            ScalarValue::C64(_) => "c64",
1489        };
1490        f.debug_struct("AnyScalar")
1491            .field("dtype", &dtype)
1492            .field("value", &self.value())
1493            .field("tracks_grad", &self.tracks_grad())
1494            .finish()
1495    }
1496}
1497
1498mod reciprocal;
1499
1500#[cfg(test)]
1501mod tests;