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