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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
use nonempty::NonEmpty;
use num::{One, Zero};
/// Fallible sequential composition: `mul_two(A, B)` means "do A, then B".
///
/// For matrices this corresponds to right-multiplication (i.e. `B * A` in standard notation),
/// matching the opposite-algebra convention used by `DynMatrix`.
pub trait ChainMultiplyable: Sized {
type MultiplicationError;
/// `self ; then_this`
/// which means
/// `then_this * self`
///
/// # Errors
///
/// If the multiplication failed.
/// When these are matrices this would be
/// when there was a matrix multiplication
/// of incompatible shapes.
fn mul_two(self, then_this: Self) -> Result<Self, Self::MultiplicationError>;
/// Multiply all of `these_ops`
/// For `[a1...an]`
/// Do `a1` first and then `a2`
/// and so on.
/// So as multiplication
/// this means `an * ... a1`
///
/// # Errors
///
/// If at any step the multiplication failed.
/// When `ai` are matrices this would be
/// when there was a matrix multiplication
/// of incompatible shapes.
fn nonempty_chain_multiply(
mut these_ops: NonEmpty<Self>,
) -> Result<Self, Self::MultiplicationError> {
if these_ops.len() == 1 {
Ok(these_ops.head)
} else if these_ops.len() == 2 {
let a = these_ops.head;
let b = these_ops.tail.pop().expect("Length is 2");
Self::mul_two(a, b)
} else {
let (first, tail) = (these_ops.head, these_ops.tail);
first.chain_multiply_after(tail)
}
}
/// Multiply all of `these_ops`
/// with `self` as `a0`
/// For `[a1...an]`
/// Do `self` first then `a1` and then `a2`
/// and so on.
/// So as multiplication
/// this means `an * ... a1 * self`
///
/// # Errors
///
/// If at any step the multiplication failed.
/// When `ai` are matrices this would be
/// when there was a matrix multiplication
/// of incompatible shapes.
fn chain_multiply_after(
self,
these_ops: impl IntoIterator<Item = Self>,
) -> Result<Self, Self::MultiplicationError>;
}
impl<T> ChainMultiplyable for T
where
T: Mul<T, Output = T> + MulAssign<T>,
{
type MultiplicationError = ();
fn chain_multiply_after(
self,
these_ops: impl IntoIterator<Item = Self>,
) -> Result<Self, Self::MultiplicationError> {
let res = these_ops
.into_iter()
.fold(self, |acc, next_op| next_op * acc);
Ok(res)
}
fn mul_two(self, mut then_this: Self) -> Result<Self, Self::MultiplicationError> {
then_this *= self;
Ok(then_this)
}
}
/// Addition that can fail — e.g. when matrix dimensions do not match.
pub trait CheckedAdd {
type AdditionError;
/// Returns `true` if adding `rhs` to `self` would fail (e.g. shape mismatch).
fn will_error(&self, rhs: &Self) -> bool;
/// Add but there is a possibility for not being able to be added
/// like matrix dimensions mismatching
///
/// # Errors
/// For some reason we could not multiply. Likely something about matrix addition.
fn checked_add(self, rhs: Self) -> Result<Self, Self::AdditionError>
where
Self: Sized;
}
impl<T> CheckedAdd for T
where
T: Add<T, Output = T>,
{
type AdditionError = ();
fn will_error(&self, _rhs: &Self) -> bool {
false
}
fn checked_add(self, rhs: Self) -> Result<Self, Self::AdditionError>
where
Self: Sized,
{
Ok(self + rhs)
}
}
/// In-place addition that can fail — e.g. when matrix dimensions do not match.
pub trait CheckedAddAssign {
type AdditionError;
/// Returns `true` if adding `rhs` to `self` in place would fail.
fn will_error(&self, rhs: &Self) -> bool;
/// Add but there is a possibility for not being able to be added
/// like matrix dimensions mismatching
///
/// # Errors
/// For some reason we could not multiply. Likely something about matrix addition.
fn checked_add_assign(&mut self, rhs: Self) -> Result<(), Self::AdditionError>;
}
impl<T> CheckedAddAssign for T
where
T: AddAssign<T>,
{
type AdditionError = ();
fn will_error(&self, _rhs: &Self) -> bool {
false
}
fn checked_add_assign(&mut self, rhs: Self) -> Result<(), Self::AdditionError>
where
Self: Sized,
{
*self += rhs;
Ok(())
}
}
/// Unified error type for operations on types that implement all three checked arithmetic traits.
#[allow(clippy::enum_variant_names)]
pub enum CheckedArithError<T>
where
T: CheckedAdd + CheckedAddAssign + ChainMultiplyable,
{
AddError(<T as CheckedAdd>::AdditionError),
AddAssignError(<T as CheckedAddAssign>::AdditionError),
ChainMulError(<T as ChainMultiplyable>::MultiplicationError),
}
impl<T> CheckedArithError<T>
where
T: CheckedAdd + CheckedAddAssign + ChainMultiplyable,
{
/// Wrap a `CheckedAdd` error.
pub fn from_add(value: <T as CheckedAdd>::AdditionError) -> Self {
Self::AddError(value)
}
/// Wrap a `CheckedAddAssign` error.
pub fn from_add_assign(value: <T as CheckedAddAssign>::AdditionError) -> Self {
Self::AddAssignError(value)
}
/// Wrap a `ChainMultiplyable` error.
pub fn from_mul(value: <T as ChainMultiplyable>::MultiplicationError) -> Self {
Self::ChainMulError(value)
}
}
/// A commutative monoid under `+` and a monoid under `*`, with distributivity. No subtraction.
pub trait SemiRing:
Add<Self, Output = Self>
+ AddAssign<Self>
+ Mul<Self, Output = Self>
+ MulAssign<Self>
+ Clone
+ One
+ Zero
{
}
impl<T> SemiRing for T where
T: Add<Self, Output = Self>
+ AddAssign<Self>
+ Mul<Self, Output = Self>
+ MulAssign<Self>
+ Clone
+ One
+ Zero
{
}
/// A `SemiRing` that also supports subtraction and negation.
pub trait Ring: SemiRing + Sub<Self, Output = Self> + SubAssign<Self> + Neg<Output = Self> {}
impl<T> Ring for T where
T: SemiRing + Sub<Self, Output = Self> + SubAssign<Self> + Neg<Output = Self>
{
}
/// A minimal field-like trait for the exact linear algebra used by the
/// Hochschild complex implementation.
///
/// This intentionally stays separate from `Ring` because Gaussian elimination
/// needs distinguished `0`, `1`, and multiplicative inverses for nonzero pivots.
pub trait Field: Ring + PartialEq {
#[must_use = "division is hard, don't waste it"]
fn inv(self) -> Self;
}
impl<T> Field for T
where
T: Ring + PartialEq + Div<Self, Output = Self>,
{
fn inv(self) -> Self {
Self::one() / self
}
}
#[allow(clippy::needless_range_loop)]
#[must_use = "Use the rank of each differential to determine rank of cohomology vector spaces"]
pub fn rank<Scalar>(matrix: &[Vec<Scalar>]) -> usize
where
Scalar: Field,
{
if matrix.is_empty() {
return 0;
}
let mut mat = matrix.to_vec();
let m = mat.len();
let n = mat[0].len();
let mut r = 0;
let mut c = 0;
while r < m && c < n {
let pivot = (r..m).find(|&i| !mat[i][c].is_zero());
let Some(pivot_row) = pivot else {
c += 1;
continue;
};
if pivot_row != r {
mat.swap(r, pivot_row);
}
let inv = mat[r][c].clone().inv();
for j in c..n {
mat[r][j] *= inv.clone();
}
for i in 0..m {
if i == r || mat[i][c].is_zero() {
continue;
}
let lambda = mat[i][c].clone();
for j in c..n {
let correction = lambda.clone() * mat[r][j].clone();
mat[i][j] -= correction;
}
}
r += 1;
c += 1;
}
r
}