Skip to main content

conspire/math/matrix/
mod.rs

1pub mod square;
2pub mod vector;
3
4use crate::math::{
5    Scalar, Tensor, TensorRank1, TensorRank1Vec, TensorRank2, TensorTuple, TensorVec,
6};
7use std::{
8    iter::Sum,
9    ops::{AddAssign, Div, DivAssign, Index, IndexMut, Mul},
10};
11use vector::Vector;
12
13/// A matrix.
14#[derive(Clone, Debug, PartialEq)]
15pub struct Matrix(Vec<Vector>);
16
17impl Default for Matrix {
18    fn default() -> Self {
19        Self::zero(0, 0)
20    }
21}
22
23impl Matrix {
24    pub fn height(&self) -> usize {
25        self.0.len()
26    }
27    pub fn is_empty(&self) -> bool {
28        self.0.is_empty()
29    }
30    pub fn iter(&self) -> impl Iterator<Item = &Vector> {
31        self.0.iter()
32    }
33    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Vector> {
34        self.0.iter_mut()
35    }
36    pub fn len(&self) -> usize {
37        self.0.len()
38    }
39    pub fn transpose(&self) -> Self {
40        (0..self.width())
41            .map(|i| (0..self.len()).map(|j| self[j][i]).collect())
42            .collect()
43        // let mut transpose = Self::zero(self.width(), self.len());
44        // self.iter().enumerate().for_each(|(i, self_i)|
45        //     self_i.iter().zip(transpose.iter_mut()).for_each(|(self_ij, transpose_j)|
46        //         transpose_j[i] = *self_ij
47        //     )
48        // );
49        // transpose
50    }
51    pub fn width(&self) -> usize {
52        self.0[0].len()
53    }
54    pub fn zero(height: usize, width: usize) -> Self {
55        (0..height).map(|_| Vector::zero(width)).collect()
56    }
57}
58
59impl TensorVec for Matrix {
60    type Item = Vector;
61    fn append(&mut self, other: &mut Self) {
62        self.0.append(&mut other.0)
63    }
64    fn capacity(&self) -> usize {
65        self.0.capacity()
66    }
67    fn is_empty(&self) -> bool {
68        self.0.is_empty()
69    }
70    fn new() -> Self {
71        Self(Vec::new())
72    }
73    fn push(&mut self, item: Self::Item) {
74        self.0.push(item)
75    }
76    fn remove(&mut self, index: usize) -> Self::Item {
77        self.0.remove(index)
78    }
79    fn reserve(&mut self, additional: usize) {
80        self.0.reserve(additional)
81    }
82    fn retain<F>(&mut self, f: F)
83    where
84        F: FnMut(&Self::Item) -> bool,
85    {
86        self.0.retain(f)
87    }
88    fn swap_remove(&mut self, index: usize) -> Self::Item {
89        self.0.swap_remove(index)
90    }
91}
92
93impl From<Matrix> for Vec<Vec<Scalar>> {
94    fn from(matrix: Matrix) -> Self {
95        matrix.into_iter().map(|vector| vector.into()).collect()
96    }
97}
98
99impl FromIterator<Vector> for Matrix {
100    fn from_iter<Ii: IntoIterator<Item = Vector>>(into_iterator: Ii) -> Self {
101        Self(Vec::from_iter(into_iterator))
102    }
103}
104
105impl Index<usize> for Matrix {
106    type Output = Vector;
107    fn index(&self, index: usize) -> &Self::Output {
108        &self.0[index]
109    }
110}
111
112impl IndexMut<usize> for Matrix {
113    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
114        &mut self.0[index]
115    }
116}
117
118impl IntoIterator for Matrix {
119    type Item = Vector;
120    type IntoIter = std::vec::IntoIter<Self::Item>;
121    fn into_iter(self) -> Self::IntoIter {
122        self.0.into_iter()
123    }
124}
125
126impl Sum for Matrix {
127    fn sum<Ii>(iter: Ii) -> Self
128    where
129        Ii: Iterator<Item = Self>,
130    {
131        iter.reduce(|mut acc, item| {
132            acc += item;
133            acc
134        })
135        .unwrap_or_else(Self::default)
136    }
137}
138
139impl Div<Scalar> for Matrix {
140    type Output = Self;
141    fn div(mut self, scalar: Scalar) -> Self::Output {
142        self /= scalar;
143        self
144    }
145}
146
147impl DivAssign<Scalar> for Matrix {
148    fn div_assign(&mut self, scalar: Scalar) {
149        self.iter_mut().for_each(|entry| *entry /= &scalar);
150    }
151}
152
153impl Mul<Vector> for &Matrix {
154    type Output = Vector;
155    fn mul(self, vector: Vector) -> Self::Output {
156        self.iter().map(|self_i| self_i * &vector).collect()
157    }
158}
159
160impl Mul<&Vector> for &Matrix {
161    type Output = Vector;
162    fn mul(self, vector: &Vector) -> Self::Output {
163        self.iter().map(|self_i| self_i * vector).collect()
164    }
165}
166
167impl Mul<&Scalar> for &Matrix {
168    type Output = Vector;
169    fn mul(self, _tensor_rank_0: &Scalar) -> Self::Output {
170        unimplemented!()
171    }
172}
173
174impl AddAssign for Matrix {
175    fn add_assign(&mut self, matrix: Self) {
176        self.iter_mut()
177            .zip(matrix)
178            .for_each(|(self_i, matrix_i)| *self_i += matrix_i);
179    }
180}
181
182impl<const D: usize, const I: usize> Mul<&TensorRank1<D, I>> for &Matrix {
183    type Output = Vector;
184    fn mul(self, _tensor_rank_1: &TensorRank1<D, I>) -> Self::Output {
185        unimplemented!()
186    }
187}
188
189impl<const D: usize, const I: usize> Mul<&TensorRank1Vec<D, I>> for &Matrix {
190    type Output = Vector;
191    fn mul(self, tensor_rank_1_vec: &TensorRank1Vec<D, I>) -> Self::Output {
192        self.iter()
193            .map(|self_i| self_i * tensor_rank_1_vec)
194            .collect()
195    }
196}
197
198impl<const D: usize, const I: usize, const J: usize> Mul<&TensorRank2<D, I, J>> for &Matrix {
199    type Output = Vector;
200    fn mul(self, tensor_rank_2: &TensorRank2<D, I, J>) -> Self::Output {
201        self.iter().map(|self_i| self_i * tensor_rank_2).collect()
202    }
203}
204
205impl<const D: usize, const I: usize, const J: usize, const K: usize, const L: usize>
206    Mul<&TensorTuple<TensorRank2<D, I, J>, TensorRank2<D, K, L>>> for &Matrix
207{
208    type Output = Vector;
209    fn mul(
210        self,
211        tensor_tuple: &TensorTuple<TensorRank2<D, I, J>, TensorRank2<D, K, L>>,
212    ) -> Self::Output {
213        self.iter().map(|self_i| self_i * tensor_tuple).collect()
214    }
215}
216
217impl Mul for Matrix {
218    type Output = Self;
219    fn mul(self, matrix: Self) -> Self::Output {
220        let mut output = Self::zero(self.len(), matrix.width());
221        self.iter()
222            .zip(output.iter_mut())
223            .for_each(|(self_i, output_i)| {
224                self_i
225                    .iter()
226                    .zip(matrix.iter())
227                    .for_each(|(self_ij, matrix_j)| *output_i += matrix_j * self_ij)
228            });
229        output
230    }
231}