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
//! Conjugacy class structures.

use std::collections::HashSet;
use std::fmt;
use std::hash::Hash;
use std::ops::Mul;

use anyhow::{self, ensure, format_err};
use derive_builder::Builder;
use indexmap::IndexMap;
use itertools::Itertools;
use ndarray::{s, Array2};
use num_traits::Inv;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::chartab::chartab_group::CharacterProperties;
use crate::chartab::chartab_symbols::{
    CollectionSymbol, LinearSpaceSymbol, ReducibleLinearSpaceSymbol,
};
use crate::group::{
    FiniteOrder, GroupProperties, HasUnitarySubgroup, MagneticRepresentedGroup,
    UnitaryRepresentedGroup,
};

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

/// Trait for conjugacy class properties of a finite group.
pub trait ClassProperties: GroupProperties
where
    Self::ClassSymbol: CollectionSymbol<CollectionElement = Self::GroupElement>,
    <Self as GroupProperties>::GroupElement: Inv<Output = <Self as GroupProperties>::GroupElement>,
{
    /// The type of class symbols.
    type ClassSymbol;

    // ----------------
    // Required methods
    // ----------------

    /// Computes the class structure of the group and store the result.
    fn compute_class_structure(&mut self) -> Result<(), anyhow::Error>;

    /// Given a class index, returns an optional shared reference to the set containing the indices
    /// of all elements in that class.
    ///
    /// # Arguments
    ///
    /// * `cc_idx` - A class index.
    ///
    /// # Returns
    ///
    /// Returns a shared reference to the set containing the indices of all elements in that class, or
    /// `None` if `cc_idx` is not a valid class index of the group.
    #[must_use]
    fn get_cc_index(&self, cc_idx: usize) -> Option<&HashSet<usize>>;

    /// Given an element index, returns an optional index of the conjugacy class to which the
    /// element belongs.
    ///
    /// # Arguments
    ///
    /// * `e_idx` - An element index.
    ///
    /// # Returns
    ///
    /// Returns an index of the conjugacy class to which the element belongs, or `None` if either
    /// the element does not have a conjugacy class, or the index is out of range.
    #[must_use]
    fn get_cc_of_element_index(&self, e_idx: usize) -> Option<usize>;

    /// Given a class index, returns an optional representative element of that conjugacy class.
    ///
    /// # Arguments
    ///
    /// * `cc_idx` - A class index.
    ///
    /// # Returns
    ///
    /// Returns a representative element of the class, or `None` if the class index is out of
    /// range.
    #[must_use]
    fn get_cc_transversal(&self, cc_idx: usize) -> Option<Self::GroupElement>;

    /// Given a conjugacy class symbol, returns the index of the corresponding conjugacy class.
    ///
    /// # Arguments
    ///
    /// * `cc_sym` - A conjugacy class symbol.
    ///
    /// # Returns
    ///
    /// Returns an index corresponding to the conjugacy class of `cc_sym`, or `None` if `cc_sym`
    /// does not exist in the group.
    #[must_use]
    fn get_index_of_cc_symbol(&self, cc_sym: &Self::ClassSymbol) -> Option<usize>;

    /// Given a class index, returns its conjugacy class symbol, if any.
    ///
    /// # Arguments
    ///
    /// * `cc_idx` - A class index.
    ///
    /// # Returns
    ///
    /// Returns a conjugacy class symbol, or `None` if such a symbol does not exist for the class,
    /// or if the class index is out of range.
    #[must_use]
    fn get_cc_symbol_of_index(&self, cc_idx: usize) -> Option<Self::ClassSymbol>;

    /// Given a predicate, returns conjugacy class symbols satisfying it.
    ///
    /// # Arguments
    ///
    /// * `predicate` - A predicate to filter conjugacy class symbols.
    ///
    /// # Returns
    ///
    /// Returns conjugacy class symbols satisfying `predicate`, or `None` if such a symbol does not
    /// exist for the class.
    #[must_use]
    fn filter_cc_symbols<P: FnMut(&Self::ClassSymbol) -> bool>(
        &self,
        predicate: P,
    ) -> Vec<Self::ClassSymbol>;

    /// Sets the conjugacy class symbols for this group.
    ///
    /// # Arguments
    ///
    /// `cc_symbols` - A sliced of owned conjugacy class symbols.
    fn set_class_symbols(&mut self, cc_symbols: &[Self::ClassSymbol]);

    /// Given a class index, returns an index for its inverse.
    ///
    /// The inverse of a class contains the inverses of its elements.
    ///
    /// # Arguments
    ///
    /// `cc_idx` - A class index.
    ///
    /// # Returns
    ///
    /// The index of the inverse of `cc_idx`, or `None` if the class index is out of range.
    #[must_use]
    fn get_inverse_cc(&self, cc_idx: usize) -> Option<usize>;

    /// Returns the number of conjugacy classes in the group.
    #[must_use]
    fn class_number(&self) -> usize;

    /// Given a class index, returns its size.
    ///
    /// # Arguments
    ///
    /// `cc_idx` - A class index.
    ///
    /// # Returns
    ///
    /// The size of the class with index `cc_idx`, or `None` if the class index is out of range.
    #[must_use]
    fn class_size(&self, cc_idx: usize) -> Option<usize>;

    // ----------------
    // Provided methods
    // ----------------

    /// The class matrix $`\mathbf{N}_r`$ for the conjugacy classes in the group.
    ///
    /// Let $`K_i`$ be the $`i^{\textrm{th}}`$ conjugacy class of the group. The
    /// elements of the class matrix $`\mathbf{N}_r`$ are given by
    ///
    /// ```math
    ///     N_{r, st} = \lvert \{ (x, y) \in K_r \times K_s : xy = z \in K_t \} \rvert,
    /// ```
    ///
    /// independent of any $`z \in K_t`$.
    ///
    /// # Arguments
    ///
    /// * `ctb_opt` - An optional Cayley table.
    /// * `r` - The index $`r`$.
    ///
    /// # Returns
    ///
    /// The class matrix $`\mathbf{N}_r`$.
    #[must_use]
    fn class_matrix(&self, ctb_opt: Option<&Array2<usize>>, r: usize) -> Array2<usize> {
        let class_number = self.class_number();
        let mut nmat_r = Array2::<usize>::zeros((class_number, class_number));
        let class_r = &self
            .get_cc_index(r)
            .unwrap_or_else(|| panic!("Conjugacy class index `{r}` not found."));

        if let Some(ctb) = ctb_opt {
            log::debug!("Computing class matrix N{r} using the Cayley table...");
            (0..class_number).for_each(|t| {
                let class_t = self
                    .get_cc_index(t)
                    .unwrap_or_else(|| panic!("Conjugacy class index `{t}` not found."));
                let rep_z_idx = *class_t
                    .iter()
                    .next()
                    .expect("No conjugacy classes can be empty.");
                for &x_idx in class_r.iter() {
                    let x_inv_idx = ctb
                        .slice(s![.., x_idx])
                        .iter()
                        .position(|&x| x == 0)
                        .unwrap_or_else(|| {
                            panic!("The inverse of element index `{x_idx}` cannot be found.")
                        });
                    let y_idx = ctb[[x_inv_idx, rep_z_idx]];
                    let s = self.get_cc_of_element_index(y_idx).unwrap_or_else(|| {
                        panic!("Conjugacy class of element index `{y_idx}` not found.")
                    });
                    nmat_r[[s, t]] += 1;
                }
            });
        } else {
            log::debug!("Computing class matrix N{r} without the Cayley table...");
            (0..class_number).for_each(|t| {
                let class_t = self
                    .get_cc_index(t)
                    .unwrap_or_else(|| panic!("Conjugacy class index `{t}` not found."));
                let rep_z_idx = *class_t
                    .iter()
                    .next()
                    .expect("No conjugacy classes can be empty.");
                let z = self
                    .get_index(rep_z_idx)
                    .unwrap_or_else(|| panic!("No element with index `{rep_z_idx}` found."));
                for &x_idx in class_r.iter() {
                    let x = self
                        .get_index(x_idx)
                        .unwrap_or_else(|| panic!("No element with index `{x_idx}` found."));
                    let y = x.clone().inv() * z.clone();
                    let y_idx = self
                        .get_index_of(&y)
                        .unwrap_or_else(|| panic!("Element `{y:?}` not found in this group."));
                    let s = self
                        .get_cc_of_element_index(y_idx)
                        .unwrap_or_else(|| panic!("Conjugacy class of element `{y:?}` not found."));
                    nmat_r[[s, t]] += 1;
                }
            });
        };

        log::debug!("Computing class matrix N{r}... Done.");
        nmat_r
    }
}

/// Trait for outputting summaries of conjugacy class properties.
pub trait ClassPropertiesSummary: ClassProperties
where
    <Self as GroupProperties>::GroupElement: fmt::Display,
{
    /// Outputs a class transversal as a nicely formatted table.
    fn class_transversal_to_string(&self) -> String {
        let cc_transversal = (0..self.class_number())
            .filter_map(|i| {
                let cc_opt = self.get_cc_symbol_of_index(i);
                let op_opt = self.get_cc_transversal(i);
                match (cc_opt, op_opt) {
                    (Some(cc), Some(op)) => Some((cc.to_string(), op.to_string())),
                    _ => None,
                }
            })
            .collect::<Vec<_>>();
        let cc_width = cc_transversal
            .iter()
            .map(|(cc, _)| cc.chars().count())
            .max()
            .unwrap_or(5)
            .max(5);
        let op_width = cc_transversal
            .iter()
            .map(|(_, op)| op.chars().count())
            .max()
            .unwrap_or(14)
            .max(14);

        let divider = "┈".repeat(cc_width + op_width + 4);
        let header = format!(" {:<cc_width$}  {:<}", "Class", "Representative");
        let body = Itertools::intersperse(
            cc_transversal
                .iter()
                .map(|(cc, op)| format!(" {:<cc_width$}  {:<}", cc, op)),
            "\n".to_string(),
        )
        .collect::<String>();

        Itertools::intersperse(
            [divider.clone(), header, divider.clone(), body, divider].into_iter(),
            "\n".to_string(),
        )
        .collect::<String>()
    }
}

// Blanket implementation
impl<G> ClassPropertiesSummary for G
where
    G: ClassProperties,
    G::GroupElement: fmt::Display,
{
}

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

/// Structure for managing class structures eagerly, *i.e.* all elements and their class maps are
/// stored.
#[derive(Builder, Clone, Serialize, Deserialize)]
pub(super) struct EagerClassStructure<T, ClassSymbol>
where
    T: Mul<Output = T> + Inv<Output = T> + Hash + Eq + Clone + Sync + fmt::Debug + FiniteOrder,
    ClassSymbol: CollectionSymbol<CollectionElement = T>,
{
    /// A vector of conjugacy classes.
    ///
    /// Each element in the vector is a hashset containing the indices of the
    /// elements in a certain ordered collection of group elements for a particular conjugacy
    /// class. This thus defines a multi-valued map from each conjugacy class index to one
    /// or more element indices in said collection.
    conjugacy_classes: Vec<HashSet<usize>>,

    /// The conjugacy class index of the elements in a certain ordered collection of group
    /// elements.
    ///
    /// This is the so-called inverse map of [`Self::conjugacy_classes`]. This maps
    /// each element index in said collection to its corresponding conjugacy class index.
    element_to_conjugacy_classes: Vec<Option<usize>>,

    /// The conjugacy class representatives of the group.
    ///
    /// Each element in the vector is an index for a representative element of the corresponding
    /// conjugacy class in a certain ordered collection of group elements.
    #[builder(setter(custom))]
    conjugacy_class_transversal: Vec<usize>,

    /// An index map of symbols for the conjugacy classes in this group.
    ///
    /// Each key in the index map is a class symbol, and the associated value is the index of
    /// the corresponding conjugacy class in [`Self::conjugacy_classes`].
    #[builder(setter(custom))]
    conjugacy_class_symbols: IndexMap<ClassSymbol, usize>,

    /// A vector containing the indices of inverse conjugacy classes.
    ///
    /// Each index gives the inverse conjugacy class for the corresponding
    /// conjugacy class.
    #[builder(setter(custom))]
    inverse_conjugacy_classes: Vec<usize>,
}

impl<T, ClassSymbol> EagerClassStructureBuilder<T, ClassSymbol>
where
    T: Mul<Output = T> + Inv<Output = T> + Hash + Eq + Clone + Sync + fmt::Debug + FiniteOrder,
    ClassSymbol: CollectionSymbol<CollectionElement = T>,
{
    fn conjugacy_class_transversal(&mut self) -> &mut Self {
        self.conjugacy_class_transversal = Some(
            self.conjugacy_classes
                .as_ref()
                .expect("Conjugacy classes have not been found.")
                .iter()
                .map(|cc| *cc.iter().min().expect("No conjugacy classes can be empty."))
                .collect::<Vec<usize>>(),
        );
        self
    }

    fn conjugacy_class_symbols(
        &mut self,
        group: &impl GroupProperties<GroupElement = T>,
    ) -> &mut Self {
        log::debug!("Assigning generic class symbols...");
        let class_sizes: Vec<_> = self
            .conjugacy_classes
            .as_ref()
            .expect("Conjugacy classes have not been found.")
            .iter()
            .map(HashSet::len)
            .collect();
        let class_symbols_iter = self
            .conjugacy_class_transversal
            .as_ref()
            .expect("A conjugacy class transversal has not been found.")
            .iter()
            .enumerate()
            .map(|(i, &rep_ele_index)| {
                let rep_ele = group.get_index(rep_ele_index).unwrap_or_else(|| {
                    panic!("Element with index {rep_ele_index} cannot be retrieved.")
                });
                (
                    ClassSymbol::from_reps(
                        format!("{}||K{i}||", class_sizes[i]).as_str(),
                        Some(vec![rep_ele]),
                    )
                    .unwrap_or_else(|_| {
                        panic!(
                            "Unable to construct a class symbol from `{}||K{i}||`.",
                            class_sizes[i]
                        )
                    }),
                    i,
                )
            });
        self.conjugacy_class_symbols = Some(class_symbols_iter.collect::<IndexMap<_, _>>());
        log::debug!("Assigning generic class symbols... Done.");
        self
    }

    fn inverse_conjugacy_classes(&mut self, ctb: &Array2<usize>) -> &mut Self {
        log::debug!("Finding inverse conjugacy classes...");
        let mut iccs: Vec<_> = self
            .conjugacy_classes
            .as_ref()
            .expect("Conjugacy classes have not been found.")
            .iter()
            .map(|_| 0usize)
            .collect();
        let class_number = self
            .conjugacy_classes
            .as_ref()
            .expect("Conjugacy classes have not been found.")
            .len();
        let mut remaining_classes: HashSet<_> = (1..class_number).collect();
        while !remaining_classes.is_empty() {
            let class_index = *remaining_classes
                .iter()
                .next()
                .expect("Unexpected empty `remaining_classes`.");
            remaining_classes.remove(&class_index);
            let g = *self
                .conjugacy_classes
                .as_ref()
                .expect("Conjugacy classes have not been found.")[class_index]
                .iter()
                .next()
                .expect("No conjugacy classes can be empty.");
            let g_inv = ctb
                .slice(s![g, ..])
                .iter()
                .position(|&x| x == 0)
                .unwrap_or_else(|| {
                    panic!("No identity element can be found in row `{g}` of Cayley table.")
                });
            let inv_class_index = self
                .element_to_conjugacy_classes
                .as_ref()
                .expect("Map from element to conjugacy class has not been found.")[g_inv]
                .unwrap_or_else(|| {
                    panic!("Element index `{g_inv}` does not have a conjugacy class.",)
                });
            iccs[class_index] = inv_class_index;
            if remaining_classes.contains(&inv_class_index) {
                remaining_classes.remove(&inv_class_index);
                iccs[inv_class_index] = class_index;
            }
        }
        assert!(iccs.iter().skip(1).all(|&x| x > 0));
        self.inverse_conjugacy_classes = Some(iccs);
        log::debug!("Finding inverse conjugacy classes... Done.");
        self
    }

    fn custom_inverse_conjugacy_classes(&mut self, iccs: Vec<usize>) -> &mut Self {
        log::debug!("Setting custom inverse conjugacy classes...");
        assert_eq!(
            iccs.len(),
            self.conjugacy_classes
                .as_ref()
                .expect("Conjugacy classes have not been set.")
                .len(),
            "The provided inverse conjugacy class structure does not have the correct class number."
        );
        self.inverse_conjugacy_classes = Some(iccs);
        log::debug!("Setting custom inverse conjugacy classes... Done.");
        self
    }
}

impl<T, ClassSymbol> EagerClassStructure<T, ClassSymbol>
where
    T: Mul<Output = T> + Inv<Output = T> + Hash + Eq + Clone + Sync + fmt::Debug + FiniteOrder,
    ClassSymbol: CollectionSymbol<CollectionElement = T>,
{
    /// Returns a builder to construct a new class structure.
    fn builder() -> EagerClassStructureBuilder<T, ClassSymbol> {
        EagerClassStructureBuilder::<T, ClassSymbol>::default()
    }

    /// Constructs a new eager class structure.
    ///
    /// # Arguments
    ///
    /// * `group` - A group with its Cayley table computed.
    /// * `conjugacy_classes` - A vector of hashsets, each of which contains the indices of the
    /// elements in `group` that are in the same conjugacy class.
    /// * `element_to_conjugacy_classes` - A vector containing the conjugacy class indices for the
    /// elements in `group`. An element might not have a conjugacy class (*e.g.*
    /// antiunitary-represented elements in magnetic-represented groups).
    ///
    /// # Returns
    ///
    /// A new class structure.
    fn new(
        group: &impl GroupProperties<GroupElement = T>,
        conjugacy_classes: Vec<HashSet<usize>>,
        element_to_conjugacy_classes: Vec<Option<usize>>,
    ) -> Self {
        let ctb_opt = group.cayley_table();
        let ctb = ctb_opt
            .as_ref()
            .expect("Cayley table not found for this group.");
        Self::builder()
            .conjugacy_classes(conjugacy_classes)
            .element_to_conjugacy_classes(element_to_conjugacy_classes)
            .conjugacy_class_transversal()
            .conjugacy_class_symbols(group)
            .inverse_conjugacy_classes(ctb)
            .build()
            .expect("Unable to construct a `EagerClassStructure`.")
    }

    /// Constructs a new eager class structure without using any information from any Cayley table.
    ///
    /// # Arguments
    ///
    /// * `conjugacy_classes` - A vector of hashsets, each of which contains the indices of the
    /// elements in `group` that are in the same conjugacy class.
    /// * `element_to_conjugacy_classes` - A vector containing the conjugacy class indices for the
    /// elements in `group`. An element might not have a conjugacy class (*e.g.*
    /// antiunitary-represented elements in magnetic-represented groups).
    ///
    /// # Returns
    ///
    /// A new class structure.
    fn new_no_ctb(
        group: &impl GroupProperties<GroupElement = T>,
        conjugacy_classes: Vec<HashSet<usize>>,
        element_to_conjugacy_classes: Vec<Option<usize>>,
        inverse_conjugacy_classes: Vec<usize>,
    ) -> Self {
        Self::builder()
            .conjugacy_classes(conjugacy_classes)
            .element_to_conjugacy_classes(element_to_conjugacy_classes)
            .conjugacy_class_transversal()
            .conjugacy_class_symbols(group)
            .custom_inverse_conjugacy_classes(inverse_conjugacy_classes)
            .build()
            .expect("Unable to construct a `EagerClassStructure`.")
    }

    /// Returns the number of conjugacy classes in the class structure.
    #[must_use]
    fn class_number(&self) -> usize {
        self.conjugacy_classes.len()
    }

    /// Sets the symbols of the conjugacy classes.
    ///
    /// # Arguments
    ///
    /// `csyms` - A slice of class symbols.
    ///
    /// # Panics
    ///
    /// Panics if the length of `csyms` does not match that of [`Self::conjugacy_classes`].
    fn set_class_symbols(&mut self, csyms: &[ClassSymbol]) {
        assert_eq!(csyms.len(), self.conjugacy_classes.len());
        self.conjugacy_class_symbols = csyms
            .iter()
            .enumerate()
            .map(|(i, cc)| (cc.clone(), i))
            .collect::<IndexMap<_, _>>();
    }
}

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

// ---------------------------------------------
// UnitaryRepresentedGroup trait implementations
// ---------------------------------------------

impl<T, RowSymbol, ColSymbol> ClassProperties for UnitaryRepresentedGroup<T, RowSymbol, ColSymbol>
where
    T: Mul<Output = T> + Inv<Output = T> + Hash + Eq + Clone + Sync + fmt::Debug + FiniteOrder,
    for<'a, 'b> &'b T: Mul<&'a T, Output = T>,
    <Self as GroupProperties>::GroupElement: Inv,
    RowSymbol: LinearSpaceSymbol,
    ColSymbol: CollectionSymbol<CollectionElement = T>,
{
    type ClassSymbol = ColSymbol;

    /// Compute the class structure of this unitary-represented group that is induced by the
    /// following equivalence relation:
    ///
    /// ```math
    ///     g \sim h \Leftrightarrow \exists u : h = u g u^{-1}.
    /// ```
    fn compute_class_structure(&mut self) -> Result<(), anyhow::Error> {
        log::debug!("Finding unitary conjugacy classes...");
        let order = self.abstract_group.order();
        let (ccs, e2ccs) = if self.abstract_group.is_abelian() {
            log::debug!("Abelian group found.");
            // Abelian group; each element is in its own conjugacy class.
            (
                (0usize..order)
                    .map(|i| HashSet::from([i]))
                    .collect::<Vec<_>>(),
                (0usize..order).map(Some).collect::<Vec<_>>(),
            )
        } else {
            // Non-Abelian group.
            log::debug!("Non-Abelian group found.");
            let mut ccs: Vec<HashSet<usize>> = vec![HashSet::from([0usize])];
            let mut e2ccs = vec![0usize; order];
            let mut remaining_elements: HashSet<usize> = (1usize..order).collect();
            let ctb = self.abstract_group.cayley_table.as_ref().expect(
                "Cayley table required for computing unitary class structure, but not found.",
            );

            while !remaining_elements.is_empty() {
                // For a fixed g, find all h such that sg = hs for all s in the group.
                let g = *remaining_elements
                    .iter()
                    .next()
                    .expect("Unexpected empty `remaining_elements`.");
                let mut cur_cc = HashSet::from([g]);
                for s in 0usize..order {
                    let sg = ctb[[s, g]];
                    let ctb_xs = ctb.slice(s![.., s]);
                    let h = ctb_xs.iter().position(|&x| x == sg).ok_or_else(|| {
                        format_err!(
                            "No element `{sg}` can be found in column `{s}` of Cayley table."
                        )
                    })?;
                    if remaining_elements.contains(&h) {
                        remaining_elements.remove(&h);
                        cur_cc.insert(h);
                    }
                }
                ccs.push(cur_cc);
            }
            ccs.sort_by_key(|cc| {
                *cc.iter()
                    .min()
                    .expect("Unable to find the minimum element index in one conjugacy class.")
            });
            ccs.iter().enumerate().for_each(|(i, cc)| {
                cc.iter().for_each(|&j| e2ccs[j] = i);
            });
            assert!(e2ccs.iter().skip(1).all(|&x| x > 0));
            (ccs, e2ccs.iter().map(|&i| Some(i)).collect::<Vec<_>>())
        };

        let class_structure =
            EagerClassStructure::<T, Self::ClassSymbol>::new(&self.abstract_group, ccs, e2ccs);
        self.class_structure = Some(class_structure);
        log::debug!("Finding unitary conjugacy classes... Done.");
        Ok(())
    }

    #[must_use]
    fn get_cc_index(&self, cc_idx: usize) -> Option<&HashSet<usize>> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_classes
            .get(cc_idx)
    }

    #[must_use]
    fn get_cc_of_element_index(&self, e_idx: usize) -> Option<usize> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .element_to_conjugacy_classes[e_idx]
    }

    #[must_use]
    fn get_cc_transversal(&self, cc_idx: usize) -> Option<Self::GroupElement> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_class_transversal
            .get(cc_idx)
            .and_then(|&i| self.get_index(i))
    }

    #[must_use]
    fn get_index_of_cc_symbol(&self, cc_sym: &Self::ClassSymbol) -> Option<usize> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_class_symbols
            .get_index_of(cc_sym)
    }

    #[must_use]
    fn get_cc_symbol_of_index(&self, cc_idx: usize) -> Option<Self::ClassSymbol> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_class_symbols
            .get_index(cc_idx)
            .map(|(cc_sym, _)| cc_sym.clone())
    }

    #[must_use]
    fn filter_cc_symbols<P: FnMut(&Self::ClassSymbol) -> bool>(
        &self,
        predicate: P,
    ) -> Vec<Self::ClassSymbol> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_class_symbols
            .keys()
            .cloned()
            .filter(predicate)
            .collect::<Vec<_>>()
    }

    fn set_class_symbols(&mut self, cc_symbols: &[Self::ClassSymbol]) {
        self.class_structure
            .as_mut()
            .unwrap()
            .set_class_symbols(cc_symbols);
    }

    #[must_use]
    fn get_inverse_cc(&self, cc_idx: usize) -> Option<usize> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .inverse_conjugacy_classes
            .get(cc_idx)
            .cloned()
    }

    #[must_use]
    fn class_number(&self) -> usize {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .class_number()
    }

    #[must_use]
    fn class_size(&self, cc_idx: usize) -> Option<usize> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_classes
            .get(cc_idx)
            .map(|cc| cc.len())
    }
}

// ----------------------------------------------
// MagneticRepresentedGroup trait implementations
// ----------------------------------------------

impl<T, UG, RowSymbol> ClassProperties for MagneticRepresentedGroup<T, UG, RowSymbol>
where
    T: Mul<Output = T> + Inv<Output = T> + Hash + Eq + Clone + Sync + fmt::Debug + FiniteOrder,
    for<'a, 'b> &'b T: Mul<&'a T, Output = T>,
    <Self as GroupProperties>::GroupElement: Inv,
    UG: Clone + GroupProperties<GroupElement = T> + CharacterProperties,
    RowSymbol: ReducibleLinearSpaceSymbol<Subspace = UG::RowSymbol> + Serialize + DeserializeOwned,
    <UG as ClassProperties>::ClassSymbol: Serialize + DeserializeOwned,
    <UG as CharacterProperties>::CharTab: Serialize + DeserializeOwned,
{
    type ClassSymbol = UG::ClassSymbol;

    /// Compute the class structure of this magnetic-represented group that is induced by the
    /// following equivalence relation:
    ///
    /// ```math
    ///     g \sim h \Leftrightarrow
    ///     \exists u : h = u g u^{-1}
    ///     \quad \textrm{or} \quad
    ///     \exists a : h = a g^{-1} a^{-1},
    /// ```
    ///
    /// where $`u`$ is unitary-represented and $`a`$ is antiunitary-represented in the group.
    fn compute_class_structure(&mut self) -> Result<(), anyhow::Error> {
        log::debug!("Finding magnetic conjugacy classes...");
        let order = self.abstract_group.order();
        let mut ccs: Vec<HashSet<usize>> = vec![HashSet::from([0usize])];
        let mut e2ccs = vec![None; order];
        let mut remaining_unitary_elements = self
            .elements()
            .iter()
            .enumerate()
            .skip(1)
            .filter_map(|(i, op)| {
                if self.check_elem_antiunitary(op) {
                    None
                } else {
                    Some(i)
                }
            })
            .collect::<HashSet<usize>>();
        let ctb =
            self.abstract_group.cayley_table.as_ref().expect(
                "Cayley table required for computing magnetic class structure, but not found.",
            );

        while !remaining_unitary_elements.is_empty() {
            // For a fixed unitary g, find all unitary h such that ug = hu for all unitary u
            // in the group, and all unitary h such that ag^(-1) = ha for all antiunitary a in
            // the group.
            let g = *remaining_unitary_elements
                .iter()
                .next()
                .expect("Unexpected empty `remaining_elements`.");
            let ctb_xg = ctb.slice(s![.., g]);
            let ginv = ctb_xg
                .iter()
                .position(|&x| x == 0)
                .unwrap_or_else(|| panic!("The inverse of `{g}` cannot be found."));
            let mut cur_cc = HashSet::from([g]);
            for (s, op) in self.elements().iter().enumerate() {
                let h = if self.check_elem_antiunitary(op) {
                    // s denotes a.
                    let sginv = ctb[[s, ginv]];
                    let ctb_xs = ctb.slice(s![.., s]);
                    ctb_xs.iter().position(|&x| x == sginv).ok_or_else(|| {
                        format_err!("No element `{sginv}` can be found in column `{s}` of Cayley table.")
                    })?
                } else {
                    // s denotes u.
                    let sg = ctb[[s, g]];
                    let ctb_xs = ctb.slice(s![.., s]);
                    ctb_xs.iter().position(|&x| x == sg).ok_or_else(|| {
                        format_err!("No element `{sg}` can be found in column `{s}` of Cayley table.")
                    })?
                };
                if remaining_unitary_elements.contains(&h) {
                    remaining_unitary_elements.remove(&h);
                    cur_cc.insert(h);
                }
            }
            ccs.push(cur_cc);
        }
        ccs.sort_by_key(|cc| {
            *cc.iter()
                .min()
                .expect("Unable to find the minimum element index in one conjugacy class.")
        });
        ccs.iter().enumerate().for_each(|(i, cc)| {
            cc.iter().for_each(|&j| e2ccs[j] = Some(i));
        });
        ensure!(e2ccs
            .iter()
            .skip(1)
            .all(|x_opt| if let Some(x) = x_opt { *x > 0 } else { true }));

        let class_structure =
            EagerClassStructure::<T, Self::ClassSymbol>::new(&self.abstract_group, ccs, e2ccs);
        self.class_structure = Some(class_structure);
        log::debug!("Finding magnetic conjugacy classes... Done.");
        Ok(())
    }

    #[must_use]
    fn get_cc_index(&self, cc_idx: usize) -> Option<&HashSet<usize>> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_classes
            .get(cc_idx)
    }

    #[must_use]
    fn get_cc_of_element_index(&self, e_idx: usize) -> Option<usize> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .element_to_conjugacy_classes[e_idx]
    }

    #[must_use]
    fn get_cc_transversal(&self, cc_idx: usize) -> Option<Self::GroupElement> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_class_transversal
            .get(cc_idx)
            .and_then(|&i| self.get_index(i))
    }

    #[must_use]
    fn get_index_of_cc_symbol(&self, cc_sym: &Self::ClassSymbol) -> Option<usize> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_class_symbols
            .get_index_of(cc_sym)
    }

    #[must_use]
    fn get_cc_symbol_of_index(&self, cc_idx: usize) -> Option<Self::ClassSymbol> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_class_symbols
            .get_index(cc_idx)
            .map(|(cc_sym, _)| cc_sym.clone())
    }

    #[must_use]
    fn filter_cc_symbols<P: FnMut(&Self::ClassSymbol) -> bool>(
        &self,
        predicate: P,
    ) -> Vec<Self::ClassSymbol> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_class_symbols
            .keys()
            .cloned()
            .filter(predicate)
            .collect::<Vec<_>>()
    }

    fn set_class_symbols(&mut self, cc_symbols: &[Self::ClassSymbol]) {
        self.class_structure
            .as_mut()
            .unwrap()
            .set_class_symbols(cc_symbols);
    }

    #[must_use]
    fn get_inverse_cc(&self, cc_idx: usize) -> Option<usize> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .inverse_conjugacy_classes
            .get(cc_idx)
            .cloned()
    }

    #[must_use]
    fn class_number(&self) -> usize {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .class_number()
    }

    #[must_use]
    fn class_size(&self, cc_idx: usize) -> Option<usize> {
        self.class_structure
            .as_ref()
            .expect("No class structure found.")
            .conjugacy_classes
            .get(cc_idx)
            .map(|cc| cc.len())
    }
}