conspire/math/tensor/rank_0/
mod.rs

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