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
//! Analyze and debug matrices.


use crate::{algebra::rings::traits::SemiringOperations, utilities::iterators::is_sorted::IsSortedBy};
use crate::algebra::vectors::entries::{KeyValGet, KeyValNew, KeyValSet};
use std::{collections::HashMap, fmt::Debug};
use itertools::Itertools;
use crate::algebra::matrices::types::product::ProductMatrix;

use super::operations::solve::echelon::RowEchelonSolver;
use super::{query::{MatrixAlgebra, MatrixOracle, }, types::transpose::OrderAntiTranspose};



//  VERIFY THAT ROWS AND COLUMNS ARE COMPATIBLE
//  -----------------------------------------------------------------------------------------------



/// Ensures that each entry in each row also appears in the corresponding column; panics otherwise.
/// 
/// Only checks the rows returned by `iter_row_index`.
/// 
/// See comments in source code for details.
pub fn verify_rows_compatible_with_columns_helper_function< Matrix, RowIndexIterator >(
                matrix:             Matrix,
                iter_row_index:        RowIndexIterator,              
            )
    where
        Matrix:                             MatrixOracle,
        Matrix::RowEntry:                   KeyValNew,
        Matrix::ColumnEntry:                KeyValNew,
        RowIndexIterator:                   Clone + Iterator< Item = Matrix::RowIndex >,
{
    // Make sure all the entries in the row also appear in the corresponding column
    for row_index in iter_row_index {
        let row = matrix.row( & row_index );
        // for each entry in the row ..
        for row_entry in row {
            // construct the corresponding entry of the corresponding (reverse) column
            let columnreverse_entry 
                    = Matrix::ColumnEntry::new( row_index.clone(), row_entry.val() );
            // check that this entry does in fact lie in the (reverse) column
            let reversed_column = matrix.column( & row_entry.key() );
            let exists_in_column = reversed_column.into_iter().any( |x| x == columnreverse_entry );
            if ! exists_in_column {
                println!("\nerror: entry {:?} appears in row {:?} of the matrix, but entry {:?} does not appear in column {:?}.\n", 
                    (row_entry.key(), row_entry.val()), 
                    row_index.clone(),  
                    (columnreverse_entry.key(), columnreverse_entry.val()),            
                    row_entry.key(),
                );      
                println!("\nrow {:?} of the matrix is:\n{:?}\n", row_index.clone(), matrix.row( & row_index ).collect_vec() );
                println!("\ncolumn {:?} of the matrix is:\n{:?}\n", row_entry.key(),  matrix.column( &  row_entry.key() ).collect_vec(), );                
            }
            assert!( exists_in_column );
        }
    }  
}


/// Ensures that each entry in each row also appears in the corresponding column, and vice versa; panics otherwise.
/// 
/// Specifically checks that 
/// - each entry `(column_index,coefficient)` in `matrix.row(&row_index)` has a corresponding entry `(row_index,coefficient)` in `matrix.column(&column_index)`
/// - each entry `(row_index,coefficient)` in `matrix.column_reverse(&row_index)` has a corresponding entry `(column_index,coefficient)` in `matrix.row_reverse(&row_index)`
/// 
/// # Caveats
/// 
/// - This test only looks at rows and columns specified by `iter_row_index` and `iter_column_index`
/// - Does not check that 
///   - `matrix.row(&index)` returns the same entries (in reverse order) as `matrix.row_reverse(&index)`
///   - `matrix.column(&index)` returns the same entries (in reverse order) as `matrix.column_reverse(&index)`
pub fn verify_rows_compatible_with_columns< Matrix, RowIndexIterator, ColumnIndexIterator >(
                matrix:         Matrix,
                iter_column_index:    ColumnIndexIterator,                
                iter_row_index:    RowIndexIterator,
            )
    where
        Matrix:                             MatrixOracle,
        Matrix::Row:            IntoIterator,
        Matrix::RowEntry:       Debug + std::cmp::PartialEq + KeyValNew < Key = Matrix::ColumnIndex, Val = Matrix::Coefficient >,
        Matrix::ColumnReverse:           IntoIterator,
        Matrix::ColumnEntry:      Debug + std::cmp::PartialEq + KeyValNew < Key = Matrix::RowIndex, Val = Matrix::Coefficient >,
        Matrix::ColumnIndex:                     Clone + Debug,
        Matrix::RowIndex:                     Clone + Debug,
        Matrix::Coefficient:                     Debug,        
        RowIndexIterator:                         Clone + Iterator< Item = Matrix::RowIndex >,
        ColumnIndexIterator:                         Clone + Iterator< Item = Matrix::ColumnIndex >,        
{
    verify_rows_compatible_with_columns_helper_function( 
            & matrix, 
            iter_row_index,                 
        );

    verify_rows_compatible_with_columns_helper_function( 
            OrderAntiTranspose::new( matrix ), 
            iter_column_index,                           
        );        
}





//  CHECK THAT MATRIX ORACLE IS VALID
//  ==============================================================================



/// Checks that the commands to lookup rows, columns, and entries are mutually consistent
/// 
/// Specifically checks that
/// - `.row()` and `.row_reverse()` return the same entries in opposite order
/// - `.column()` and `.column_reverse()` return the same entries in opposite order
/// - `.row(r)` returns the same sequence of entries that one would obtain by iterating over column indices in sorted order, calling `.structural_nonzero_entry( r, c )` for each index `c`
/// - `.column(c)` returns the same sequence of entries that one would obtain by iterating over row indices in sorted order, calling `.structural_nonzero_entry( r, c )` for each index `r`
/// 
/// # Inputs
/// 
/// The function takes as input a vector called `sorted_row_indices`, which should contain the row indices of the matrix in sorted order,
/// and `sorted_column_indices`, which contains the column indices of the matrix in sorted order.
/// It's necessary for the user to provide these inputs because matrix oracles have no way of knowing what the set of all valid row and column indices, a priori;
/// they are simply passive structures that return information about specific rows and columns when requested by the user.
pub fn matrix_oracle_is_internally_consistent< Matrix, I, J >
    (
        matrix:                 Matrix,
        sorted_row_indices:     I,
        sorted_column_indices:  J,
    )
    ->
    bool

    where
        Matrix:     MatrixOracle,
        I:          IntoIterator< Item = Matrix::RowIndex >,
        J:          IntoIterator< Item = Matrix::ColumnIndex >,        
{

    // collect the row and column indices into vectors
    let sorted_row_indices      =   sorted_row_indices.into_iter().collect::<Vec<_>>();
    let sorted_column_indices   =   sorted_column_indices.into_iter().collect::<Vec<_>>();    

    // check that matrix.row() and matrix.structural_nonzero_entry() agree.
    for row_index in sorted_row_indices.iter().cloned() {
        let row_from_entries: Vec<_>     =   sorted_column_indices
                        .iter()
                        .cloned()
                        .filter_map(
                            |column_index| 
                            {
                                let coefficient     =   matrix.structural_nonzero_entry( & row_index, & column_index );
                                // if coefficient is Some(a), then return (column_index, a)
                                coefficient.map( |a|  (column_index, a) )
                            }                            
                        ) // filters out None's
                        .collect();
        let row_from_iter: Vec<_>         =   matrix.row( & row_index ).collect();

        if row_from_iter.len() != row_from_entries.len() {
            println!(
                "\nInvalid matrix: calling\n\n   matrix.row({:?})\n\nreturns the following sequence of structural nonzero entries:\n",
                &row_index
            );
                println!("    < begin list of entries >");              
            for entry in &row_from_iter {                      
                println!("    {:?}", entry);                    
            }
                println!("    < end   list of entries >");                
            println!(
                "\nHowever, the sequence of structural nonzero entries obtained by\n\n    matrix.structural_nonzero_entry({:?}, column_index)\n\nfor all values of `column_index` provided by the user is not equal:",
                &row_index
            );
                println!("    < begin list of entries >");              
            for entry in &row_from_entries {                                      
                println!("    {:?}", entry);                  
            }
                println!("    < end   list of entries >");                  
            return false
        }        

        for ( entry_number, entry ) in row_from_iter.iter().cloned().enumerate() {
            let entry       =   ( entry.key(), entry.val() );
            if entry != row_from_entries[ entry_number ] {
                println!(
                    "\nInvalid matrix: the {:?}th structural nonzero entry of row {:?} is\n\n    {:?}\n\naccording to entry look-up, but it is\n\n    {:?}\n\naccording to the row iterator.", 
                    entry_number, 
                    row_index.clone(), 
                    row_from_entries[ entry_number],
                    entry,                    
                );
                return false
            }
        }   
    }

    // check that matrix.column() and matrix.structural_nonzero_entry() agree.
    for column_index in sorted_column_indices.iter().cloned() {
        let column_from_entries: Vec<_>     =   sorted_row_indices
                        .iter()
                        .cloned()
                        .filter_map(
                            |row_index| 
                            {
                                let coefficient     =   matrix.structural_nonzero_entry( &row_index, & column_index );
                                // if coefficient is Some(a), then return (row_index, a)
                                coefficient.map( |a|  (row_index, a) )
                            }                            
                        )                        
                        .collect();
        let column_from_iter: Vec<_>         =   matrix.column( & column_index ).collect();


        if column_from_iter.len() != column_from_entries.len() {
            println!(
                "\nInvalid matrix: calling\n\n    matrix.column({:?})\n\nreturns the following sequence of structural nonzero entries:\n",
                &column_index
            );
                println!("    < begin list of entries >");              
            for entry in &column_from_iter {              
                println!("    {:?}", entry);                   
            }
                println!("    < end   list of entries >");                 
            println!(
                "\nHowever, the sequence of structural nonzero entries obtained by calling\n\n    matrix.structural_nonzero_entry(row_index, {:?})\n\nfor every value of `row_index` provided by the user is not equal:",
                &column_index
            );
                println!("    < begin list of entries >");            
            for entry in &column_from_entries {
                println!("    {:?}", entry);              
            }
                println!("    < end   list of entries >");              
            return false
        }        

        for ( entry_number, entry ) in column_from_iter.iter().cloned().enumerate() {
            let entry       =   ( entry.key(), entry.val() );
            if entry != column_from_entries[ entry_number ] {
                println!(
                    "Invalid matrix: the {:?}th structural nonzero entry of column\n\n    {:?}\n\nis equal to\n\n    {:?}\n\naccording to entry look-up, but it is\n\n    {:?}\n\naccording to the column iterator\n\n    matrix.column({:?}).", 
                    entry_number, 
                    column_index.clone(), 
                    column_from_entries[ entry_number],
                    entry,                
                    column_index.clone(),                         
                );
                return false
            }
        }        
    }   
    
    // check that row() agrees with row_reverse()
    for row_index in sorted_row_indices.iter().cloned() {    
        let     row_from_forward: Vec<_>    =   matrix.row( & row_index ).collect();
        let mut row_from_reverse: Vec<_>    =   matrix.row_reverse( & row_index ).collect();
        ( &mut row_from_reverse ).reverse();
        if ! row_from_forward.iter().eq( row_from_reverse.iter() ) {
            println!(
                "\nInvalid matrix: the sequence of entries returned by `matrix.row_reverse({:?})` is \n\n{:?}\n\nThis does not equal the reverse of the sequence of entries returned by `matrix.row({:?}); that reversed sequence is\n\n{:?}\n\n`.",
                row_index.clone(),
                row_from_reverse,
                row_index.clone(),
                row_from_forward,
            );
            return false
        }
    }

    // check that column() agrees with column_reverse()
    for column_index in sorted_column_indices.iter().cloned() {    
        let     column_from_forward: Vec<_>             =   matrix.column( & column_index ).collect();
        let mut column_from_reverse: Vec<_>     =   matrix.column_reverse( & column_index ).collect();
        ( &mut column_from_reverse ).reverse();        
        if ! column_from_forward.iter().eq( column_from_reverse.iter() ) {
            println!(
                "\nInvalid matrix: the sequence of entries returned by `matrix.column_reverse({:?})` is \n\n{:?}\n\nThis does not equal the reverse of the sequence of entries returned by `matrix.column({:?}); that reversed sequence is\n\n{:?}\n\n`.",                                
                column_index.clone(),
                column_from_reverse,
                column_index.clone(),
                column_from_forward,                
            );
            return false
        }
    }    

    return true

}






/// Verifies that the entries in each row and column appear in strictly sorted order
/// 
/// Concretely, for each `row_index` in `sorted_row_indices` and each `column_index` in `column_indices`,
/// verifies that
/// - the entries in `matrix.row(row_index)` appear in strictly ascending order, as measured by `matrix.order_operator_for_row_entries()`
/// - the entries in `matrix.row(row_index)` appear in strictly ascending order, as measured by `matrix.order_operator_for_row_indices()`
/// - the entries in `matrix.column(column_index)` appear in strictly ascending order, as measured by `matrix.order_operator_for_column_entries()`
/// - the entries in `matrix.column(column_index)` appear in strictly ascending order, as measured by `matrix.order_operator_for_column_indices()`
pub fn matrix_order_operators_are_internally_consistent< Matrix: MatrixAlgebra, I, J >
    (
        matrix:                 Matrix,
        sorted_row_indices:     I,
        sorted_column_indices:  J,
    )
        ->
    Result< (), String >


    where
        I:  IntoIterator< Item = Matrix::RowIndex >,
        J:  IntoIterator< Item = Matrix::ColumnIndex >,        
{

    // get copies of the order operators    
    let order_operator_for_row_entries                  =   matrix.order_operator_for_row_entries();
    let order_operator_for_column_entries               =   matrix.order_operator_for_column_entries();
    let order_operator_for_row_indices                  =   matrix.order_operator_for_row_indices();
    let order_operator_for_column_indices               =   matrix.order_operator_for_column_indices();

    // collect the row and column indices into vectors
    let sorted_row_indices      =   sorted_row_indices.into_iter().collect::<Vec<_>>();
    let sorted_column_indices   =   sorted_column_indices.into_iter().collect::<Vec<_>>(); 

    // check that the user-provided row and column indices are sorted
    // ------------------------------------------------------------------------    
    if ! sorted_row_indices.iter().cloned().is_sorted_strictly_by_order_operator( order_operator_for_row_indices.clone() ).is_ok() {
        let cap = sorted_row_indices.len().min(100);
        println!( "The user-provided list of row indices is not sorted. Here are the first 100 (or fewer) entries:\n{:?}", &sorted_row_indices[..cap]);
        return Err( "The user-provided list of row indices is not sorted.".to_owned() )
    }
    if ! sorted_column_indices.iter().cloned().is_sorted_strictly_by_order_operator( order_operator_for_column_indices.clone() ).is_ok() {
        let cap = sorted_column_indices.len().min(100);        
        println!( "The user-provided list of column indices is not sorted. Here are the first 100 (or fewer) entries:\n{:?}", &sorted_column_indices[..cap]);        
        return Err( "The user-provided list of column indices is not sorted.".to_owned() )
    }  


    // check that rows are sorted
    // ------------------------------------------------------------------------    
    for index in sorted_row_indices.iter().cloned() {     
        // check sorting by the entry order
        if !    matrix.row( & index )
                    .is_sorted_strictly_by_order_operator( order_operator_for_row_entries.clone() )
                    .is_ok()
        {
            let row_vec = matrix.row( & index ).collect::<Vec<_>>();
            let mut msg = format!("\nThe entries of row {:?} do not appear in strictly sorted (ascending) order. The entries are:\n", index);
            for entry in &row_vec {
                msg.push_str(&format!("    {:?}\n", entry));
            }
            return Err(msg);
        }
        // check sorting by the index order
        if !    matrix.row( & index )
                    .map(
                        |entry|
                        entry.key()
                    )
                    .is_sorted_strictly_by_order_operator( order_operator_for_column_indices.clone() )
                    .is_ok()
        {
            let row_vec = matrix.row( & index ).collect::<Vec<_>>();
            let mut msg = format!("\nThe entries of row {:?} do not appear in strictly sorted (ascending) order. The entries are:\n", index);
            for entry in &row_vec {
                msg.push_str(&format!("    {:?}\n", entry));
            }
            return Err(msg);
        }        
    }  


    // check that columns are sorted
    // ------------------------------------------------------------------------
    for index in sorted_column_indices.iter().cloned() {
        // check sorting by the entry order
        if !    matrix.column( & index )
                    .is_sorted_strictly_by_order_operator( order_operator_for_column_entries.clone() )
                    .is_ok()
        {
            let column_vec = matrix.column( & index ).collect::<Vec<_>>();                
            let mut msg = format!("\nThe entries of column {:?} do not appear in strictly sorted (ascending) order. The entries are:\n", index);
            for entry in &column_vec {
                msg.push_str(&format!("    {:?}\n", entry));
            }
            return Err(msg);
        }
        // check sorting by the index order
        if !    matrix.column( & index )
                    .map(
                        |entry|
                        entry.key()
                    )
                    .is_sorted_strictly_by_order_operator( order_operator_for_row_indices.clone() )
                    .is_ok()
        {
            let column_vec = matrix.column( & index ).collect::<Vec<_>>();            
            let mut msg = format!("\nThe entries of column {:?} do not appear in strictly sorted (ascending) order. The entries are:\n", index);
            for entry in &column_vec {
                msg.push_str(&format!("    {:?}\n", entry));
            }
            return Err(msg);
        }        
    }      


    // check that the information contained in the matrix oracle is internally consistent
    if !    matrix_oracle_is_internally_consistent(
                & matrix, 
                sorted_row_indices.iter().cloned(), 
                sorted_column_indices.iter().cloned()
            )
    {
        return Err( "Underlying matrix oracle failed to pass the `matrix_oracle_is_internally_consistent` test".to_owned() )
    } else {
        return Ok(())
    }
}











//  CHECK THAT TWO MATRICES ARE EQUAL
//  ==============================================================================


/// Determines if both matrices are internally consistent and equal
/// 
/// Returns a `HashMap< String, bool >` which records the following data:
/// - matrix 1 is internally consistent:              true/false 
/// - matrix 2 is internally consistent:              true/false 
/// - matrices are internally consistent and equal:   true/false   
/// 
/// For details about "internal consistency," see the documentation for [matrix_oracle_is_internally_consistent].
fn matrices_are_internally_consistent_and_equal< Matrix1, Matrix2, RowIndexIter, ColumnIndexIter >
    ( 
        matrix_1:                   Matrix1,
        matrix_2:                   Matrix2,
        sorted_row_indices:         RowIndexIter,
        sorted_column_indices:      ColumnIndexIter
    ) 
    -> HashMap< String, bool > 

    where 
        Matrix1:            MatrixOracle,
        Matrix2:            MatrixOracle< RowIndex=Matrix1::RowIndex, ColumnIndex=Matrix1::ColumnIndex, Coefficient=Matrix1::Coefficient >,
        RowIndexIter:       IntoIterator< Item = Matrix1::RowIndex    >,
        ColumnIndexIter:    IntoIterator< Item = Matrix1::ColumnIndex >, 

{
    let sorted_row_indices: Vec<_>              =   sorted_row_indices.into_iter().collect();
    let sorted_column_indices: Vec<_>           =   sorted_column_indices.into_iter().collect();


    let a = matrix_oracle_is_internally_consistent( &matrix_1, sorted_row_indices.clone(), sorted_column_indices.clone() );
    let b = matrix_oracle_is_internally_consistent( &matrix_2, sorted_row_indices.clone(), sorted_column_indices.clone() );





    // Check that "structural nonzero entry" look-up operattions agree.
    let mut c                                   =   true;
    for row_index in sorted_row_indices.iter().cloned() {
        let iter_1  =   matrix_1
                                                        .row( & row_index )
                                                        .map(
                                                            |x| 
                                                            ( x.key(), x.val() )  // convert entry to a tuple for ease of comparison
                                                        );
        let iter_2  =   matrix_2
                                                        .row( & row_index.clone() )
                                                        .map(
                                                            |x| 
                                                            ( x.key(), x.val() )  // convert entry to a tuple for ease of comparison
                                                        );
        if ! iter_1.eq( iter_2 ) { c = false }                                                        

    }

    let mut output = HashMap::new();
    output.insert( String::from("matrix 1 is internally consistent"), a  );
    output.insert( String::from("matrix 2 is internally consistent"), b  );
    output.insert( String::from("matrices are internally consistent and equal"), a && b && c  );   

    output     

}











//  UTILITY FUNCTION FOR TESTING: CHECK THAT A PRODUCT OF TWO MATRICES IS IDENTITY
//  ==============================================================================

/// Checks that a (user-specified) set of rows of the product of two matrices equals the corresponding set of rows of an identityt matrix
/// 
/// Concretely, returns `false` if `product.row( k )` does not equal the `k`th standard unit vector, for any `k` in `iter_row_index`, where
/// `product` is the product of the two matrices.
pub fn product_is_identity_matrix<
            Matrix1, 
            Matrix2,               
            RowIndexIterator,
        > 
        (
            matrix_1:           Matrix1,
            matrix_2:           Matrix2,
            iter_row_index:     RowIndexIterator,
        )
        ->
        bool
    where
        Matrix1:                            MatrixAlgebra<
                                                RowEntry:       KeyValSet,
                                                ColumnEntry:    KeyValSet,
                                            >,
        Matrix2:                            MatrixAlgebra< 
                                                Coefficient                 =   Matrix1::Coefficient, 
                                                RowIndex                    =   Matrix1::ColumnIndex, 
                                                ColumnIndex                 =   Matrix1::RowIndex, 
                                                RingOperator                =   Matrix1::RingOperator,
                                                OrderOperatorForRowIndices  =   Matrix1::OrderOperatorForColumnIndices,
                                                RowEntry:                       KeyValSet,
                                            >, 
        RowIndexIterator:                   Iterator< Item = Matrix1::RowIndex >,                    
{

    let product = ProductMatrix::new( matrix_1, matrix_2, );
    let one = Matrix1::RingOperator::one();

    for row_index in iter_row_index {
        let row = product.row( & row_index );
        itertools::assert_equal(
            row.map( |x| (x.key(), x.val()) ),
            std::iter::once( ( row_index, one.clone() ) )
        );
        // match equals_standard_unit_vector { true => {continue}, false=> {return false } }
    }
    true
}