conspire/math/tensor/
mod.rs1pub mod test;
2
3pub mod rank_0;
4pub mod rank_1;
5pub mod rank_2;
6pub mod rank_3;
7pub mod rank_4;
8
9use super::{SquareMatrix, Vector};
10use crate::defeat_message;
11use rank_0::TensorRank0;
12use std::{
13 fmt::{self, Debug, Display, Formatter},
14 ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Sub, SubAssign},
15};
16
17pub type Scalar = TensorRank0;
19
20#[derive(PartialEq)]
22pub enum TensorError {
23 NotPositiveDefinite,
24}
25
26impl Debug for TensorError {
27 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
28 let error = match self {
29 Self::NotPositiveDefinite => "\x1b[1;91mResult is not positive definite.".to_string(),
30 };
31 write!(f, "\n{error}\n\x1b[0;2;31m{}\x1b[0m\n", defeat_message())
32 }
33}
34
35impl Display for TensorError {
36 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
37 let error = match self {
38 Self::NotPositiveDefinite => "\x1b[1;91mResult is not positive definite.".to_string(),
39 };
40 write!(f, "{error}\x1b[0m")
41 }
42}
43
44pub trait Solution
46where
47 Self: Tensor,
48{
49 fn decrement_from_chained(&mut self, other: &mut Vector, vector: Vector);
51}
52
53pub trait Jacobian
55where
56 Self: Tensor + Sub<Vector, Output = Self> + for<'a> Sub<&'a Vector, Output = Self>,
57{
58 fn fill_into(self, vector: &mut Vector);
60 fn fill_into_chained(self, other: Vector, vector: &mut Vector);
62}
63
64pub trait Hessian
66where
67 Self: Tensor,
68{
69 fn fill_into(self, square_matrix: &mut SquareMatrix);
71}
72
73pub trait Rank2
75where
76 Self: Sized,
77{
78 type Transpose;
80 fn deviatoric(&self) -> Self;
82 fn deviatoric_and_trace(&self) -> (Self, TensorRank0);
84 fn is_diagonal(&self) -> bool;
86 fn is_identity(&self) -> bool;
88 fn is_symmetric(&self) -> bool;
90 fn second_invariant(&self) -> TensorRank0 {
92 0.5 * (self.trace().powi(2) - self.squared_trace())
93 }
94 fn squared_trace(&self) -> TensorRank0;
96 fn trace(&self) -> TensorRank0;
98 fn transpose(&self) -> Self::Transpose;
100}
101
102pub trait Tensor
104where
105 for<'a> Self: Sized
106 + Debug
107 + Display
108 + Add<Self, Output = Self>
109 + Add<&'a Self, Output = Self>
110 + AddAssign
111 + AddAssign<&'a Self>
112 + Clone
113 + Div<TensorRank0, Output = Self>
114 + DivAssign<TensorRank0>
115 + Mul<TensorRank0, Output = Self>
116 + MulAssign<TensorRank0>
117 + Sub<Self, Output = Self>
118 + Sub<&'a Self, Output = Self>
119 + SubAssign
120 + SubAssign<&'a Self>,
121 Self::Item: Tensor,
122{
123 type Item;
125 fn error_count(&self, other: &Self, tol_abs: &Scalar, tol_rel: &Scalar) -> Option<usize> {
127 let error_count = self
128 .iter()
129 .zip(other.iter())
130 .filter_map(|(self_entry, other_entry)| {
131 self_entry.error_count(other_entry, tol_abs, tol_rel)
132 })
133 .sum();
134 if error_count > 0 {
135 Some(error_count)
136 } else {
137 None
138 }
139 }
140 fn full_contraction(&self, tensor: &Self) -> TensorRank0 {
142 self.iter()
143 .zip(tensor.iter())
144 .map(|(self_entry, tensor_entry)| self_entry.full_contraction(tensor_entry))
145 .sum()
146 }
147 fn is_zero(&self) -> bool {
149 self.iter().filter(|entry| !entry.is_zero()).count() == 0
150 }
151 fn iter(&self) -> impl Iterator<Item = &Self::Item>;
155 fn iter_mut(&mut self) -> impl Iterator<Item = &mut Self::Item>;
159 fn norm(&self) -> TensorRank0 {
161 self.norm_squared().sqrt()
162 }
163 fn norm_inf(&self) -> TensorRank0 {
165 unimplemented!()
166 }
167 fn norm_squared(&self) -> TensorRank0 {
169 self.full_contraction(self)
170 }
171 fn normalize(&mut self) {
173 *self /= self.norm()
174 }
175 fn normalized(self) -> Self {
177 let norm = self.norm();
178 self / norm
179 }
180 fn num_entries(&self) -> usize {
182 unimplemented!()
183 }
184 fn sub_abs(&self, other: &Self) -> Self {
186 let mut difference = self.clone();
187 difference
188 .iter_mut()
189 .zip(self.iter().zip(other.iter()))
190 .for_each(|(entry, (self_entry, other_entry))| {
191 *entry = self_entry.sub_abs(other_entry)
192 });
193 difference
194 }
195 fn sub_rel(&self, other: &Self) -> Self {
197 let mut difference = self.clone();
198 difference
199 .iter_mut()
200 .zip(self.iter().zip(other.iter()))
201 .for_each(|(entry, (self_entry, other_entry))| {
202 *entry = self_entry.sub_rel(other_entry)
203 });
204 difference
205 }
206}
207
208pub trait TensorArray {
210 type Array;
212 type Item;
214 fn as_array(&self) -> Self::Array;
216 fn identity() -> Self;
218 fn new(array: Self::Array) -> Self;
220 fn zero() -> Self;
222}
223
224pub trait TensorVec
226where
227 Self: FromIterator<Self::Item> + Index<usize, Output = Self::Item> + IndexMut<usize>,
228{
229 type Item;
231 type Slice<'a>;
233 fn append(&mut self, other: &mut Self);
235 fn is_empty(&self) -> bool;
237 fn len(&self) -> usize;
239 fn new(slice: Self::Slice<'_>) -> Self;
241 fn push(&mut self, item: Self::Item);
243 fn remove(&mut self, _index: usize) -> Self::Item;
245 fn retain<F>(&mut self, f: F)
247 where
248 F: FnMut(&Self::Item) -> bool;
249 fn swap_remove(&mut self, _index: usize) -> Self::Item;
251 fn zero(len: usize) -> Self;
253}