oat_rust 0.2.0

User-friendly tools for applied topology
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
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
//! The Dowker complex of a set relation.
//! 
//! Equivalently, a simplicial complex `K` represented by a 
//! subset of simplices `Q` containing every maximal simplex of `K`.
//! 
//! 
//! 
//! # Example
//! 
//! Here we compute a basis for homology in dimension 1,
//! for the simplicial complex on vertex set `{0,1,2,3}`
//! whose maximal faces are `{0,1,2}, {0,3}, {1,3}, {2,3}`.
//! It "looks" like a tetrahedron where the 3-face and 
//! all but one of the 2-faces have been removed.
//! The coefficient field is the finite field of order 3.
//! 
//! 
//! ```
//! use oat_rust::algebra::matrices::operations::umatch::differential::DifferentialUmatch;   
//! use oat_rust::algebra::rings::types::field_prime_order::PrimeOrderField;
//!             
//! use oat_rust::topology::simplicial::from::relation::DowkerComplex;
//! use oat_rust::topology::simplicial::simplices::vector::{dimension_d_simplices_in_lexicographic_order_iter, dimension_d_simplices_in_reverse_lexicographic_order_iter}; 
//! use oat_rust::topology::simplicial::simplices::vector::{dimension_0_through_d_simplices_in_ascending_dimension_descending_lexicographic_order_iter}; 
//!     
//! use oat_rust::utilities::order::OrderOperatorAuto;        
//! use oat_rust::utilities::sequences_and_ordinals::SortedVec;
//! 
//! use itertools::Itertools;
//!     
//! // Parameters
//! // ----------
//!     
//! // Define the maximum homology dimensino we want to compute.
//! let min_homology_dimension                  =   0;
//! let max_homology_dimension                  =   2;
//!     
//! // Define the ring operator for the finite field of order 3.
//! // You can use this object to perform arithmetic operations, e.g., 
//! // to add 1 and 1, you can run `ring_operator.add(1,1)`.
//! let ring_operator          =   PrimeOrderField::new(3);
//!     
//! // We will build a dowker complex.
//! // A dowker complex is defined by a vertex set V and a family S
//! // of subsets of V.  A subset of V forms a simplex iff it is 
//! // a subset of some element of S.  We refer to the elements 
//! // of S as "dowker simplices".
//!     
//! // Each dowker simplex is represented by a SortedVec of vertices.
//! // We store the list of all such simplices inside a larger vector.
//! let dowker_simplices: Vec<SortedVec<usize>>
//!     =   vec![    
//!                 vec![0,1,2], 
//!                 vec![0,3], 
//!                 vec![1,3], 
//!                 vec![2,3]  
//!             ]
//!             .into_iter()
//!             .map( |x| SortedVec::new(x).unwrap() )  // we unwrap because `new` can return an error
//!             .collect_vec();
//!     
//! //  Boundary matrix
//! //  ---------------
//!     
//! // This is a lazy object that generates rows/columns of the boundary matrix, on demand.
//! let boundary_matrix = DowkerComplex::new( dowker_simplices.clone(), ring_operator.clone() );
//!     
//! //  Simplex iterators
//! //  -----------------
//!     
//! // An iterator that runs over all triangles in the complex, in ascending 
//! // lexicographic order
//! let triangles_ascending_order = dimension_d_simplices_in_lexicographic_order_iter( &dowker_simplices, 2);
//!     
//! // An iterator that runs over all edges in the complex, in descending 
//! // lexicographic order
//! let triangles_descending_order = dimension_d_simplices_in_reverse_lexicographic_order_iter( &dowker_simplices, 2);   
//!     
//! // An iterator that runs over simplices of dimension 0 through max_homology_dimension,
//! // ordered first by dimension (ascending) and second by lexicographic order (descending)
//! let row_indices = boundary_matrix.simplices_in_row_reduction_order( max_homology_dimension );
//! 
//! // row_indices contains a reference to boundary_matrix, which will cause problems. Instead,
//! // we can construct an iterator that runs over the same sequence of simplices, like so:
//! let row_indices     =   dimension_0_through_d_simplices_in_ascending_dimension_descending_lexicographic_order_iter(&dowker_simplices, max_homology_dimension);
//!     
//! //  Matrix factorization (used to compute homology)
//! //  -----------------------------------------------
//!     
//! // Factor the boundary matrix
//! let factored    =   DifferentialUmatch::new( 
//!                         boundary_matrix, 
//!                         min_homology_dimension,
//!                         max_homology_dimension,
//!                     );
//!                   
//! // Betti numbers  
//! // -------------
//! // 
//! // To extract betti numbers, the user has to provide a function that assigns a
//! // dimension to each index (i.e. to each simplex). This is just the length of 
//! // the simplex minus 1.
//! // The resulting object, `betti_numbers`, is a hashmap that maps dimensions to betti numbers.
//! let betti_numbers   =   factored.betti_numbers(); 
//! 
//! // This loop prints the betti numbers in dimensions 0 and 1
//! for dim in 0 .. 2 {
//!     println!(
//!             // we'll insert two values into this string
//!             "The betti number in dimension {:?} is {:?}.",
//!             dim,                        
//!             betti_numbers.get( & dim )  
//!         );            
//! }
//! println!(""); // insert line break
//! 
//! // This should print the following:
//! // The betti number in dimension 0 is 1.
//! // The betti number in dimension 1 is 2.
//! 
//! // Cycle representatives for homology
//! // ----------------------------------
//! 
//! println!(
//!         "The following are basis vectors for homology in dimensions 0 through {:?}",
//!         max_homology_dimension,
//!     );
//! for (cycle_number, cycle) in factored.homology_basis().enumerate() {
//!     // `cycle` is an iterator.  For convenience, collect the elements of the
//!     // iterator into a Rust vector.
//!     let cycle: Vec<_> = cycle.collect();
//!     println!("Basis vector {:?} = {:?}", cycle_number, cycle);
//! }
//! 
//! // This should print the following:
//! //
//! // The following are basis vectors for homology in dimensions 0 through 2
//! // Basis vector 0 = [([0], 1)]
//! // Basis vector 1 = [([0, 2], 1), ([0, 3], 2), ([2, 3], 1)]
//! // Basis vector 2 = [([0, 1], 1), ([0, 3], 2), ([1, 3], 1)]
//! ```
//! 
//! ### Try changing the coefficient ring
//! 
//! OAT has a number of different [predefined coefficient rings](crate::algebra::rings::types), which you can substitute into
//! the example above in order to calculate homology with different coefficients.  Simply replace `PrimeOrderField::new(3)` 
//! in the following line
//! ```
//! # use oat_rust::algebra::rings::types::field_prime_order::PrimeOrderField;       
//! let ring_operator   =   PrimeOrderField::new(3);
//! ``` 
//! with the [ring operator](crate::algebra::rings) of your choice.
//! 
//! ### Try changing the complex
//! 
//! These are the maximal faces in a (minimal) triangulation of the real projective plane:
//! 
//! ```
//! # use oat_rust::utilities::sequences_and_ordinals::SortedVec;
//! # use std::iter::FromIterator;
//! let dowker_simplices    =   Vec::from_iter(
//!     vec![    
//!             vec![1,2,6], vec![1,3,6], vec![1,4,5], 
//!             vec![1,2,5], vec![2,3,5], vec![2,4,6], 
//!             vec![1,3,4], vec![2,3,4], vec![3,5,6], 
//!             vec![4,5,6],  
//!         ]
//!         .into_iter()
//!         .map( |x| SortedVec::new(x) ) 
//! );
//! ```
//! 
//! # Implementation notes
//! 
//! This module uses the [SortedVec] data structure
//! for a variety of internal opertaions, e.g. binary search, set union, set difference, evaluation of
//! set containment, etc.

use crate::algebra::chain_complexes::ChainComplex;
use crate::algebra::matrices::query::{MatrixAlgebra, MatrixOracle, };
use crate::algebra::matrices::operations::MatrixOracleOperations;
use crate::algebra::rings::traits::RingOperations;
use crate::utilities::iterators::general::{symmetric_difference_of_ordered_iterators, IntersectOrderedIterators, TwoTypeIterator};
use crate::utilities::iterators::merge::hit::IteratorsMergedInSortedOrder;
use crate::utilities::iterators::is_sorted::IsSortedBy;
use crate::utilities::sequences_and_ordinals::{SortedVec, CombinationsReverse};

use std::fmt::Debug;
use std::hash::Hash;
use std::iter::{ Cloned, Flatten};
use std::slice::Iter;

use itertools::{Combinations, Dedup, Itertools, KMerge};


use crate::topology::simplicial::boundary::{SimplexBoundaryAscend, SimplexBoundaryDescend};
use crate::topology::simplicial::simplices::vector::{dimension_0_through_d_simplices_in_ascending_dimension_descending_lexicographic_order_iter, dimension_0_through_d_simplices_in_dimensionwise_lexicographic_order_iter, dimension_d_simplices_in_lexicographic_order_iter, dimension_d_simplices_in_reverse_lexicographic_order_iter};
use crate::utilities::order::{ LexicographicOrderDominatedByReverselength, OrderOperatorAuto, OrderOperatorAutoReverse, OrderOperatorByKey, OrderOperatorByKeyCustom};        








//  ===================================================================================
//  DOWKER COMPLEX
//  ===================================================================================


/// Represents the Dowker complex of a binary relation
/// 
/// Here with think of a binary relation between sets `S` and `T` as a binary matrix
/// with rows labeled by the elements of `S` and columns labeled by the elements of `T`.
/// 
/// In order to assist with indexing, we constrain the elements of `T` as follows. 
/// Note that **we call the elements of `T` vertices**.
/// - They should implement [Ord].
/// - It should be possible to convert them to `usize`; this requirement is formalized
///   by requiring `usize` to implement `From<Vertex>`.
/// - The order on vertices should be the same as the order obtained by converting
///   vertices to usize.
/// 
/// We represent the relation as a  `Vec< SortedVec< usize > >`. If `r` has this type
/// then we regard `r` as the relation that contains `(s,t)` iff `r[s]` contains `t`.
#[derive(Clone)]
pub struct DowkerComplex
            < Vertex, RingOperator >
    where
        Vertex:             Clone + Ord + Hash,
        RingOperator:       Clone + RingOperations,
        usize:              From< Vertex >,                
{
    relation_rows:          Vec< SortedVec< Vertex > >,
    relation_columns:       Vec< SortedVec< usize > >,    
    ring_operator:          RingOperator,
}


impl < Vertex, RingOperator >
    
    DowkerComplex
        < Vertex, RingOperator >
    where
        Vertex:             Clone + Debug + Ord + Hash,
        RingOperator:       Clone + RingOperations,
        usize:              From< Vertex >,                
{
    /// Construct a new Dowker complex
    pub fn new( relation_rows: Vec< SortedVec< Vertex > >, ring_operator: RingOperator ) -> Self {

        let max_column_index: Option<usize>        =   relation_rows
                                            .iter()
                                            .map(|simplex| simplex.vec().iter().cloned().map(|x| x.into())  )
                                            .flatten()
                                            .max();

        // if no dowker simplex is nonempty then we don't need to do any more work
        if max_column_index.is_none() {
            return DowkerComplex{ relation_rows, relation_columns: Vec::with_capacity(0), ring_operator, }
        }

        // otherwise let's construct the dual dowker sets
        // first we will count how much space to allocate to each row
        let mut row_counts          =   vec![ 0usize; max_column_index.unwrap() + 1 ];
        for dowker_set in relation_rows.iter() {
            for vertex in dowker_set.vec() {
                let vertex_usize: usize     =   vertex.clone().into();
                row_counts[ vertex_usize ] += 1; // the `into` method convertex the vertex to usize
            }
        }
        // now allocate the dual
        let mut transpose: Vec<Vec<usize>>    =   Vec::with_capacity( max_column_index.unwrap() );
        for row_count in row_counts { transpose.push( Vec::with_capacity(row_count) ) }

        // now construct the dual
        for ( row_index, dowker_set ) in relation_rows.iter().enumerate() {
            for vertex in dowker_set.vec() {
                let vertex_usize: usize     =   vertex.clone().into();
                let row_of_transpose: &mut Vec<usize> = transpose.get_mut( vertex_usize ).unwrap();
                row_of_transpose.push( row_index ); // the `into` method convertex the vertex to usize
            }
        }      
        let mut transpose_safe =   Vec::with_capacity( transpose.len() );
        for sorted_set in transpose.into_iter() {
            transpose_safe.push( SortedVec::new(sorted_set).ok().unwrap() );
        }

        DowkerComplex{ relation_rows, relation_columns: transpose_safe, ring_operator, }
    }

    /// Construct a new Dowker complex from vectors; panics if one of the vectors is not sorted in strictly ascending order.
    pub fn from_vectors( dowker_simplices: Vec< Vec< Vertex > >, ring_operator: RingOperator ) -> Result< Self, Vec< Vertex > > {
        let mut relation_rows = Vec::with_capacity( dowker_simplices.len() );
        for ( counter, simplex ) in dowker_simplices.into_iter().enumerate() {
            match SortedVec::new(simplex) {
                Err( vec ) => { 
                    println!("Error: attempted to create a Dowker boundary matrix from a sequence of simplices, but simplex number {:?}, which is {:?}, is not sorted.", counter, &vec); 
                    return Err( vec );
                } Ok(set) => { 
                    relation_rows.push( set ); 
                }
            }            
        }
        // let relation_rows = dowker_simplices.into_iter().map(|sequence| SortedVec::new(sequence)? ).collect();
        Ok( DowkerComplex::new( relation_rows, ring_operator ) )
    }    
    
    /// The rows of the relation
    /// 
    /// Each row is formatted as a [SortedVec] of elements (or vertices).
    pub fn relation_rows( &self ) -> & Vec< SortedVec< Vertex > > { & self.relation_rows }  

    /// The columns of the relation
    /// 
    /// Each column is formatted as a [SortedVec] of `usize` integers
    pub fn relation_columns( &self ) -> & Vec< SortedVec< usize > > { & self.relation_columns }      


    /// Returns the maximum index of any vertex (convertex to `usize`)
    fn max_vertex( &self ) -> Option< Vertex > {
        self.relation_rows
            .iter()
            .map(|simplex| simplex.vec().iter().cloned()  )
            .flatten()
            .max()
    }    

    /// Returns the sequence of simplices
    /// contained in the Dowker complex that have dimension ≤ `max_simplex_dimension`, ordered (first) 
    /// in ascending order of dimension, and (second) in descending lexicographic
    /// order (excluding simplices of dimension `> max_simplex_dimension`).
    /// 
    /// This is the same order in which we visit rows of the bounday matrix during the cohomology reduction
    /// algorithm.
    /// 
    /// 
    /// **There are many other tools to enumerate the simplices in the Dowker complex, in addition.** See the
    /// methods in [crate::topology::simplicial::simplices::vector] for examples.
    pub fn simplices_in_row_reduction_order( 
                    &self, 
                    max_simplex_dimension: isize 
                ) -> 
        Flatten< std::vec::IntoIter<
            TwoTypeIterator<
                std::iter::Empty< Vec<Vertex> >,
                Dedup< 
                    IteratorsMergedInSortedOrder< 
                        CombinationsReverse< Vertex, &Vec< Vertex > >,
                        OrderOperatorAutoReverse,
                    >,  
                >        
            >,   
        >>                            
    {
        return dimension_0_through_d_simplices_in_ascending_dimension_descending_lexicographic_order_iter( 
                    self.relation_rows(), 
                    max_simplex_dimension 
                )

    } 

    /// Returns the simplices of the Dowker comples (up to dimension `max_simplex_dimension`) in lexicographic order
    /// 
    /// **There are many other tools to enumerate the simplices in the Dowker complex, in addition.** See the
    /// methods in [crate::topology::simplicial::simplices::vector] for examples. Most of these methods take a
    /// `& Vec< SortedVec< Vertex > >` as input; you can obtain this from a [DowkerComplex] using the [DowkerComplex::relation_rows]
    /// method.
    pub fn simplices_in_lexicographic_order( &self, max_simplex_dimension: isize )
        ->
        Flatten<
            std::vec::IntoIter<
                TwoTypeIterator<
                    std::iter::Empty< Vec<Vertex> >,
                    Dedup< KMerge<  Combinations<Cloned<Iter<Vertex>>> > >,
                >
            >
        >        
    {
        dimension_0_through_d_simplices_in_dimensionwise_lexicographic_order_iter( & self.relation_rows, max_simplex_dimension )
    }


}






impl < Vertex, RingOperator >

    MatrixOracle for 

    DowkerComplex
        < Vertex, RingOperator >

    where
        Vertex:                     Clone + Debug + Ord + Hash,
        RingOperator:               Clone + RingOperations,
        usize:                      From< Vertex >,
{
    type Coefficient            =   RingOperator::Element;

    type RowIndex               =   Vec< Vertex >;

    type ColumnIndex            =   Vec< Vertex >;

    type RowEntry               =   ( Vec< Vertex >, Self::Coefficient );

    type ColumnEntry            =   ( Vec< Vertex >, Self::Coefficient );

    type Row                    =   DowkerBoundaryMatrixRow< Vertex, RingOperator >;

    type RowReverse             =   DowkerBoundaryMatrixRowReverse< Vertex, RingOperator >;

    type Column                 =   SimplexBoundaryAscend< Vertex, RingOperator >;

    type ColumnReverse          =   SimplexBoundaryDescend< Vertex, RingOperator >;

    fn row(                     &   self, index: & Self::RowIndex   )   -> Self::Row {
        DowkerBoundaryMatrixRow::from_vec_of_dowker_sets( index.clone(), & self.relation_rows, self.ring_operator.clone() ).unwrap()
    }

    fn row_reverse(             &   self, index: & Self::RowIndex   )   -> Self::RowReverse {
        DowkerBoundaryMatrixRowReverse::from_vec_of_dowker_sets( index.clone(), & self.relation_rows, self.ring_operator.clone() ).unwrap()
    }

    fn column(                  &   self, index: & Self::ColumnIndex)   -> Self::Column {
        SimplexBoundaryAscend::new( index.clone(), self.ring_operator.clone() )
    }

    fn column_reverse(          &   self, index: & Self::ColumnIndex)   -> Self::ColumnReverse {
        SimplexBoundaryDescend::new( index.clone(), self.ring_operator.clone() )
    }

    fn has_row_for_index(     &   self, index: & Self::RowIndex   )   -> bool {

        // the vector must be strictly sorted
        if ! index.iter().is_sorted_by( |a,b| a.lt(b) ) {
            return false
        }

        // we have an `index` which is a collection of integers indexing the columns of the binary relation
        // matrix. the question is whether there exists a row index where all of these rows are
        // nonzero. we can answer this question by intersecting the column indices of all the rows
        IntersectOrderedIterators::new(
            index.iter().map(|v| self.relation_columns[ usize::from(v.clone()) ].vec().iter().cloned() )               
        )
        .next()
        .is_some()
    }

    fn has_column_for_index(  &   self, index: & Self::ColumnIndex)   -> bool {
        // the test for containing a row is the same as the test for containing a column
        self.has_row_for_index(index)
    }

    fn structural_nonzero_entry(&   self, row:   & Self::RowIndex, column: & Self::ColumnIndex ) ->  Option< Self::Coefficient > {

        // both indices must be
        if !  self.has_row_for_index(row)  {
            panic!("Attempted to look up the entry in row {:?}, column {:?} of a Dowker boundary matrix, but {:?} is either an invalid simplex (not sorted or has a repeat entry) or it is not contained in the Dowker compelx", row, column, row );
        }
        if !  self.has_column_for_index(row)  {
            panic!("Attempted to look up the entry in row {:?}, column {:?} of a Dowker boundary matrix, but {:?} is either an invalid simplex (not sorted or has a repeat entry) or it is not contained in the Dowker compelx", row, column, column );
        }        

        // this criterion has to be satisfied to have a facet-cofacet pair
        if column.len() != row.len() + 1 { 
            return None 
        }

        // get the symmetric difference
        let mut symmetric_difference                        =   symmetric_difference_of_ordered_iterators( row.iter(), column.iter() );
        let first_different_vertex                      =   symmetric_difference.next();  
        
        if symmetric_difference.next().is_some() {
            // in this case the row and column indices differ by more than one vertex, so they cannot be a facet-cofacet pair
            return None
        } else {
            // in this case we do have a facet-cofacet pair, so we have to calculate the corresponding coefficient by finding 
            // which vertex in column is not contained in row, and then calculating the corresponding coefficient
            for (counter, vertex) in column.iter().enumerate() {
                if Some(vertex) == first_different_vertex {
                    let coefficient         =   self.ring_operator.minus_one_to_power( counter );
                    return Some( coefficient )
                }
            }
        }

        panic!("Error retreiving structural nonzero entry.");
    }
}









impl < Vertex, RingOperator >

    MatrixAlgebra for 

    DowkerComplex
        < Vertex, RingOperator >

    where
        Vertex:                     Clone + Debug + Ord + Hash,
        RingOperator:               Clone + RingOperations,
        usize:                      From< Vertex >,
{
    type RingOperator                                   =   RingOperator;

    type OrderOperatorForRowEntries                     =   OrderOperatorByKeyCustom<LexicographicOrderDominatedByReverselength>;

    type OrderOperatorForRowIndices                     =   LexicographicOrderDominatedByReverselength;

    type OrderOperatorForColumnEntries                  =   OrderOperatorByKeyCustom<LexicographicOrderDominatedByReverselength>;

    type OrderOperatorForColumnIndices                  =   LexicographicOrderDominatedByReverselength;

    fn ring_operator( &self ) -> Self::RingOperator {
        self.ring_operator.clone()
    }

    fn order_operator_for_row_entries( &self ) -> Self::OrderOperatorForRowEntries {
        OrderOperatorByKeyCustom::new(LexicographicOrderDominatedByReverselength::new())
    }

    fn order_operator_for_row_indices( &self ) -> Self::OrderOperatorForRowIndices {
        LexicographicOrderDominatedByReverselength::new()
    }

    fn order_operator_for_column_entries( &self ) -> Self::OrderOperatorForColumnEntries {
        OrderOperatorByKeyCustom::new(LexicographicOrderDominatedByReverselength::new())
    }

    fn order_operator_for_column_indices( &self ) -> Self::OrderOperatorForColumnIndices {
        LexicographicOrderDominatedByReverselength::new()
    }
}










impl < Vertex, RingOperator >

    MatrixOracleOperations for 

    DowkerComplex
        < Vertex, RingOperator >

    where // these are the requirements to implement the `MatrixOracle` trait
        Vertex:                     Clone + Debug + Ord + Hash,
        RingOperator:               Clone + RingOperations,
        usize:                      From< Vertex >,

{}





















impl < Vertex, RingOperator >

    ChainComplex for 

    DowkerComplex
        < Vertex, RingOperator >

    where
        Vertex:                     Clone + Debug + Ord + Hash,
        RingOperator:               Clone + RingOperations,
        usize:                      From< Vertex >,
{
    type BasisVectorIndicesIterable         =   Vec< Vec< Vertex > >;

    /// Returns simplices of dimension `dimension` in lexicographic order.
    fn basis_vector_indices_for_dimension( &self, dimension: isize ) -> Self::BasisVectorIndicesIterable {
        dimension_d_simplices_in_lexicographic_order_iter(
            & self.relation_rows, 
            dimension
        ).collect()
    }

    fn dimension_for_basis_vector_with_index( &self, index: & Self::RowIndex ) -> Result<isize, Self::RowIndex> {
        if ! self.has_row_for_index(index) {
            return Err( index.clone() )
        }
        Ok( (index.len() as isize) - 1 )
    }
}
















//  ===================================================================================
//  ROWS AND COLUMNS
//  ===================================================================================


//  COUBOUNDARY VECTOR DESCENDING
//  -----------------------------------------------------------------------------------


/// Iterates over the terms of the coboundary of a simplex in descending lexicographic order.
/// 
/// The coefficient is calculated as `(-1)^k`, where `xk` is the vertex which appears twice in the template.
/// 
/// # Examples
/// 
/// ```
/// use oat_rust::topology::simplicial::from::relation::DowkerBoundaryMatrixRowReverse;
/// use oat_rust::algebra::rings::types::field_prime_order::PrimeOrderField;
/// use oat_rust::utilities::sequences_and_ordinals::SortedVec;
/// 
/// let ring_operator = PrimeOrderField::new(3);
/// let simplex = vec![1,3];
/// let relation_rows = vec![ SortedVec::from_iter( vec![0,1,2,3,4] ).unwrap() ];
/// // note we have to unwrap, because the constructor returns a Result
/// let coboundary = DowkerBoundaryMatrixRowReverse::from_vec_of_dowker_sets( simplex, &relation_rows, ring_operator ).unwrap();
/// 
/// itertools::assert_equal( coboundary, vec![ (vec![1,3,4], 1), (vec![1,2,3], 2), (vec![0,1,3], 1) ]);
/// ```
#[derive(Clone, Debug)]
pub struct DowkerBoundaryMatrixRowReverse< Vertex, RingOperator >
    where 
        RingOperator:       RingOperations,
        Vertex:             Ord + Clone,
{
    next_cofacet_opt:           Option< Vec< Vertex > >,
    next_coefficient:           RingOperator::Element,
    vertices_to_insert:         Vec< Vertex >,              // should be sorted in ascending order
    retrieval_locus:            usize,                      // the place where the vertex inserted into `next_cofacet` was drawn from 
    insertion_locus:            usize,                      // this points to the place in `next_cofacet` where we inserted a vertex    
    ring_operator:              RingOperator,
}   


impl < Vertex, RingOperator >

DowkerBoundaryMatrixRowReverse
        < Vertex, RingOperator >

    where 
        RingOperator:       RingOperations,
        Vertex:             Clone + Debug + Hash + Ord,
{
    /// Generates a [DowkerBoundaryMatrixRow] for a simplex, given a CSR representation of the binary dowker matrix.    
    pub fn from_vec_of_dowker_sets( facet: Vec< Vertex >, relation_rows: &Vec< SortedVec< Vertex > >, ring_operator: RingOperator ) -> Result< Self, Vec< Vertex > > {

        //  the coboundary of the empty simplex is zero;
        //  ---------------------------------------------
        if facet.is_empty() {
            // this iterator will be identified as empty, because `vertices_to_insert` is empty
            return Ok( DowkerBoundaryMatrixRowReverse{
                next_cofacet_opt:           None,
                next_coefficient:           RingOperator::one(),   // these are arbitrary values            
                vertices_to_insert:         vec![],                // these are arbitrary values  
                retrieval_locus:            0,                     // these are arbitrary values  
                insertion_locus:            0,                     // these are arbitrary values  
                ring_operator,         // these are arbitrary values                    
            } )                   
        }

        let facet = SortedVec::new( facet ); // place the facet in a safe wrapper; this will panic if the facet isn't strictly sorted
        if let Err(vec) = facet { 
            println!("Error: attempted to compute the coboundary of simplex {:?}, but the vertices of the simplex are not sorted in strictly ascending order", &vec );
            return Err( vec );
        }
        let facet = facet.unwrap(); // we have handled the error, so it's safe to unwrap
        let vertices_to_insert: Vec< Vertex >   =   relation_rows
                                                        .iter()
                                                        .filter( |x|  x.contains_subset( &facet ) )
                                                        .map(|x| x.vec().iter())
                                                        .kmerge()
                                                        .dedup()
                                                        .filter(|x| ! facet.contains(x) )
                                                        .cloned()
                                                        .collect();


        // //  obtain a list of vertices to insert
        // //  ---------------------------------------------        
        // let facet_vertex_set = SortedVec::from_iter( facet.iter().cloned() );
        // let mut vertices_to_insert = SortedVec::new();

        // // take the union of all dowker super-simplices
        // for dowker_set in relation_rows
        //                                 .iter() // iterate over dowker siplices
        //                                 .filter( |x| x.is_superset( &facet_vertex_set) ) // only keep those that contain the facet
        //     {
        //         vertices_to_insert.extend( dowker_set.iter().cloned() )
        //     }

        // // take a set difference: vertices_to_insert \ facet
        // for vertex in facet.iter() { vertices_to_insert.remove( vertex ); }

        // // convert to a vector
        // let mut vertices_to_insert: Vec< Vertex > = vertices_to_insert.into_iter().collect();

        // // sort the vector
        // vertices_to_insert.sort();
        

        //  if there are no vertices to insert, then the coboundary is zero
        //  ---------------------------------------------
        if vertices_to_insert.is_empty() {
            // this iterator is empty, because `next_cofacet_opt` is None
            return Ok( DowkerBoundaryMatrixRowReverse{
                next_cofacet_opt:           None,
                next_coefficient:           RingOperator::one(),     // these are abitrary values            
                vertices_to_insert:         vec![],                  // these are abitrary values  
                retrieval_locus:            0,                       // these are abitrary values  
                insertion_locus:            0,                       // these are abitrary values  
                ring_operator,           // these are abitrary values                    
            } )                  
        }


        //  generate the rest of the initial data
        //  ---------------------------------------------                

        let mut coefficient = ring_operator.minus_one_to_power( facet.len() );
        let mut next_cofacet = facet.into_vec();
        let mut insertion_locus = next_cofacet.len();
        let retrieval_locus = vertices_to_insert.len() - 1;        
        let inserted_vertex = vertices_to_insert[retrieval_locus].clone();
        while   ( insertion_locus > 0 ) 
                && 
                ( inserted_vertex < next_cofacet[ insertion_locus - 1 ] ) {
            insertion_locus -= 1;
            coefficient = ring_operator.negate( coefficient );
        }
        next_cofacet.insert( insertion_locus, inserted_vertex );

        Ok( DowkerBoundaryMatrixRowReverse{
            next_cofacet_opt:           Some( next_cofacet ),
            next_coefficient:           coefficient,
            vertices_to_insert,         // should be sorted in ascending order
            retrieval_locus,            // the place where the inserted vertex was drawn from 
            insertion_locus,            // this points to the place in `next_cofacet` where we inserted a vertex
            ring_operator,             
        } )

    }

    // /// Generates a [DowkerBoundaryMatrixRow] for a simplex, given CSR and CSC representations of the binary dowker matrix.
    // fn from_csr_and_csc_matrices( facet: Vec< Vertex >, dowker_matrix_csr: Vec< SortedVec< Vertex > >, dowker_matrix_csc: Vec< SortedVec< Vertex > > ) {        
    // }
}


impl < Vertex, RingOperator >

    Iterator for

    DowkerBoundaryMatrixRowReverse
        < Vertex, RingOperator >

    where 
        RingOperator:       RingOperations,
        Vertex:             Ord + Clone,   
{
    type Item = (Vec<Vertex>, RingOperator::Element);

    fn next( &mut self ) -> Option< Self::Item >{

        match self.next_cofacet_opt {
            None => { None }
            Some( ref mut next_cofacet ) => {

                // make a copy of the return value, before we change the internal state of the iterator
                let return_value    =   ( next_cofacet.clone(), self.next_coefficient.clone() );

                // if there are more vertices to insert, update the internal state with data for the next entry
                if self.retrieval_locus > 0 {
                    
                    // grab the next vertex
                    self.retrieval_locus -= 1; 
                    let inserted_vertex     =   self.vertices_to_insert[ self.retrieval_locus ].clone();
                    
                    // update pointers and coefficients
                    while   ( self.insertion_locus > 0 ) 
                            && 
                            ( inserted_vertex < next_cofacet[ self.insertion_locus - 1 ] ) {
                        next_cofacet[ self.insertion_locus ] = next_cofacet[ self.insertion_locus - 1 ].clone();
                        self.insertion_locus -= 1;
                        self.next_coefficient = self.ring_operator.negate( self.next_coefficient.clone() );
                    }
                    // update the cofacet
                    next_cofacet[ self.insertion_locus ] = inserted_vertex;
                } 
                // otherwise flag the fact that there will be no more entries, after we return the one that we have already extracted
                else {
                    self.next_cofacet_opt = None;
                }

                Some( return_value )
            }
        }

    }
}       



//  COUBOUNDARY VECTOR ASCENDING
//  -----------------------------------------------------------------------------------

/// Iterates over the terms of the coboundary of a simplex in ascending lexicographic order.
/// 
/// The coefficient is calculated as `(-1)^k`, where `xk` is the vertex which appears twice in the template.
/// 
/// # Examples
/// 
/// ```
/// use oat_rust::topology::simplicial::from::relation::DowkerBoundaryMatrixRow;
/// use oat_rust::algebra::rings::types::field_prime_order::PrimeOrderField;
/// use oat_rust::utilities::sequences_and_ordinals::SortedVec;
/// 
/// let ring_operator = PrimeOrderField::new(3);
/// let simplex = vec![1,3];
/// let relation_rows = vec![ SortedVec::from_iter( vec![0,1,2,3,4] ).unwrap() ];
/// 
/// // note we have to unwrap, because the constructor returns a Result
/// let coboundary = DowkerBoundaryMatrixRow::from_vec_of_dowker_sets( simplex, &relation_rows, ring_operator ).unwrap();
/// 
/// itertools::assert_equal( coboundary, vec![ (vec![0,1,3], 1), (vec![1,2,3], 2), (vec![1,3,4], 1) ]);
/// ```
#[derive(Clone, Debug)]
pub struct DowkerBoundaryMatrixRow< Vertex, RingOperator >
    where 
        RingOperator:       RingOperations,
        Vertex:             Ord + Clone,
{
    next_cofacet_opt:           Option< Vec< Vertex > >,
    next_coefficient:           RingOperator::Element,
    vertices_to_insert:         Vec< Vertex >,              // should be sorted in ascending order
    retrieval_locus:            usize,                      // the place where the vertex inserted into `next_cofacet` was drawn from 
    insertion_locus:            usize,                      // this points to the place in `next_cofacet` where we inserted a vertex    
    ring_operator:              RingOperator,
}   


impl < Vertex, RingOperator >

    DowkerBoundaryMatrixRow
        < Vertex, RingOperator >

    where 
        RingOperator:       RingOperations,
        Vertex:             Clone + Debug + Hash + Ord,
{
    /// Generates a [DowkerBoundaryMatrixRow] for a simplex, given a CSR representation of the binary dowker matrix.    
    pub fn from_vec_of_dowker_sets( facet: Vec< Vertex >, relation_rows: &Vec< SortedVec< Vertex > >, ring_operator: RingOperator ) -> Result< Self, Vec< Vertex > > {

        //  the coboundary of the empty simplex is zero;
        //  ---------------------------------------------
        if facet.is_empty() {
            // this iterator will be identified as empty, because `vertices_to_insert` is empty
            return Ok( DowkerBoundaryMatrixRow{
                next_cofacet_opt:           None,
                next_coefficient:           RingOperator::one(),   // these are arbitrary values            
                vertices_to_insert:         vec![],                // these are arbitrary values  
                retrieval_locus:            0,                     // these are arbitrary values  
                insertion_locus:            0,                     // these are arbitrary values  
                ring_operator,         // these are arbitrary values                    
            } )                
        }


        let facet = SortedVec::new( facet ); // place the facet in a safe wrapper; this will panic if the facet isn't strictly sorted
        if let Err(vec) = facet { 
            println!("Error: attempted to compute the coboundary of simplex {:?}, but the vertices of the simplex are not sorted in strictly ascending order", &vec );
            return Err( vec );
        }
        let facet = facet.unwrap(); // we have handled the error, so it's safe to unwrap
        let vertices_to_insert: Vec< Vertex >   =   relation_rows
                                                        .iter()
                                                        .filter( |x|  x.contains_subset( &facet ) )
                                                        .map(|x| x.vec().iter())
                                                        .kmerge()
                                                        .dedup()
                                                        .filter(|x| ! facet.contains(x) )
                                                        .cloned()
                                                        .collect();
        // //  obtain a list of vertices to insert
        // //  ---------------------------------------------        
        // let facet_vertex_set = SortedVec::from_iter( facet.iter().cloned() );
        // let mut vertices_to_insert = SortedVec::new();

        // // take the union of all dowker super-simplices
        // for dowker_simplex in relation_rows
        //                                 .iter() // iterate over dowker siplices
        //                                 .filter( |x| x.is_superset( &facet_vertex_set) ) // only keep those that contain the facet
        //     {
        //         vertices_to_insert.extend( dowker_simplex.iter().cloned() )
        //     }

        // // take a set difference: vertices_to_insert \ facet
        // for vertex in facet.iter() { vertices_to_insert.remove( vertex ); }

        // // convert to a vector
        // let mut vertices_to_insert: Vec< Vertex > = vertices_to_insert.into_iter().collect();

        // // sort the vector
        // vertices_to_insert.sort();
        

        //  the coboundary of the empty simplex is zero;
        //  ---------------------------------------------
        if vertices_to_insert.is_empty() {
            // this iterator is empty, because `vertices_to_insert` is empty
            return Ok( DowkerBoundaryMatrixRow{
                next_cofacet_opt:           None,
                next_coefficient:           RingOperator::one(),
                vertices_to_insert:         vec![],     
                retrieval_locus:            0,          
                insertion_locus:            0,          
                ring_operator,                
            } )                   
        }


        //  generate the rest of the initial data
        //  ---------------------------------------------                

        let mut coefficient = RingOperator::one();
        let mut next_cofacet = facet.into_vec();
        let mut insertion_locus = 0;
        let retrieval_locus = 0;        
        let inserted_vertex = vertices_to_insert[retrieval_locus].clone();
        while   ( insertion_locus < next_cofacet.len() ) 
                && 
                ( inserted_vertex > next_cofacet[ insertion_locus ] ) {
            insertion_locus += 1;
            coefficient = ring_operator.negate( coefficient );
        }
        next_cofacet.insert( insertion_locus, inserted_vertex );

        Ok( DowkerBoundaryMatrixRow{
            next_cofacet_opt:           Some( next_cofacet ),
            next_coefficient:           coefficient,
            vertices_to_insert,         // should be sorted in ascending order
            retrieval_locus,            // the place where the inserted vertex was drawn from 
            insertion_locus,            // this points to the place in `next_cofacet` where we inserted a vertex
            ring_operator,             
        } )

    }

    // /// Generates a [DowkerBoundaryMatrixRow] for a simplex, given CSR and CSC representations of the binary dowker matrix.
    // fn from_csr_and_csc_matrices( facet: Vec< Vertex >, dowker_matrix_csr: Vec< SortedVec< Vertex > >, dowker_matrix_csc: Vec< SortedVec< Vertex > > ) {        
    // }
}


impl < Vertex, RingOperator >

    Iterator for

    DowkerBoundaryMatrixRow
        < Vertex, RingOperator >

    where 
        RingOperator:       RingOperations,
        Vertex:             Ord + Clone,   
{
    type Item = (Vec<Vertex>, RingOperator::Element);

    fn next( &mut self ) -> Option< Self::Item >{

        // println!("{:?} -- DELETE THIS AND ALL DEBUG REQUIREMENTS ON THIS IMPLEMENTATION OF ITER", &self);

        match self.next_cofacet_opt {
            None => { None }
            Some( ref mut next_cofacet ) => {

                // make a copy of the return value, before we change the internal state of the iterator
                let return_value    =   ( next_cofacet.clone(), self.next_coefficient.clone() );

                // if there are more vertices to insert, update the internal state with data for the next entry
                if self.retrieval_locus + 1 < self.vertices_to_insert.len() {
                    
                    // grab the next vertex
                    self.retrieval_locus += 1; 
                    let inserted_vertex     =   self.vertices_to_insert[ self.retrieval_locus ].clone();
                    
                    // update pointers and coefficients
                    while   ( self.insertion_locus + 1 < next_cofacet.len() ) 
                            && 
                            ( inserted_vertex > next_cofacet[ self.insertion_locus + 1 ] ) {
                        next_cofacet[ self.insertion_locus ] = next_cofacet[ self.insertion_locus +1 ].clone();
                        self.insertion_locus += 1;
                        self.next_coefficient = self.ring_operator.negate( self.next_coefficient.clone() );
                    }
                    // update the cofacet
                    next_cofacet[ self.insertion_locus ] = inserted_vertex;
                } 
                // otherwise flag the fact that there will be no more entries, after we return the one that we have already extracted
                else {
                    self.next_cofacet_opt = None;
                }

                Some( return_value )
            }
        }

    }
}    















//  ===================================================================================
//  CONSTRUCTORS
//  ===================================================================================



/// Construct a "sideways ladder".  
/// 
/// # Examples
/// 
/// The following is created by calling
/// `sideways_ladder_edges( 0, 2 )`
/// 
/// ```text
/// 1 ---- 3 ---- 5
/// |      |      |
/// |      |      |
/// 0 ---- 2 ---- 4
/// ```
/// 
/// The following is created by calling
/// `sideways_ladder( 1,3 )`
/// 
/// ```text
/// 3 ---- 5 ---- 7 ---- 9
/// |      |      |      |
/// |      |      |      |
/// 2 ---- 4 ---- 6 ---- 8
/// ```
/// # Code example
/// 
/// ```
/// use oat_rust::topology::simplicial::from::relation::sideways_ladder_edges;
/// 
/// // construct the edges
/// let number_of_holes         =   1;
/// let offset_from_left        =   1;
/// let mut edges           =   sideways_ladder_edges(number_of_holes, offset_from_left);
/// edges.sort();
/// 
/// // this is the ground truth
/// let ground_truth            =   vec![ 
///                                     vec![2,3], 
///                                     vec![2,4],
///                                     vec![3,5],
///                                     vec![4,5],                                            
///                                 ];
/// 
/// assert_eq!( edges, ground_truth );
/// ```
pub fn sideways_ladder_edges( offset_from_left: usize, number_of_holes: usize ) -> Vec< Vec< usize > > {
    let mut hyperedges                              =   Vec::with_capacity( 1 + 3 * number_of_holes );

    // vertical parts
    for p in offset_from_left .. offset_from_left + number_of_holes + 1 {
        hyperedges.push( vec![2*p, 2*p + 1] )
    }
        
    // horizontal parts
    for p in offset_from_left .. offset_from_left + number_of_holes {
        hyperedges.push( vec![ 2*p,     2*(p+1)     ] );
        hyperedges.push( vec![ 2*p + 1, 2*(p+1) + 1 ] );
    }

    return hyperedges
}






















//  ===================================================================================
//  TESTING
//  ===================================================================================




use crate::algebra::matrices::debug::{matrix_oracle_is_internally_consistent, matrix_order_operators_are_internally_consistent };
use crate::algebra::rings::types::field_prime_order::PrimeOrderField;


/// Builds a Dowker boundary matrix for the user-provided list of relation rows, and validates it with [matrix_oracle_is_internally_consistent] and [matrix_order_operators_are_internally_consistent]
/// 
/// The `relation_rows` argument is a list of sorted lists, where each sorted list records the column indices of the nonzero entries in a given row of the 
/// binary relation matrix.
pub fn validate_dowker_boundary_matrix<Vertex>( 
        relation_rows: Vec<SortedVec<Vertex>>, 
        max_dim: isize, 
    )  

    where 
        Vertex:     Clone + Hash + Debug + Ord,
        usize:      From< Vertex >,
{
    
    // define the boundary matrix
    // --------------------------

    let ring_operator                               =   PrimeOrderField::new(47);    
    let boundary_matrix                             =   DowkerComplex::new( relation_rows.clone(), ring_operator );        

    // verify that the matrix lookup operations are internally consistent
    // ------------------------------------------------------------------

    // get row indices in order
    let mut sorted_row_indices: Vec<_>               =   boundary_matrix.simplices_in_row_reduction_order( max_dim ).collect();
    ( &mut sorted_row_indices ).reverse(); // row reduction order is the REVERSE of the actual order on rows

    // get column indices in order    
    let mut sorted_column_indices: Vec<_>               =   boundary_matrix.simplices_in_row_reduction_order( max_dim + 1 ).collect();
    ( &mut sorted_column_indices ).reverse(); // row reduction order is the REVERSE of the actual order on rows    


    assert!(
        matrix_oracle_is_internally_consistent(
            & boundary_matrix,
            sorted_row_indices.clone(),
            sorted_column_indices.clone(),
        )
    );

    // verify that the matrix order operators are internally consistent
    // ----------------------------------------------------------------
    assert!(
        matrix_order_operators_are_internally_consistent(
            & boundary_matrix,
            sorted_row_indices.clone(),
            sorted_column_indices.clone(),
        )
        .is_ok()
    ); 
}




#[cfg(test)]
mod tests {
    

use crate::utilities::random::random_sequences;

    // Note this useful idiom: importing names from outer (for mod tests) scope.
    use super::*;

    #[test]
    fn test_dowker_boundary_small(){
        
        // used for enumerating simplices
        let relation_rows: Vec<Vec<usize>> =  
                vec![ 
                        vec![0,1,2],
                        vec![0,3],                      
                        vec![1,3], 
                        vec![2,3]                                           
                    ];     
        let relation_rows =     relation_rows.into_iter()
                                    .map(|v| SortedVec::new(v).ok().unwrap() )
                                    .collect::<Vec<_>>();

        validate_dowker_boundary_matrix( relation_rows, 2 );
    }



    #[test]
    fn test_dowker_boundary_big(){
        
        for _ in 0..10 {
            
            let relation_rows    =   random_sequences(7, (0..7).step_by(2), 0.1 );
            let _verbose = false;
            validate_dowker_boundary_matrix( relation_rows, 2 );
        }

    }    



}   



#[cfg(test)]
mod docstring_tests {
    use crate::topology::simplicial::simplices::vector::{dimension_d_simplices_in_lexicographic_order_iter, dimension_d_simplices_in_reverse_lexicographic_order_iter, dimension_0_through_d_simplices_in_ascending_dimension_descending_lexicographic_order_iter};

    



    #[test]
    fn docstring_test_dowker_homology() {
        use itertools::Itertools;
            
        use crate::topology::simplicial::from::relation::DowkerComplex;
            
        use crate::algebra::matrices::operations::umatch::differential::DifferentialUmatch;     
        use crate::algebra::rings::types::field_prime_order::PrimeOrderField;        
      
        use crate::utilities::sequences_and_ordinals::SortedVec;
            
        // Parameters
        // ----------
            
        // Define the maximum homology dimensino we want to compute.
        let min_homology_dimension                         =   0;
        let max_homology_dimension                  =   2;
            
        // Define the ring operator for the finite field of order 3.
        // You can use this object to perform arithmetic operations, e.g., 
        // to add 1 and 1, you can run `ring_operator.add(1,1)`.
        let ring_operator          =   PrimeOrderField::new(3);
            
        // We will build a dowker complex.
        // A dowker complex is defined by a vertex set V and a family S
        // of subsets of V.  A subset of V forms a simplex iff it is 
        // a subset of some element of S.  We refer to the elements 
        // of S as "dowker simplices".
            
        // Each dowker simplex is represented by a SortedVec of vertices.
        // We store the list of all such simplices inside a larger vector.
        let dowker_simplices: Vec< SortedVec< usize > > 
            =   vec![    
                        vec![0,1,2], 
                        vec![0,3], 
                        vec![1,3], 
                        vec![2,3]  
                    ]
                    .into_iter()
                    .map( |x| SortedVec::new(x).unwrap() ) 
                    .collect_vec();
            
        //  Boundary matrix
        //  ---------------
            
        // This is a lazy object that generates rows/columns of the boundary matrix, on demand.
        let boundary_matrix = DowkerComplex::new( dowker_simplices.clone(), ring_operator );
            
        //  Simplex iterators
        //  -----------------
            
        // An iterator that runs over all triangles in the complex, in ascending 
        // lexicographic order
        let _triangles_ascending_order = dimension_d_simplices_in_lexicographic_order_iter( &dowker_simplices, 2);
            
        // An iterator that runs over all edges in the complex, in descending 
        // lexicographic order
        let _triangles_descending_order = dimension_d_simplices_in_reverse_lexicographic_order_iter( &dowker_simplices, 2);   
            
        // An iterator that runs over simplices of dimension 0 through max_homology_dimension,
        // ordered first by dimension (ascending) and second by lexicographic order (descending)
        let _row_indices = boundary_matrix.simplices_in_row_reduction_order( max_homology_dimension );

        // row_indices contains a reference to boundary_matrix, which will cause problems. Instead,
        // we can construct an iterator that runs over the same sequence of simplices, like so:
        let row_indices     =   dimension_0_through_d_simplices_in_ascending_dimension_descending_lexicographic_order_iter(&dowker_simplices, max_homology_dimension);
            
        //  Homology computation (by matrix factorization)
        //  ----------------------------------------------
            
        // Factor the boundary matrix
        let factored    =   DifferentialUmatch::new( 
                                boundary_matrix, 
                                min_homology_dimension,
                                max_homology_dimension,
                            );
                        
        //  Printing results
        //  ----------------
                        
        // Betti numbers.  For this computation we have to provide a
        // function that assigns a dimension to each index (i.e. to each simplex)
        let homology_dimensions   =   factored.betti_numbers(); 
        for dim in 0 .. 2 {
            println!(
                    // we'll insert two values into this string
                    "The betti number in dimension {:?} is {:?}.",
                    dim,                  // the dimension
                    homology_dimensions         // and the betti number
                        .get( & dim )     // this looks up a value in the hashmap
                        .unwrap_or( & 0)  // if the hashmap doesn't have the value, then use 0
                );            
        }
        println!(); // insert line break
        
        // Cycle representatives for homology
        println!(
                "The following are basis vectors for homology in dimensions 0 through {:?}",
                max_homology_dimension,
            );
        for (cycle_number, cycle) in factored.homology_basis().enumerate() {
            // `cycle` is an iterator.  For convenience, collect the elements of the
            // iterator into a Rust vector.
            let cycle: Vec<_> = cycle.collect();
            println!("Basis vector {:?} = {:?}", cycle_number, cycle);
        }
    }  


    #[test]
    fn test() {
        use crate::topology::simplicial::from::relation::DowkerBoundaryMatrixRow;
        use crate::algebra::rings::types::field_prime_order::PrimeOrderField;
        use crate::utilities::sequences_and_ordinals::SortedVec;

        let ring_operator = PrimeOrderField::new(3);
        let simplex = vec![1,3];
        let relation_rows = vec![ SortedVec::from_iter( vec![0,1,2,3,4] ).unwrap() ];
        let coboundary = DowkerBoundaryMatrixRow::from_vec_of_dowker_sets( simplex, &relation_rows, ring_operator ).unwrap();

        itertools::assert_equal( coboundary, vec![ (vec![0,1,3], 1), (vec![1,2,3], 2), (vec![1,3,4], 1) ]);        
    }    


    #[test]
    fn doc_test_sideways_ladder() {
        use crate::topology::simplicial::from::relation::sideways_ladder_edges;

        // construct the edges
        let number_of_holes         =   1;
        let offset_from_left        =   1;
        let mut edges           =   sideways_ladder_edges(number_of_holes, offset_from_left);
        edges.sort();

        // this is the ground truth
        let ground_truth            =   vec![ 
                                            vec![2,3], 
                                            vec![2,4], 
                                            vec![3,5], 
                                            vec![4,5],
                                        ];

        assert_eq!( edges, ground_truth );
    }   




    #[test]
    fn doc_test_misc_REDUNDANT_OK_TO_DELETE() {
        use crate::algebra::matrices::operations::umatch::differential::DifferentialUmatch;   
        use crate::algebra::rings::types::field_prime_order::PrimeOrderField;
        use crate::topology::simplicial::from::relation::DowkerComplex;
        use crate::topology::simplicial::simplices::vector::{dimension_d_simplices_in_lexicographic_order_iter, dimension_d_simplices_in_reverse_lexicographic_order_iter}; 
        use crate::topology::simplicial::simplices::vector::dimension_0_through_d_simplices_in_ascending_dimension_descending_lexicographic_order_iter;        
        use crate::utilities::sequences_and_ordinals::SortedVec;

        use itertools::Itertools;

        // Parameters
        // ----------

        // Define the maximum homology dimensino we want to compute.
        let min_homology_dimension                  =   0;
        let max_homology_dimension                  =   2;        

        // Define the ring operator for the finite field of order 3.
        // You can use this object to perform arithmetic operations, e.g., 
        // to add 1 and 1, you can run `ring_operator.add(1,1)`.
        let ring_operator          =   PrimeOrderField::new(3);

        // We will build a dowker complex.
        // A dowker complex is defined by a vertex set V and a family S
        // of subsets of V.  A subset of V forms a simplex iff it is 
        // a subset of some element of S.  We refer to the elements 
        // of S as "dowker simplices".

        // Each dowker simplex is represented by a SortedVec of vertices.
        // We store the list of all such simplices inside a larger vector.
        let dowker_simplices: Vec<SortedVec<usize>>
            =   vec![    
                        vec![0,1,2], 
                        vec![0,3], 
                        vec![1,3], 
                        vec![2,3]  
                    ]
                    .into_iter()
                    .map( |x| SortedVec::new(x).unwrap() )  // we unwrap because `new` can return an error
                    .collect_vec();

        //  Boundary matrix
        //  ---------------

        // This is a lazy object that generates rows/columns of the boundary matrix, on demand.
        let boundary_matrix = DowkerComplex::new( dowker_simplices.clone(), ring_operator.clone() );

        //  Simplex iterators
        //  -----------------

        // An iterator that runs over all triangles in the complex, in ascending 
        // lexicographic order
        let triangles_ascending_order = dimension_d_simplices_in_lexicographic_order_iter( &dowker_simplices, 2);

        // An iterator that runs over all edges in the complex, in descending 
        // lexicographic order
        let triangles_descending_order = dimension_d_simplices_in_reverse_lexicographic_order_iter( &dowker_simplices, 2);   

        // An iterator that runs over simplices of dimension 0 through max_homology_dimension,
        // ordered first by dimension (ascending) and second by lexicographic order (descending)
        let row_indices = boundary_matrix.simplices_in_row_reduction_order( max_homology_dimension );

        // row_indices contains a reference to boundary_matrix, which will cause problems. Instead,
        // we can construct an iterator that runs over the same sequence of simplices, like so:
        let row_indices     =   dimension_0_through_d_simplices_in_ascending_dimension_descending_lexicographic_order_iter(&dowker_simplices, max_homology_dimension);

        //  Homology computation (by matrix factorization)
        //  ----------------------------------------------

        // Factor the boundary matrix
        let factored    =   DifferentialUmatch::new( 
                                boundary_matrix, 
                                min_homology_dimension,
                                max_homology_dimension,
                            );
                        
        //  Printing results
        //  ----------------
                        
        // Betti numbers.  For this computation we have to provide a
        // function that assigns a dimension to each index (i.e. to each simplex)
        let homology_dimensions   =   factored.betti_numbers(); 
        for dim in 0 .. 2 {
            println!(
                    // we'll insert two values into this string
                    "The betti number in dimension {:?} is {:?}.",
                    dim,                  // the dimension
                    homology_dimensions         // and the betti number
                        .get( & dim )     // this looks up a value in the hashmap
                        .unwrap_or( & 0)  // if the hashmap doesn't have the value, then use 0
                );            
        }
        println!(""); // insert line break

        // Cycle representatives for homology
        println!(
                "The following are basis vectors for homology in dimensions 0 through {:?}",
                max_homology_dimension,
            );
        for (cycle_number, cycle) in factored.homology_basis().enumerate() {
            // `cycle` is an iterator.  For convenience, collect the elements of the
            // iterator into a Rust vector.
            let cycle: Vec<_> = cycle.collect();
            println!("Basis vector {:?} = {:?}", cycle_number, cycle);
        }
    }     

}