moyo 0.10.0

Library for Crystal Symmetry in Rust
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
use itertools::Itertools;
use nalgebra::allocator::Allocator;
use nalgebra::{
    DMatrix, DVector, DefaultAllocator, Dim, DimName, Matrix2, Matrix3, OMatrix, OVector, U3,
    Vector3,
};

use super::cycle_checker::CycleChecker;
use super::elementary::swapping_column_matrix;

const EPS: f64 = 1e-8;

/// basis is column-wise
/// If the basis is already Minkowski reduced, return the input basis and identity matrix
pub fn minkowski_reduce(basis: &Matrix3<f64>) -> (Matrix3<f64>, Matrix3<i32>) {
    if is_minkowski_reduced(basis) {
        return (*basis, Matrix3::<i32>::identity());
    }

    let mut reduced_basis = *basis;
    let mut trans_mat = Matrix3::<i32>::identity();
    minkowski_reduce_greedy(U3, &mut reduced_basis, &mut trans_mat, 3);

    // Preserve parity
    if trans_mat.map(|e| e as f64).determinant() < 0. {
        reduced_basis *= -1.;
        trans_mat *= -1;
    }

    (reduced_basis, trans_mat)
}

/// Implement Fig.3 in [1]
/// basis * trans_mat is always preserved
///
/// [1] https://royalsocietypublishing.org/doi/abs/10.1098/rspa.1992.0004
fn minkowski_reduce_greedy<N: Dim + DimName>(
    dim: N,
    basis: &mut OMatrix<f64, N, N>,
    trans_mat: &mut OMatrix<i32, N, N>,
    rank: usize,
) where
    DefaultAllocator: Allocator<N, N> + Allocator<N, N> + Allocator<N>,
{
    // Line 1: exit condition
    if rank == 1 {
        return;
    }

    let mut cc = CycleChecker::new();
    loop {
        // Line 3: sort basis vectors by their lengths
        let lengths: Vec<f64> = basis.column_iter().map(|column| column.norm()).collect();
        for i in 0..rank {
            for j in 0..(rank - 1 - i) {
                if lengths[j] > lengths[j + 1] + EPS {
                    basis.swap_columns(j, j + 1);
                    *trans_mat *= swapping_column_matrix(dim, j, j + 1);
                }
            }
        }

        // Line 4: Recursive call
        minkowski_reduce_greedy(dim, basis, trans_mat, rank - 1);

        // Line 5: Solve the closest vector problem (CVP) for basis.column(d - 1)
        // linear projection (Gram-Schmidt)
        let h = DMatrix::from_fn(rank - 1, rank - 1, |i, j| {
            basis.column(i).dot(&basis.column(j)) / basis.column(i).norm_squared()
        });
        let u = DVector::from_fn(rank - 1, |i, _| {
            basis.column(i).dot(&basis.column(rank - 1)) / basis.column(i).norm_squared()
        });
        let gs_coeffs = h.try_inverse().unwrap() * u;
        let gs_coeffs_rint = DVector::from_fn(rank - 1, |i, _| gs_coeffs[(i, 0)].round() as i32);

        // Since basis[0..(rank-1)] are already Minkowski reduced, we only need to check Voronoi relevant vectors from y_int
        let mut cvp_min = f64::INFINITY;
        let mut coeffs_argmin = DVector::<i32>::zeros(rank - 1);
        let mut c_argmin = OVector::<f64, N>::zeros();

        // For up to three dimensions, `[-1, 0, 1]` is sufficient once
        // `basis[0..(rank-1)]` is already Minkowski reduced, so this bounded
        // integer search is exact under that precondition.
        for voronoi_vector in (0..rank - 1).map(|_| -1..=1).multi_cartesian_product() {
            let coeffs = gs_coeffs_rint.clone() + DVector::from_iterator(rank - 1, voronoi_vector);
            let c = basis.columns(0, rank - 1) * coeffs.map(|e| e as f64);
            let cvp = (c.clone() - basis.column(rank - 1)).norm();
            if cvp < cvp_min {
                cvp_min = cvp;
                coeffs_argmin = coeffs.clone();
                c_argmin = c;
            }
        }

        // Line 6: update basis.column(rank - 1)
        for j in 0..3 {
            basis[(j, rank - 1)] -= c_argmin[j];
        }
        let mut add_mat = OMatrix::<i32, N, N>::identity();
        for i in 0..(rank - 1) {
            add_mat[(i, rank - 1)] = -coeffs_argmin[i];
        }
        *trans_mat *= add_mat;

        // Line 7: loop until length ordering is changed
        if basis.column(rank - 1).norm() + EPS > basis.column(rank - 2).norm() {
            break;
        }

        // If the new basis is already visited, stop the loop
        if !cc.insert(trans_mat) {
            break;
        }
    }
}

/// basis is column-wise
pub fn is_minkowski_reduced(basis: &Matrix3<f64>) -> bool {
    let norms = basis.column_iter().map(|v| v.norm()).collect_vec();

    // Check ordering: norms[0] <= norms[1] <= norms[2]
    if norms[0] > norms[1] + EPS {
        return false;
    }
    if norms[1] > norms[2] + EPS {
        return false;
    }

    // Check for norms[1]
    for coeffs in [[1, -1, 0], [1, 1, 0]] {
        let v = basis * Vector3::new(coeffs[0] as f64, coeffs[1] as f64, coeffs[2] as f64);
        if v.norm() + EPS < norms[1] {
            return false;
        }
    }

    // Check for norms[2]
    for coeffs in [
        [1, 0, 1],
        [1, 0, -1],
        [0, 1, 1],
        [0, 1, -1],
        [1, -1, -1],
        [1, -1, 1],
        [1, 1, -1],
        [1, 1, 1],
    ] {
        let v = basis * Vector3::new(coeffs[0] as f64, coeffs[1] as f64, coeffs[2] as f64);
        if v.norm() + EPS < norms[2] {
            return false;
        }
    }

    true
}

// Lagrange-Gauss provably terminates in O(log(max norm)) steps; the bound is
// only a defensive guard against pathological f64 inputs.
const MINKOWSKI_2D_MAX_ITERATIONS: usize = 1024;

/// Lagrange-Gauss reduction of a 2D basis (column-vector convention).
/// Returns the reduced basis and the unimodular transformation `T` such that
/// `basis * T == reduced` (exact under integer linear combinations of input columns).
/// Parity is preserved (det(T) > 0). If the input is already reduced, returns
/// `(basis, identity)`.
#[allow(dead_code)]
pub fn minkowski_reduce_2d(basis: &Matrix2<f64>) -> (Matrix2<f64>, Matrix2<i32>) {
    if is_minkowski_reduced_2d(basis) {
        return (*basis, Matrix2::<i32>::identity());
    }

    let mut b = *basis;
    let mut t = Matrix2::<i32>::identity();

    for _ in 0..MINKOWSKI_2D_MAX_ITERATIONS {
        if b.column(0).norm() > b.column(1).norm() + EPS {
            b.swap_columns(0, 1);
            t = t * Matrix2::new(0, 1, 1, 0);
        }

        let denom = b.column(0).norm_squared();
        if denom < EPS {
            break;
        }
        let m = (b.column(0).dot(&b.column(1)) / denom).round() as i32;
        if m == 0 {
            break;
        }

        let new_b2 = b.column(1) - (m as f64) * b.column(0);
        b.set_column(1, &new_b2);
        t = t * Matrix2::new(1, -m, 0, 1);
    }

    // Preserve parity: det(T) > 0. In 2D, negating both columns leaves det unchanged,
    // so flip a single column instead (negate b2 / second column of T).
    if t.map(|e| e as f64).determinant() < 0. {
        let new_b2 = -b.column(1);
        b.set_column(1, &new_b2);
        t = t * Matrix2::new(1, 0, 0, -1);
    }

    (b, t)
}

/// Lift a 2x2 unimodular transformation (acting on the in-plane axes) to a 3x3
/// transformation that leaves the third axis untouched. Used to compose the
/// in-plane reduction with the layer-group block form W_33 = +/- 1 (paper eq. 4).
#[allow(dead_code)]
pub fn lift_2d_to_3d(t: &Matrix2<i32>) -> Matrix3<i32> {
    Matrix3::new(t[(0, 0)], t[(0, 1)], 0, t[(1, 0)], t[(1, 1)], 0, 0, 0, 1)
}

/// Returns true iff the 2D basis is Minkowski reduced
/// (`|b1| <= |b2|` and `|2 b1 . b2| <= |b1|^2`).
#[allow(dead_code)]
pub fn is_minkowski_reduced_2d(basis: &Matrix2<f64>) -> bool {
    let n1 = basis.column(0).norm();
    let n2 = basis.column(1).norm();
    if n1 > n2 + EPS {
        return false;
    }
    let dot12 = basis.column(0).dot(&basis.column(1));
    if 2.0 * dot12.abs() > basis.column(0).norm_squared() + EPS {
        return false;
    }
    true
}

#[cfg(test)]
mod tests {
    use nalgebra::{Matrix2, Matrix3, Vector2, Vector3};
    use rand::SeedableRng;
    use rand::prelude::*;
    use rand::rngs::StdRng;

    use super::{
        is_minkowski_reduced, is_minkowski_reduced_2d, lift_2d_to_3d, minkowski_reduce,
        minkowski_reduce_2d,
    };

    #[test]
    fn test_is_minkowski_reduced() {
        let basis = Matrix3::<f64>::from_columns(&[
            Vector3::new(1.0, 0.0, 0.0),
            Vector3::new(0.0, 1.0, 0.0),
            Vector3::new(0.0, 0.0, 1.0),
        ]);
        assert!(is_minkowski_reduced(&basis));

        let basis = Matrix3::<f64>::from_columns(&[
            Vector3::new(0.0, 1.0, 0.0),
            Vector3::new(1.0, 1.0, 0.0),
            Vector3::new(1.0, 1.0, 1.0),
        ]);
        assert!(!is_minkowski_reduced(&basis));
    }

    #[test]
    fn test_minkowski_reduction_small() {
        let basis = Matrix3::<f64>::from_columns(&[
            Vector3::new(1.0, 0.0, 0.0),
            Vector3::new(0.0, 1.0, 0.0),
            Vector3::new(0.0, 0.0, 1.0),
        ]);
        let (reduced_basis, trans_mat) = minkowski_reduce(&basis);
        assert_relative_eq!(reduced_basis, basis);
        assert_eq!(trans_mat, Matrix3::<i32>::identity());

        let basis = Matrix3::<f64>::from_columns(&[
            Vector3::new(0.0, 1.0, 0.0),
            Vector3::new(1.0, 1.0, 0.0),
            Vector3::new(1.0, 1.0, 1.0),
        ]);
        let (reduced_basis, _) = minkowski_reduce(&basis);
        assert_relative_eq!(
            reduced_basis,
            Matrix3::<f64>::from_columns(&[
                Vector3::new(0.0, 1.0, 0.0),
                Vector3::new(1.0, 0.0, 0.0),
                Vector3::new(0.0, 0.0, 1.0),
            ])
        );

        let basis = Matrix3::<f64>::from_columns(&[
            Vector3::new(-5.0, -10.0, 17.0),
            Vector3::new(17.0, 24.0, 12.0),
            Vector3::new(-127.0, 73.0, 5.0),
        ]);
        let _ = minkowski_reduce(&basis);
    }

    #[test]
    fn test_minkowski_reduction_random() {
        let mut rng: StdRng = SeedableRng::from_seed([0; 32]);

        for _ in 0..256 {
            let basis = Matrix3::<f64>::new(
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
            );
            let (reduced_basis, trans_mat) = minkowski_reduce(&basis);
            assert!(is_minkowski_reduced(&reduced_basis));
            assert_relative_eq!(basis * trans_mat.map(|e| e as f64), reduced_basis);
        }

        for _ in 0..256 {
            let basis = Matrix3::<f64>::new(
                rng.random(),
                rng.random(),
                rng.random(),
                rng.random(),
                rng.random(),
                rng.random(),
                rng.random(),
                rng.random(),
                rng.random(),
            );
            let (reduced_basis, trans_mat) = minkowski_reduce(&basis);
            assert!(is_minkowski_reduced(&reduced_basis));
            assert_relative_eq!(
                basis * trans_mat.map(|e| e as f64),
                reduced_basis,
                epsilon = 1e-4
            );
        }
    }

    #[test]
    fn test_minkowski_idempotent() {
        let basis = Matrix3::<f64>::from_columns(&[
            Vector3::new(1.0, 0.0, 0.0),
            Vector3::new(-0.5, 3.0_f64.sqrt() / 2.0, 0.0),
            Vector3::new(0.0, 0.0, 10.0),
        ]);
        assert!(is_minkowski_reduced(&basis));

        let (reduced_basis, _) = minkowski_reduce(&basis);
        assert_relative_eq!(reduced_basis, basis);
    }

    #[test]
    fn test_minkowski_2d_basic() {
        let basis = Matrix2::<f64>::from_columns(&[Vector2::new(1.0, 0.0), Vector2::new(0.0, 1.0)]);
        let (reduced, t) = minkowski_reduce_2d(&basis);
        assert!(is_minkowski_reduced_2d(&reduced));
        assert_relative_eq!(reduced, basis);
        assert_eq!(t, Matrix2::<i32>::identity());

        let basis = Matrix2::<f64>::from_columns(&[Vector2::new(1.0, 0.0), Vector2::new(5.0, 1.0)]);
        let (reduced, t) = minkowski_reduce_2d(&basis);
        assert!(is_minkowski_reduced_2d(&reduced));
        assert_relative_eq!(reduced, basis * t.map(|e| e as f64));
    }

    #[test]
    fn test_minkowski_2d_random() {
        let mut rng: StdRng = SeedableRng::from_seed([1; 32]);

        for _ in 0..512 {
            let basis = Matrix2::<f64>::new(
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
                rng.random::<i8>() as f64,
            );
            // Skip degenerate (rank-deficient) cases where reduction is undefined.
            if basis.determinant().abs() < 1e-6 {
                continue;
            }
            let (reduced, t) = minkowski_reduce_2d(&basis);
            assert!(is_minkowski_reduced_2d(&reduced));
            assert_relative_eq!(basis * t.map(|e| e as f64), reduced);
            // Idempotence.
            let (reduced2, t2) = minkowski_reduce_2d(&reduced);
            assert_relative_eq!(reduced2, reduced);
            assert_eq!(t2, Matrix2::<i32>::identity());
            // Parity.
            assert!(t.map(|e| e as f64).determinant() > 0.0);
        }
    }

    #[test]
    fn test_minkowski_2d_swap_invariance() {
        let basis = Matrix2::<f64>::from_columns(&[Vector2::new(3.0, 0.0), Vector2::new(1.0, 2.0)]);
        let (r1, _) = minkowski_reduce_2d(&basis);

        let mut swapped = basis;
        swapped.swap_columns(0, 1);
        let (r2, _) = minkowski_reduce_2d(&swapped);
        // Reduced bases agree up to column swap and column sign flip.
        let mut equal_pair = false;
        for s0 in [-1.0_f64, 1.0] {
            for s1 in [-1.0_f64, 1.0] {
                let cand = Matrix2::from_columns(&[s0 * r2.column(0), s1 * r2.column(1)]);
                if (cand - r1).norm() < 1e-6 {
                    equal_pair = true;
                }
                let cand_swap = Matrix2::from_columns(&[s0 * r2.column(1), s1 * r2.column(0)]);
                if (cand_swap - r1).norm() < 1e-6 {
                    equal_pair = true;
                }
            }
        }
        assert!(equal_pair);
    }

    #[test]
    fn test_lift_2d_to_3d() {
        let t = Matrix2::<i32>::new(2, -1, 1, 0);
        let lifted = lift_2d_to_3d(&t);
        assert_eq!(lifted, Matrix3::<i32>::new(2, -1, 0, 1, 0, 0, 0, 0, 1));
    }
}