|
| 1 | +//! Inverting an affine map on a box without scanning the box: finding every |
| 2 | +//! index `x` with `basis·x = c` and `0 <= x_j < extents[j]`. |
| 3 | +//! |
| 4 | +//! Integer column operations of determinant ±1 bring the basis to |
| 5 | +//! `basis·[particular | kernel] = [h | 0]`, where `h` is a staircase of full |
| 6 | +//! column rank. Every integer solution is then `x = particular·z + kernel·t`, |
| 7 | +//! exactly: `z` is unique and found by substitution down the staircase of `h`, |
| 8 | +//! and `t` ranges over the integer kernel. The kernel's basis is brought to a |
| 9 | +//! staircase too, so that each coordinate of `t` in turn gets an exact range |
| 10 | +//! from the extents. |
| 11 | +
|
| 12 | +/// The indices of a box that `x ↦ basis·x` sends to a given point. |
| 13 | +pub(crate) struct Preimage { |
| 14 | + h: Vec<Vec<i64>>, |
| 15 | + h_pivots: Vec<usize>, |
| 16 | + particular: Vec<Vec<i64>>, |
| 17 | + kernel: Vec<Vec<i64>>, |
| 18 | + kernel_pivots: Vec<usize>, |
| 19 | + extents: Vec<i64>, |
| 20 | +} |
| 21 | + |
| 22 | +impl Preimage { |
| 23 | + pub(crate) fn new(basis: &[Vec<i64>], extents: &[usize]) -> Self { |
| 24 | + let n = extents.len(); |
| 25 | + let (echelon, operations, h_pivots) = column_echelon(basis.to_vec(), n); |
| 26 | + let r = h_pivots.len(); |
| 27 | + let h = echelon.iter().map(|row| row[..r].to_vec()).collect(); |
| 28 | + let particular = operations.iter().map(|row| row[..r].to_vec()).collect(); |
| 29 | + let free = operations.iter().map(|row| row[r..].to_vec()).collect(); |
| 30 | + let (kernel, _, kernel_pivots) = column_echelon(free, n - r); |
| 31 | + Self { |
| 32 | + h, |
| 33 | + h_pivots, |
| 34 | + particular, |
| 35 | + kernel, |
| 36 | + kernel_pivots, |
| 37 | + extents: extents.iter().map(|&extent| extent as i64).collect(), |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// Every index `x` of the box with `basis·x = c`. |
| 42 | + pub(crate) fn solve(&self, c: &[i64]) -> Vec<Vec<usize>> { |
| 43 | + let mut z = vec![0; self.h_pivots.len()]; |
| 44 | + for (i, &row) in self.h_pivots.iter().enumerate() { |
| 45 | + let rest = c[row] - dot(&self.h[row][..i], &z[..i]); |
| 46 | + let step = self.h[row][i]; |
| 47 | + if rest % step != 0 { |
| 48 | + return Vec::new(); |
| 49 | + } |
| 50 | + z[i] = rest / step; |
| 51 | + } |
| 52 | + // The rows between pivots must hold too. |
| 53 | + if self |
| 54 | + .h |
| 55 | + .iter() |
| 56 | + .zip(c) |
| 57 | + .any(|(row, &target)| dot(row, &z) != target) |
| 58 | + { |
| 59 | + return Vec::new(); |
| 60 | + } |
| 61 | + let mut x: Vec<i64> = self.particular.iter().map(|row| dot(row, &z)).collect(); |
| 62 | + // Coordinates above the kernel's first pivot are already final. |
| 63 | + let fixed = self.kernel_pivots.first().copied().unwrap_or(x.len()); |
| 64 | + if !(0..fixed).all(|j| (0..self.extents[j]).contains(&x[j])) { |
| 65 | + return Vec::new(); |
| 66 | + } |
| 67 | + let mut found = Vec::new(); |
| 68 | + self.enumerate(0, &mut x, &mut found); |
| 69 | + found |
| 70 | + } |
| 71 | + |
| 72 | + /// Tries every value of `t[level]` that keeps the coordinates it settles in |
| 73 | + /// range, these being the rows from its pivot to the next one. |
| 74 | + fn enumerate(&self, level: usize, x: &mut [i64], found: &mut Vec<Vec<usize>>) { |
| 75 | + if level == self.kernel_pivots.len() { |
| 76 | + found.push(x.iter().map(|&coord| coord as usize).collect()); |
| 77 | + return; |
| 78 | + } |
| 79 | + let end = self |
| 80 | + .kernel_pivots |
| 81 | + .get(level + 1) |
| 82 | + .copied() |
| 83 | + .unwrap_or(x.len()); |
| 84 | + let rows = self.kernel_pivots[level]..end; |
| 85 | + let settled = self.kernel[rows.clone()] |
| 86 | + .iter() |
| 87 | + .zip(&x[rows.clone()]) |
| 88 | + .zip(&self.extents[rows]); |
| 89 | + let (mut low, mut high) = (i64::MIN, i64::MAX); |
| 90 | + for ((row, &coord), &extent) in settled { |
| 91 | + let (step, last) = (row[level], extent - 1); |
| 92 | + if step > 0 { |
| 93 | + low = low.max(ceil_div(-coord, step)); |
| 94 | + high = high.min(floor_div(last - coord, step)); |
| 95 | + } else if step < 0 { |
| 96 | + low = low.max(ceil_div(last - coord, step)); |
| 97 | + high = high.min(floor_div(-coord, step)); |
| 98 | + } else if !(0..=last).contains(&coord) { |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + for t in low..=high { |
| 103 | + for (coord, row) in x.iter_mut().zip(&self.kernel) { |
| 104 | + *coord += row[level] * t; |
| 105 | + } |
| 106 | + self.enumerate(level + 1, x, found); |
| 107 | + for (coord, row) in x.iter_mut().zip(&self.kernel) { |
| 108 | + *coord -= row[level] * t; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/// Brings `matrix` to column echelon form by integer column operations of |
| 115 | +/// determinant ±1. Returns the echelon form `e`, the operations `v` with |
| 116 | +/// `matrix·v = e`, and the pivot row of each column of `e` that is not zero. |
| 117 | +/// Those columns come first: each is zero above its pivot, and every column is |
| 118 | +/// zero to the right of a pivot. |
| 119 | +fn column_echelon( |
| 120 | + mut matrix: Vec<Vec<i64>>, |
| 121 | + cols: usize, |
| 122 | +) -> (Vec<Vec<i64>>, Vec<Vec<i64>>, Vec<usize>) { |
| 123 | + let mut operations: Vec<Vec<i64>> = (0..cols) |
| 124 | + .map(|i| (0..cols).map(|j| i64::from(i == j)).collect()) |
| 125 | + .collect(); |
| 126 | + let mut pivots = Vec::new(); |
| 127 | + for row in 0..matrix.len() { |
| 128 | + let c = pivots.len(); |
| 129 | + if c == cols { |
| 130 | + break; |
| 131 | + } |
| 132 | + // Folds every entry right of column `c` into it, leaving their gcd there. |
| 133 | + for j in c + 1..cols { |
| 134 | + let (a, b) = (matrix[row][c], matrix[row][j]); |
| 135 | + if b != 0 { |
| 136 | + let (g, s, t) = ext_gcd(a, b); |
| 137 | + let operation = (s, t, -b / g, a / g); |
| 138 | + column_op(&mut matrix, c, j, operation); |
| 139 | + column_op(&mut operations, c, j, operation); |
| 140 | + } |
| 141 | + } |
| 142 | + if matrix[row][c] != 0 { |
| 143 | + pivots.push(row); |
| 144 | + } |
| 145 | + } |
| 146 | + (matrix, operations, pivots) |
| 147 | +} |
| 148 | + |
| 149 | +/// Turns columns `c` and `j` into `s·c + t·j` and `u·c + w·j`. |
| 150 | +fn column_op(matrix: &mut [Vec<i64>], c: usize, j: usize, (s, t, u, w): (i64, i64, i64, i64)) { |
| 151 | + for row in matrix { |
| 152 | + let (x, y) = (row[c], row[j]); |
| 153 | + row[c] = s * x + t * y; |
| 154 | + row[j] = u * x + w * y; |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +/// `(g, s, t)` with `s·a + t·b = g`, where `g` is the gcd of `a` and `b`, and |
| 159 | +/// is not negative. |
| 160 | +fn ext_gcd(a: i64, b: i64) -> (i64, i64, i64) { |
| 161 | + if b == 0 { |
| 162 | + if a < 0 { (-a, -1, 0) } else { (a, 1, 0) } |
| 163 | + } else { |
| 164 | + let (g, s, t) = ext_gcd(b, a % b); |
| 165 | + (g, t, s - (a / b) * t) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +fn dot(a: &[i64], b: &[i64]) -> i64 { |
| 170 | + a.iter().zip(b).map(|(x, y)| x * y).sum() |
| 171 | +} |
| 172 | + |
| 173 | +fn floor_div(n: i64, d: i64) -> i64 { |
| 174 | + if d < 0 { |
| 175 | + (-n).div_euclid(-d) |
| 176 | + } else { |
| 177 | + n.div_euclid(d) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +fn ceil_div(n: i64, d: i64) -> i64 { |
| 182 | + -floor_div(-n, d) |
| 183 | +} |
0 commit comments