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(
18        &self,
19        comparator: &Self,
20        tol_abs: &TensorRank0,
21        tol_rel: &TensorRank0,
22    ) -> Option<usize> {
23        if &(self - comparator).abs() >= tol_abs && &(self / comparator - 1.0).abs() >= tol_rel {
24            Some(1)
25        } else {
26            None
27        }
28    }
29    fn error_fd(&self, comparator: &Self, epsilon: &TensorRank0) -> Option<(bool, usize)> {
30        if &(self / comparator - 1.0).abs() >= epsilon {
31            Some((true, 1))
32        } else {
33            None
34        }
35    }
36}
37
38impl Solution for TensorRank0 {
39    fn decrement_from_chained(&mut self, _other: &mut Vector, _vector: Vector) {
40        unimplemented!()
41    }
42}
43
44impl Jacobian for TensorRank0 {
45    fn fill_into(self, _vector: &mut Vector) {
46        unimplemented!()
47    }
48    fn fill_into_chained(self, _other: Vector, _vector: &mut Vector) {
49        unimplemented!()
50    }
51}
52
53impl Sub<Vector> for TensorRank0 {
54    type Output = Self;
55    fn sub(self, _vector: Vector) -> Self::Output {
56        unimplemented!()
57    }
58}
59
60impl Sub<&Vector> for TensorRank0 {
61    type Output = Self;
62    fn sub(self, _vector: &Vector) -> Self::Output {
63        unimplemented!()
64    }
65}
66
67impl Hessian for TensorRank0 {
68    fn fill_into(self, _square_matrix: &mut SquareMatrix) {
69        unimplemented!()
70    }
71}
72
73impl Tensor for TensorRank0 {
74    type Item = TensorRank0;
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}
94
95impl TensorArray for TensorRank0 {
96    type Array = [Self; 1];
97    type Item = TensorRank0;
98    fn as_array(&self) -> Self::Array {
99        [*self]
100    }
101    fn identity() -> Self {
102        1.0
103    }
104    fn new(array: Self::Array) -> Self {
105        array[0]
106    }
107    fn zero() -> Self {
108        0.0
109    }
110}
111
112impl From<TensorRank0> for Vector {
113    fn from(tensor_rank_0: TensorRank0) -> Self {
114        Vector::new(&[tensor_rank_0])
115    }
116}