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
use std::ops::Mul;
use crate::general::{Field, MultiplicativeGroup, MultiplicativeMonoid};
use crate::linear::FiniteDimVectorSpace;
pub trait Matrix:
Sized + Clone + Mul<<Self as Matrix>::Row, Output = <Self as Matrix>::Column>
{
type Field: Field;
type Row: FiniteDimVectorSpace<Field = Self::Field>;
type Column: FiniteDimVectorSpace<Field = Self::Field>;
type Transpose: Matrix<Field = Self::Field, Row = Self::Column, Column = Self::Row>;
fn nrows(&self) -> usize;
fn ncolumns(&self) -> usize;
fn row(&self, i: usize) -> Self::Row;
fn column(&self, i: usize) -> Self::Column;
unsafe fn get_unchecked(&self, i: usize, j: usize) -> Self::Field;
fn get(&self, i: usize, j: usize) -> Self::Field {
assert!(
i < self.nrows() && j < self.ncolumns(),
"Matrix indexing: index out of bounds."
);
unsafe { self.get_unchecked(i, j) }
}
fn transpose(&self) -> Self::Transpose;
}
pub trait MatrixMut: Matrix {
#[inline]
fn set_row(&self, i: usize, row: &Self::Row) -> Self {
let mut res = self.clone();
res.set_row_mut(i, row);
res
}
fn set_row_mut(&mut self, i: usize, row: &Self::Row);
#[inline]
fn set_column(&self, i: usize, col: &Self::Column) -> Self {
let mut res = self.clone();
res.set_column_mut(i, col);
res
}
fn set_column_mut(&mut self, i: usize, col: &Self::Column);
unsafe fn set_unchecked(&mut self, i: usize, j: usize, val: Self::Field);
fn set(&mut self, i: usize, j: usize, val: Self::Field) {
assert!(
i < self.nrows() && j < self.ncolumns(),
"Matrix indexing: index out of bounds."
);
unsafe { self.set_unchecked(i, j, val) }
}
}
pub trait SquareMatrix:
Matrix<
Row = <Self as SquareMatrix>::Vector,
Column = <Self as SquareMatrix>::Vector,
Transpose = Self,
> + MultiplicativeMonoid
{
type Vector: FiniteDimVectorSpace<Field = Self::Field>;
fn diagonal(&self) -> Self::Vector;
fn determinant(&self) -> Self::Field;
#[inline]
fn try_inverse(&self) -> Option<Self>;
#[inline]
fn dimension(&self) -> usize {
self.nrows()
}
#[inline]
fn transpose_mut(&mut self) {
*self = self.transpose()
}
}
pub trait SquareMatrixMut:
SquareMatrix
+ MatrixMut<
Row = <Self as SquareMatrix>::Vector,
Column = <Self as SquareMatrix>::Vector,
Transpose = Self,
>
{
fn from_diagonal(diag: &Self::Vector) -> Self;
#[inline]
fn set_diagonal(&self, diag: &Self::Vector) -> Self {
let mut res = self.clone();
res.set_diagonal_mut(diag);
res
}
fn set_diagonal_mut(&mut self, diag: &Self::Vector);
}
pub trait InversibleSquareMatrix: SquareMatrix + MultiplicativeGroup {}