conspire/math/tensor/vec/
mod.rs1use crate::math::{
2 ContractWith, Differentiable, Erase, Quantity, Tensor, TensorRank0, TensorRank1,
3 TensorRank1List, TensorVec,
4};
5use crate::units::Dimensionless;
6use std::{
7 collections::VecDeque,
8 fmt::{Display, Formatter, Result},
9 iter::Sum,
10 ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Sub, SubAssign},
11 slice, vec,
12};
13
14#[derive(Clone, Debug, PartialEq)]
16#[repr(transparent)]
17pub struct TensorVector<T>(Vec<T>);
18
19impl<T> Erase for TensorVector<T>
20where
21 T: Erase + Tensor,
22{
23 type Erased = TensorVector<<T as Erase>::Erased>;
24 fn erase(&self) -> &Self::Erased {
25 unsafe { &*(self as *const Self as *const Self::Erased) }
26 }
27}
28pub type TensorRank1RefVec<'a, const D: usize, I, U = Dimensionless> =
35 TensorVector<&'a TensorRank1<D, I, U>>;
36
37impl<'a, const D: usize, I, U> TensorRank1RefVec<'a, D, I, U> {
38 pub fn bounding_box(&self) -> TensorRank1List<D, I, 2, U> {
39 self.iter()
40 .skip(1)
41 .fold(
42 [self[0].clone(), self[0].clone()],
43 |[mut min, mut max], entry| {
44 entry
45 .iter()
46 .zip(min.iter_mut().zip(max.iter_mut()))
47 .for_each(|(&entry_i, (min_i, max_i))| {
48 *min_i = min_i.min(entry_i);
49 *max_i = max_i.max(entry_i);
50 });
51 [min, max]
52 },
53 )
54 .into()
55 }
56 pub fn iter(&self) -> impl Iterator<Item = &&TensorRank1<D, I, U>> {
57 self.0.iter()
58 }
59 pub fn is_empty(&self) -> bool {
60 self.0.is_empty()
61 }
62 pub fn len(&self) -> usize {
63 self.0.len()
64 }
65}
66
67impl<'a, const D: usize, I, U> Index<usize> for TensorRank1RefVec<'a, D, I, U> {
68 type Output = TensorRank1<D, I, U>;
69 fn index(&self, index: usize) -> &Self::Output {
70 self.0[index]
71 }
72}
73
74impl<T> TensorVector<T>
77where
78 T: Tensor,
79{
80 pub const fn as_ptr(&self) -> *const T {
82 self.0.as_ptr()
83 }
84 pub fn as_mut_slice(&mut self) -> &mut [T] {
85 self.0.as_mut_slice()
86 }
87 pub fn as_slice(&self) -> &[T] {
88 self.0.as_slice()
89 }
90}
91
92impl<T> Default for TensorVector<T>
93where
94 T: Tensor,
95{
96 fn default() -> Self {
97 Self(Vec::new())
98 }
99}
100
101impl<T, const N: usize> From<[T; N]> for TensorVector<T>
102where
103 T: Tensor,
104{
105 fn from(array: [T; N]) -> Self {
106 Self(array.to_vec())
107 }
108}
109
110impl<T> From<&[T]> for TensorVector<T>
111where
112 T: Tensor,
113{
114 fn from(slice: &[T]) -> Self {
115 Self(slice.to_vec())
116 }
117}
118
119impl<T> From<Vec<T>> for TensorVector<T>
120where
121 T: Tensor,
122{
123 fn from(vec: Vec<T>) -> Self {
124 Self(vec)
125 }
126}
127
128impl<T> From<TensorVector<T>> for Vec<T>
129where
130 T: Tensor,
131{
132 fn from(tensor_vector: TensorVector<T>) -> Self {
133 tensor_vector.0
134 }
135}
136
137impl<T> From<VecDeque<T>> for TensorVector<T>
138where
139 T: Tensor,
140{
141 fn from(vec_deque: VecDeque<T>) -> Self {
142 Self(vec_deque.into())
143 }
144}
145
146impl<T> From<TensorVector<T>> for VecDeque<T>
147where
148 T: Tensor,
149{
150 fn from(tensor_vector: TensorVector<T>) -> Self {
151 tensor_vector.0.into()
152 }
153}
154
155impl<T> Display for TensorVector<T>
156where
157 T: Tensor,
158{
159 fn fmt(&self, f: &mut Formatter) -> Result {
160 write!(f, "Need to implement Display")
161 }
176}
177
178impl<T> Extend<T> for TensorVector<T>
179where
180 T: Tensor,
181{
182 fn extend<I>(&mut self, iter: I)
183 where
184 I: IntoIterator<Item = T>,
185 {
186 self.0.extend(iter)
187 }
188}
189
190impl<T> Index<usize> for TensorVector<T>
191where
192 T: Tensor,
193{
194 type Output = T;
195 fn index(&self, index: usize) -> &Self::Output {
196 &self.0[index]
197 }
198}
199
200impl<T> IndexMut<usize> for TensorVector<T>
201where
202 T: Tensor,
203{
204 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
205 &mut self.0[index]
206 }
207}
208
209impl<T> Tensor for TensorVector<T>
210where
211 T: Tensor,
212{
213 type Item = T;
214 type Unit = <T as Tensor>::Unit;
215 fn iter(&self) -> impl Iterator<Item = &Self::Item> {
216 self.0.iter()
217 }
218 fn iter_mut(&mut self) -> impl Iterator<Item = &mut Self::Item> {
219 self.0.iter_mut()
220 }
221 fn len(&self) -> usize {
222 self.0.len()
223 }
224 fn size(&self) -> usize {
225 self.len() * self[0].size() }
227}
228
229impl<T> FromIterator<T> for TensorVector<T>
230{
233 fn from_iter<Ii: IntoIterator<Item = T>>(into_iterator: Ii) -> Self {
234 Self(Vec::from_iter(into_iterator))
235 }
236}
237
238impl<T> IntoIterator for TensorVector<T>
239where
240 T: Tensor,
241{
242 type Item = T;
243 type IntoIter = vec::IntoIter<Self::Item>;
244 fn into_iter(self) -> Self::IntoIter {
245 self.0.into_iter()
246 }
247}
248
249impl<'a, T> IntoIterator for &'a TensorVector<T>
250where
251 T: Tensor,
252{
253 type Item = &'a T;
254 type IntoIter = slice::Iter<'a, T>;
255 fn into_iter(self) -> Self::IntoIter {
256 self.0.iter()
257 }
258}
259
260impl<T> Sum for TensorVector<T>
261where
262 T: Tensor,
263{
264 fn sum<Ii>(iter: Ii) -> Self
265 where
266 Ii: Iterator<Item = Self>,
267 {
268 iter.reduce(|mut acc, item| {
269 acc += item;
270 acc
271 })
272 .unwrap_or_else(Self::default)
273 }
274}
275
276impl<T> TensorVec for TensorVector<T>
277where
278 T: Tensor,
279{
280 type Item = T;
281 fn append(&mut self, other: &mut Self) {
282 self.0.append(&mut other.0)
283 }
284 fn capacity(&self) -> usize {
285 self.0.capacity()
286 }
287 fn is_empty(&self) -> bool {
288 self.0.is_empty()
289 }
290 fn new() -> Self {
291 Self(Vec::new())
292 }
293 fn push(&mut self, item: Self::Item) {
294 self.0.push(item)
295 }
296 fn remove(&mut self, index: usize) -> Self::Item {
297 self.0.remove(index)
298 }
299 fn reserve(&mut self, additional: usize) {
300 self.0.reserve(additional)
301 }
302 fn retain<F>(&mut self, f: F)
303 where
304 F: FnMut(&Self::Item) -> bool,
305 {
306 self.0.retain(f)
307 }
308 fn swap_remove(&mut self, index: usize) -> Self::Item {
309 self.0.swap_remove(index)
310 }
311 fn with_capacity(capacity: usize) -> Self {
312 Self(Vec::with_capacity(capacity))
313 }
314}
315
316impl<T, V> Div<Quantity<V>> for TensorVector<T>
317where
318 T: Div<Quantity<V>> + Tensor,
319 <T as Div<Quantity<V>>>::Output: Tensor,
320{
321 type Output = TensorVector<<T as Div<Quantity<V>>>::Output>;
322 fn div(self, quantity: Quantity<V>) -> Self::Output {
323 self.into_iter().map(|entry| entry / quantity).collect()
324 }
325}
326
327impl<T, V> Div<Quantity<V>> for &TensorVector<T>
328where
329 T: Clone + Div<Quantity<V>> + Tensor,
330 <T as Div<Quantity<V>>>::Output: Tensor,
331{
332 type Output = TensorVector<<T as Div<Quantity<V>>>::Output>;
333 fn div(self, quantity: Quantity<V>) -> Self::Output {
334 self.iter().map(|entry| entry.clone() / quantity).collect()
335 }
336}
337
338impl<T> Div<TensorRank0> for TensorVector<T>
339where
340 T: Tensor,
341{
342 type Output = Self;
343 fn div(mut self, tensor_rank_0: TensorRank0) -> Self::Output {
344 self /= tensor_rank_0;
345 self
346 }
347}
348
349impl<T> Div<&TensorRank0> for TensorVector<T>
350where
351 T: Tensor,
352{
353 type Output = Self;
354 fn div(mut self, tensor_rank_0: &TensorRank0) -> Self::Output {
355 self /= tensor_rank_0;
356 self
357 }
358}
359
360impl<T> DivAssign<TensorRank0> for TensorVector<T>
361where
362 T: Tensor,
363{
364 fn div_assign(&mut self, tensor_rank_0: TensorRank0) {
365 self.iter_mut().for_each(|entry| *entry /= &tensor_rank_0);
366 }
367}
368
369impl<T> DivAssign<&TensorRank0> for TensorVector<T>
370where
371 T: Tensor,
372{
373 fn div_assign(&mut self, tensor_rank_0: &TensorRank0) {
374 self.iter_mut().for_each(|entry| *entry /= tensor_rank_0);
375 }
376}
377
378impl<T, V> Mul<Quantity<V>> for TensorVector<T>
381where
382 T: Mul<Quantity<V>> + Tensor,
383 <T as Mul<Quantity<V>>>::Output: Tensor,
384{
385 type Output = TensorVector<<T as Mul<Quantity<V>>>::Output>;
386 fn mul(self, quantity: Quantity<V>) -> Self::Output {
387 self.into_iter().map(|entry| entry * quantity).collect()
388 }
389}
390
391impl<T, V> Mul<Quantity<V>> for &TensorVector<T>
392where
393 T: Mul<Quantity<V>> + Tensor,
394 <T as Mul<Quantity<V>>>::Output: Tensor,
395{
396 type Output = TensorVector<<T as Mul<Quantity<V>>>::Output>;
397 fn mul(self, quantity: Quantity<V>) -> Self::Output {
398 self.iter().map(|entry| entry.clone() * quantity).collect()
399 }
400}
401
402impl<T> Mul<TensorRank0> for TensorVector<T>
403where
404 T: Tensor,
405{
406 type Output = Self;
407 fn mul(mut self, tensor_rank_0: TensorRank0) -> Self::Output {
408 self *= tensor_rank_0;
409 self
410 }
411}
412
413impl<T> Mul<&TensorRank0> for TensorVector<T>
414where
415 T: Tensor,
416{
417 type Output = Self;
418 fn mul(mut self, tensor_rank_0: &TensorRank0) -> Self::Output {
419 self *= tensor_rank_0;
420 self
421 }
422}
423
424impl<T> Mul<TensorRank0> for &TensorVector<T>
425where
426 T: Tensor,
427 for<'a> &'a T: Mul<&'a TensorRank0, Output = T>,
428{
429 type Output = TensorVector<T>;
430 fn mul(self, tensor_rank_0: TensorRank0) -> Self::Output {
431 self.iter().map(|self_i| self_i * &tensor_rank_0).collect()
432 }
433}
434
435impl<T> MulAssign<TensorRank0> for TensorVector<T>
447where
448 T: Tensor,
449{
450 fn mul_assign(&mut self, tensor_rank_0: TensorRank0) {
451 self.iter_mut().for_each(|entry| *entry *= &tensor_rank_0);
452 }
453}
454
455impl<T> MulAssign<&TensorRank0> for TensorVector<T>
456where
457 T: Tensor,
458{
459 fn mul_assign(&mut self, tensor_rank_0: &TensorRank0) {
460 self.iter_mut().for_each(|entry| *entry *= tensor_rank_0);
461 }
462}
463
464impl<T> Add for TensorVector<T>
465where
466 T: Tensor,
467{
468 type Output = Self;
469 fn add(mut self, tensor_vec: Self) -> Self::Output {
470 self += tensor_vec;
471 self
472 }
473}
474
475impl<T> Add<&Self> for TensorVector<T>
476where
477 T: Tensor,
478{
479 type Output = Self;
480 fn add(mut self, tensor_vec: &Self) -> Self::Output {
481 self += tensor_vec;
482 self
483 }
484}
485
486impl<T> AddAssign for TensorVector<T>
487where
488 T: Tensor,
489{
490 fn add_assign(&mut self, tensor_vec: Self) {
491 self.iter_mut()
492 .zip(tensor_vec)
493 .for_each(|(self_entry, entry)| *self_entry += entry);
494 }
495}
496
497impl<T> AddAssign<&Self> for TensorVector<T>
498where
499 T: Tensor,
500{
501 fn add_assign(&mut self, tensor_vec: &Self) {
502 self.iter_mut()
503 .zip(tensor_vec.iter())
504 .for_each(|(self_entry, entry)| *self_entry += entry);
505 }
506}
507
508impl<T> Sub for TensorVector<T>
509where
510 T: Tensor,
511{
512 type Output = Self;
513 fn sub(mut self, tensor_vec: Self) -> Self::Output {
514 self -= tensor_vec;
515 self
516 }
517}
518
519impl<T> Sub<&Self> for TensorVector<T>
520where
521 T: Tensor,
522{
523 type Output = Self;
524 fn sub(mut self, tensor_vec: &Self) -> Self::Output {
525 self -= tensor_vec;
526 self
527 }
528}
529
530impl<T> Sub for &TensorVector<T>
531where
532 T: Tensor,
533{
534 type Output = TensorVector<T>;
535 fn sub(self, tensor_vec: Self) -> Self::Output {
536 self.clone() - tensor_vec
537 }
538}
539
540impl<T> SubAssign for TensorVector<T>
541where
542 T: Tensor,
543{
544 fn sub_assign(&mut self, tensor_vec: Self) {
545 self.iter_mut()
546 .zip(tensor_vec)
547 .for_each(|(self_entry, entry)| *self_entry -= entry);
548 }
549}
550
551impl<T> SubAssign<&Self> for TensorVector<T>
552where
553 T: Tensor,
554{
555 fn sub_assign(&mut self, tensor_vec: &Self) {
556 self.iter_mut()
557 .zip(tensor_vec.iter())
558 .for_each(|(self_entry, entry)| *self_entry -= entry);
559 }
560}
561
562impl<T, V> ContractWith<TensorVector<V>> for TensorVector<T>
563where
564 T: ContractWith<V> + Tensor,
565 V: Tensor,
566 <T as ContractWith<V>>::Output: Sum,
567{
568 type Output = <T as ContractWith<V>>::Output;
569 fn contract_with(&self, tensor_vector: &TensorVector<V>) -> Self::Output {
570 self.iter()
571 .zip(tensor_vector.iter())
572 .map(|(entry, other)| entry.contract_with(other))
573 .sum()
574 }
575}
576
577impl<E, T> Differentiable<T> for TensorVector<E>
578where
579 E: Differentiable<T> + Tensor,
580 <E as Differentiable<T>>::Derivative: Tensor,
581{
582 type Derivative = TensorVector<<E as Differentiable<T>>::Derivative>;
583}