conspire/math/tensor/rank_4/list/
mod.rs

1#[cfg(test)]
2mod test;
3
4use std::{
5    array::from_fn,
6    fmt::{Display, Formatter, Result},
7    ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Sub, SubAssign},
8};
9
10use super::{Tensor, TensorArray, TensorRank0, TensorRank4};
11
12/// A list of *d*-dimensional tensor of rank 4.
13///
14/// `D` is the dimension, `I`, `J`, `K`, `L` are the configurations, `W` is the list length.
15#[derive(Clone, Debug)]
16pub struct TensorRank4List<
17    const D: usize,
18    const I: usize,
19    const J: usize,
20    const K: usize,
21    const L: usize,
22    const W: usize,
23>([TensorRank4<D, I, J, K, L>; W]);
24
25impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
26    Display for TensorRank4List<D, I, J, K, L, W>
27{
28    fn fmt(&self, _f: &mut Formatter) -> Result {
29        Ok(())
30    }
31}
32
33impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
34    Tensor for TensorRank4List<D, I, J, K, L, W>
35{
36    type Item = TensorRank4<D, I, J, K, L>;
37    fn iter(&self) -> impl Iterator<Item = &Self::Item> {
38        self.0.iter()
39    }
40    fn iter_mut(&mut self) -> impl Iterator<Item = &mut Self::Item> {
41        self.0.iter_mut()
42    }
43}
44
45impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
46    TensorArray for TensorRank4List<D, I, J, K, L, W>
47{
48    type Array = [[[[[TensorRank0; D]; D]; D]; D]; W];
49    type Item = TensorRank4<D, I, J, K, L>;
50    fn as_array(&self) -> Self::Array {
51        let mut array = [[[[[0.0; D]; D]; D]; D]; W];
52        array
53            .iter_mut()
54            .zip(self.iter())
55            .for_each(|(entry_rank_4, tensor_rank_4)| *entry_rank_4 = tensor_rank_4.as_array());
56        array
57    }
58    fn identity() -> Self {
59        Self(from_fn(|_| Self::Item::identity()))
60    }
61    fn new(array: Self::Array) -> Self {
62        array.into_iter().map(Self::Item::new).collect()
63    }
64    fn zero() -> Self {
65        Self(from_fn(|_| Self::Item::zero()))
66    }
67}
68
69impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
70    FromIterator<TensorRank4<D, I, J, K, L>> for TensorRank4List<D, I, J, K, L, W>
71{
72    fn from_iter<Ii: IntoIterator<Item = TensorRank4<D, I, J, K, L>>>(into_iterator: Ii) -> Self {
73        let mut tensor_rank_4_list = Self::zero();
74        tensor_rank_4_list
75            .iter_mut()
76            .zip(into_iterator)
77            .for_each(|(tensor_rank_4_list_entry, entry)| *tensor_rank_4_list_entry = entry);
78        tensor_rank_4_list
79    }
80}
81
82impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
83    Index<usize> for TensorRank4List<D, I, J, K, L, W>
84{
85    type Output = TensorRank4<D, I, J, K, L>;
86    fn index(&self, index: usize) -> &Self::Output {
87        &self.0[index]
88    }
89}
90
91impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
92    IndexMut<usize> for TensorRank4List<D, I, J, K, L, W>
93{
94    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
95        &mut self.0[index]
96    }
97}
98
99impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
100    Add for TensorRank4List<D, I, J, K, L, W>
101{
102    type Output = Self;
103    fn add(mut self, tensor_rank_4: Self) -> Self::Output {
104        self += tensor_rank_4;
105        self
106    }
107}
108
109impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
110    Add<&Self> for TensorRank4List<D, I, J, K, L, W>
111{
112    type Output = Self;
113    fn add(mut self, tensor_rank_4_list: &Self) -> Self::Output {
114        self += tensor_rank_4_list;
115        self
116    }
117}
118
119impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
120    AddAssign for TensorRank4List<D, I, J, K, L, W>
121{
122    fn add_assign(&mut self, tensor_rank_4_list: Self) {
123        self.iter_mut()
124            .zip(tensor_rank_4_list.iter())
125            .for_each(|(self_entry, tensor_rank_4)| *self_entry += tensor_rank_4);
126    }
127}
128
129impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
130    AddAssign<&Self> for TensorRank4List<D, I, J, K, L, W>
131{
132    fn add_assign(&mut self, tensor_rank_4_list: &Self) {
133        self.iter_mut()
134            .zip(tensor_rank_4_list.iter())
135            .for_each(|(self_entry, tensor_rank_4)| *self_entry += tensor_rank_4);
136    }
137}
138
139impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
140    Div<TensorRank0> for TensorRank4List<D, I, J, K, L, W>
141{
142    type Output = Self;
143    fn div(mut self, tensor_rank_0: TensorRank0) -> Self::Output {
144        self /= &tensor_rank_0;
145        self
146    }
147}
148
149impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
150    Div<&TensorRank0> for TensorRank4List<D, I, J, K, L, W>
151{
152    type Output = Self;
153    fn div(mut self, tensor_rank_0: &TensorRank0) -> Self::Output {
154        self /= tensor_rank_0;
155        self
156    }
157}
158
159impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
160    DivAssign<TensorRank0> for TensorRank4List<D, I, J, K, L, W>
161{
162    fn div_assign(&mut self, tensor_rank_0: TensorRank0) {
163        self.iter_mut().for_each(|entry| *entry /= &tensor_rank_0);
164    }
165}
166
167impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
168    DivAssign<&TensorRank0> for TensorRank4List<D, I, J, K, L, W>
169{
170    fn div_assign(&mut self, tensor_rank_0: &TensorRank0) {
171        self.iter_mut().for_each(|entry| *entry /= tensor_rank_0);
172    }
173}
174
175impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
176    Mul<TensorRank0> for TensorRank4List<D, I, J, K, L, W>
177{
178    type Output = Self;
179    fn mul(mut self, tensor_rank_0: TensorRank0) -> Self::Output {
180        self *= &tensor_rank_0;
181        self
182    }
183}
184
185impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
186    Mul<&TensorRank0> for TensorRank4List<D, I, J, K, L, W>
187{
188    type Output = Self;
189    fn mul(mut self, tensor_rank_0: &TensorRank0) -> Self::Output {
190        self *= tensor_rank_0;
191        self
192    }
193}
194
195impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
196    MulAssign<TensorRank0> for TensorRank4List<D, I, J, K, L, W>
197{
198    fn mul_assign(&mut self, tensor_rank_0: TensorRank0) {
199        self.iter_mut().for_each(|entry| *entry *= &tensor_rank_0);
200    }
201}
202
203impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
204    MulAssign<&TensorRank0> for TensorRank4List<D, I, J, K, L, W>
205{
206    fn mul_assign(&mut self, tensor_rank_0: &TensorRank0) {
207        self.iter_mut().for_each(|entry| *entry *= tensor_rank_0);
208    }
209}
210
211impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
212    Sub for TensorRank4List<D, I, J, K, L, W>
213{
214    type Output = Self;
215    fn sub(mut self, tensor_rank_4_list: Self) -> Self::Output {
216        self -= tensor_rank_4_list;
217        self
218    }
219}
220
221impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
222    Sub<&Self> for TensorRank4List<D, I, J, K, L, W>
223{
224    type Output = Self;
225    fn sub(mut self, tensor_rank_4_list: &Self) -> Self::Output {
226        self -= tensor_rank_4_list;
227        self
228    }
229}
230
231impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
232    SubAssign for TensorRank4List<D, I, J, K, L, W>
233{
234    fn sub_assign(&mut self, tensor_rank_4_list: Self) {
235        self.iter_mut()
236            .zip(tensor_rank_4_list.iter())
237            .for_each(|(self_entry, tensor_rank_4)| *self_entry -= tensor_rank_4);
238    }
239}
240
241impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize, const W: usize>
242    SubAssign<&Self> for TensorRank4List<D, I, J, K, L, W>
243{
244    fn sub_assign(&mut self, tensor_rank_4_list: &Self) {
245        self.iter_mut()
246            .zip(tensor_rank_4_list.iter())
247            .for_each(|(self_entry, tensor_rank_4)| *self_entry -= tensor_rank_4);
248    }
249}