1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crateTensor;
use ;
/// A trait for [`Tensor`]-based types, allowing to borrow the enclosed tensor.
///
/// This trait is used by the types that build on the `Tensor` type to implement multi-dimensional
/// collections of various kind. In essence, this trait allows to extract a tensor properly
/// qualified to use all the methods of the `Tensor` type:
/// ```rust
/// use concrete_core::math::tensor::{Tensor, AsRefTensor, AsRefSlice};
///
/// pub struct Matrix<Cont>{
/// tensor: Tensor<Cont>,
/// row_length: usize,
/// }
///
/// pub struct Row<Cont>{
/// tensor: Tensor<Cont>
/// }
///
/// impl<Cont> AsRefTensor for Matrix<Cont> where Cont: AsRefSlice{
/// type Element = Cont::Element;
/// type Container = Cont;
/// fn as_tensor(&self) -> &Tensor<Cont> {
/// &self.tensor
/// }
/// }
///
/// impl<Cont> Matrix<Cont> {
/// // Returns an iterator over the matrix rows.
/// pub fn row_iter(&self) -> impl Iterator<Item = Row<&[<Self as AsRefTensor>::Element]>>
/// where
/// Self: AsRefTensor
/// {
/// self.as_tensor() // `AsRefTensor` method returning a `&Tensor<Cont>`
/// .as_slice() // Since `Cont` is `AsView`, so is `Tensor<Cont>`
/// .chunks(self.row_length) // Split in chunks of the size of the rows.
/// .map(|sub| Row{tensor: Tensor::from_container(sub)}) // Wraps into a row type.
/// }
/// }
/// ```
/// A trait for [`Tensor`]-based types, allowing to mutably borrow the enclosed tensor.
///
/// This trait implements the same logic as `AsRefTensor`, but for mutable borrow instead. See the
/// [`AsRefTensor`] documentation for more explanations on the logic.