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

//! Create a new matrix by applying a transformation to each row or column of an existing matrix.
//! 
//! See also [`transform_entry_wise`](oat_rust::algebra::matrices::operations::transform_entry_wise)



use derive_new::new;
use itertools::PutBack;
use itertools::put_back;

use crate::{algebra::matrices::query::{MatrixAlgebra, MatrixOracle,}, utilities::sets::MapHasKeyOrSequenceHasElement, };











//  PUT BACK ROWS
//  -------------------------------------------------------------------------------------



/// Wraps every row, column, reverse row, and reverse column of a matrix in a [`PutBack`](itertools::PutBack) iterator.
/// 
/// This is useful when you want to iterate over the rows or columns of a matrix, but you also want to be able to "put back" the last entry that you have already iterated over.
/// 
/// # Example
/// 
/// ```
/// use oat_rust::algebra::matrices::operations::transform_vector_wise::PutbackIteratorMatrix;
/// use oat_rust::algebra::matrices::query::MatrixOracle;
/// use oat_rust::algebra::matrices::types::vec_of_vec::sorted::VecOfVec;
/// 
/// let matrix = VecOfVec::new( 
///     vec![ 
///         vec![(1,1), (2,2), (3,3)], 
///         vec![(4,4), (5,5), (6,6)] 
///     ] 
/// ).ok().unwrap();    
/// let put_back_matrix = PutbackIteratorMatrix::new( & matrix );
/// 
/// let mut row = put_back_matrix.row(&0);
/// assert_eq!( row.next(), Some( (1,1) ) );
/// row.put_back( (1,1) );
/// assert_eq!( row.next(), Some( (1,1) ) );
/// 
/// let mut column = put_back_matrix.column(&2);
/// assert_eq!( column.next(), Some( (0,2) ) );
/// column.put_back( (0,2) );
/// assert_eq!( column.next(), Some( (0,2) ) );
/// ```
/// 
#[derive(new,Clone,Copy,Debug,Eq,PartialEq,Ord,PartialOrd)]
pub struct PutbackIteratorMatrix<
        Matrix,
    >
{
    matrix:                         Matrix,
}
  

// Implement MatrixOracle
impl < Matrix >

    MatrixOracle for  

    PutbackIteratorMatrix
        < Matrix >

    where
        Matrix:                             MatrixOracle,    
{
    type Coefficient            =     Matrix::Coefficient;   // The type of coefficient stored in each entry of the matrix    
    type RowIndex               =     Matrix::RowIndex;      // The type key used to look up rows.  Matrices can have rows indexed by anything: integers, simplices, strings, etc.
    type ColumnIndex            =     Matrix::ColumnIndex;   // The type of column indices    
    type RowEntry               =     Matrix::RowEntry;      // The type of entries in each row; these are essentially pairs of form `(column_index, coefficient)`
    type ColumnEntry            =     Matrix::ColumnEntry;   // The type of entries in each column; these are essentially pairs of form `(row_index, coefficient)`
    type Row                    =     PutBack< Matrix::Row >;           // What you get when you ask for a row.
    type RowReverse             =     PutBack< Matrix::RowReverse >;    // What you get when you ask for a row with the order of entries reversed
    type Column                 =     PutBack< Matrix::Column >;        // What you get when you ask for a column
    type ColumnReverse          =     PutBack< Matrix::ColumnReverse >; // What you get when you ask for a column with the order of entries reversed                             


    fn row(                     &   self, index: &Self::RowIndex   )   -> Self::Row
        { put_back( self.matrix.row(index) ) }         
    fn row_reverse(             &   self, index: &Self::RowIndex   )   -> Self::RowReverse
        { put_back( self.matrix.row_reverse(index) ) }   
    fn column(                  &   self, index: & Self::ColumnIndex)   -> Self::Column
        { put_back( self.matrix.column(index) ) }
    fn column_reverse(          &   self, index: & Self::ColumnIndex)   -> Self::ColumnReverse
        { put_back( self.matrix.column_reverse(index) ) }

    fn has_row_for_index(     &   self, index: &Self::RowIndex   )   -> bool
        { self.matrix.has_row_for_index(index) }
    fn has_column_for_index(  &   self, index: & Self::ColumnIndex)   -> bool
        { 
            self.matrix.has_column_for_index(index)
        }    
    fn structural_nonzero_entry(                   &   self, row:   & Self::RowIndex, column: & Self::ColumnIndex ) ->  Option< Self::Coefficient >
        { self.matrix.structural_nonzero_entry( row, column ) }

}



// Implement MatrixAlgebra
impl < Matrix >

    MatrixAlgebra for  

    PutbackIteratorMatrix
        < Matrix >

    where
        Matrix:                             MatrixAlgebra,    
{
    type RingOperator                   =   Matrix::RingOperator;
    type OrderOperatorForRowEntries     =   Matrix::OrderOperatorForRowEntries;
    type OrderOperatorForRowIndices     =   Matrix::OrderOperatorForRowIndices;
    type OrderOperatorForColumnEntries  =   Matrix::OrderOperatorForColumnEntries;
    type OrderOperatorForColumnIndices  =   Matrix::OrderOperatorForColumnIndices;

    fn ring_operator( &self ) -> Self::RingOperator { self.matrix.ring_operator() }
    fn order_operator_for_row_entries( &self ) -> Self::OrderOperatorForRowEntries { self.matrix.order_operator_for_row_entries() }
    fn order_operator_for_row_indices( &self ) -> Self::OrderOperatorForRowIndices { self.matrix.order_operator_for_row_indices() }    
    fn order_operator_for_column_entries( &self ) -> Self::OrderOperatorForColumnEntries { self.matrix.order_operator_for_column_entries() }
    fn order_operator_for_column_indices( &self ) -> Self::OrderOperatorForColumnIndices { self.matrix.order_operator_for_column_indices() }   
}
















//  MASK: ONLY INDICES OUTSIDE
//  -------------------------------------------------------------------------------------

//  COLUMNS
//  -----------------------------------------

use crate::algebra::vectors::operations::OnlyIndicesOutsideCollection;


/// A matrix oracle whose rows iterate only over entries that have indices **outside**
/// a given collection.
#[derive(Clone,Copy,Debug,Eq,PartialEq,Ord,PartialOrd)]
pub struct OnlyColumnIndicesOutsideCollection<
        Matrix, ColumnIndicesToExclude,
    >
    where
        Matrix:                         MatrixOracle,    
        ColumnIndicesToExclude:         MapHasKeyOrSequenceHasElement< Matrix::ColumnIndex >,                  
{
    matrix:                         Matrix,
    column_indices_to_exclude:       ColumnIndicesToExclude,
}

// implement the object
impl < Matrix, ColumnIndicesToExclude, >

    OnlyColumnIndicesOutsideCollection
        < Matrix, ColumnIndicesToExclude >

    where
        Matrix:                         MatrixOracle,    
        ColumnIndicesToExclude:               MapHasKeyOrSequenceHasElement< Matrix::ColumnIndex >,                          

    {
        /// Create a new [OnlyColumnIndicesOutsideCollection] from a matrix and a collection
        /// of column indices.
        /// 
        /// In order to operate properly, the collection of column indices should implement
        /// the [`MapHasKeyOrSequenceHasElement`](crate::utilities::sets::MapHasKeyOrSequenceHasElement) 
        /// trait.
        pub fn new( matrix: Matrix, column_indices_to_exclude: ColumnIndicesToExclude ) 
        -> 
        OnlyColumnIndicesOutsideCollection
            < Matrix, ColumnIndicesToExclude >
        where
            Matrix:                         MatrixOracle,    
            ColumnIndicesToExclude:               MapHasKeyOrSequenceHasElement< Matrix::ColumnIndex >,              
        { OnlyColumnIndicesOutsideCollection{ matrix, column_indices_to_exclude: column_indices_to_exclude, } }
    }     

// Implement MatrixOracle
impl < Matrix, ColumnIndicesToExclude >

    MatrixOracle for  

    OnlyColumnIndicesOutsideCollection
        < Matrix, ColumnIndicesToExclude >

    where
        Matrix:                             MatrixOracle,    
        ColumnIndicesToExclude:             Copy + MapHasKeyOrSequenceHasElement< Matrix::ColumnIndex >,    
{
    type Coefficient            =     Matrix::Coefficient;   // The type of coefficient stored in each entry of the matrix    
    type RowIndex               =     Matrix::RowIndex;      // The type key used to look up rows.  Matrices can have rows indexed by anything: integers, simplices, strings, etc.
    type ColumnIndex            =     Matrix::ColumnIndex;   // The type of column indices    
    type RowEntry               =     Matrix::RowEntry;      // The type of entries in each row; these are essentially pairs of form `(column_index, coefficient)`
    type ColumnEntry            =     Matrix::ColumnEntry;   // The type of entries in each column; these are essentially pairs of form `(row_index, coefficient)`
    type Row                    =     OnlyIndicesOutsideCollection< Matrix::Row, ColumnIndicesToExclude >;           // What you get when you ask for a row.
    type RowReverse             =     OnlyIndicesOutsideCollection< Matrix::RowReverse, ColumnIndicesToExclude >;    // What you get when you ask for a row with the order of entries reversed
    type Column                 =     Matrix::Column;        // What you get when you ask for a column
    type ColumnReverse          =     Matrix::ColumnReverse; // What you get when you ask for a column with the order of entries reversed                             


    fn row(                     &   self, index: &Self::RowIndex   )   -> Self::Row
        { OnlyIndicesOutsideCollection::new( self.matrix.row(index), self.column_indices_to_exclude  )  }        
    fn row_reverse(             &   self, index: &Self::RowIndex   )   -> Self::RowReverse
        { OnlyIndicesOutsideCollection::new( self.matrix.row_reverse(index), self.column_indices_to_exclude  )  }    
    fn column(                  &   self, index: & Self::ColumnIndex)   -> Self::Column
        { self.matrix.column(index) }    
    fn column_reverse(          &   self, index: & Self::ColumnIndex)   -> Self::ColumnReverse
        { self.matrix.column_reverse(index) }

    fn has_row_for_index(     &   self, index: &Self::RowIndex   )   -> bool
        { self.matrix.has_row_for_index(index) }
    fn has_column_for_index(  &   self, index: & Self::ColumnIndex)   -> bool
        { 
            let matrix_has_column = self.matrix.has_column_for_index(index);
            let column_is_excluded = self.column_indices_to_exclude.map_has_key_or_sequence_has_element(index);
            return matrix_has_column && (! column_is_excluded)
        }    
    fn structural_nonzero_entry(                   &   self, row:   & Self::RowIndex, column: & Self::ColumnIndex ) ->  Option< Self::Coefficient >
        { self.matrix.structural_nonzero_entry( row, column ) }

}



// Implement MatrixAlgebra
impl < Matrix, ColumnIndicesToExclude >

    MatrixAlgebra for  

    OnlyColumnIndicesOutsideCollection
        < Matrix, ColumnIndicesToExclude >

    where
        Matrix:                             MatrixAlgebra,    
        ColumnIndicesToExclude:             Copy + MapHasKeyOrSequenceHasElement< Matrix::ColumnIndex >,   
{
    type RingOperator                   =   Matrix::RingOperator;
    type OrderOperatorForRowEntries     =   Matrix::OrderOperatorForRowEntries;
    type OrderOperatorForRowIndices     =   Matrix::OrderOperatorForRowIndices;
    type OrderOperatorForColumnEntries  =   Matrix::OrderOperatorForColumnEntries;
    type OrderOperatorForColumnIndices  =   Matrix::OrderOperatorForColumnIndices;

    fn ring_operator( &self ) -> Self::RingOperator { self.matrix.ring_operator() }
    fn order_operator_for_row_entries( &self ) -> Self::OrderOperatorForRowEntries { self.matrix.order_operator_for_row_entries() }
    fn order_operator_for_row_indices( &self ) -> Self::OrderOperatorForRowIndices { self.matrix.order_operator_for_row_indices() }    
    fn order_operator_for_column_entries( &self ) -> Self::OrderOperatorForColumnEntries { self.matrix.order_operator_for_column_entries() }
    fn order_operator_for_column_indices( &self ) -> Self::OrderOperatorForColumnIndices { self.matrix.order_operator_for_column_indices() }   
}




//  ROWS
//  -----------------------------------------


/// A matrix oracle whose columns iterate only over entries that have indices **outside**
/// a given collection.
#[derive(Clone,Copy,Debug,Eq,PartialEq,Ord,PartialOrd)]
pub struct OnlyRowIndicesOutsideCollection<
        Matrix, RowIndicesToExclude,
    >
    where
        Matrix:                             MatrixOracle,    
        RowIndicesToExclude:                MapHasKeyOrSequenceHasElement< Matrix::RowIndex >,                  
{
    matrix:                                 Matrix,
    row_indices_to_exclude:                 RowIndicesToExclude,
}

// implement the object
impl < Matrix, RowIndicesToExclude, >

    OnlyRowIndicesOutsideCollection
        < Matrix, RowIndicesToExclude >

    where
        Matrix:                        MatrixOracle,    
        RowIndicesToExclude:               MapHasKeyOrSequenceHasElement< Matrix::RowIndex >,                          

    {
        /// Create a new [OnlyRowIndicesOutsideCollection] from a matrix and a collection
        /// of column indices.
        /// 
        /// In order to operate properly, the collection of column indices should implement
        /// the [`MapHasKeyOrSequenceHasElement`](crate::utilities::sets::MapHasKeyOrSequenceHasElement) 
        /// trait.
        pub fn new( matrix: Matrix, row_indices_to_exclude: RowIndicesToExclude ) 
        -> 
        OnlyRowIndicesOutsideCollection
            < Matrix, RowIndicesToExclude >
        { OnlyRowIndicesOutsideCollection{ matrix, row_indices_to_exclude: row_indices_to_exclude, } }
    }    


// Implement MatrixOracle
impl < Matrix, RowIndicesToExclude >

    MatrixOracle for  

    OnlyRowIndicesOutsideCollection
        < Matrix, RowIndicesToExclude >

    where
        Matrix:                         MatrixOracle,    
        RowIndicesToExclude:            Copy + MapHasKeyOrSequenceHasElement< Matrix::RowIndex >,    
{
    type Coefficient            =     Matrix::Coefficient;   // The type of coefficient stored in each entry of the matrix    
    type RowIndex               =     Matrix::RowIndex;      // The type key used to look up rows.  Matrices can have rows indexed by anything: integers, simplices, strings, etc.
    type ColumnIndex            =     Matrix::ColumnIndex;   // The type of column indices    
    type RowEntry               =     Matrix::RowEntry;      // The type of entries in each row; these are essentially pairs of form `(column_index, coefficient)`
    type ColumnEntry            =     Matrix::ColumnEntry;   // The type of entries in each column; these are essentially pairs of form `(row_index, coefficient)`
    type Row                    =     Matrix::Row;           // What you get when you ask for a row.
    type RowReverse             =     Matrix::RowReverse;    // What you get when you ask for a row with the order of entries reversed
    type Column                 =     OnlyIndicesOutsideCollection< Matrix::Column,        RowIndicesToExclude >;        // What you get when you ask for a column
    type ColumnReverse          =     OnlyIndicesOutsideCollection< Matrix::ColumnReverse, RowIndicesToExclude >; // What you get when you ask for a column with the order of entries reversed                             


    fn row(                     &   self, index: &Self::RowIndex   )   -> Self::Row
        { self.matrix.row(index) }    
    fn row_reverse(             &   self, index: &Self::RowIndex   )   -> Self::RowReverse
        { self.matrix.row_reverse(index) }
    fn column(                  &   self, index: & Self::ColumnIndex)   -> Self::Column
        { OnlyIndicesOutsideCollection::new( self.matrix.column(index), self.row_indices_to_exclude  )  }        
    fn column_reverse(          &   self, index: & Self::ColumnIndex)   -> Self::ColumnReverse
        { OnlyIndicesOutsideCollection::new( self.matrix.column_reverse(index), self.row_indices_to_exclude  )  }    

    fn has_row_for_index(     &   self, index: &Self::RowIndex   )   -> bool
        { 
            let matrix_has_row = self.matrix.has_row_for_index(index);
            let row_is_excluded = self.row_indices_to_exclude.map_has_key_or_sequence_has_element(index);
            return matrix_has_row && (! row_is_excluded)
        }    
    fn has_column_for_index(  &   self, index: & Self::ColumnIndex)   -> bool
        { self.matrix.has_column_for_index(index) }    
    fn structural_nonzero_entry(                   &   self, row:   & Self::RowIndex, column: & Self::ColumnIndex ) ->  Option< Self::Coefficient >
        { self.matrix.structural_nonzero_entry( row, column ) }

}



// Implement MatrixAlgebra
impl < Matrix, RowIndicesToExclude >

    MatrixAlgebra for  

    OnlyRowIndicesOutsideCollection
        < Matrix, RowIndicesToExclude >

    where
        Matrix:                         MatrixAlgebra,    
        RowIndicesToExclude:            Copy + MapHasKeyOrSequenceHasElement< Matrix::RowIndex >,    
{
    type RingOperator                   =   Matrix::RingOperator;
    type OrderOperatorForRowEntries     =   Matrix::OrderOperatorForRowEntries;
    type OrderOperatorForRowIndices     =   Matrix::OrderOperatorForRowIndices;
    type OrderOperatorForColumnEntries  =   Matrix::OrderOperatorForColumnEntries;
    type OrderOperatorForColumnIndices  =   Matrix::OrderOperatorForColumnIndices;

    fn ring_operator( &self ) -> Self::RingOperator { self.matrix.ring_operator() }
    fn order_operator_for_row_entries( &self ) -> Self::OrderOperatorForRowEntries { self.matrix.order_operator_for_row_entries() }
    fn order_operator_for_row_indices( &self ) -> Self::OrderOperatorForRowIndices { self.matrix.order_operator_for_row_indices() }    
    fn order_operator_for_column_entries( &self ) -> Self::OrderOperatorForColumnEntries { self.matrix.order_operator_for_column_entries() }
    fn order_operator_for_column_indices( &self ) -> Self::OrderOperatorForColumnIndices { self.matrix.order_operator_for_column_indices() }   
}



//  MASK: ONLY INDICES INSIDE
//  -------------------------------------------------------------------------------------



//  COLUMNS
//  -----------------------------------------


use crate::algebra::vectors::operations::OnlyIndicesInsideCollection;


/// A matrix oracle whose rows iterate only over entries that have indices **outside**
/// a given collection.
#[derive(Clone,Copy,Debug,Eq,PartialEq,Ord,PartialOrd)]
pub struct OnlyColumnIndicesInsideCollection
                < Matrix, ColumnIndicesToInclude, >
{
    matrix:                             Matrix,
    column_indices_to_include:          ColumnIndicesToInclude,   
}

// implement the object
impl < Matrix, ColumnIndicesToInclude >

    OnlyColumnIndicesInsideCollection
        < Matrix, ColumnIndicesToInclude >

    {
        /// Create a new [OnlyColumnIndicesInsideCollection] from a matrix and a collection
        /// of column indices.
        /// 
        /// In order to operate properly, the collection of column indices should implement
        /// the [`MapHasKeyOrSequenceHasElement`](crate::utilities::sets::MapHasKeyOrSequenceHasElement) 
        /// trait.
        pub fn new( matrix: Matrix, column_indices_to_include: ColumnIndicesToInclude ) 
        -> 
        OnlyColumnIndicesInsideCollection
            < Matrix, ColumnIndicesToInclude, >
        where
            Matrix:                         MatrixOracle,    
            ColumnIndicesToInclude:         MapHasKeyOrSequenceHasElement< Matrix::ColumnIndex >,              
        { OnlyColumnIndicesInsideCollection{ matrix, column_indices_to_include } }
    }    





// Implement MatrixOracle
impl < Matrix, ColumnIndicesToInclude >

    MatrixOracle for  

    OnlyColumnIndicesInsideCollection
        < Matrix, ColumnIndicesToInclude >

    where
        Matrix:                             MatrixOracle,    
        ColumnIndicesToInclude:             Copy + MapHasKeyOrSequenceHasElement< Matrix::ColumnIndex >,    
{
    type Coefficient            =     Matrix::Coefficient;   // The type of coefficient stored in each entry of the matrix    
    type RowIndex               =     Matrix::RowIndex;      // The type key used to look up rows.  Matrices can have rows indexed by anything: integers, simplices, strings, etc.
    type ColumnIndex            =     Matrix::ColumnIndex;   // The type of column indices    
    type RowEntry               =     Matrix::RowEntry;      // The type of entries in each row; these are essentially pairs of form `(column_index, coefficient)`
    type ColumnEntry            =     Matrix::ColumnEntry;   // The type of entries in each column; these are essentially pairs of form `(row_index, coefficient)`
    type Row                    =     OnlyIndicesInsideCollection< Matrix::Row,        ColumnIndicesToInclude >;           // What you get when you ask for a row.
    type RowReverse             =     OnlyIndicesInsideCollection< Matrix::RowReverse, ColumnIndicesToInclude >;    // What you get when you ask for a row with the order of entries reversed
    type Column                 =     Matrix::Column;        // What you get when you ask for a column
    type ColumnReverse          =     Matrix::ColumnReverse; // What you get when you ask for a column with the order of entries reversed                             


    fn row(                     &   self, index: &Self::RowIndex   )   -> Self::Row
        { OnlyIndicesInsideCollection::new( self.matrix.row(index), self.column_indices_to_include  )  }        
    fn row_reverse(             &   self, index: &Self::RowIndex   )   -> Self::RowReverse
        { OnlyIndicesInsideCollection::new( self.matrix.row_reverse(index), self.column_indices_to_include  )  }    
    fn column(                  &   self, index: & Self::ColumnIndex)   -> Self::Column
        { self.matrix.column(index) }    
    fn column_reverse(          &   self, index: & Self::ColumnIndex)   -> Self::ColumnReverse
        { self.matrix.column_reverse(index) }

    fn has_row_for_index(     &   self, index: &Self::RowIndex   )   -> bool
        { self.matrix.has_row_for_index(index) }
    fn has_column_for_index(  &   self, index: & Self::ColumnIndex)   -> bool
        { 
            let matrix_has_column = self.matrix.has_column_for_index(index);
            let column_is_included = self.column_indices_to_include.map_has_key_or_sequence_has_element(index);
            return matrix_has_column && column_is_included
        }    
    fn structural_nonzero_entry(                   &   self, row:   & Self::RowIndex, column: & Self::ColumnIndex ) ->  Option< Self::Coefficient >
        { self.matrix.structural_nonzero_entry( row, column ) }

}


// Implement MatrixAlgebra
impl < Matrix, ColumnIndicesToExclude >

    MatrixAlgebra for  

    OnlyColumnIndicesInsideCollection
        < Matrix, ColumnIndicesToExclude >

    where
        Matrix:                             MatrixAlgebra,    
        ColumnIndicesToExclude:             Copy + MapHasKeyOrSequenceHasElement< Matrix::ColumnIndex >,   
{
    type RingOperator                   =   Matrix::RingOperator;
    type OrderOperatorForRowEntries     =   Matrix::OrderOperatorForRowEntries;
    type OrderOperatorForRowIndices     =   Matrix::OrderOperatorForRowIndices;
    type OrderOperatorForColumnEntries  =   Matrix::OrderOperatorForColumnEntries;
    type OrderOperatorForColumnIndices  =   Matrix::OrderOperatorForColumnIndices;

    fn ring_operator( &self ) -> Self::RingOperator { self.matrix.ring_operator() }
    fn order_operator_for_row_entries( &self ) -> Self::OrderOperatorForRowEntries { self.matrix.order_operator_for_row_entries() }
    fn order_operator_for_row_indices( &self ) -> Self::OrderOperatorForRowIndices { self.matrix.order_operator_for_row_indices() }    
    fn order_operator_for_column_entries( &self ) -> Self::OrderOperatorForColumnEntries { self.matrix.order_operator_for_column_entries() }
    fn order_operator_for_column_indices( &self ) -> Self::OrderOperatorForColumnIndices { self.matrix.order_operator_for_column_indices() }   
}


//  ROWS
//  -----------------------------------------


/// A matrix oracle whose rows iterate only over entries that have indices **outside**
/// a given collection.
#[derive(Clone,Copy,Debug,Eq,PartialEq,Ord,PartialOrd)]
pub struct OnlyRowIndicesInsideCollection<
        Matrix, RowIndicesToInclude,
    >
    where
        Matrix:                         MatrixOracle,    
        RowIndicesToInclude:            MapHasKeyOrSequenceHasElement< Matrix::RowIndex >,                  
{
    matrix:                             Matrix,
    row_indices_to_include:             RowIndicesToInclude,
}

// implement the object
impl < Matrix, RowIndicesToInclude, >

    OnlyRowIndicesInsideCollection
        < Matrix, RowIndicesToInclude >

    where
        Matrix:                        MatrixOracle,    
        RowIndicesToInclude:           MapHasKeyOrSequenceHasElement< Matrix::RowIndex >,                          

    {
        /// Create a new [OnlyRowIndicesInsideCollection] from a matrix and a collection
        /// of row indices.
        /// 
        /// In order to operate properly, the collection of row indices should implement
        /// the [`MapHasKeyOrSequenceHasElement`](crate::utilities::sets::MapHasKeyOrSequenceHasElement) 
        /// trait.
        pub fn new( matrix: Matrix, row_indices_to_include: RowIndicesToInclude ) 
        -> 
        OnlyRowIndicesInsideCollection
            < Matrix, RowIndicesToInclude >
        where
            RowIndicesToInclude:                MapHasKeyOrSequenceHasElement< Matrix::RowIndex >,              
        { OnlyRowIndicesInsideCollection{ matrix, row_indices_to_include } }
    }    



impl < Matrix, RowIndicesToExclude >

    MatrixOracle for  

    OnlyRowIndicesInsideCollection
        < Matrix, RowIndicesToExclude >

    where
        Matrix:                          MatrixOracle,    
        RowIndicesToExclude:             Copy + MapHasKeyOrSequenceHasElement< Matrix::RowIndex >,    
{
    type Coefficient            =     Matrix::Coefficient;   // The type of coefficient stored in each entry of the matrix    
    type RowIndex               =     Matrix::RowIndex;      // The type key used to look up rows.  Matrices can have rows indexed by anything: integers, simplices, strings, etc.
    type ColumnIndex            =     Matrix::ColumnIndex;   // The type of column indices    
    type RowEntry               =     Matrix::RowEntry;      // The type of entries in each row; these are essentially pairs of form `(column_index, coefficient)`
    type ColumnEntry            =     Matrix::ColumnEntry;   // The type of entries in each column; these are essentially pairs of form `(row_index, coefficient)`
    type Row                    =     Matrix::Row;           // What you get when you ask for a row.
    type RowReverse             =     Matrix::RowReverse;    // What you get when you ask for a row with the order of entries reversed
    type Column                 =     OnlyIndicesInsideCollection< Matrix::Column,        RowIndicesToExclude >;        // What you get when you ask for a column
    type ColumnReverse          =     OnlyIndicesInsideCollection< Matrix::ColumnReverse, RowIndicesToExclude >; // What you get when you ask for a column with the order of entries reversed                             


    fn row(                     &   self, index: &Self::RowIndex   )   -> Self::Row
        { self.matrix.row(index) }    
    fn row_reverse(             &   self, index: &Self::RowIndex   )   -> Self::RowReverse
        { self.matrix.row_reverse(index) }
    fn column(                  &   self, index: & Self::ColumnIndex)   -> Self::Column
        { OnlyIndicesInsideCollection::new( self.matrix.column(index), self.row_indices_to_include  )  }        
    fn column_reverse(          &   self, index: & Self::ColumnIndex)   -> Self::ColumnReverse
        { OnlyIndicesInsideCollection::new( self.matrix.column_reverse(index), self.row_indices_to_include  )  }    

    fn has_row_for_index(     &   self, index: &Self::RowIndex   )   -> bool
        { 
            let matrix_has_row = self.matrix.has_row_for_index(index);
            let row_is_included = self.row_indices_to_include.map_has_key_or_sequence_has_element(index);
            return matrix_has_row && row_is_included
        }    
    fn has_column_for_index(  &   self, index: & Self::ColumnIndex)   -> bool
        { self.matrix.has_column_for_index(index) }    
    fn structural_nonzero_entry(                   &   self, row:   & Self::RowIndex, column: & Self::ColumnIndex ) ->  Option< Self::Coefficient >
        { self.matrix.structural_nonzero_entry( row, column ) }

}    




// Implement MatrixAlgebra
impl < Matrix, RowIndicesToExclude >

    MatrixAlgebra for  

    OnlyRowIndicesInsideCollection
        < Matrix, RowIndicesToExclude >

    where
        Matrix:                         MatrixAlgebra,    
        RowIndicesToExclude:            Copy + MapHasKeyOrSequenceHasElement< Matrix::RowIndex >,    
{
    type RingOperator                   =   Matrix::RingOperator;
    type OrderOperatorForRowEntries     =   Matrix::OrderOperatorForRowEntries;
    type OrderOperatorForRowIndices     =   Matrix::OrderOperatorForRowIndices;
    type OrderOperatorForColumnEntries  =   Matrix::OrderOperatorForColumnEntries;
    type OrderOperatorForColumnIndices  =   Matrix::OrderOperatorForColumnIndices;

    fn ring_operator( &self ) -> Self::RingOperator { self.matrix.ring_operator() }
    fn order_operator_for_row_entries( &self ) -> Self::OrderOperatorForRowEntries { self.matrix.order_operator_for_row_entries() }
    fn order_operator_for_row_indices( &self ) -> Self::OrderOperatorForRowIndices { self.matrix.order_operator_for_row_indices() }    
    fn order_operator_for_column_entries( &self ) -> Self::OrderOperatorForColumnEntries { self.matrix.order_operator_for_column_entries() }
    fn order_operator_for_column_indices( &self ) -> Self::OrderOperatorForColumnIndices { self.matrix.order_operator_for_column_indices() }   
}









//  TESTS
//  =========================================================================================================

//  Doc-test drafts
//  ---------------------------------------------------------------------

//  We use the following module to draft doc tests, which are easier to debug here than in doc strings.

#[cfg(test)]
mod doc_test_drafts {



}