rest_tensors/
tensor_basic_operation.rs

1use std::{fmt::Display, ops::{Index, IndexMut, Range, RangeFull}, slice::SliceIndex};
2
3use crate::{index::{TensorIndex, TensorIndexUncheck}, 
4            ERIFull, ERIFold4, MatrixFull, MatrixFullSliceMut, 
5            MatrixFullSlice, MatrixUpperSliceMut, MatrixUpper, 
6            MatrixUpperSlice, RIFull};
7
8
9/// Trait definitions for tensor basic operations, mainly including
10///    getting a (mutable) number, or a (mutable) slice from a defined tensor
11pub trait TensorOpt<T> where Self: TensorIndex {
12    fn get1d(&self, position:usize) -> Option<&T> {None}
13    fn get2d(&self, position:[usize;2]) -> Option<&T> {None}
14    fn get3d(&self, position:[usize;3]) -> Option<&T> {None}
15    fn get4d(&self, position:[usize;4]) -> Option<&T> {None}
16    fn get(&self, position:&[usize]) -> Option<&T> {None}
17}
18
19pub trait TensorOptUncheck<T> where Self: TensorIndexUncheck {
20    // For MatrixUpper
21    fn get2d_uncheck(&self, position:[usize;2]) -> Option<&T> {None}
22    // For ERIFold4
23    fn get4d_uncheck(&self, position:[usize;4]) -> Option<&T> {None}
24}
25
26pub trait TensorOptMut<'a, T> where Self: TensorIndex {
27    fn get1d_mut(&mut self, position:usize) -> Option<&mut T> {None}
28    fn get2d_mut(&mut self, position:[usize;2]) -> Option<&mut T> {None}
29    fn get3d_mut(&mut self, position:[usize;3]) -> Option<&mut T> {None}
30    fn get4d_mut(&mut self, position:[usize;4]) -> Option<&mut T> {None}
31    fn get_mut(&mut self, position:&[usize]) -> Option<&mut T> {None}
32
33    fn set1d(&mut self, position:usize, new_data: T) {}
34    fn set2d(&mut self, position:[usize;2], new_data: T) {}
35    fn set3d(&mut self, position:[usize;3], new_data: T) {}
36    fn set4d(&mut self, position:[usize;4], new_data: T) {}
37    fn set(&mut self, position:&[usize], new_data: T) {}
38}
39
40pub trait TensorOptMutUncheck<'a, T> where Self: TensorIndexUncheck {
41    // For MatrixUpperMut
42    fn get2d_mut_uncheck(&mut self, position:[usize;2]) -> Option<&mut T> {None}
43    // For ERIFold4
44    fn get4d_mut_uncheck(&mut self, position:[usize;4]) -> Option<&mut T> {None}
45    // For MatrixUpperMut
46    fn set2d_uncheck(&mut self, position:[usize;2], new_data: T) {}
47    // For ERIFold4
48    fn set4d_uncheck(&mut self, position:[usize;4], new_data: T) {}
49}
50
51pub trait TensorSlice<T> where Self: TensorIndex+TensorOpt<T> {
52    fn get1d_slice(&self, position:usize, length: usize) -> Option<&[T]> {None}
53    fn get2d_slice(&self, position:[usize;2], length: usize) -> Option<&[T]> {None}
54    fn get3d_slice(&self, position:[usize;3], length: usize) -> Option<&[T]> {None}
55    fn get4d_slice(&self, position:[usize;4], length: usize) -> Option<&[T]> {None}
56    fn get_slice(&self, position:&[usize], length: usize) -> Option<&[T]> {None}
57}
58
59pub trait TensorSliceUncheck<T> where Self: TensorIndexUncheck+TensorOptUncheck<T> {
60    // For MatrixUpper
61    fn get2d_slice_uncheck(&self, position:[usize;2],length: usize) -> Option<&[T]> {None}
62    // For ERIFold4
63    fn get4d_slice_uncheck(&self, position:[usize;4],length: usize) -> Option<&[T]> {None}
64}
65pub trait TensorSliceMut<'a, T> where Self: TensorIndex+TensorOptMut<'a,T> {
66    fn get1d_slice_mut(&mut self, position:usize, length: usize) -> Option<&mut [T]> {None}
67    fn get2d_slice_mut(&mut self, position:[usize;2], length: usize) -> Option<&mut [T]> {None}
68    fn get3d_slice_mut(&mut self, position:[usize;3], length: usize) -> Option<&mut [T]> {None}
69    fn get4d_slice_mut(&mut self, position:[usize;4], length: usize) -> Option<&mut [T]> {None}
70    fn get_slice_mut(&mut self, position:&[usize], length: usize) -> Option<&mut [T]> {None}
71}
72
73/// Define for upper-formated tensors specifically, like 
74///  ERIFold4 with the indice of `[i,j,k,l]`, and
75///  MatrixUpper with the indice of `[i,j]`.
76/// According to the upper-formated tensor definition, in principle, i=<j and k<=l.
77/// WARNING: For efficiency, "uncheck" here means that we don't check the index order during the tensor operations.
78/// WARNING: As a result, a wrong index order could lead to a wrong operation.
79pub trait TensorSliceMutUncheck<'a, T> where Self: TensorIndexUncheck+TensorOptMutUncheck<'a,T> {
80    // For MatrixUpperMut
81    fn get2d_slice_mut_uncheck(&mut self, position:[usize;2], length: usize) -> Option<&mut [T]> {None}
82    // For ERIFold4
83    fn get4d_slice_mut_uncheck(&mut self, position:[usize;4], length: usize) -> Option<&mut [T]> {None}
84}
85
86
87/// Implementation of the traits for specific tensor structures
88impl<T> TensorOpt<T> for ERIFull<T> {
89    #[inline]
90    fn get1d(&self, position:usize) -> Option<&T> {
91        self.data.get(position)
92    }
93    #[inline]
94    fn get4d(&self, position:[usize;4]) -> Option<&T> {
95        self.data.get(self.index4d(position).unwrap())
96    }
97    #[inline]
98    fn get(&self, position:&[usize]) -> Option<&T> {
99        let tp = [position[0],position[1],position[2],position[3]];
100        self.data.get(self.index4d(tp).unwrap())
101    }
102}
103impl<'a, T> TensorOptMut<'a, T> for ERIFull<T> {
104    #[inline]
105    fn get1d_mut(&mut self, position:usize) -> Option<&mut T> {
106        self.data.get_mut(position)
107    }
108    #[inline]
109    fn get4d_mut(&mut self, position:[usize;4]) -> Option<&mut T> {
110        let tp = self.index4d(position).unwrap();
111        self.data.get_mut(tp)
112    }
113    #[inline]
114    fn get_mut(&mut self, position:&[usize]) -> Option<&mut T> {
115        let tp = self.index4d([position[0],position[1],position[2],position[3]]).unwrap();
116        self.data.get_mut(tp)
117    }
118    #[inline]
119    fn set1d(&mut self, position:usize, new_data: T) {
120        if let Some(tmp_value) = self.data.get_mut(position) {
121            *tmp_value = new_data
122        } else {
123          panic!("Error in setting the tensor element located at the position of {:?}", position);
124        };
125    }
126    #[inline]
127    fn set4d(&mut self, position:[usize;4], new_data: T) {
128        let tp = self.index4d(position).unwrap();
129        if let Some(tmp_value) = self.data.get_mut(tp) {
130            *tmp_value = new_data
131        } else {
132          panic!("Error in setting the tensor element located at the position of {:?}", position);
133        };
134    }
135    #[inline]
136    fn set(&mut self, position:&[usize], new_data: T) {
137        //let tp = self.index4d([position[0],position[1],position[2],position[3]]);
138        self.set4d([position[0],position[1],position[2],position[3]], new_data);
139        
140    }
141}
142
143impl<T> TensorSlice<T> for ERIFull<T> {
144    #[inline]
145    fn get1d_slice(&self, position:usize, length: usize) -> Option<&[T]> {
146        Some(&self.data[position..position+length])
147    }
148    #[inline]
149    fn get4d_slice(&self, position:[usize;4], length: usize) -> Option<&[T]> {
150        let tp = self.index4d(position).unwrap();
151        Some(&self.data[tp..tp+length])
152    }
153    #[inline]
154    fn get_slice(&self, position:&[usize], length: usize) -> Option<&[T]> {
155        self.get4d_slice([position[0],position[1],position[2],position[3]], length)
156    }
157}
158impl<'a, T> TensorSliceMut<'a,T> for ERIFull<T> {
159    #[inline]
160    fn get1d_slice_mut(&mut self, position:usize, length: usize) -> Option<&mut [T]> {
161        Some(&mut self.data[position..position+length])
162    }
163    #[inline]
164    fn get4d_slice_mut(&mut self, position:[usize;4], length: usize) -> Option<&mut [T]> {
165        let tp = self.index4d(position).unwrap();
166        Some(&mut self.data[tp..tp+length])
167    }
168    #[inline]
169    fn get_slice_mut(&mut self, position:&[usize], length: usize) -> Option<&mut [T]> {
170        self.get4d_slice_mut([position[0],position[1],position[2],position[3]], length)
171    }
172}
173
174/// For ERIFold4
175impl<T> TensorOpt<T> for ERIFold4<T> {
176    #[inline]
177    fn get1d(&self, position:usize) -> Option<&T> {
178        self.data.get(position)
179    }
180    #[inline]
181    fn get2d(&self, position:[usize;2]) -> Option<&T> {
182        self.data.get(self.index2d(position).unwrap())
183    }
184    #[inline]
185    fn get4d(&self, position:[usize;4]) -> Option<&T> {
186        self.data.get(self.index4d(position).unwrap())
187    }
188    #[inline]
189    fn get(&self, position:&[usize]) -> Option<&T> {
190        let tp = [position[0],position[1],position[2],position[3]];
191        self.data.get(self.index4d(tp).unwrap())
192    }
193
194    fn get3d(&self, position:[usize;3]) -> Option<&T> {None}
195}
196impl<T> TensorOptUncheck<T> for ERIFold4<T> {
197    #[inline]
198    fn get4d_uncheck(&self, position:[usize;4]) -> Option<&T> {
199        self.data.get(self.index4d_uncheck(position).unwrap())
200    }
201}
202impl<'a, T> TensorOptMut<'a, T> for ERIFold4<T> {
203    #[inline]
204    fn get1d_mut(&mut self, position:usize) -> Option<&mut T> {
205        self.data.get_mut(position)
206    }
207    #[inline]
208    fn get2d_mut(&mut self, position:[usize;2]) -> Option<&mut T> {
209        let tp = self.index2d(position).unwrap();
210        self.data.get_mut(tp)
211    }
212    #[inline]
213    fn get4d_mut(&mut self, position:[usize;4]) -> Option<&mut T> {
214        let tp = self.index4d(position).unwrap();
215        self.data.get_mut(tp)
216    }
217    #[inline]
218    fn get_mut(&mut self, position:&[usize]) -> Option<&mut T> {
219        let tp = self.index4d([position[0],position[1],position[2],position[3]]).unwrap();
220        self.data.get_mut(tp)
221    }
222    #[inline]
223    fn set1d(&mut self, position:usize, new_data: T) {
224        if let Some(tmp_value) = self.data.get_mut(position) {
225            *tmp_value = new_data
226        } else {
227          panic!("Error in setting the tensor element located at the position of {:?}", position);
228        };
229    }
230    #[inline]
231    fn set2d(&mut self, position:[usize;2], new_data: T) {
232        let tp = self.index2d(position).unwrap();
233        if let Some(tmp_value) = self.data.get_mut(tp) {
234            *tmp_value = new_data
235        } else {
236          panic!("Error in setting the tensor element located at the position of {:?}", position);
237        };
238    }
239    #[inline]
240    fn set4d(&mut self, position:[usize;4], new_data: T) {
241        let tp = self.index4d(position).unwrap();
242        if let Some(tmp_value) = self.data.get_mut(tp) {
243            *tmp_value = new_data
244        } else {
245          panic!("Error in setting the tensor element located at the position of {:?}", position);
246        };
247    }
248    #[inline]
249    fn set(&mut self, position:&[usize], new_data: T) {
250        self.set4d([position[0],position[1],position[2],position[3]], new_data);
251        
252    }
253}
254impl<'a, T> TensorOptMutUncheck<'a, T> for ERIFold4<T> {
255    #[inline]
256    fn set4d_uncheck(&mut self, position:[usize;4], new_data: T) {
257        let tp = self.index4d_uncheck(position).unwrap();
258        if let Some(tmp_value) = self.data.get_mut(tp) {
259            *tmp_value = new_data
260        } else {
261          panic!("Error in setting the tensor element located at the position of {:?}", position);
262        };
263    }
264    #[inline]
265    fn get4d_mut_uncheck(&mut self, position:[usize;4]) -> Option<&mut T> {
266        let tp = self.index4d_uncheck(position).unwrap();
267        self.data.get_mut(tp)
268    }
269}
270
271impl<T> TensorSlice<T> for ERIFold4<T> {
272    #[inline]
273    fn get1d_slice(&self, position:usize, length: usize) -> Option<&[T]> {
274        Some(&self.data[position..position+length])
275    }
276    #[inline]
277    fn get2d_slice(&self, position:[usize;2], length: usize) -> Option<&[T]> {
278        let tp = self.index2d(position).unwrap();
279        Some(&self.data[tp..tp+length])
280    }
281    #[inline]
282    fn get4d_slice(&self, position:[usize;4], length: usize) -> Option<&[T]> {
283        let tp = self.index4d(position).unwrap();
284        Some(&self.data[tp..tp+length])
285    }
286    #[inline]
287    fn get_slice(&self, position:&[usize], length: usize) -> Option<&[T]> {
288        self.get4d_slice([position[0],position[1],position[2],position[3]], length)
289    }
290}
291
292impl<T> TensorSliceUncheck<T> for ERIFold4<T> {
293    #[inline]
294    fn get4d_slice_uncheck(&self, position:[usize;4], length: usize) -> Option<&[T]> {
295        let tp = self.index4d_uncheck(position).unwrap();
296        Some(&self.data[tp..tp+length])
297    }
298}
299
300impl<'a, T> TensorSliceMut<'a,T> for ERIFold4<T> {
301    #[inline]
302    fn get1d_slice_mut(&mut self, position:usize, length: usize) -> Option<&mut [T]> {
303        Some(&mut self.data[position..position+length])
304    }
305    #[inline]
306    fn get2d_slice_mut(&mut self, position:[usize;2], length: usize) -> Option<&mut [T]> {
307        let tp = self.index2d(position).unwrap();
308        Some(&mut self.data[tp..tp+length])
309    }
310    #[inline]
311    fn get4d_slice_mut(&mut self, position:[usize;4], length: usize) -> Option<&mut [T]> {
312        let tp = self.index4d(position).unwrap();
313        Some(&mut self.data[tp..tp+length])
314    }
315    #[inline]
316    fn get_slice_mut(&mut self, position:&[usize], length: usize) -> Option<&mut [T]> {
317        self.get4d_slice_mut([position[0],position[1],position[2],position[3]], length)
318    }
319}
320
321impl<'a, T> TensorSliceMutUncheck<'a,T> for ERIFold4<T> {
322    #[inline]
323    fn get4d_slice_mut_uncheck(&mut self, position:[usize;4], length: usize) -> Option<&mut [T]> {
324        let tp = self.index4d_uncheck(position).unwrap();
325        Some(&mut self.data[tp..tp+length])
326    }
327}
328
329// Trait implementations for MatrixFull
330impl<'a,T> TensorOpt<T> for MatrixFull<T> {
331    #[inline]
332    fn get1d(&self, position:usize) -> Option<&T> {
333        self.data.get(position)
334    }
335    #[inline]
336    fn get2d(&self, position:[usize;2]) -> Option<&T> {
337        self.data.get(self.index2d(position).unwrap())
338    }
339    #[inline]
340    fn get(&self, position:&[usize]) -> Option<&T> {
341        let tp = [position[0],position[1]];
342        self.data.get(self.index2d(tp).unwrap())
343    }
344}
345impl<'a, T> TensorSlice<T> for MatrixFull<T> {
346    #[inline]
347    fn get1d_slice(&self, position:usize, length: usize) -> Option<&[T]> {
348        Some(&self.data[position..position+length])
349    }
350    #[inline]
351    fn get2d_slice(&self, position:[usize;2], length: usize) -> Option<&[T]> {
352        let tp = self.index2d(position).unwrap();
353        Some(&self.data[tp..tp+length])
354    }
355    #[inline]
356    fn get_slice(&self, position:&[usize], length: usize) -> Option<&[T]> {
357        self.get2d_slice([position[0],position[1]], length)
358    }
359}
360
361impl<'a, T> TensorOptMut<'a, T> for MatrixFull<T> {
362    #[inline]
363    fn get1d_mut(&mut self, position:usize) -> Option<&mut T> {
364        self.data.get_mut(position)
365    }
366    #[inline]
367    fn get2d_mut(&mut self, position:[usize;2]) -> Option<&mut T> {
368        let tp = self.index2d(position).unwrap();
369        self.data.get_mut(tp)
370    }
371    #[inline]
372    fn get_mut(&mut self, position:&[usize]) -> Option<&mut T> {
373        let tp = self.index2d([position[0],position[1]]).unwrap();
374        self.data.get_mut(tp)
375    }
376    #[inline]
377    fn set1d(&mut self, position:usize, new_data: T) {
378        if let Some(tmp_value) = self.data.get_mut(position) {
379            *tmp_value = new_data
380        } else {
381          panic!("Error in setting the tensor element located at the position of {:?}", position);
382        };
383    }
384    #[inline]
385    fn set2d(&mut self, position:[usize;2], new_data: T) {
386        let tp = self.index2d(position).unwrap();
387        if let Some(tmp_value) = self.data.get_mut(tp) {
388            *tmp_value = new_data
389        } else {
390          panic!("Error in setting the tensor element located at the position of {:?}", position);
391        };
392    }
393    #[inline]
394    fn set(&mut self, position:&[usize], new_data: T) {
395        //let tp = self.index4d([position[0],position[1],position[2],position[3]]);
396        self.set2d([position[0],position[1]], new_data);
397        
398    }
399
400    fn get3d_mut(&mut self, position:[usize;3]) -> Option<&mut T> {None}
401
402    fn get4d_mut(&mut self, position:[usize;4]) -> Option<&mut T> {None}
403
404    fn set3d(&mut self, position:[usize;3], new_data: T) {}
405
406    fn set4d(&mut self, position:[usize;4], new_data: T) {}
407}
408impl<'a, T> TensorSliceMut<'a, T> for MatrixFull<T> {
409    #[inline]
410    fn get1d_slice_mut(&mut self, position:usize, length: usize) -> Option<&mut [T]> {
411        Some(&mut self.data[position..position+length])
412    }
413    #[inline]
414    fn get2d_slice_mut(&mut self, position:[usize;2], length: usize) -> Option<&mut [T]> {
415        let tp = self.index2d(position).unwrap();
416        Some(&mut self.data[tp..tp+length])
417    }
418    #[inline]
419    fn get_slice_mut(&mut self, position:&[usize], length: usize) -> Option<&mut [T]> {
420        self.get2d_slice_mut([position[0],position[1]], length)
421    }
422}
423
424// Trait implementations for MatrixFullSliceMut
425impl<'a, T> TensorOptMut<'a, T> for MatrixFullSliceMut<'a,T> {
426    #[inline]
427    fn get1d_mut(&mut self, position:usize) -> Option<&mut T> {
428        self.data.get_mut(position)
429    }
430    #[inline]
431    fn get2d_mut(&mut self, position:[usize;2]) -> Option<&mut T> {
432        let tp = self.index2d(position).unwrap();
433        self.data.get_mut(tp)
434    }
435    #[inline]
436    fn get_mut(&mut self, position:&[usize]) -> Option<&mut T> {
437        let tp = self.index2d([position[0],position[1]]).unwrap();
438        self.data.get_mut(tp)
439    }
440    #[inline]
441    fn set1d(&mut self, position:usize, new_data: T) {
442        if let Some(tmp_value) = self.data.get_mut(position) {
443            *tmp_value = new_data
444        } else {
445          panic!("Error in setting the tensor element located at the position of {:?}", position);
446        };
447    }
448    #[inline]
449    fn set2d(&mut self, position:[usize;2], new_data: T) {
450        let tp = self.index2d(position).unwrap();
451        if let Some(tmp_value) = self.data.get_mut(tp) {
452            *tmp_value = new_data
453        } else {
454          panic!("Error in setting the tensor element located at the position of {:?}", position);
455        };
456    }
457    #[inline]
458    fn set(&mut self, position:&[usize], new_data: T) {
459        //let tp = self.index4d([position[0],position[1],position[2],position[3]]);
460        self.set2d([position[0],position[1]], new_data);
461        
462    }
463}
464impl<'a, T> TensorSliceMut<'a, T> for MatrixFullSliceMut<'a, T> {
465    #[inline]
466    fn get1d_slice_mut(&mut self, position:usize, length: usize) -> Option<&mut [T]> {
467        Some(&mut self.data[position..position+length])
468    }
469    #[inline]
470    fn get2d_slice_mut(&mut self, position:[usize;2], length: usize) -> Option<&mut [T]> {
471        let tp = self.index2d(position).unwrap();
472        Some(&mut self.data[tp..tp+length])
473    }
474    #[inline]
475    fn get_slice_mut(&mut self, position:&[usize], length: usize) -> Option<&mut [T]> {
476        self.get2d_slice_mut([position[0],position[1]], length)
477    }
478}
479
480//Trait implementations for MatrixFullSlice
481impl<'a,T> TensorOpt<T> for MatrixFullSlice<'a,T> {
482    #[inline]
483    fn get1d(&self, position:usize) -> Option<&T> {
484        self.data.get(position)
485    }
486    #[inline]
487    fn get2d(&self, position:[usize;2]) -> Option<&T> {
488        self.data.get(self.index2d(position).unwrap())
489    }
490    #[inline]
491    fn get(&self, position:&[usize]) -> Option<&T> {
492        let tp = [position[0],position[1]];
493        self.data.get(self.index2d(tp).unwrap())
494    }
495}
496impl<'a, T> TensorSlice<T> for MatrixFullSlice<'a, T> {
497    #[inline]
498    fn get1d_slice(&self, position:usize, length: usize) -> Option<&[T]> {
499        Some(&self.data[position..position+length])
500    }
501    #[inline]
502    fn get2d_slice(&self, position:[usize;2], length: usize) -> Option<&[T]> {
503        let tp = self.index2d(position).unwrap();
504        Some(&self.data[tp..tp+length])
505    }
506    #[inline]
507    fn get_slice(&self, position:&[usize], length: usize) -> Option<&[T]> {
508        self.get2d_slice([position[0],position[1]], length)
509    }
510}
511
512//Trait implementations for MatrixUpperSliceMut
513impl<'a, T> TensorOptMut<'a, T> for MatrixUpperSliceMut<'a,T> {
514    #[inline]
515    fn get1d_mut(&mut self, position:usize) -> Option<&mut T> {
516        self.data.get_mut(position)
517    }
518    #[inline]
519    fn get2d_mut(&mut self, position:[usize;2]) -> Option<&mut T> {
520        let tp = self.index2d(position).unwrap();
521        self.data.get_mut(tp)
522    }
523    #[inline]
524    fn get_mut(&mut self, position:&[usize]) -> Option<&mut T> {
525        let tp = self.index2d([position[0],position[1]]).unwrap();
526        self.data.get_mut(tp)
527    }
528    #[inline]
529    fn set1d(&mut self, position:usize, new_data: T) {
530        if let Some(tmp_value) = self.data.get_mut(position) {
531            *tmp_value = new_data
532        } else {
533          panic!("Error in setting the tensor element located at the position of {:?}", position);
534        };
535    }
536    #[inline]
537    fn set2d(&mut self, position:[usize;2], new_data: T) {
538        let tp = self.index2d(position).unwrap();
539        if let Some(tmp_value) = self.data.get_mut(tp) {
540            *tmp_value = new_data
541        } else {
542          panic!("Error in setting the tensor element located at the position of {:?}", position);
543        };
544    }
545    #[inline]
546    fn set(&mut self, position:&[usize], new_data: T) {
547        //let tp = self.index4d([position[0],position[1],position[2],position[3]]);
548        self.set2d([position[0],position[1]], new_data);
549        
550    }
551}
552impl<'a, T> TensorOptMutUncheck<'a, T> for MatrixUpperSliceMut<'a,T> {
553    #[inline]
554    fn get2d_mut_uncheck(&mut self, position:[usize;2]) -> Option<&mut T> {
555        let tp = self.index2d_uncheck(position).unwrap();
556        self.data.get_mut(tp)
557    }
558    #[inline]
559    fn set2d_uncheck(&mut self, position:[usize;2], new_data: T) {
560        let tp = self.index2d_uncheck(position).unwrap();
561        if let Some(tmp_value) = self.data.get_mut(tp) {
562            *tmp_value = new_data
563        } else {
564          panic!("Error in setting the tensor element located at the position of {:?}", position);
565        };
566    }
567}
568impl<'a, T> TensorSliceMut<'a, T> for MatrixUpperSliceMut<'a, T> {
569    #[inline]
570    fn get1d_slice_mut(&mut self, position:usize, length: usize) -> Option<&mut [T]> {
571        Some(&mut self.data[position..position+length])
572    }
573    #[inline]
574    fn get2d_slice_mut(&mut self, position:[usize;2], length: usize) -> Option<&mut [T]> {
575        let tp = self.index2d(position).unwrap();
576        Some(&mut self.data[tp..tp+length])
577    }
578    #[inline]
579    fn get_slice_mut(&mut self, position:&[usize], length: usize) -> Option<&mut [T]> {
580        self.get2d_slice_mut([position[0],position[1]], length)
581    }
582}
583impl<'a, T> TensorSliceMutUncheck<'a, T> for MatrixUpperSliceMut<'a, T> {
584    #[inline]
585    fn get2d_slice_mut_uncheck(&mut self, position:[usize;2], length: usize) -> Option<&mut [T]> {
586        let tp = self.index2d_uncheck(position).unwrap();
587        Some(&mut self.data[tp..tp+length])
588    }
589}
590
591//Trait implementations for MatrixUpperSlice
592impl<'a,T> TensorOpt<T> for MatrixUpperSlice<'a,T> {
593    #[inline]
594    fn get1d(&self, position:usize) -> Option<&T> {
595        self.data.get(position)
596    }
597    #[inline]
598    fn get2d(&self, position:[usize;2]) -> Option<&T> {
599        self.data.get(self.index2d(position).unwrap())
600    }
601    #[inline]
602    fn get(&self, position:&[usize]) -> Option<&T> {
603        let tp = [position[0],position[1]];
604        self.data.get(self.index2d(tp).unwrap())
605    }
606}
607impl<'a,T> TensorOptUncheck<T> for MatrixUpperSlice<'a,T> {
608    #[inline]
609    fn get2d_uncheck(&self, position:[usize;2]) -> Option<&T> {
610        self.data.get(self.index2d_uncheck(position).unwrap())
611    }
612}
613impl<'a, T> TensorSlice<T> for MatrixUpperSlice<'a, T> {
614    #[inline]
615    fn get1d_slice(&self, position:usize, length: usize) -> Option<&[T]> {
616        Some(&self.data[position..position+length])
617    }
618    #[inline]
619    fn get2d_slice(&self, position:[usize;2], length: usize) -> Option<&[T]> {
620        let tp = self.index2d(position).unwrap();
621        Some(&self.data[tp..tp+length])
622    }
623    #[inline]
624    fn get_slice(&self, position:&[usize], length: usize) -> Option<&[T]> {
625        self.get2d_slice([position[0],position[1]], length)
626    }
627}
628impl<'a, T> TensorSliceUncheck<T> for MatrixUpperSlice<'a, T> {
629    #[inline]
630    fn get2d_slice_uncheck(&self, position:[usize;2], length: usize) -> Option<&[T]> {
631        let tp = self.index2d_uncheck(position).unwrap();
632        Some(&self.data[tp..tp+length])
633    }
634}
635
636//Trait implementations for MatrixUpper
637impl<'a, T> TensorOptMut<'a, T> for MatrixUpper<T> {
638    #[inline]
639    fn get1d_mut(&mut self, position:usize) -> Option<&mut T> {
640        self.data.get_mut(position)
641    }
642    #[inline]
643    fn get2d_mut(&mut self, position:[usize;2]) -> Option<&mut T> {
644        let tp = self.index2d(position).unwrap();
645        self.data.get_mut(tp)
646    }
647    #[inline]
648    fn get_mut(&mut self, position:&[usize]) -> Option<&mut T> {
649        let tp = self.index2d([position[0],position[1]]).unwrap();
650        self.data.get_mut(tp)
651    }
652    #[inline]
653    fn set1d(&mut self, position:usize, new_data: T) {
654        if let Some(tmp_value) = self.data.get_mut(position) {
655            *tmp_value = new_data
656        } else {
657          panic!("Error in setting the tensor element located at the position of {:?}", position);
658        };
659    }
660    #[inline]
661    fn set2d(&mut self, position:[usize;2], new_data: T) {
662        let tp = self.index2d(position).unwrap();
663        if let Some(tmp_value) = self.data.get_mut(tp) {
664            *tmp_value = new_data
665        } else {
666          panic!("Error in setting the tensor element located at the position of {:?}", position);
667        };
668    }
669    #[inline]
670    fn set(&mut self, position:&[usize], new_data: T) {
671        //let tp = self.index4d([position[0],position[1],position[2],position[3]]);
672        self.set2d([position[0],position[1]], new_data);
673        
674    }
675}
676impl<'a, T> TensorOptMutUncheck<'a, T> for MatrixUpper<T> {
677    #[inline]
678    fn get2d_mut_uncheck(&mut self, position:[usize;2]) -> Option<&mut T> {
679        let tp = self.index2d_uncheck(position).unwrap();
680        self.data.get_mut(tp)
681    }
682    #[inline]
683    fn set2d_uncheck(&mut self, position:[usize;2], new_data: T) {
684        let tp = self.index2d_uncheck(position).unwrap();
685        if let Some(tmp_value) = self.data.get_mut(tp) {
686            *tmp_value = new_data
687        } else {
688          panic!("Error in setting the tensor element located at the position of {:?}", position);
689        };
690    }
691}
692impl<'a, T> TensorSliceMut<'a, T> for MatrixUpper<T> {
693    #[inline]
694    fn get1d_slice_mut(&mut self, position:usize, length: usize) -> Option<&mut [T]> {
695        Some(&mut self.data[position..position+length])
696    }
697    #[inline]
698    fn get2d_slice_mut(&mut self, position:[usize;2], length: usize) -> Option<&mut [T]> {
699        let tp = self.index2d(position).unwrap();
700        Some(&mut self.data[tp..tp+length])
701    }
702    #[inline]
703    fn get_slice_mut(&mut self, position:&[usize], length: usize) -> Option<&mut [T]> {
704        self.get2d_slice_mut([position[0],position[1]], length)
705    }
706}
707impl<'a, T> TensorSliceMutUncheck<'a, T> for MatrixUpper<T> {
708    #[inline]
709    fn get2d_slice_mut_uncheck(&mut self, position:[usize;2], length: usize) -> Option<&mut [T]> {
710        let tp = self.index2d_uncheck(position).unwrap();
711        Some(&mut self.data[tp..tp+length])
712    }
713}
714impl<T> TensorOpt<T> for MatrixUpper<T> {
715    #[inline]
716    fn get1d(&self, position:usize) -> Option<&T> {
717        self.data.get(position)
718    }
719    #[inline]
720    fn get2d(&self, position:[usize;2]) -> Option<&T> {
721        self.data.get(self.index2d(position).unwrap())
722    }
723    #[inline]
724    fn get(&self, position:&[usize]) -> Option<&T> {
725        let tp = [position[0],position[1]];
726        self.data.get(self.index2d(tp).unwrap())
727    }
728}
729impl<T> TensorOptUncheck<T> for MatrixUpper<T> {
730    #[inline]
731    fn get2d_uncheck(&self, position:[usize;2]) -> Option<&T> {
732        self.data.get(self.index2d_uncheck(position).unwrap())
733    }
734}
735impl<T> TensorSlice<T> for MatrixUpper<T> {
736    #[inline]
737    fn get1d_slice(&self, position:usize, length: usize) -> Option<&[T]> {
738        Some(&self.data[position..position+length])
739    }
740    #[inline]
741    fn get2d_slice(&self, position:[usize;2], length: usize) -> Option<&[T]> {
742        let tp = self.index2d(position).unwrap();
743        Some(&self.data[tp..tp+length])
744    }
745    #[inline]
746    fn get_slice(&self, position:&[usize], length: usize) -> Option<&[T]> {
747        self.get2d_slice([position[0],position[1]], length)
748    }
749}
750impl<T> TensorSliceUncheck<T> for MatrixUpper<T> {
751    #[inline]
752    fn get2d_slice_uncheck(&self, position:[usize;2], length: usize) -> Option<&[T]> {
753        let tp = self.index2d_uncheck(position).unwrap();
754        Some(&self.data[tp..tp+length])
755    }
756}
757
758
759//==========================================================================
760// Now implement the Index and IndexMut traits for different Structrues 
761//==========================================================================
762
763impl<T> Index<[usize;3]> for RIFull<T> {
764    type Output = T;
765    #[inline]
766    fn index(&self, position:[usize;3]) -> &Self::Output {
767        let tmp_p = position.iter()
768            .zip(self.indicing.iter())
769            .fold(0_usize,|acc, i| {acc + i.0*i.1});
770        Index::index(&self.data, tmp_p)
771    }
772}
773
774impl<T> IndexMut<[usize;3]> for RIFull<T> {
775    #[inline]
776    fn index_mut(&mut self, position:[usize;3]) -> &mut Self::Output {
777        let tmp_p = position.iter()
778            .zip(self.indicing.iter())
779            .fold(0_usize,|acc, i| {acc + i.0*i.1});
780        IndexMut::index_mut(&mut self.data, tmp_p)
781    }
782}
783
784impl<T, I:SliceIndex<[T]>> Index<I> for MatrixUpper<T> {
785    type Output = I::Output;
786    fn index(&self, index: I) -> &Self::Output {
787        Index::index(&self.data, index)
788    }
789}
790
791impl<T, I:SliceIndex<[T]>> IndexMut<I> for MatrixUpper<T> {
792    fn index_mut(&mut self, index: I) -> &mut Self::Output {
793        IndexMut::index_mut(&mut self.data, index)
794    }
795}
796
797impl<T> Index<[usize;2]> for ERIFold4<T> {
798    type Output = T;
799    #[inline]
800    fn index(&self, position:[usize;2]) -> &Self::Output {
801        let tmp_p = position.iter()
802            .zip(self.indicing.iter())
803            .fold(0_usize,|acc, i| {acc + i.0*i.1});
804        Index::index(&self.data, tmp_p)
805    }
806}
807
808impl<T> IndexMut<[usize;2]> for ERIFold4<T> {
809    #[inline]
810    fn index_mut(&mut self, position:[usize;2]) -> &mut Self::Output {
811        let tmp_p = position.iter()
812            .zip(self.indicing.iter())
813            .fold(0_usize,|acc, i| {acc + i.0*i.1});
814        IndexMut::index_mut(&mut self.data, tmp_p)
815    }
816}