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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
//! Symmetry operations.

use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Mul;

use approx;
use derive_builder::Builder;
use fraction;
use nalgebra::{Point3, Vector3};
use ndarray::{Array2, Axis, ShapeBuilder};
use num_traits::{Inv, Pow, Zero};
use ordered_float::OrderedFloat;
use serde::{Deserialize, Serialize};

use crate::auxiliary::geometry::{
    self, improper_rotation_matrix, proper_rotation_matrix, PositiveHemisphere, Transform, IMINV,
};
use crate::auxiliary::misc::{self, HashableFloat};
use crate::group::FiniteOrder;
use crate::permutation::{IntoPermutation, PermutableCollection, Permutation};
use crate::symmetry::symmetry_element::{
    AntiunitaryKind, SymmetryElement, SymmetryElementKind, INV, ROT, SIG, SO3, SU2_0, SU2_1, TRINV,
    TRROT, TRSIG,
};
use crate::symmetry::symmetry_element_order::ElementOrder;

type F = fraction::GenericFraction<u32>;
type Quaternion = (f64, Vector3<f64>);

#[cfg(test)]
#[path = "symmetry_operation_tests.rs"]
mod symmetry_operation_tests;

// =================
// Trait definitions
// =================

/// Trait for special symmetry transformations.
pub trait SpecialSymmetryTransformation {
    // =================
    // Group-theoretical
    // =================

    /// Checks if the proper rotation part of the symmetry operation is in $`\mathsf{SU}(2)`$.
    ///
    /// # Returns
    ///
    /// A boolean indicating if this symmetry operation contains an $`\mathsf{SU}(2)`$ proper
    /// rotation.
    fn is_su2(&self) -> bool;

    /// Checks if the proper rotation part of the symmetry operation is in $`\mathsf{SU}(2)`$ and
    /// connected to the identity via a homotopy path of class 1.
    ///
    /// # Returns
    ///
    /// A boolean indicating if this symmetry operation contains an $`\mathsf{SU}(2)`$ proper
    /// rotation connected to the identity via a homotopy path of class 1.
    fn is_su2_class_1(&self) -> bool;

    // ============
    // Spatial part
    // ============

    /// Checks if the spatial part of the symmetry operation is proper.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the spatial part of the symmetry operation is proper.
    fn is_proper(&self) -> bool;

    /// Checks if the spatial part of the symmetry operation is the spatial identity.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the spatial part of the symmetry operation is the spatial identity.
    fn is_spatial_identity(&self) -> bool;

    /// Checks if the spatial part of the symmetry operation is a spatial binary rotation.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the spatial part of the symmetry operation is a spatial binary
    /// rotation.
    fn is_spatial_binary_rotation(&self) -> bool;

    /// Checks if the spatial part of the symmetry operation is the spatial inversion.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the spatial part of the symmetry operation is the spatial inversion.
    fn is_spatial_inversion(&self) -> bool;

    /// Checks if the spatial part of the symmetry operation is a spatial reflection.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the spatial part of the symmetry operation is a spatial reflection.
    fn is_spatial_reflection(&self) -> bool;

    // ==================
    // Time-reversal part
    // ==================

    /// Checks if the symmetry operation contains time reversal.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the symmetry oppperation contains time reversal.
    fn contains_time_reversal(&self) -> bool;

    // ==========================
    // Overall - provided methods
    // ==========================

    /// Checks if the symmetry operation is the identity in $`\mathsf{O}(3)`$, `E`, or
    /// in $`\mathsf{SU}(2)`$, `E(Σ)`.
    ///
    /// # Returns
    ///
    /// A boolean indicating if this symmetry operation is the identity.
    fn is_identity(&self) -> bool {
        self.is_spatial_identity() && !self.contains_time_reversal() && !self.is_su2_class_1()
    }

    /// Checks if the symmetry operation is a pure time-reversal in $`\mathsf{O}(3)`$, `θ`, or
    /// in $`\mathsf{SU}(2)`$, `θ(Σ)`.
    ///
    /// # Returns
    ///
    /// A boolean indicating if this symmetry operation is a pure time-reversal.
    fn is_time_reversal(&self) -> bool {
        self.is_spatial_identity() && self.contains_time_reversal() && !self.is_su2_class_1()
    }

    /// Checks if the symmetry operation is an inversion in $`\mathsf{O}(3)`$, `i`, but not in
    /// $`\mathsf{SU}(2)`$, `i(Σ)`.
    ///
    /// # Returns
    ///
    /// A boolean indicating if this symmetry operation is an inversion in $`\mathsf{O}(3)`$.
    fn is_inversion(&self) -> bool {
        self.is_spatial_inversion() && !self.contains_time_reversal() && !self.is_su2()
    }

    /// Checks if the symmetry operation is a reflection in $`\mathsf{O}(3)`$, `σ`, but not in
    /// $`\mathsf{SU}(2)`$, `σ(Σ)`.
    ///
    /// # Returns
    ///
    /// A boolean indicating if this symmetry operation is a reflection in $`\mathsf{O}(3)`$.
    fn is_reflection(&self) -> bool {
        self.is_spatial_reflection() && !self.contains_time_reversal() && !self.is_su2()
    }
}

// ======================================
// Struct definitions and implementations
// ======================================

/// Structure for managing symmetry operations generated from symmetry elements.
///
/// A symmetry element serves as a generator for symmetry operations. Thus, a symmetry element
/// together with a signed integer indicating the number of times the symmetry element is applied
/// (positively or negatively) specifies a symmetry operation.
#[derive(Builder, Clone, Serialize, Deserialize)]
pub struct SymmetryOperation {
    /// The generating symmetry element for this symmetry operation.
    pub generating_element: SymmetryElement,

    /// The integral power indicating the number of times
    /// [`Self::generating_element`] is applied to form the symmetry operation.
    pub power: i32,

    /// The total proper rotation angle associated with this operation (after taking into account
    /// the power of the operation).
    ///
    /// This is simply the proper rotation angle of [`Self::generating_element`] multiplied by
    /// [`Self::power`] and then folded onto the open interval $`(-\pi, \pi]`$.
    ///
    /// This angle lies in the open interval $`(-\pi, \pi]`$. For improper operations, this angle
    /// depends on the convention used to describe the [`Self::generating_element`].
    #[builder(setter(skip), default = "self.calc_total_proper_angle()")]
    total_proper_angle: f64,

    /// The fraction $`pk/n \in (-1/2, 1/2]`$ of the proper rotation, represented exactly for
    /// hashing and comparison purposes.
    ///
    /// This is not defined for operations with infinite-order generating elements.
    #[builder(setter(skip), default = "self.calc_total_proper_fraction()")]
    pub(crate) total_proper_fraction: Option<F>,

    /// The positive hemisphere used for distinguishing positive and negative rotation poles. If
    /// `None`, the standard positive hemisphere as defined in S.L. Altmann, Rotations,
    /// Quaternions, and Double Groups (Dover Publications, Inc., New York, 2005) is used.
    #[builder(default = "None")]
    pub positive_hemisphere: Option<PositiveHemisphere>,
}

impl SymmetryOperationBuilder {
    fn calc_total_proper_angle(&self) -> f64 {
        let (total_proper_angle, _) = geometry::normalise_rotation_angle(
            self.generating_element
                .as_ref()
                .expect("Generating element has not been set.")
                .proper_angle
                .expect("Proper angle has not been set.")
                * (f64::from(self.power.expect("Power has not been set."))),
            self.generating_element
                .as_ref()
                .expect("Generating element has not been set.")
                .threshold,
        );
        total_proper_angle
    }

    fn calc_total_proper_fraction(&self) -> Option<F> {
        match self
            .generating_element
            .as_ref()
            .expect("Generating element has not been set.")
            .proper_fraction
        {
            Some(frac) => {
                let pow = self.power.expect("Power has not been set.");
                let (total_proper_fraction, _) =
                    geometry::normalise_rotation_fraction(frac * F::from(pow));
                Some(total_proper_fraction)
            }
            None => None,
        }
    }
}

impl SymmetryOperation {
    /// Returns a builder to construct a new symmetry operation.
    ///
    /// # Returns
    ///
    /// A builder to construct a new symmetry operation.
    #[must_use]
    pub(crate) fn builder() -> SymmetryOperationBuilder {
        SymmetryOperationBuilder::default()
    }

    /// Constructs a finite-order-element-generated symmetry operation from a quaternion.
    ///
    /// The rotation angle encoded in the quaternion is taken to be non-negative and assigned as
    /// the proper rotation angle associated with the element generating the operation.
    ///
    /// If an improper operation is required, its generator will be constructed in the
    /// inversion-centre convention.
    ///
    /// # Arguments
    ///
    /// * `qtn` - A quaternion encoding the proper rotation associated with the
    /// generating element of the operation to be constructed.
    /// * `proper` - A boolean indicating if the operation is proper or improper.
    /// * `thresh` - Threshold for comparisons.
    /// * `tr` - A boolean indicating if the resulting symmetry operation should be accompanied by
    /// a time-reversal operation.
    /// * `su2` - A boolean indicating if the resulting symmetry operation is to contain a proper
    /// rotation in $`\mathsf{SU}(2)`$. The homotopy class of the operation will be deduced from
    /// the specified quaternion.
    /// * `poshem` - An option containing any custom positive hemisphere used to distinguish
    /// positive and negative rotation poles.
    ///
    /// # Returns
    ///
    /// The constructed symmetry operation.
    ///
    /// # Panics
    ///
    /// Panics when the scalar part of the provided quaternion lies outside $`[0, 1]`$ by more than
    /// the specified threshold `thresh`, or when the rotation angle associated with the quaternion
    /// cannot be gracefully converted into an integer tuple of order and power.
    #[must_use]
    pub fn from_quaternion(
        qtn: Quaternion,
        proper: bool,
        thresh: f64,
        max_trial_power: u32,
        tr: bool,
        su2: bool,
        poshem: Option<PositiveHemisphere>,
    ) -> Self {
        let (scalar_part, vector_part) = qtn;
        let kind = if proper {
            if tr {
                TRROT
            } else {
                ROT
            }
        } else if tr {
            TRINV
        } else {
            INV
        };
        let element = if su2 {
            // SU(2)
            assert!(
                -1.0 - thresh <= scalar_part && scalar_part <= 1.0 + thresh,
                "The scalar part of the quaternion must be in the interval [-1, +1]."
            );
            let (axis, order, power, su2_grp) =
                if approx::abs_diff_eq!(scalar_part, 1.0, epsilon = thresh,) {
                    // Zero-degree rotation, i.e. identity or inversion, class 0
                    (Vector3::z(), 1u32, 1u32, SU2_0)
                } else if approx::abs_diff_eq!(scalar_part, -1.0, epsilon = thresh,) {
                    // 360-degree rotation, i.e. identity or inversion, class 1
                    (Vector3::z(), 1u32, 1u32, SU2_1)
                } else if approx::abs_diff_eq!(scalar_part, 0.0, epsilon = thresh,) {
                    // 180-degree rotation, i.e. binary rotation or reflection. Whether the resultant
                    // operation is in class 0 or class 1 depends on whether the vector part is in the
                    // positive hemisphere or negative hemisphere.
                    let positive_axis = poshem
                        .as_ref()
                        .cloned()
                        .unwrap_or_default()
                        .get_positive_pole(&vector_part, thresh);
                    (
                        positive_axis,
                        2u32,
                        1u32,
                        if poshem
                            .as_ref()
                            .cloned()
                            .unwrap_or_default()
                            .check_positive_pole(&vector_part, thresh)
                        {
                            SU2_0
                        } else {
                            SU2_1
                        },
                    )
                } else {
                    // scalar_part != 0, 1, or -1
                    let (standardised_scalar_part, standardised_vector_part, su2_grp) =
                        if scalar_part > 0.0 {
                            (scalar_part, vector_part, SU2_0)
                        } else {
                            (-scalar_part, -vector_part, SU2_1)
                        };
                    let half_proper_angle = standardised_scalar_part.acos();
                    let proper_angle = 2.0 * half_proper_angle;
                    let axis = standardised_vector_part / half_proper_angle.sin();
                    let proper_fraction =
                        geometry::get_proper_fraction(proper_angle, thresh, max_trial_power)
                            .unwrap_or_else(|| {
                                panic!(
                                    "No proper fraction could be found for angle `{proper_angle}`."
                                )
                            });
                    (
                        axis,
                        *proper_fraction.denom().unwrap_or_else(|| {
                            panic!("Unable to extract the denominator of `{proper_fraction}`.")
                        }),
                        *proper_fraction.numer().unwrap_or_else(|| {
                            panic!("Unable to extract the numerator of `{proper_fraction}`.")
                        }),
                        su2_grp,
                    )
                };
            SymmetryElement::builder()
                .threshold(thresh)
                .proper_order(ElementOrder::Int(order))
                .proper_power(
                    power
                        .try_into()
                        .expect("Unable to convert the proper power to `i32`."),
                )
                .raw_axis(axis)
                .kind(kind)
                .rotation_group(su2_grp)
                .build()
                .unwrap_or_else(|_|
                    panic!("Unable to construct a symmetry element of kind `{kind}` with the proper part in SU(2).")
                )
        } else {
            // SO(3)
            assert!(
                -thresh <= scalar_part && scalar_part <= 1.0 + thresh,
                "The scalar part of the quaternion must be in the interval [0, +1] when only SO(3) rotations are considered."
            );
            let (axis, order, power) = if approx::abs_diff_eq!(scalar_part, 1.0, epsilon = thresh,)
            {
                // Zero-degree rotation, i.e. identity or inversion
                (Vector3::z(), 1u32, 1i32)
            } else {
                let half_proper_angle = scalar_part.acos(); // acos returns values in [0, π]
                let proper_angle = 2.0 * half_proper_angle;
                let axis = vector_part / half_proper_angle.sin();
                let proper_fraction =
                    geometry::get_proper_fraction(proper_angle, thresh, max_trial_power)
                        .unwrap_or_else(|| {
                            panic!("No proper fraction could be found for angle `{proper_angle}`.")
                        });
                let proper_power = if proper_fraction.is_sign_positive() {
                    i32::try_from(*proper_fraction.numer().unwrap_or_else(|| {
                        panic!("Unable to extract the numerator of `{proper_fraction}`.")
                    }))
                    .expect("Unable to convert the numerator of the proper fraction to `i32`.")
                } else {
                    -i32::try_from(*proper_fraction.numer().unwrap_or_else(|| {
                        panic!("Unable to extract the numerator of `{proper_fraction}`.")
                    }))
                    .expect("Unable to convert the numerator of the proper fraction to `i32`.")
                };
                (
                    axis,
                    *proper_fraction.denom().unwrap_or_else(|| {
                        panic!("Unable to extract the denominator of `{proper_fraction}`.")
                    }),
                    proper_power,
                )
            };

            SymmetryElement::builder()
                .threshold(thresh)
                .proper_order(ElementOrder::Int(order))
                .proper_power(power)
                .raw_axis(axis)
                .kind(kind)
                .rotation_group(SO3)
                .build()
                .expect(
                    "Unable to construct a symmetry element without an associated spin rotation.",
                )
        };

        SymmetryOperation::builder()
            .generating_element(element)
            .power(1)
            .positive_hemisphere(poshem)
            .build()
            .unwrap_or_else(|_|
                panic!(
                    "Unable to construct a symmetry operation of kind `{kind}` with {} rotation from the quaternion `{qtn:?}`.",
                    if su2 { "SU(2)" } else { "SO(3)" }
                )
            )
    }

    /// Finds the quaternion associated with this operation.
    ///
    /// The rotation angle encoded in the quaternion is taken to be non-negative and assigned as
    /// the proper rotation angle associated with the element generating the operation.
    ///
    /// If this is an operation generated from an improper element, the inversion-centre convention
    /// will be used to determine the angle of proper rotation.
    ///
    /// Both $`\mathsf{SO}(3)`$ and $`\mathsf{SU}(2)`$ proper rotations are supported. For
    /// $`\mathsf{SO}(3)`$ proper rotations, only quaternions in the standardised form are
    /// returned.
    ///
    /// See S.L. Altmann, Rotations, Quaternions, and Double Groups (Dover Publications, Inc., New
    /// York, 2005) (Chapter 9) for further information.
    ///
    /// # Returns
    ///
    /// The quaternion associated with this operation.
    ///
    /// # Panics
    ///
    /// Panics if the calculated scalar part of the quaternion lies outside the closed interval
    /// $`[0, 1]`$ by more than the threshold value stored in the generating element in `self`.
    #[must_use]
    pub fn calc_quaternion(&self) -> Quaternion {
        let c_self = if self.is_proper() {
            self.clone()
        } else {
            // Time-reversal does not matter here.
            self.convert_to_improper_kind(&INV)
        };
        debug_assert_eq!(
            self.is_su2_class_1(),
            c_self.is_su2_class_1(),
            "`{self}` and `{c_self}` are in different homotopy classes."
        );

        // We only need the absolute value of the angle. Its sign information is
        // encoded in the pole. `abs_angle` thus lies in [0, π], and so
        //  cos(abs_angle/2) >= 0 and sin(abs_angle/2) >= 0.
        // The scalar part is guaranteed to be in [0, 1].
        // For binary rotations, the scalar part is zero, but the definition of pole ensures that
        // the vector part still lies in the positive hemisphere.
        let abs_angle = c_self.total_proper_angle.abs();
        let scalar_part = (0.5 * abs_angle).cos();
        let vector_part = (0.5 * abs_angle).sin() * c_self.calc_pole().coords;
        debug_assert!(
            -self.generating_element.threshold <= scalar_part
                && scalar_part <= 1.0 + self.generating_element.threshold
        );
        debug_assert!(if approx::abs_diff_eq!(
            scalar_part,
            0.0,
            epsilon = c_self.generating_element.threshold
        ) {
            c_self
                .positive_hemisphere
                .as_ref()
                .cloned()
                .unwrap_or_default()
                .check_positive_pole(&vector_part, c_self.generating_element.threshold)
        } else {
            true
        },);

        if self.is_su2_class_1() {
            (-scalar_part, -vector_part)
        } else {
            (scalar_part, vector_part)
        }
    }

    /// Finds the pole associated with this operation with respect to the positive hemisphere
    /// defined in [`Self::positive_hemisphere`].
    ///
    /// This is the point on the unit sphere that is left invariant by the operation.
    ///
    /// For improper operations, the inversion-centre convention is used to define
    /// the pole. This allows a proper rotation and its improper partner to have the
    /// same pole, thus facilitating the consistent specification of poles for the
    /// identity / inversion and binary rotations / reflections.
    ///
    /// Note that binary rotations / reflections have unique poles on the positive
    /// hemisphere (*i.e.*, $`C_2(\hat{\mathbf{n}}) = C_2^{-1}(\hat{\mathbf{n}})`$
    /// and $`\sigma(\hat{\mathbf{n}}) = \sigma^{-1}(\hat{\mathbf{n}})`$).
    ///
    /// See S.L. Altmann, Rotations, Quaternions, and Double Groups (Dover
    /// Publications, Inc., New York, 2005) (Chapter 9) for further information.
    ///
    /// # Returns
    ///
    /// The pole associated with this operation.
    ///
    /// # Panics
    ///
    /// Panics when no total proper fractions could be found for this operation.
    #[must_use]
    pub fn calc_pole(&self) -> Point3<f64> {
        let op = if self.is_proper() {
            self.clone()
        } else {
            // Time-reversal does not matter here.
            self.convert_to_improper_kind(&INV)
        };
        match *op.generating_element.raw_proper_order() {
            ElementOrder::Int(_) => {
                let frac_1_2 = F::new(1u32, 2u32);
                let total_proper_fraction = op
                    .total_proper_fraction
                    .expect("No total proper fractions found.");
                if total_proper_fraction == frac_1_2 {
                    // Binary rotations or reflections
                    Point3::from(
                        self.positive_hemisphere
                            .as_ref()
                            .cloned()
                            .unwrap_or_default()
                            .get_positive_pole(
                                &op.generating_element.raw_axis,
                                op.generating_element.threshold,
                            ),
                    )
                } else if total_proper_fraction > F::zero() {
                    // Positive rotation angles
                    Point3::from(op.generating_element.raw_axis)
                } else if total_proper_fraction < F::zero() {
                    // Negative rotation angles
                    Point3::from(-op.generating_element.raw_axis)
                } else {
                    // Identity or inversion
                    assert!(total_proper_fraction.is_zero());
                    Point3::from(Vector3::z())
                }
            }
            ElementOrder::Inf => {
                if approx::abs_diff_eq!(
                    op.total_proper_angle,
                    std::f64::consts::PI,
                    epsilon = op.generating_element.threshold
                ) {
                    // Binary rotations or reflections
                    Point3::from(
                        self.positive_hemisphere
                            .as_ref()
                            .cloned()
                            .unwrap_or_default()
                            .get_positive_pole(
                                &op.generating_element.raw_axis,
                                op.generating_element.threshold,
                            ),
                    )
                } else if approx::abs_diff_ne!(
                    op.total_proper_angle,
                    0.0,
                    epsilon = op.generating_element.threshold
                ) {
                    Point3::from(op.total_proper_angle.signum() * op.generating_element.raw_axis)
                } else {
                    approx::assert_abs_diff_eq!(
                        op.total_proper_angle,
                        0.0,
                        epsilon = op.generating_element.threshold
                    );
                    Point3::from(Vector3::z())
                }
            }
        }
    }

    /// Finds the pole associated with the proper rotation of this operation.
    ///
    /// This is the point on the unit sphere that is left invariant by the proper rotation part of
    /// the operation.
    ///
    /// For improper operations, no conversions will be performed, unlike in [`Self::calc_pole`].
    ///
    /// Note that binary rotations have unique poles on the positive hemisphere (*i.e.*,
    /// $`C_2(\hat{\mathbf{n}}) = C_2^{-1}(\hat{\mathbf{n}})`$ and
    /// $`\sigma(\hat{\mathbf{n}}) = \sigma^{-1}(\hat{\mathbf{n}})`$).
    ///
    /// See S.L. Altmann, Rotations, Quaternions, and Double Groups (Dover
    /// Publications, Inc., New York, 2005) (Chapter 9) for further information.
    ///
    /// # Returns
    ///
    /// The pole associated with the proper rotation of this operation.
    ///
    /// # Panics
    ///
    /// Panics when no total proper fractions could be found for this operation.
    #[must_use]
    pub fn calc_proper_rotation_pole(&self) -> Point3<f64> {
        match *self.generating_element.raw_proper_order() {
            ElementOrder::Int(_) => {
                let frac_1_2 = F::new(1u32, 2u32);
                let total_proper_fraction = self
                    .total_proper_fraction
                    .expect("No total proper fractions found.");
                if total_proper_fraction == frac_1_2 {
                    // Binary rotations or reflections
                    Point3::from(
                        self.positive_hemisphere
                            .as_ref()
                            .cloned()
                            .unwrap_or_default()
                            .get_positive_pole(
                                &self.generating_element.raw_axis,
                                self.generating_element.threshold,
                            ),
                    )
                } else if total_proper_fraction > F::zero() {
                    // Positive rotation angles
                    Point3::from(self.generating_element.raw_axis)
                } else if total_proper_fraction < F::zero() {
                    // Negative rotation angles
                    Point3::from(-self.generating_element.raw_axis)
                } else {
                    // Identity or inversion
                    assert!(total_proper_fraction.is_zero());
                    Point3::from(Vector3::z())
                }
            }
            ElementOrder::Inf => {
                if approx::abs_diff_eq!(
                    self.total_proper_angle,
                    std::f64::consts::PI,
                    epsilon = self.generating_element.threshold
                ) {
                    // Binary rotations or reflections
                    Point3::from(
                        self.positive_hemisphere
                            .as_ref()
                            .cloned()
                            .unwrap_or_default()
                            .get_positive_pole(
                                &self.generating_element.raw_axis,
                                self.generating_element.threshold,
                            ),
                    )
                } else if approx::abs_diff_ne!(
                    self.total_proper_angle,
                    0.0,
                    epsilon = self.generating_element.threshold
                ) {
                    Point3::from(
                        self.total_proper_angle.signum() * self.generating_element.raw_axis,
                    )
                } else {
                    approx::assert_abs_diff_eq!(
                        self.total_proper_angle,
                        0.0,
                        epsilon = self.generating_element.threshold
                    );
                    Point3::from(Vector3::z())
                }
            }
        }
    }

    /// Finds the pole angle associated with this operation.
    ///
    /// This is the non-negative angle that, together with the pole, uniquely determines the proper
    /// part of this operation. This angle lies in the interval $`[0, \pi]`$.
    ///
    /// For improper operations, the inversion-centre convention is used to define the pole angle.
    /// This allows a proper rotation and its improper partner to have the same pole angle, thus
    /// facilitating the consistent specification of poles for the identity / inversion and binary
    /// rotations / reflections.
    ///
    /// # Returns
    ///
    /// The pole angle associated with this operation.
    ///
    /// # Panics
    ///
    /// Panics when no total proper fractions could be found for this operation.
    #[must_use]
    pub fn calc_pole_angle(&self) -> f64 {
        let c_self = if self.is_proper() {
            self.clone()
        } else {
            // Time-reversal does not matter here.
            self.convert_to_improper_kind(&INV)
        };

        c_self.total_proper_angle.abs()
    }

    /// Returns a copy of the current symmetry operation with the generating element
    /// converted to the requested improper kind (power-preserving), provided that
    /// it is an improper element.
    ///
    /// # Arguments
    ///
    /// * `improper_kind` - The improper kind to which `self` is to be converted. There is no need
    /// to make sure the time reversal specification in `improper_kind` matches that of the
    /// generating element of `self` as the conversion will take care of this.
    ///
    /// # Panics
    ///
    /// Panics if the converted symmetry operation cannot be constructed.
    #[must_use]
    pub fn convert_to_improper_kind(&self, improper_kind: &SymmetryElementKind) -> Self {
        let c_element = self
            .generating_element
            .convert_to_improper_kind(improper_kind, true);
        debug_assert_eq!(
            self.generating_element.is_su2_class_1(),
            c_element.is_su2_class_1()
        );
        Self::builder()
            .generating_element(c_element)
            .power(self.power)
            .positive_hemisphere(self.positive_hemisphere.clone())
            .build()
            .expect("Unable to construct a symmetry operation.")
    }

    /// Converts the current symmetry operation $`O`$ to an equivalent symmetry element $`E`$ such
    /// that $`O = E^1`$.
    ///
    /// The proper rotation axis of $`E`$ is the proper rotation pole (*not* the overall pole) of
    /// $`O`$, and the proper rotation angle of $`E`$ is the total proper rotation angle of $`O`$,
    /// either as an (order, power) integer tuple or an angle floating-point number.
    ///
    /// If $`O`$ is improper, then the improper generating element for $`E`$ is the same as that in
    /// the generating element of $`O`$.
    ///
    /// # Returns
    ///
    /// The equivalent symmetry element $`E`$.
    pub fn to_symmetry_element(&self) -> SymmetryElement {
        let kind = if self.is_proper() {
            let tr = self.contains_time_reversal();
            if tr {
                TRROT
            } else {
                ROT
            }
        } else {
            self.generating_element.kind
        };
        let additional_superscript = if self.is_proper() {
            String::new()
        } else {
            self.generating_element.additional_superscript.clone()
        };
        let additional_subscript = if self.is_proper() {
            String::new()
        } else {
            self.generating_element.additional_subscript.clone()
        };
        let rotation_group = if self.is_su2_class_1() {
            SU2_1
        } else if self.is_su2() {
            SU2_0
        } else {
            SO3
        };
        let axis = if self.is_spatial_reflection() {
            self.positive_hemisphere
                .as_ref()
                .cloned()
                .unwrap_or_default()
                .get_positive_pole(
                    self.generating_element.raw_axis(),
                    self.generating_element.threshold,
                )
        } else {
            self.calc_proper_rotation_pole().coords
        };

        if let Some(total_proper_fraction) = self.total_proper_fraction {
            let proper_order = *total_proper_fraction
                .denom()
                .expect("Unable to extract the denominator of the total proper fraction.");
            let numer = *total_proper_fraction
                .numer()
                .expect("Unable to extract the numerator of the total proper fraction.");
            let proper_power =
                i32::try_from(numer).expect("Unable to convert the numerator to `i32`.");
            SymmetryElement::builder()
                .threshold(self.generating_element.threshold())
                .proper_order(ElementOrder::Int(proper_order))
                .proper_power(proper_power)
                .raw_axis(axis)
                .kind(kind)
                .rotation_group(rotation_group)
                .additional_superscript(additional_superscript)
                .additional_subscript(additional_subscript)
                .build()
                .unwrap()
        } else {
            let proper_angle = self.total_proper_angle;
            SymmetryElement::builder()
                .threshold(self.generating_element.threshold())
                .proper_order(ElementOrder::Inf)
                .proper_angle(proper_angle)
                .raw_axis(axis)
                .kind(kind)
                .rotation_group(rotation_group)
                .additional_superscript(additional_superscript)
                .additional_subscript(additional_subscript)
                .build()
                .unwrap()
        }
    }

    /// Generates the abbreviated symbol for this symmetry operation.
    #[must_use]
    pub fn get_abbreviated_symbol(&self) -> String {
        self.to_symmetry_element()
            .get_simplified_symbol_signed_power()
    }

    /// Returns the representation matrix for the spatial part of this symmetry operation.
    ///
    /// This representation matrix is in the basis of coordinate *functions* $`(y, z, x)`$.
    #[must_use]
    pub fn get_3d_spatial_matrix(&self) -> Array2<f64> {
        if self.is_proper() {
            if self.is_identity() || self.is_time_reversal() {
                Array2::<f64>::eye(3)
            } else {
                let angle = self.calc_pole_angle();
                let axis = self.calc_pole().coords;
                let mat = proper_rotation_matrix(angle, &axis, 1);

                // nalgebra matrix iter is column-major.
                Array2::<f64>::from_shape_vec(
                    (3, 3).f(),
                    mat.iter().copied().collect::<Vec<_>>(),
                )
                .unwrap_or_else(
                    |_| panic!(
                        "Unable to construct a three-dimensional rotation matrix for angle {angle} and axis {axis}."
                    )
                )
                .select(Axis(0), &[1, 2, 0])
                .select(Axis(1), &[1, 2, 0])
            }
        } else if self.is_spatial_inversion() {
            -Array2::<f64>::eye(3)
        } else {
            // Pole and pole angle are obtained in the inversion-centre convention.
            let angle = self.calc_pole_angle();
            let axis = self.calc_pole().coords;
            let mat = improper_rotation_matrix(angle, &axis, 1, &IMINV);

            // nalgebra matrix iter is column-major.
            Array2::<f64>::from_shape_vec(
                (3, 3).f(),
                mat.iter().copied().collect::<Vec<_>>(),
            )
            .unwrap_or_else(
                |_| panic!(
                    "Unable to construct a three-dimensional improper rotation matrix for angle {angle} and axis {axis}."
                )
            )
            .select(Axis(0), &[1, 2, 0])
            .select(Axis(1), &[1, 2, 0])
        }
    }

    /// Convert the proper rotation of the current operation to one in hopotopy class 0 of
    /// $`\mathsf{SU}(2)`$.
    ///
    /// # Returns
    ///
    /// A symmetry element in $`\mathsf{SU}(2)`$.
    pub fn to_su2_class_0(&self) -> Self {
        let q_identity = Self::from_quaternion(
            (-1.0, -Vector3::z()),
            true,
            self.generating_element.threshold(),
            1,
            false,
            true,
            None,
        );
        if self.is_su2() {
            if self.is_su2_class_1() {
                self * q_identity
            } else {
                self.clone()
            }
        } else {
            let mut op = self.clone();
            op.generating_element.rotation_group = SU2_0;
            if op.is_su2_class_1() {
                let mut q_op = op * q_identity;
                if !q_op.is_proper() {
                    q_op = q_op.convert_to_improper_kind(&SIG);
                }
                q_op
            } else {
                op
            }
        }
    }

    /// Sets the positive hemisphere governing this symmetry operation.
    ///
    /// # Arguments
    ///
    /// * `poshem` - An `Option` containing a custom positive hemisphere, if any.
    pub fn set_positive_hemisphere(&mut self, poshem: Option<&PositiveHemisphere>) {
        self.positive_hemisphere = poshem.cloned();
    }
}

// =====================
// Trait implementations
// =====================

impl FiniteOrder for SymmetryOperation {
    type Int = u32;

    /// Calculates the order of this symmetry operation.
    fn order(&self) -> Self::Int {
        let denom = *self
            .total_proper_fraction
            .expect("No total proper fractions found.")
            .denom()
            .expect("Unable to extract the denominator.");
        let spatial_order =
            if (self.is_proper() && !self.contains_time_reversal()) || denom.rem_euclid(2) == 0 {
                denom
            } else {
                2 * denom
            };
        if self.is_su2() {
            2 * spatial_order
        } else {
            spatial_order
        }
    }
}

impl SpecialSymmetryTransformation for SymmetryOperation {
    // ============
    // Spatial part
    // ============

    /// Checks if the spatial part of the symmetry operation is proper.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the spatial part of the symmetry operation is proper.
    fn is_proper(&self) -> bool {
        let au = self.generating_element.contains_antiunitary();
        self.generating_element.is_o3_proper(au) || self.power.rem_euclid(2) == 0
    }

    /// Checks if the spatial part of the symmetry operation is the spatial identity.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the spatial part of the symmetry operation is the spatial identity.
    fn is_spatial_identity(&self) -> bool {
        self.is_proper()
            && match *self.generating_element.raw_proper_order() {
                ElementOrder::Int(_) => self
                    .total_proper_fraction
                    .expect("Total proper fraction not found for a finite-order operation.")
                    .is_zero(),
                ElementOrder::Inf => approx::abs_diff_eq!(
                    self.total_proper_angle,
                    0.0,
                    epsilon = self.generating_element.threshold
                ),
            }
    }

    /// Checks if the spatial part of the symmetry operation is a spatial binary rotation.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the spatial part of the symmetry operation is a spatial binary
    /// rotation.
    fn is_spatial_binary_rotation(&self) -> bool {
        self.is_proper()
            && match *self.generating_element.raw_proper_order() {
                ElementOrder::Int(_) => {
                    self.total_proper_fraction
                        .expect("Total proper fraction not found for a finite-order operation.")
                        == F::new(1u32, 2u32)
                }
                ElementOrder::Inf => {
                    approx::abs_diff_eq!(
                        self.total_proper_angle,
                        std::f64::consts::PI,
                        epsilon = self.generating_element.threshold
                    )
                }
            }
    }

    /// Checks if the spatial part of the symmetry operation is the spatial inversion.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the spatial part of the symmetry operation is the spatial inversion.
    fn is_spatial_inversion(&self) -> bool {
        !self.is_proper()
            && match self.generating_element.kind {
                SymmetryElementKind::ImproperMirrorPlane(_) => {
                    if let ElementOrder::Int(_) = *self.generating_element.raw_proper_order() {
                        self.total_proper_fraction
                            .expect("Total proper fraction not found for a finite-order operation.")
                            == F::new(1u32, 2u32)
                    } else {
                        approx::abs_diff_eq!(
                            self.total_proper_angle,
                            std::f64::consts::PI,
                            epsilon = self.generating_element.threshold
                        )
                    }
                }
                SymmetryElementKind::ImproperInversionCentre(_) => {
                    if let ElementOrder::Int(_) = *self.generating_element.raw_proper_order() {
                        self.total_proper_fraction
                            .expect("Total proper fraction not found for a finite-order operation.")
                            .is_zero()
                    } else {
                        approx::abs_diff_eq!(
                            self.total_proper_angle,
                            0.0,
                            epsilon = self.generating_element.threshold
                        )
                    }
                }
                _ => false,
            }
    }

    /// Checks if the spatial part of the symmetry operation is a spatial reflection.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the spatial part of the symmetry operation is a spatial reflection.
    fn is_spatial_reflection(&self) -> bool {
        !self.is_proper()
            && match self.generating_element.kind {
                SymmetryElementKind::ImproperMirrorPlane(_) => {
                    if let ElementOrder::Int(_) = *self.generating_element.raw_proper_order() {
                        self.total_proper_fraction
                            .expect("Total proper fraction not found for a finite-order operation.")
                            .is_zero()
                    } else {
                        approx::abs_diff_eq!(
                            self.total_proper_angle,
                            0.0,
                            epsilon = self.generating_element.threshold
                        )
                    }
                }
                SymmetryElementKind::ImproperInversionCentre(_) => {
                    if let ElementOrder::Int(_) = self.generating_element.raw_proper_order() {
                        self.total_proper_fraction
                            .expect("Total proper fraction not found for a finite-order operation.")
                            == F::new(1u32, 2u32)
                    } else {
                        approx::abs_diff_eq!(
                            self.total_proper_angle,
                            std::f64::consts::PI,
                            epsilon = self.generating_element.threshold
                        )
                    }
                }
                _ => false,
            }
    }

    // ==================
    // Time-reversal part
    // ==================

    /// Checks if the symmetry operation is antiunitary or not.
    ///
    /// # Returns
    ///
    /// A boolean indicating if the symmetry oppperation is antiunitary.
    fn contains_time_reversal(&self) -> bool {
        self.generating_element.contains_time_reversal() && self.power.rem_euclid(2) == 1
    }

    // ==================
    // Spin rotation part
    // ==================

    /// Checks if the proper rotation part of the symmetry operation is in $`\mathsf{SU}(2)`$.
    ///
    /// # Returns
    ///
    /// A boolean indicating if this symmetry operation contains an $`\mathsf{SU}(2)`$ proper
    /// rotation.
    fn is_su2(&self) -> bool {
        self.generating_element.rotation_group.is_su2()
    }

    /// Checks if the proper rotation part of the symmetry operation is in $`\mathsf{SU}(2)`$ and
    /// connected to the identity via a homotopy path of class 1.
    ///
    /// # Returns
    ///
    /// A boolean indicating if this symmetry operation contains an $`\mathsf{SU}(2)`$ proper
    /// rotation connected to the identity via a homotopy path of class 1.
    fn is_su2_class_1(&self) -> bool {
        if self.is_su2() {
            // The following is wrong, because `self.is_proper()` takes into account the power applied
            // to the spatial part, but not yet to the spin rotation part. Then, for example,
            // [QΣ·S3(+0.816, -0.408, +0.408)]^2 would become Σ'·[C3(+0.816, -0.408, +0.408)]^2 where
            // Σ' is the associated spin rotation of [C3(+0.816, -0.408, +0.408)]^2, which is not the
            // same as Σ^2.
            // let c_self = if self.is_proper() {
            //     self.clone()
            // } else {
            //     self.convert_to_improper_kind(&INV)
            // };
            //
            // The following is correct.
            let c_self = match self.generating_element.kind {
                SymmetryElementKind::Proper(_)
                | SymmetryElementKind::ImproperInversionCentre(_) => self.clone(),
                SymmetryElementKind::ImproperMirrorPlane(au) => {
                    self.convert_to_improper_kind(&SymmetryElementKind::ImproperInversionCentre(au))
                }
            };
            let generating_element_au = c_self.generating_element.contains_antiunitary();
            let spatial_proper_identity = c_self
                .generating_element
                .is_o3_identity(generating_element_au)
                || c_self
                    .generating_element
                    .is_o3_inversion_centre(generating_element_au);

            let inverse_from_time_reversal =
                if self.is_su2() && generating_element_au == Some(AntiunitaryKind::TimeReversal) {
                    self.power.rem_euclid(4) == 2 || self.power.rem_euclid(4) == 3
                } else {
                    false
                };

            let inverse_from_rotation_group = if spatial_proper_identity {
                // The proper part of the generating element is the identity. In this case, no
                // matter the value of proper power, the result is always the identity.
                false
            } else {
                let thresh = c_self.generating_element.threshold;
                let odd_jumps_from_angle = c_self
                    .generating_element
                    .proper_fraction
                    .map(|frac| {
                        let pow = c_self.power;
                        let total_unormalised_proper_fraction = frac * F::from(pow);
                        let (_, x) = geometry::normalise_rotation_fraction(
                            total_unormalised_proper_fraction,
                        );
                        x.rem_euclid(2) == 1
                    })
                    .unwrap_or_else(|| {
                        let total_unormalised_proper_angle = c_self
                            .generating_element
                            .proper_angle
                            .expect("Proper angle of generating element not found.")
                            * f64::from(c_self.power);
                        let (_, x) = geometry::normalise_rotation_angle(
                            total_unormalised_proper_angle,
                            thresh,
                        );
                        x.rem_euclid(2) == 1
                    });
                let single_jump_from_c2 = (c_self.is_spatial_binary_rotation()
                    || c_self.is_spatial_reflection())
                    && !self
                        .positive_hemisphere
                        .as_ref()
                        .cloned()
                        .unwrap_or_default()
                        .check_positive_pole(c_self.generating_element.raw_axis(), thresh);
                odd_jumps_from_angle != single_jump_from_c2
            };

            let intrinsic_inverse = c_self.generating_element.rotation_group().is_su2_class_1()
                && c_self.power.rem_euclid(2) == 1;

            let inverse_count = [
                inverse_from_time_reversal,
                inverse_from_rotation_group,
                intrinsic_inverse,
            ]
            .into_iter()
            .filter(|&inverse| inverse)
            .count();

            inverse_count.rem_euclid(2) == 1
        } else {
            false
        }
    }
}

impl fmt::Debug for SymmetryOperation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.power == 1 {
            write!(f, "{:?}", self.generating_element)
        } else if self.power >= 0 {
            write!(f, "[{:?}]^{}", self.generating_element, self.power)
        } else {
            write!(f, "[{:?}]^({})", self.generating_element, self.power)
        }
    }
}

impl fmt::Display for SymmetryOperation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.power == 1 {
            write!(f, "{}", self.generating_element)
        } else if self.power >= 0 {
            write!(f, "[{}]^{}", self.generating_element, self.power)
        } else {
            write!(f, "[{}]^({})", self.generating_element, self.power)
        }
    }
}

impl PartialEq for SymmetryOperation {
    fn eq(&self, other: &Self) -> bool {
        if (*self.generating_element.raw_proper_order() == ElementOrder::Inf)
            != (*other.generating_element.raw_proper_order() == ElementOrder::Inf)
        {
            // We disable comparisons between operations with infinite-order and
            // finite-order generating elements, because they cannot be made to
            // have the same hashes without losing the fidelity of exact-fraction
            // representations for operations with finite-order generating elements.
            return false;
        }

        // =================
        // Group-theoretical
        // =================

        if self.is_su2() != other.is_su2() {
            return false;
        }

        if self.is_su2_class_1() != other.is_su2_class_1() {
            return false;
        }

        // ==========================
        // Special general operations
        // ==========================

        if self.is_proper() != other.is_proper() {
            return false;
        }

        if self.contains_time_reversal() != other.contains_time_reversal() {
            return false;
        }

        // ===========================
        // Special specific operations
        // ===========================

        // At this stage, `self` and `other` must have the same spatial parity, unitarity, and
        // SO3/SU2 properties.
        if self.is_spatial_identity() && other.is_spatial_identity() {
            // assert_eq!(misc::calculate_hash(self), misc::calculate_hash(other));
            // return true;
            return misc::calculate_hash(self) == misc::calculate_hash(other);
        }

        // ======
        // Others
        // ======

        let thresh =
            (self.generating_element.threshold * other.generating_element.threshold).sqrt();

        let result = if (self.is_spatial_binary_rotation() && other.is_spatial_binary_rotation())
            || (self.is_spatial_reflection() && other.is_spatial_reflection())
        {
            approx::relative_eq!(
                self.calc_pole(),
                other.calc_pole(),
                epsilon = thresh,
                max_relative = thresh
            )
        } else {
            let c_self = if self.is_proper() {
                self.clone()
            } else {
                // Time-reversal does not matter here.
                self.convert_to_improper_kind(&INV)
            };
            let c_other = if other.is_proper() {
                other.clone()
            } else {
                // Time-reversal does not matter here.
                other.convert_to_improper_kind(&INV)
            };

            let angle_comparison = if let (Some(s_frac), Some(o_frac)) =
                (c_self.total_proper_fraction, c_other.total_proper_fraction)
            {
                s_frac.abs() == o_frac.abs()
            } else {
                approx::relative_eq!(
                    c_self.total_proper_angle.abs(),
                    c_other.total_proper_angle.abs(),
                    epsilon = thresh,
                    max_relative = thresh
                )
            };

            angle_comparison
                && approx::relative_eq!(
                    self.calc_pole(),
                    other.calc_pole(),
                    epsilon = thresh,
                    max_relative = thresh
                )
        };

        // if result {
        //     assert_eq!(
        //         misc::calculate_hash(self),
        //         misc::calculate_hash(other),
        //         "`{self}` and `{other}` have unequal hashes.",
        //     );
        // }
        result && (misc::calculate_hash(self) == misc::calculate_hash(other))
    }
}

impl Eq for SymmetryOperation {}

impl Hash for SymmetryOperation {
    fn hash<H: Hasher>(&self, state: &mut H) {
        let c_self = match self.generating_element.kind {
            SymmetryElementKind::Proper(_) | SymmetryElementKind::ImproperInversionCentre(_) => {
                self.clone()
            }
            SymmetryElementKind::ImproperMirrorPlane(_) => self.convert_to_improper_kind(&INV),
        };
        // ==========================
        // Special general operations
        // ==========================
        c_self.is_proper().hash(state);
        c_self.contains_time_reversal().hash(state);
        c_self.is_su2().hash(state);
        c_self.is_su2_class_1().hash(state);

        // ===========================
        // Special specific operations
        // ===========================
        if c_self.is_spatial_identity() {
            true.hash(state);
        } else {
            let pole = c_self.calc_pole();
            pole[0]
                .round_factor(c_self.generating_element.threshold)
                .integer_decode()
                .hash(state);
            pole[1]
                .round_factor(c_self.generating_element.threshold)
                .integer_decode()
                .hash(state);
            pole[2]
                .round_factor(c_self.generating_element.threshold)
                .integer_decode()
                .hash(state);

            if !c_self.is_spatial_binary_rotation() && !c_self.is_spatial_reflection() {
                if let Some(frac) = c_self.total_proper_fraction {
                    // self.total_proper_fraction lies in (-1/2, 0) ∪ (0, 1/2).
                    // 0 and 1/2 are excluded because this is not an identity,
                    // inversion, binary rotation, or reflection.
                    frac.abs().hash(state);
                } else {
                    // self.total_proper_angle lies in (-π, 0) ∪ (0, π).
                    // 0 and π are excluded because this is not an identity,
                    // inversion, binary rotation, or reflection.
                    let abs_ang = c_self.total_proper_angle.abs();
                    abs_ang
                        .round_factor(c_self.generating_element.threshold)
                        .integer_decode()
                        .hash(state);
                };
            }
        };
    }
}

impl Mul<&'_ SymmetryOperation> for &SymmetryOperation {
    type Output = SymmetryOperation;

    fn mul(self, rhs: &SymmetryOperation) -> Self::Output {
        assert_eq!(
            self.is_su2(),
            rhs.is_su2(),
            "`self` and `rhs` must both have or not have associated spin rotations."
        );
        assert_eq!(self.positive_hemisphere, rhs.positive_hemisphere);
        let su2 = self.is_su2();
        let (q1_s, q1_v) = self.calc_quaternion();
        let (q2_s, q2_v) = rhs.calc_quaternion();

        let q3_s = q1_s * q2_s - q1_v.dot(&q2_v);
        let q3_v = q1_s * q2_v + q2_s * q1_v + q1_v.cross(&q2_v);

        // Is the resulting operation proper?
        let proper = self.is_proper() == rhs.is_proper();

        // Does the resulting operation contain a time reversal?
        let tr = self.contains_time_reversal() != rhs.contains_time_reversal();

        // Does the resulting operation pick up a quaternion sign change due to θ^2?
        let tr2 = self.contains_time_reversal() && rhs.contains_time_reversal();

        let thresh = (self.generating_element.threshold * rhs.generating_element.threshold).sqrt();
        let max_trial_power = u32::MAX;

        let q3 = if su2 {
            if tr2 {
                (-q3_s, -q3_v)
            } else {
                (q3_s, q3_v)
            }
        } else if q3_s >= 0.0 {
            (q3_s, q3_v)
        } else {
            (-q3_s, -q3_v)
        };

        SymmetryOperation::from_quaternion(
            q3,
            proper,
            thresh,
            max_trial_power,
            tr,
            su2,
            self.positive_hemisphere.clone(),
        )
    }
}

impl Mul<&'_ SymmetryOperation> for SymmetryOperation {
    type Output = SymmetryOperation;

    fn mul(self, rhs: &SymmetryOperation) -> Self::Output {
        &self * rhs
    }
}

impl Mul<SymmetryOperation> for SymmetryOperation {
    type Output = SymmetryOperation;

    fn mul(self, rhs: SymmetryOperation) -> Self::Output {
        &self * &rhs
    }
}

impl Mul<SymmetryOperation> for &SymmetryOperation {
    type Output = SymmetryOperation;

    fn mul(self, rhs: SymmetryOperation) -> Self::Output {
        self * &rhs
    }
}

impl Pow<i32> for &SymmetryOperation {
    type Output = SymmetryOperation;

    fn pow(self, rhs: i32) -> Self::Output {
        SymmetryOperation::builder()
            .generating_element(self.generating_element.clone())
            .power(self.power * rhs)
            .positive_hemisphere(self.positive_hemisphere.clone())
            .build()
            .expect("Unable to construct a symmetry operation.")
    }
}

impl Pow<i32> for SymmetryOperation {
    type Output = SymmetryOperation;

    fn pow(self, rhs: i32) -> Self::Output {
        (&self).pow(rhs)
    }
}

impl Inv for &SymmetryOperation {
    type Output = SymmetryOperation;

    fn inv(self) -> Self::Output {
        SymmetryOperation::builder()
            .generating_element(self.generating_element.clone())
            .power(-self.power)
            .positive_hemisphere(self.positive_hemisphere.clone())
            .build()
            .expect("Unable to construct an inverse symmetry operation.")
    }
}

impl Inv for SymmetryOperation {
    type Output = SymmetryOperation;

    fn inv(self) -> Self::Output {
        (&self).inv()
    }
}

impl<M> IntoPermutation<M> for SymmetryOperation
where
    M: Transform + PermutableCollection<Rank = usize>,
{
    fn act_permute(&self, rhs: &M) -> Option<Permutation<usize>> {
        let angle = self.calc_pole_angle();
        let axis = self.calc_pole().coords;
        let mut t_mol = if self.is_proper() {
            rhs.rotate(angle, &axis)
        } else {
            rhs.improper_rotate(angle, &axis, &IMINV)
        };
        if self.contains_time_reversal() {
            t_mol.reverse_time_mut();
        }
        rhs.get_perm_of(&t_mol)
    }
}

// =================
// Utility functions
// =================
/// Sorts symmetry operations in-place based on:
///
/// * whether they are unitary or antiunitary
/// * whether they are proper or improper
/// * whether they are the identity or inversion
/// * whether they are a spatial binary rotation or spatial reflection
/// * their orders
/// * their powers
/// * their closeness to Cartesian axes
/// * the axes of closest inclination
/// * whether they are of homotopy class 1 in $`\mathsf{SU}'(2)`$.
///
/// # Arguments
///
/// * `operations` - A mutable reference to a vector of symmetry operations.
pub(crate) fn sort_operations(operations: &mut [SymmetryOperation]) {
    operations.sort_by_key(|op| {
        let (axis_closeness, closest_axis) = op.generating_element.closeness_to_cartesian_axes();
        let c_op = if op.is_proper()
            || op.generating_element.kind == SIG
            || op.generating_element.kind == TRSIG
        {
            op.clone()
        } else if op.contains_time_reversal() {
            op.convert_to_improper_kind(&TRSIG)
        } else {
            op.convert_to_improper_kind(&SIG)
        };

        let total_proper_fraction = c_op
            .total_proper_fraction
            .expect("No total proper fractions found.");
        let denom = i64::try_from(
            *total_proper_fraction
                .denom()
                .expect("The denominator of the total proper fraction cannot be extracted."),
        )
        .unwrap_or_else(|_| {
            panic!("Unable to convert the denominator of `{total_proper_fraction:?}` to `i64`.")
        });
        let numer = i64::try_from(
            *total_proper_fraction
                .numer()
                .expect("The numerator of the total proper fraction cannot be extracted."),
        )
        .unwrap_or_else(|_| {
            panic!("Unable to convert the numerator of `{total_proper_fraction:?}` to `i64`.")
        });

        let negative_rotation = !c_op
            .positive_hemisphere
            .as_ref()
            .cloned()
            .unwrap_or_default()
            .check_positive_pole(
                &c_op.calc_proper_rotation_pole().coords,
                c_op.generating_element.threshold(),
            );

        (
            c_op.contains_time_reversal(),
            !c_op.is_proper(),
            !(c_op.is_spatial_identity() || c_op.is_spatial_inversion()),
            c_op.is_spatial_binary_rotation() || c_op.is_spatial_reflection(),
            -denom,
            negative_rotation,
            if negative_rotation { -numer } else { numer },
            OrderedFloat(axis_closeness),
            closest_axis,
            c_op.is_su2_class_1(),
        )
    });
}