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
//! Sparse matrices
//!
//! - [About](#about)
//! - [Tools](#modules)
//! - [Build your own matrix](#build-your-own-matrix)
//!
//!
//!
//! # About
//!
//! OAT provides a variety of tools to work with sparse matrices. Key features include
//! - flexible indexing: matrices can be indexed by arbitrary keys; for example, the boundary matrix of a simplicial complex can be indexed by simplices; this feature is powerful in a variety of homology computations, where explit enumeration of row and column indices is expensive
//! - flexible coefficients: you can work over any coeficient ring or field using [ring operators](crate::algebra::rings#terminology).
//! - lazy look up: rows and columns of matrices can be constructed in a lazy fashion, consistent with current state-of-the-art practices in large scale PH computation
//! - extensive unit testing
//!
//!
//! # Build your own matrix
//!
//! Users can work with any object *as if it were a matrix*, provided the object implements the right [query traits](crate::algebra::matrices::query).
//! Any object that implements one of these traits is called a **matrix oracle**.
//!
//! Let's illustrate with an example. We'll define an object that represents an identity matrix, but throw in a twist:
//! - rows are indexed integers
//! - columns are indexed by strings
//! - for example, row 0 has a single nonzero entry, in column "0". Note the difference: the row index is a number, but the column index is a string.
//!
//! This object uses no memory at all!
//!
//!
//! ```
//! use oat_rust::algebra::matrices::query::MatrixOracle;
//! use std::iter::Once;
//!
//!
//! // -------------------------------------------------------------------
//! // Define the new struct, and create an instance
//! // -------------------------------------------------------------------
//!
//! pub struct MyMatrix; // this struct contains no data, and uses zero memory
//! let matrix = MyMatrix; // here's an instance
//!
//!
//! // -------------------------------------------------------------------
//! // Implement `MatrixOracle`
//! // -------------------------------------------------------------------
//!
//! impl MatrixOracle for MyMatrix {
//! type Coefficient = i32;
//!
//! type RowIndex = usize;
//!
//! type ColumnIndex = String;
//!
//! type RowEntry = ( String, i32 );
//!
//! type ColumnEntry = ( usize, i32 );
//!
//! type Row = Once<( String, i32 )>;
//!
//! type RowReverse = Once<( String, i32 )>;
//!
//! type Column = Once<( usize, i32 )>;
//!
//! type ColumnReverse = Once<( usize, i32 )>;
//!
//! fn row( & self, index: & Self::RowIndex ) -> Self::Row {
//! let column_index = index.to_string(); // convert the integer to a String
//! std::iter::once( ( column_index, 1 ) )
//! }
//!
//! fn row_reverse( & self, index: & Self::RowIndex ) -> Self::RowReverse {
//! let column_index = index.to_string(); // convert the integer to a String
//! std::iter::once( ( column_index, 1 ) )
//! }
//!
//! fn column( & self, index: & Self::ColumnIndex) -> Self::Column {
//! let row_index = index.parse::<usize>().unwrap(); // convert the string to an integer
//! std::iter::once( ( row_index, 1 ) )
//! }
//!
//! fn column_reverse( & self, index: & Self::ColumnIndex) -> Self::ColumnReverse {
//! let row_index = index.parse::<usize>().unwrap(); // convert the string to an integer
//! std::iter::once( ( row_index, 1 ) )
//! }
//!
//! fn has_row_for_index( & self, index: & Self::RowIndex ) -> bool {
//! true // this matrix has a row for every possible integer
//! }
//!
//! fn has_column_for_index( & self, index: & Self::ColumnIndex) -> bool {
//! index.parse::<u64>().is_ok() // the matrix has a column for index `index` if and only if the `index` can be converted to a usize
//! }
//!
//! fn structural_nonzero_entry(& self, row: & Self::RowIndex, column: & Self::ColumnIndex ) -> Option< Self::Coefficient > {
//! if column == &row.to_string() {
//! // if the row and column indices agree, then return 1.0
//! Some(1)
//! } else {
//! // otherwise return None to indicate that the entry is structurally zero
//! None
//! }
//! }
//! }
//!
//!
//!
//! // -------------------------------------------------------------------
//! // Test
//! // -------------------------------------------------------------------
//!
//!
//! assert!( matrix.row( & 0 ).eq( std::iter::once( ( 0.to_string(), 1 ) ) ) );
//! assert!( matrix.row( & 1 ).eq( std::iter::once( ( 1.to_string(), 1 ) ) ) );
//! assert!( matrix.row_reverse( & 0 ).eq( std::iter::once( ( 0.to_string(), 1 ) ) ) );
//! assert!( matrix.row_reverse( & 1 ).eq( std::iter::once( ( 1.to_string(), 1 ) ) ) );
//!
//! assert!( matrix.column( & 0.to_string() ).eq( std::iter::once( ( 0, 1 ) ) ) );
//! assert!( matrix.column( & 1.to_string() ).eq( std::iter::once( ( 1, 1 ) ) ) );
//! assert!( matrix.column_reverse( & 0.to_string() ).eq( std::iter::once( ( 0, 1 ) ) ) );
//! assert!( matrix.column_reverse( & 1.to_string() ).eq( std::iter::once( ( 1, 1 ) ) ) );
//!
//! assert_eq!( matrix.structural_nonzero_entry( & 0, & 0.to_string() ), Some(1) );
//! assert_eq!( matrix.structural_nonzero_entry( & 0, & 1.to_string() ), None );
//!
//! assert!( matrix.has_row_for_index( & 0 ) );
//! ```
//!
//!
//!