auslander 0.9.0

Computational representation theory of finite-dimensional bound quiver algebras
Documentation
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use super::dense::DenseMat;
use super::merge_scaled_terms;
use crate::field::{Fp, PrimeField};
use crate::profile::{Site, hit};

/// Sparse vector: (column, value) pairs sorted by column, values nonzero.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SparseRow {
    pub(in crate::linalg) entries: Vec<(usize, Fp)>,
}

impl SparseRow {
    /// The zero vector.
    pub fn new() -> SparseRow {
        SparseRow::default()
    }

    /// A vector from arbitrary (column, value) pairs; duplicate columns are
    /// summed and zero results dropped.
    pub fn from_entries(mut entries: Vec<(usize, Fp)>, f: &PrimeField) -> SparseRow {
        entries.sort_unstable_by_key(|&(c, _)| c);
        let mut merged: Vec<(usize, Fp)> = Vec::with_capacity(entries.len());
        for (col, val) in entries {
            if val.is_zero() {
                continue;
            }
            if let Some(&mut (last_col, ref mut last_val)) = merged.last_mut()
                && last_col == col
            {
                *last_val = f.add(*last_val, val);
                if last_val.is_zero() {
                    merged.pop();
                }
                continue;
            }
            merged.push((col, val));
        }
        SparseRow { entries: merged }
    }

    accessor_methods! {
        /// The nonzero entries, sorted by column.
        pub entries() -> &[(usize, Fp)] = |this| &this.entries;
        /// The value at `col`, zero when absent.
        pub get(col: usize) -> Fp = |this| this.entries
            .binary_search_by_key(&col, |&(c, _)| c)
            .map_or(Fp::ZERO, |i| this.entries[i].1);
    }

    /// Sets the value at `col`; a zero value removes the entry.
    pub fn set(&mut self, col: usize, val: Fp) {
        match self.entries.binary_search_by_key(&col, |&(c, _)| c) {
            Ok(i) => {
                if val.is_zero() {
                    self.entries.remove(i);
                } else {
                    self.entries[i].1 = val;
                }
            }
            Err(i) => {
                if !val.is_zero() {
                    self.entries.insert(i, (col, val));
                }
            }
        }
    }

    accessor_methods! {
        /// Whether the vector is zero.
        pub is_zero() -> bool = |this| this.entries.is_empty();
        /// Number of nonzero entries.
        pub nnz() -> usize = |this| this.entries.len();
        leading() -> Option<(usize, Fp)> = |this| this.entries.first().copied();
    }

    /// Multiplies every entry by `c`; `c = 0` clears the vector.
    pub fn scale(&mut self, c: Fp, f: &PrimeField) {
        if c.is_zero() {
            self.entries.clear();
            return;
        }
        // c is a unit, so no product can vanish.
        for (_, v) in &mut self.entries {
            *v = f.mul(*v, c);
        }
    }

    fn normalize(&mut self, f: &PrimeField) {
        if let Some((_, lc)) = self.leading()
            && lc.raw() != 1
        {
            let inv = f.inv(lc);
            self.scale(inv, f);
        }
    }

    /// Adds `c * other` to `self`, merging the two sorted entry lists.
    ///
    /// Allocates one buffer per call. In a loop, use
    /// [`SparseRow::add_scaled_into`] with a buffer you own.
    pub fn add_scaled(&mut self, other: &SparseRow, c: Fp, f: &PrimeField) {
        self.add_scaled_into(other, c, f, &mut Vec::new());
    }

    /// Adds `c * other` to `self`, merging through `scratch`.
    ///
    /// `scratch` is cleared on entry and holds the old entries of `self` on
    /// return, so a loop that passes the same buffer reuses its capacity and
    /// allocates once instead of once per call. The result matches
    /// [`SparseRow::add_scaled`] entry for entry.
    pub fn add_scaled_into(
        &mut self,
        other: &SparseRow,
        c: Fp,
        f: &PrimeField,
        scratch: &mut Vec<(usize, Fp)>,
    ) {
        merge_scaled_terms(
            &self.entries,
            &other.entries,
            (c, f),
            |left, right| left.0.cmp(&right.0),
            |term| term.1,
            |term, value| (term.0, value),
            scratch,
        );
        std::mem::swap(&mut self.entries, scratch);
    }

    /// The dot product with `other`.
    pub fn dot(&self, other: &SparseRow, f: &PrimeField) -> Fp {
        let mut i = 0;
        let mut j = 0;
        let mut acc = Fp::ZERO;
        while i < self.entries.len() && j < other.entries.len() {
            let (col_a, val_a) = self.entries[i];
            let (col_b, val_b) = other.entries[j];
            match col_a.cmp(&col_b) {
                std::cmp::Ordering::Less => i += 1,
                std::cmp::Ordering::Greater => j += 1,
                std::cmp::Ordering::Equal => {
                    acc = f.add(acc, f.mul(val_a, val_b));
                    i += 1;
                    j += 1;
                }
            }
        }
        acc
    }
}

/// Sparse matrix: one sorted row per matrix row.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SparseMat {
    pub(in crate::linalg) rows: usize,
    pub(in crate::linalg) cols: usize,
    pub(in crate::linalg) data: Vec<SparseRow>,
}

impl SparseMat {
    /// The `rows x cols` zero matrix.
    pub fn zero(rows: usize, cols: usize) -> SparseMat {
        SparseMat {
            rows,
            cols,
            data: vec![SparseRow::new(); rows],
        }
    }

    /// A matrix with the given rows and `cols` columns.
    ///
    /// # Panics
    /// Panics if any entry's column index is `>= cols`.
    pub fn from_rows(rows: Vec<SparseRow>, cols: usize) -> SparseMat {
        for row in &rows {
            assert!(
                row.entries.last().is_none_or(|&(c, _)| c < cols),
                "from_rows: column index out of range"
            );
        }
        SparseMat {
            rows: rows.len(),
            cols,
            data: rows,
        }
    }

    accessor_methods! {
        /// Number of rows.
        pub rows() -> usize = |this| this.rows;
        /// Number of columns.
        pub cols() -> usize = |this| this.cols;
        /// Row `r`.
        ///
        /// # Panics
        /// Panics if `r` is out of range.
        pub row(r: usize) -> &SparseRow = |this| &this.data[r];
    }

    /// The transpose.
    pub fn transpose(&self) -> SparseMat {
        let mut data = vec![SparseRow::new(); self.cols];
        // Source rows are visited in increasing order, so each output row is
        // built already sorted.
        for (r, row) in self.data.iter().enumerate() {
            for &(c, v) in &row.entries {
                data[c].entries.push((r, v));
            }
        }
        SparseMat {
            rows: self.cols,
            cols: self.rows,
            data,
        }
    }

    /// The product `self * rhs`.
    ///
    /// # Panics
    /// Panics unless `self.cols() == rhs.rows()`.
    pub fn mul(&self, rhs: &SparseMat, f: &PrimeField) -> SparseMat {
        hit(Site::SparseMul);
        assert_eq!(
            self.cols, rhs.rows,
            "mul: {}x{} times {}x{}",
            self.rows, self.cols, rhs.rows, rhs.cols
        );
        let mut scratch = Vec::new();
        let data = self
            .data
            .iter()
            .map(|lhs_row| {
                let mut row = SparseRow::new();
                for &(c, v) in &lhs_row.entries {
                    row.add_scaled_into(&rhs.data[c], v, f, &mut scratch);
                }
                row
            })
            .collect();
        SparseMat {
            rows: self.rows,
            cols: rhs.cols,
            data,
        }
    }

    /// `A x` for a sparse vector `x` indexed by column; the result is indexed
    /// by row.
    ///
    /// # Panics
    /// Panics if `x` has an index `>= self.cols()`.
    pub fn mul_vec(&self, x: &SparseRow, f: &PrimeField) -> SparseRow {
        assert!(
            x.entries.last().is_none_or(|&(c, _)| c < self.cols),
            "mul_vec: index out of range"
        );
        let entries = self
            .data
            .iter()
            .enumerate()
            .filter_map(|(r, row)| {
                let v = row.dot(x, f);
                (!v.is_zero()).then_some((r, v))
            })
            .collect();
        SparseRow { entries }
    }

    // Markowitz pivoting: among rows leading in the current column, the one
    // with the fewest entries becomes the pivot. The pivot row is normalized
    // first, so each elimination factor is the negated leading coefficient.
    fn echelon_in_place(&mut self, f: &PrimeField) -> Vec<usize> {
        hit(Site::SparseEchelon);
        let mut pivots = Vec::new();
        let mut scratch = Vec::new();
        let mut pr = 0;
        for col in 0..self.cols {
            if pr == self.rows {
                break;
            }
            let Some(idx) = (pr..self.rows)
                .filter(|&r| self.data[r].leading().is_some_and(|(c, _)| c == col))
                .min_by_key(|&r| self.data[r].nnz())
            else {
                continue;
            };
            self.data.swap(pr, idx);
            self.data[pr].normalize(f);
            let (head, tail) = self.data.split_at_mut(pr + 1);
            let pivot = &head[pr];
            for row in tail {
                if let Some((c, coeff)) = row.leading()
                    && c == col
                {
                    row.add_scaled_into(pivot, f.neg(coeff), f, &mut scratch);
                }
            }
            pivots.push(col);
            pr += 1;
        }
        pivots
    }

    /// The reduced row echelon form and its pivot columns.
    ///
    /// Pivot rows come first in pivot-column order; zero rows follow.
    pub fn rref(&self, f: &PrimeField) -> (SparseMat, Vec<usize>) {
        self.clone().into_rref(f)
    }

    /// [`SparseMat::rref`] on an owned matrix, reduced in place.
    pub(crate) fn into_rref(mut self, f: &PrimeField) -> (SparseMat, Vec<usize>) {
        hit(Site::SparseRref);
        let pivots = self.echelon_in_place(f);
        let mut scratch = Vec::new();
        for i in (0..pivots.len()).rev() {
            let col = pivots[i];
            let (above, rest) = self.data.split_at_mut(i);
            let pivot = &rest[0];
            for row in above {
                let v = row.get(col);
                if !v.is_zero() {
                    row.add_scaled_into(pivot, f.neg(v), f, &mut scratch);
                }
            }
        }
        (self, pivots)
    }

    /// The dimension of the row space, which equals that of the column space.
    pub fn rank(&self, f: &PrimeField) -> usize {
        self.clone().into_rank(f)
    }

    /// [`SparseMat::rank`] on an owned matrix, eliminating in place.
    pub(crate) fn into_rank(mut self, f: &PrimeField) -> usize {
        self.echelon_in_place(f).len()
    }

    /// A basis of the right null space {x : A x = 0}, one vector per row of the
    /// result; the result has `self.cols()` columns and `cols - rank` rows.
    ///
    /// Rows come in the same order as [`DenseMat::kernel_basis`]: one row per
    /// free column of the reduced row echelon form, free columns increasing.
    /// [`crate::hom::hom`] builds its Hom basis from this order.
    pub fn kernel_basis(&self, f: &PrimeField) -> SparseMat {
        self.clone().into_kernel_basis(f)
    }

    /// [`SparseMat::kernel_basis`] on an owned matrix, reduced in place.
    pub(crate) fn into_kernel_basis(self, f: &PrimeField) -> SparseMat {
        hit(Site::SparseKernelBasis);
        let cols = self.cols;
        let (m, pivots) = self.into_rref(f);
        // Column to its index among the free columns, or none for a pivot.
        let mut slot = vec![None; cols];
        let mut free = Vec::new();
        for (c, s) in slot.iter_mut().enumerate() {
            if pivots.binary_search(&c).is_err() {
                *s = Some(free.len());
                free.push(c);
            }
        }
        let mut data = vec![SparseRow::new(); free.len()];
        // Each reduced row is walked once, and the pivot columns increase with
        // the row index, so every output row comes out sorted.
        for (i, &pc) in pivots.iter().enumerate() {
            for &(c, v) in &m.data[i].entries {
                if let Some(k) = slot[c] {
                    data[k].entries.push((pc, f.neg(v)));
                }
            }
        }
        for (k, &fc) in free.iter().enumerate() {
            let at = data[k].entries.partition_point(|&(c, _)| c < fc);
            data[k].entries.insert(at, (fc, Fp::ONE));
        }
        SparseMat {
            rows: data.len(),
            cols,
            data,
        }
    }

    /// A basis of the left null space {x : x A = 0}, one vector per row of the
    /// result (each of length `self.rows()`): the kernel basis of the
    /// transpose, row for row.
    pub fn left_kernel_basis(&self, f: &PrimeField) -> SparseMat {
        self.transpose().into_kernel_basis(f)
    }

    /// A basis of the row space: the nonzero rows of the reduced row echelon
    /// form, one vector per row of the result.
    pub fn row_space_basis(&self, f: &PrimeField) -> SparseMat {
        self.clone().into_row_space_basis(f)
    }

    /// [`SparseMat::row_space_basis`] on an owned matrix, reduced in place.
    pub(crate) fn into_row_space_basis(self, f: &PrimeField) -> SparseMat {
        let (mut m, pivots) = self.into_rref(f);
        m.data.truncate(pivots.len());
        m.rows = pivots.len();
        m
    }

    /// A basis of the column space, one vector per row of the result (each of
    /// length `self.rows()`): the row-space basis of the transpose.
    pub fn image_basis(&self, f: &PrimeField) -> SparseMat {
        self.transpose().into_row_space_basis(f)
    }

    /// A solution of `A x = b` with free variables set to zero, or `None` when
    /// the system is inconsistent; `b` is indexed by row. Matches
    /// [`DenseMat::solve`] entry for entry.
    ///
    /// # Panics
    /// Panics if `b` has an index `>= self.rows()`.
    pub fn solve(&self, b: &SparseRow, f: &PrimeField) -> Option<SparseRow> {
        hit(Site::SparseSolve);
        assert!(
            b.entries.last().is_none_or(|&(r, _)| r < self.rows),
            "solve: rhs index vs {} rows",
            self.rows
        );
        let mut aug_rows = Vec::with_capacity(self.rows);
        for (r, row) in self.data.iter().enumerate() {
            let mut aug = row.clone();
            let bv = b.get(r);
            if !bv.is_zero() {
                aug.entries.push((self.cols, bv));
            }
            aug_rows.push(aug);
        }
        let aug = SparseMat {
            rows: self.rows,
            cols: self.cols + 1,
            data: aug_rows,
        };
        let (m, pivots) = aug.into_rref(f);
        if pivots.last() == Some(&self.cols) {
            return None;
        }
        let mut x = SparseRow::new();
        for (i, &pc) in pivots.iter().enumerate() {
            let v = m.data[i].get(self.cols);
            if !v.is_zero() {
                x.set(pc, v);
            }
        }
        Some(x)
    }

    /// The same matrix in dense form.
    pub fn to_dense(&self) -> DenseMat {
        let mut out = DenseMat::zero(self.rows, self.cols);
        for (r, row) in self.data.iter().enumerate() {
            for &(c, v) in &row.entries {
                out.set(r, c, v);
            }
        }
        out
    }
}