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#[derive(Debug, thiserror::Error)]
76#[error("AnyScalar eager-tensor operation failed: {source}")]
77pub struct AnyScalarError {
78 #[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#[derive(Clone)]
118pub struct AnyScalar {
119 tensor: std::result::Result<IdxTensor, AnyScalarTensorError>,
120 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 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 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 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 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 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 pub fn from_real(x: f64) -> Self {
381 Self::from_value(x)
382 }
383
384 pub fn from_complex(re: f64, im: f64) -> Self {
407 Self::from_value(Complex64::new(re, im))
408 }
409
410 pub fn new_real(x: f64) -> Self {
432 Self::from_real(x)
433 }
434
435 pub fn new_complex(re: f64, im: f64) -> Self {
458 Self::from_complex(re, im)
459 }
460
461 pub fn primal(&self) -> std::result::Result<Self, AnyScalarError> {
484 self.detach()
485 }
486
487 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 pub fn tracks_grad(&self) -> bool {
529 self.tracks_grad || self.tensor.as_ref().is_ok_and(IdxTensor::tracks_grad)
530 }
531
532 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 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 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 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 pub fn real(&self) -> f64 {
677 self.value().real()
678 }
679
680 pub fn imag(&self) -> f64 {
695 self.value().imag()
696 }
697
698 pub fn abs(&self) -> f64 {
714 self.value().abs()
715 }
716
717 pub fn is_complex(&self) -> bool {
732 self.value().is_complex()
733 }
734
735 pub fn is_real(&self) -> bool {
750 !self.is_complex()
751 }
752
753 pub fn is_zero(&self) -> bool {
768 self.value().is_zero()
769 }
770
771 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 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 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 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 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 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 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 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 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 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 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;