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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! Transposes, products elementwise, row and column operations, the trace and powers.
use super::{Element, Matrix};
impl<T: Clone> Matrix<T> {
/// The transpose `Aᵀ`, whose element in row `i` and column `j` is this one's in row `j` and
/// column `i`.
pub fn transpose(&self) -> Self {
let rows = self.rows;
Matrix::from_fn(self.cols, rows, |i, j| self.data[j + i * rows].clone())
}
}
impl<T: Element> Matrix<T> {
/// The conjugate transpose `A*`, the transpose with each element conjugated: the transpose
/// itself for real elements.
pub fn conjugate_transpose(&self) -> Self {
let rows = self.rows;
Matrix::from_fn(self.cols, rows, |i, j| self.data[j + i * rows].conj())
}
/// The product elementwise, Hadamard's `A ∘ B`.
///
/// # Panics
///
/// Panics if the matrices are not of the same shape.
#[track_caller]
pub fn hadamard(&self, other: &Matrix<T>) -> Matrix<T> {
assert!(
self.shape() == other.shape(),
"the matrices are not of the same shape"
);
Matrix {
rows: self.rows,
cols: self.cols,
data: self
.data
.iter()
.zip(&other.data)
.map(|(a, b)| T::mul(a, b))
.collect(),
}
}
/// Swaps rows `a` and `b`.
///
/// # Panics
///
/// Panics if either is outside the matrix.
#[track_caller]
pub fn swap_rows(&mut self, a: usize, b: usize) {
assert!(
a < self.rows && b < self.rows,
"the row is outside the matrix"
);
if a != b {
for j in 0..self.cols {
self.data.swap(a + j * self.rows, b + j * self.rows);
}
}
}
/// Multiplies row `i` by `k`.
///
/// # Panics
///
/// Panics if the row is outside the matrix.
#[track_caller]
pub fn scale_row(&mut self, i: usize, k: &T) {
assert!(i < self.rows, "the row is outside the matrix");
for j in 0..self.cols {
T::mul_assign(&mut self.data[i + j * self.rows], k);
}
}
/// Adds `k` times row `source` to row `target`.
///
/// # Panics
///
/// Panics if either row is outside the matrix.
#[track_caller]
pub fn add_scaled_row(&mut self, target: usize, source: usize, k: &T) {
assert!(
target < self.rows && source < self.rows,
"the row is outside the matrix"
);
for j in 0..self.cols {
let x = T::mul(&self.data[source + j * self.rows], k);
T::add_assign(&mut self.data[target + j * self.rows], &x);
}
}
/// Swaps columns `a` and `b`.
///
/// # Panics
///
/// Panics if either is outside the matrix.
#[track_caller]
pub fn swap_columns(&mut self, a: usize, b: usize) {
assert!(
a < self.cols && b < self.cols,
"the column is outside the matrix"
);
if a != b {
let (lo, hi) = (a.min(b), a.max(b));
let (left, right) = self.data.split_at_mut(hi * self.rows);
left[lo * self.rows..(lo + 1) * self.rows].swap_with_slice(&mut right[..self.rows]);
}
}
/// Multiplies column `j` by `k`.
///
/// # Panics
///
/// Panics if the column is outside the matrix.
#[track_caller]
pub fn scale_column(&mut self, j: usize, k: &T) {
for x in self.column_mut(j) {
T::mul_assign(x, k);
}
}
/// Adds `k` times column `source` to column `target`.
///
/// # Panics
///
/// Panics if either column is outside the matrix.
#[track_caller]
pub fn add_scaled_column(&mut self, target: usize, source: usize, k: &T) {
assert!(
target < self.cols && source < self.cols,
"the column is outside the matrix"
);
let rows = self.rows;
for i in 0..rows {
let x = T::mul(&self.data[i + source * rows], k);
T::add_assign(&mut self.data[i + target * rows], &x);
}
}
/// The trace `tr(A)`, the sum of the main diagonal.
///
/// # Panics
///
/// Panics if the matrix is not square.
#[track_caller]
pub fn trace(&self) -> T {
assert!(self.is_square(), "the matrix is not square");
let mut t = T::zero();
for i in 0..self.rows {
T::add_assign(&mut t, &self.data[i + i * self.rows]);
}
t
}
/// `Aⁿ`, by repeated squaring: about `2 log₂ n` products. `A⁰` is the identity.
///
/// # Panics
///
/// Panics if the matrix is not square.
#[track_caller]
pub fn pow(&self, n: u32) -> Self {
assert!(self.is_square(), "the matrix is not square");
if n == 0 {
return Matrix::identity(self.rows);
}
let mut base = self.clone();
let mut n = n;
while n & 1 == 0 {
base = &base * &base;
n >>= 1;
}
let mut r = base.clone();
n >>= 1;
while n > 0 {
base = &base * &base;
if n & 1 == 1 {
r = &r * &base;
}
n >>= 1;
}
r
}
}