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        panic!()
41    }
42}
43
44impl Jacobian for TensorRank0 {
45    fn fill_into(self, _vector: &mut Vector) {
46        panic!()
47    }
48    fn fill_into_chained(self, _other: Vector, _vector: &mut Vector) {
49        panic!()
50    }
51}
52
53impl Sub<Vector> for TensorRank0 {
54    type Output = Self;
55    fn sub(self, _vector: Vector) -> Self::Output {
56        panic!()
57    }
58}
59
60impl Hessian for TensorRank0 {
61    fn fill_into(self, _square_matrix: &mut SquareMatrix) {
62        panic!()
63    }
64    fn is_positive_definite(&self) -> bool {
65        self > &0.0
66    }
67}
68
69impl Tensor for TensorRank0 {
70    type Item = TensorRank0;
71    fn full_contraction(&self, tensor_rank_0: &Self) -> TensorRank0 {
72        self * tensor_rank_0
73    }
74    fn is_zero(&self) -> bool {
75        self == &0.0
76    }
77    fn iter(&self) -> impl Iterator<Item = &Self::Item> {
78        [0.0].iter()
79    }
80    fn iter_mut(&mut self) -> impl Iterator<Item = &mut Self::Item> {
81        [self].into_iter()
82    }
83    fn norm_inf(&self) -> TensorRank0 {
84        self.abs()
85    }
86    fn normalized(self) -> Self {
87        1.0
88    }
89}
90
91impl TensorArray for TensorRank0 {
92    type Array = [Self; 1];
93    type Item = TensorRank0;
94    fn as_array(&self) -> Self::Array {
95        [*self]
96    }
97    fn identity() -> Self {
98        1.0
99    }
100    fn new(array: Self::Array) -> Self {
101        array[0]
102    }
103    fn zero() -> Self {
104        0.0
105    }
106}
107
108impl From<TensorRank0> for Vector {
109    fn from(tensor_rank_0: TensorRank0) -> Self {
110        Vector::new(&[tensor_rank_0])
111    }
112}