1use crate::vector::Vector;
4
5use std::{
6 fmt::{self, Debug},
7 ops::{Index, IndexMut},
8};
9
10pub struct Matrix<T: Vector> {
12 columns: Vec<T>,
14
15 dimensions: (usize, usize),
17}
18
19impl<T: Vector> Matrix<T>
20where
21 T: Clone,
22{
23 pub fn init(col_num: usize, col_dim: usize) -> Self {
27 Self {
28 columns: vec![T::init(col_dim); col_num],
29 dimensions: (col_num, col_dim),
30 }
31 }
32
33 pub fn dimensions(&self) -> (usize, usize) {
35 self.dimensions
36 }
37
38 pub fn swap(&mut self, i: usize, j: usize) {
40 self.columns.swap(i, j);
41 }
42}
43
44impl<T> Index<usize> for Matrix<T>
46where
47 T: Vector,
48{
49 type Output = T;
50
51 fn index(&self, index: usize) -> &T {
52 &self.columns[index]
53 }
54}
55
56impl<T> IndexMut<usize> for Matrix<T>
58where
59 T: Vector,
60{
61 fn index_mut(&mut self, index: usize) -> &mut T {
62 &mut self.columns[index]
63 }
64}
65
66impl<T> fmt::Debug for Matrix<T>
67where
68 T: Vector + Debug,
69{
70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71 writeln!(f, "{:?}\n", self.columns)
72 }
73}