conspire/math/tensor/rank_0/
mod.rs

1#[cfg(test)]
2mod test;
3
4#[cfg(test)]
5use super::test::ErrorTensor;
6
7pub mod list;
8pub mod list_2d;
9
10use super::{Hessian, Jacobian, Solution, SquareMatrix, Tensor, TensorArray, Vector};
11use std::ops::Sub;
12
13/// A tensor of rank 0 (a scalar).
14pub type TensorRank0 = f64;
15
16#[cfg(test)]
17impl ErrorTensor for TensorRank0 {
18    fn error_fd(&self, comparator: &Self, epsilon: TensorRank0) -> Option<(bool, usize)> {
19        if ((self / comparator - 1.0).abs() >= epsilon && (self - comparator).abs() >= epsilon)
20            || self.is_nan()
21            || comparator.is_nan()
22        {
23            Some((true, 1))
24        } else {
25            None
26        }
27    }
28}
29
30impl Solution for TensorRank0 {
31    fn decrement_from(&mut self, _other: &Vector) {
32        unimplemented!()
33    }
34    fn decrement_from_chained(&mut self, _other: &mut Vector, _vector: Vector) {
35        unimplemented!()
36    }
37}
38
39impl Jacobian for TensorRank0 {
40    fn fill_into(self, _vector: &mut Vector) {
41        unimplemented!()
42    }
43    fn fill_into_chained(self, _other: Vector, _vector: &mut Vector) {
44        unimplemented!()
45    }
46}
47
48impl Sub<Vector> for TensorRank0 {
49    type Output = Self;
50    fn sub(self, _vector: Vector) -> Self::Output {
51        unimplemented!()
52    }
53}
54
55impl Sub<&Vector> for TensorRank0 {
56    type Output = Self;
57    fn sub(self, _vector: &Vector) -> Self::Output {
58        unimplemented!()
59    }
60}
61
62impl Hessian for TensorRank0 {
63    fn fill_into(self, _square_matrix: &mut SquareMatrix) {
64        unimplemented!()
65    }
66}
67
68impl Tensor for TensorRank0 {
69    type Item = TensorRank0;
70    fn error_count(
71        &self,
72        other: &Self,
73        tol_abs: TensorRank0,
74        tol_rel: TensorRank0,
75    ) -> Option<usize> {
76        if (self.sub_abs(other) < tol_abs || self.sub_rel(other) < tol_rel)
77            && !self.is_nan()
78            && !other.is_nan()
79        {
80            None
81        } else {
82            Some(1)
83        }
84    }
85    fn full_contraction(&self, tensor_rank_0: &Self) -> TensorRank0 {
86        self * tensor_rank_0
87    }
88    fn is_zero(&self) -> bool {
89        self == &0.0
90    }
91    fn iter(&self) -> impl Iterator<Item = &Self::Item> {
92        [0.0].iter()
93    }
94    fn iter_mut(&mut self) -> impl Iterator<Item = &mut Self::Item> {
95        [self].into_iter()
96    }
97    fn len(&self) -> usize {
98        1
99    }
100    fn norm_inf(&self) -> TensorRank0 {
101        self.abs()
102    }
103    fn normalized(self) -> Self {
104        1.0
105    }
106    fn size(&self) -> usize {
107        1
108    }
109    fn sub_abs(&self, other: &Self) -> Self {
110        (self - other).abs()
111    }
112    fn sub_rel(&self, other: &Self) -> Self {
113        if other == &0.0 {
114            if self == &0.0 { 0.0 } else { 1.0 }
115        } else {
116            (self / other - 1.0).abs()
117        }
118    }
119}
120
121impl TensorArray for TensorRank0 {
122    type Array = Self;
123    type Item = TensorRank0;
124    fn as_array(&self) -> Self::Array {
125        *self
126    }
127    fn identity() -> Self {
128        1.0
129    }
130    fn zero() -> Self {
131        0.0
132    }
133}
134
135impl From<Vector> for TensorRank0 {
136    fn from(_vector: Vector) -> Self {
137        unimplemented!()
138    }
139}