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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::{
    blas::Blas,
    dim::{DimDyn, DimTrait, LessDimTrait},
    index::{IndexAxisTrait, SliceTrait},
    matrix_impl::Matrix,
    memory_impl::{ViewMem, ViewMutMem},
    num::Num,
    shape_stride::ShapeStride,
    slice::Slice,
};

pub trait MatrixBase: Sized {
    type Dim: DimTrait;
    type Item: Num;

    fn shape_stride(&self) -> ShapeStride<Self::Dim>;
    fn shape(&self) -> Self::Dim {
        self.shape_stride().shape()
    }
    fn stride(&self) -> Self::Dim {
        self.shape_stride().stride()
    }

    fn is_default_stride(&self) -> bool {
        self.shape_stride().is_default_stride()
    }

    fn is_transposed_default_stride(&self) -> bool {
        self.shape_stride().is_transposed_default_stride()
    }
}

pub trait ToViewMatrix: MatrixBase {
    fn to_view(&self) -> Matrix<ViewMem<Self::Item>, Self::Dim>;
}

pub trait ToViewMutMatrix: MatrixBase {
    fn to_view_mut(&mut self) -> Matrix<ViewMutMem<Self::Item>, Self::Dim>;
}

pub trait ToOwnedMatrix: MatrixBase {
    type Owned: OwnedMatrix
    where
        Self: Sized;

    fn to_owned_matrix(&self) -> Self::Owned;
}

pub trait AsMutPtr: MatrixBase {
    fn as_mut_ptr(&mut self) -> *mut Self::Item;
}

pub trait AsPtr: MatrixBase {
    fn as_ptr(&self) -> *const Self::Item;
}

pub trait MatrixSlice<S>: ToViewMatrix
where
    S: SliceTrait<Dim = Self::Dim>,
{
    type Output<'a>: MatrixBase<Dim = Self::Dim> + ViewMatrix
    where
        Self: 'a;

    fn slice(&self, index: S) -> Self::Output<'_>;
}

pub trait MatrixSliceMut<S>: ToViewMutMatrix
where
    S: SliceTrait<Dim = Self::Dim>,
{
    type Output<'a>: MatrixBase<Dim = Self::Dim> + ViewMutMatix
    where
        Self: 'a;

    fn slice_mut(&mut self, index: S) -> Self::Output<'_>;
}

pub trait IndexAxis<I: IndexAxisTrait>: ToViewMatrix
where
    Self::Dim: LessDimTrait,
{
    type Output<'a>: MatrixBase<Dim = <Self::Dim as LessDimTrait>::LessDim, Item = Self::Item>
        + ViewMatrix
    where
        Self: 'a;

    fn index_axis(&self, index: I) -> Self::Output<'_>;
}

pub trait IndexAxisMut<I: IndexAxisTrait>: ToViewMutMatrix
where
    Self::Dim: LessDimTrait,
{
    type Output<'a>: MatrixBase<Dim = <Self::Dim as LessDimTrait>::LessDim, Item = Self::Item>
        + ViewMutMatix
    where
        Self: 'a;

    fn index_axis_mut(&mut self, index: I) -> Self::Output<'_>;
}

pub trait IndexAxisDyn<I: IndexAxisTrait>: ToViewMatrix {
    type Output<'a>: MatrixBase<Dim = DimDyn, Item = Self::Item> + ViewMatrix
    where
        Self: 'a;

    fn index_axis_dyn(&self, index: I) -> Self::Output<'_>;
}

pub trait IndexAxisMutDyn<I: IndexAxisTrait>: ToViewMutMatrix {
    type Output<'a>: MatrixBase<Dim = DimDyn, Item = Self::Item> + ViewMutMatix
    where
        Self: 'a;

    fn index_axis_mut_dyn(&mut self, index: I) -> Self::Output<'_>;
}

pub trait IndexItem: MatrixBase {
    fn index_item<I: Into<Self::Dim>>(&self, index: I) -> Self::Item;
}

pub trait IndexItemAsign: MatrixBase {
    fn index_item_asign<I: Into<Self::Dim>>(&mut self, index: I, value: <Self as MatrixBase>::Item);
}

pub trait MatrixSliceDyn: ToViewMatrix {
    type Output<'a>: MatrixBase<Dim = DimDyn> + ViewMatrix
    where
        Self: 'a;

    fn slice_dyn(&self, index: Slice) -> Self::Output<'_>;
}

pub trait MatrixSliceMutDyn: ToViewMutMatrix {
    type Output<'a>: MatrixBase<Dim = DimDyn> + ViewMutMatix
    where
        Self: 'a;

    fn slice_mut_dyn(&mut self, index: Slice) -> Self::Output<'_>;
}

pub trait BlasMatrix: MatrixBase {
    type Blas: Blas<Self::Item>;
}

pub trait ViewMatrix: MatrixBase + ToViewMatrix + ToOwnedMatrix + AsPtr + BlasMatrix {}
pub trait ViewMutMatix:
    MatrixBase + ToViewMatrix + ToViewMutMatrix + AsMutPtr + BlasMatrix + AsPtr
{
}

pub trait OwnedMatrix: MatrixBase + ToViewMatrix + ToViewMutMatrix + AsPtr + BlasMatrix {
    fn from_vec<I: Into<Self::Dim>>(vec: Vec<Self::Item>, dim: I) -> Self;
}