faer 0.24.0

linear algebra library
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
//! sparse matrix data structures
//!
//! most sparse matrix algorithms accept matrices in sparse column-oriented
//! format. this format represents each column of the matrix by storing the row
//! indices of its non-zero elements, as well as their values
//!
//! the indices and the values are each stored in a contiguous slice (or group
//! of slices for arbitrary values). in order to specify where each column
//! starts and ends, a slice of size `ncols + 1` stores the start of each
//! column, with the last element being equal to the total number of non-zeros
//! (or the capacity in uncompressed mode)
//!
//! # example
//!
//! consider the 4-by-5 matrix:
//! ```notcode
//! [[10.0, 0.0, 12.0, -1.0, 13.0]
//!  [ 0.0, 0.0, 25.0, -2.0,  0.0]
//!  [ 1.0, 0.0,  0.0,  0.0,  0.0]
//!  [ 4.0, 0.0,  0.0,  0.0,  5.0]]
//! ```
//!
//! the matrix is stored as follows:
//! ```notcode
//! column pointers: | 0                  | 3,3         | 5           | 7           | 9
//!
//! row indices    : |    0 |    2 |    3 |    0 |    1 |    0 |    1 |    0 |    3 |
//! values         : | 10.0 |  1.0 |  4.0 | 12.0 | 25.0 | -1.0 | -2.0 | 13.0 |  5.0 |
//! ```
mod csc;
mod csr;
pub(crate) const NONE: usize = usize::MAX;
/// sparse linear algebra module.
/// contains low level routines and the implementation of their corresponding
/// high level wrappers
pub mod linalg;
/// sparse matrix binary and ternary operation implementations
pub mod ops;
use crate::internal_prelude_sp::Index;
pub use csc::{
	SparseColMat, SparseColMatMut, SparseColMatRef, SymbolicSparseColMat,
	SymbolicSparseColMatRef, numeric as csc_numeric, symbolic as csc_symbolic,
};
pub use csr::{
	SparseRowMat, SparseRowMatMut, SparseRowMatRef, SymbolicSparseRowMat,
	SymbolicSparseRowMatRef, numeric as csr_numeric, symbolic as csr_symbolic,
};
use reborrow::*;
extern crate alloc;
/// pair of indices with `C`-compatible layout
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct Pair<Row, Col> {
	/// row index
	pub row: Row,
	/// column index
	pub col: Col,
}
/// triplet of indices and value with `C`-compatible layout
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct Triplet<Row, Col, T> {
	/// row index
	pub row: Row,
	/// column index
	pub col: Col,
	/// value
	pub val: T,
}
impl<Row, Col> Pair<Row, Col> {
	/// creates a new pair of indices
	#[inline]
	pub const fn new(row: Row, col: Col) -> Self {
		Pair { row, col }
	}
}
impl<Row, Col, T> Triplet<Row, Col, T> {
	/// creates a new pair of indices and value
	#[inline]
	pub const fn new(row: Row, col: Col, val: T) -> Self {
		Triplet { row, col, val }
	}
}
/// errors that can occur in sparse algorithms
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[non_exhaustive]
pub enum FaerError {
	/// an index exceeding the maximum value (`I::Signed::MAX` for a given index
	/// type `I`)
	IndexOverflow,
	/// memory allocation failed
	OutOfMemory,
}
impl From<dyn_stack::mem::AllocError> for FaerError {
	#[inline]
	fn from(value: dyn_stack::mem::AllocError) -> Self {
		_ = value;
		FaerError::OutOfMemory
	}
}
impl From<alloc::collections::TryReserveError> for FaerError {
	#[inline]
	fn from(value: alloc::collections::TryReserveError) -> Self {
		_ = value;
		FaerError::OutOfMemory
	}
}
impl core::fmt::Display for FaerError {
	#[inline]
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		core::fmt::Debug::fmt(self, f)
	}
}
impl core::error::Error for FaerError {}
/// errors that can occur during the creation of sparse matrices from user input
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum CreationError {
	/// generic error (allocation or index overflow)
	Generic(FaerError),
	/// matrix index out-of-bounds error
	OutOfBounds {
		/// row of the out-of-bounds index
		row: usize,
		/// column of the out-of-bounds index
		col: usize,
	},
}
impl From<FaerError> for CreationError {
	#[inline]
	fn from(value: FaerError) -> Self {
		Self::Generic(value)
	}
}
impl core::fmt::Display for CreationError {
	#[inline]
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		core::fmt::Debug::fmt(self, f)
	}
}
impl core::error::Error for CreationError {}
#[inline(always)]
pub(crate) fn windows2<I>(
	slice: &[I],
) -> impl DoubleEndedIterator<Item = &[I; 2]> {
	slice.windows(2).map(
		#[inline(always)]
		|window| unsafe { &*(window.as_ptr() as *const [I; 2]) },
	)
}
#[inline]
#[track_caller]
pub(crate) fn try_zeroed<I: bytemuck::Pod>(
	n: usize,
) -> Result<alloc::vec::Vec<I>, FaerError> {
	let mut v = alloc::vec::Vec::new();
	v.try_reserve_exact(n).map_err(|_| FaerError::OutOfMemory)?;
	unsafe {
		core::ptr::write_bytes::<I>(v.as_mut_ptr(), 0u8, n);
		v.set_len(n);
	}
	Ok(v)
}
#[inline]
#[track_caller]
pub(crate) fn try_collect<I: IntoIterator>(
	iter: I,
) -> Result<alloc::vec::Vec<I::Item>, FaerError> {
	let iter = iter.into_iter();
	let mut v = alloc::vec::Vec::new();
	v.try_reserve_exact(iter.size_hint().0)
		.map_err(|_| FaerError::OutOfMemory)?;
	v.extend(iter);
	Ok(v)
}
/// the order values should be read in, when constructing/filling from indices
/// and values
///
/// allows separately creating the symbolic structure and filling the numerical
/// values
#[derive(Debug, Clone)]
pub struct Argsort<I> {
	idx: alloc::vec::Vec<I>,
	all_nnz: usize,
	nnz: usize,
}
/// algorithmic primitives for sparse matrices
pub mod utils;
impl<I: Index, T> core::ops::Index<(usize, usize)>
	for SparseColMatRef<'_, I, T>
{
	type Output = T;

	#[track_caller]
	fn index(&self, (row, col): (usize, usize)) -> &Self::Output {
		self.get(row, col).unwrap()
	}
}
impl<I: Index, T> core::ops::Index<(usize, usize)>
	for SparseRowMatRef<'_, I, T>
{
	type Output = T;

	#[track_caller]
	fn index(&self, (row, col): (usize, usize)) -> &Self::Output {
		self.get(row, col).unwrap()
	}
}
impl<I: Index, T> core::ops::Index<(usize, usize)>
	for SparseColMatMut<'_, I, T>
{
	type Output = T;

	#[track_caller]
	fn index(&self, (row, col): (usize, usize)) -> &Self::Output {
		self.rb().get(row, col).unwrap()
	}
}
impl<I: Index, T> core::ops::Index<(usize, usize)>
	for SparseRowMatMut<'_, I, T>
{
	type Output = T;

	#[track_caller]
	fn index(&self, (row, col): (usize, usize)) -> &Self::Output {
		self.rb().get(row, col).unwrap()
	}
}
impl<I: Index, T> core::ops::IndexMut<(usize, usize)>
	for SparseColMatMut<'_, I, T>
{
	#[track_caller]
	fn index_mut(&mut self, (row, col): (usize, usize)) -> &mut Self::Output {
		self.rb_mut().get_mut(row, col).unwrap()
	}
}
impl<I: Index, T> core::ops::IndexMut<(usize, usize)>
	for SparseRowMatMut<'_, I, T>
{
	#[track_caller]
	fn index_mut(&mut self, (row, col): (usize, usize)) -> &mut Self::Output {
		self.rb_mut().get_mut(row, col).unwrap()
	}
}
impl<I: Index, T> core::ops::Index<(usize, usize)> for SparseColMat<I, T> {
	type Output = T;

	#[track_caller]
	fn index(&self, (row, col): (usize, usize)) -> &Self::Output {
		self.rb().get(row, col).unwrap()
	}
}
impl<I: Index, T> core::ops::Index<(usize, usize)> for SparseRowMat<I, T> {
	type Output = T;

	#[track_caller]
	fn index(&self, (row, col): (usize, usize)) -> &Self::Output {
		self.rb().get(row, col).unwrap()
	}
}
impl<I: Index, T> core::ops::IndexMut<(usize, usize)> for SparseColMat<I, T> {
	#[track_caller]
	fn index_mut(&mut self, (row, col): (usize, usize)) -> &mut Self::Output {
		self.rb_mut().get_mut(row, col).unwrap()
	}
}
impl<I: Index, T> core::ops::IndexMut<(usize, usize)> for SparseRowMat<I, T> {
	#[track_caller]
	fn index_mut(&mut self, (row, col): (usize, usize)) -> &mut Self::Output {
		self.rb_mut().get_mut(row, col).unwrap()
	}
}
#[cfg(test)]
mod tests {
	use super::*;
	use crate::assert;
	#[test]
	fn test_from_indices() {
		let nrows = 5;
		let ncols = 4;
		let indices = &[
			Pair::new(0, 0),
			Pair::new(1, 2),
			Pair::new(0, 0),
			Pair::new(1, 1),
			Pair::new(0, 1),
			Pair::new(3, 3),
			Pair::new(3, 3usize),
		];
		let values = &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0f64];
		let triplets = &[
			Triplet::new(0, 0, 1.0),
			Triplet::new(1, 2, 2.0),
			Triplet::new(0, 0, 3.0),
			Triplet::new(1, 1, 4.0),
			Triplet::new(0, 1, 5.0),
			Triplet::new(3, 3, 6.0),
			Triplet::new(3, 3usize, 7.0_f64),
		];
		{
			let mat = SymbolicSparseColMat::try_new_from_indices(
				nrows, ncols, indices,
			);
			assert!(mat.is_ok());
			let (mat, order) = mat.unwrap();
			assert!(mat.nrows() == nrows);
			assert!(mat.ncols() == ncols);
			assert!(mat.col_ptr() == &[0, 1, 3, 4, 5]);
			assert!(mat.col_nnz() == None);
			assert!(mat.row_idx() == &[0, 0, 1, 1, 3]);
			let mat =
				SparseColMat::<_, f64>::new_from_argsort(mat, &order, values)
					.unwrap();
			assert!(mat.val() == &[1.0 + 3.0, 5.0, 4.0, 2.0, 6.0 + 7.0]);
		}
		{
			let mat =
				SparseColMat::try_new_from_triplets(nrows, ncols, triplets);
			assert!(mat.is_ok());
			let mat = mat.unwrap();
			assert!(mat.nrows() == nrows);
			assert!(mat.ncols() == ncols);
			assert!(mat.col_ptr() == &[0, 1, 3, 4, 5]);
			assert!(mat.col_nnz() == None);
			assert!(mat.row_idx() == &[0, 0, 1, 1, 3]);
			assert!(mat.val() == &[1.0 + 3.0, 5.0, 4.0, 2.0, 6.0 + 7.0]);
		}
		{
			let mat = SymbolicSparseRowMat::try_new_from_indices(
				nrows, ncols, indices,
			);
			assert!(mat.is_ok());
			let (mat, order) = mat.unwrap();
			assert!(mat.nrows() == nrows);
			assert!(mat.ncols() == ncols);
			assert!(mat.row_ptr() == &[0, 2, 4, 4, 5, 5]);
			assert!(mat.row_nnz() == None);
			assert!(mat.col_idx() == &[0, 1, 1, 2, 3]);
			let mat =
				SparseRowMat::<_, f64>::new_from_argsort(mat, &order, values)
					.unwrap();
			assert!(mat.val() == &[1.0 + 3.0, 5.0, 4.0, 2.0, 6.0 + 7.0]);
		}
		{
			let mat =
				SparseRowMat::try_new_from_triplets(nrows, ncols, triplets);
			assert!(mat.is_ok());
			let mat = mat.unwrap();
			assert!(mat.nrows() == nrows);
			assert!(mat.ncols() == ncols);
			assert!(mat.row_ptr() == &[0, 2, 4, 4, 5, 5]);
			assert!(mat.row_nnz() == None);
			assert!(mat.col_idx() == &[0, 1, 1, 2, 3]);
			assert!(mat.val() == &[1.0 + 3.0, 5.0, 4.0, 2.0, 6.0 + 7.0]);
		}
	}
	#[test]
	fn test_from_nonnegative_indices() {
		let nrows = 5;
		let ncols = 4;
		let indices = &[
			Pair::new(0, 0),
			Pair::new(1, 2),
			Pair::new(0, 0),
			Pair::new(1, 1),
			Pair::new(0, 1),
			Pair::new(-1, 2),
			Pair::new(-2, 1),
			Pair::new(-3, -4),
			Pair::new(3, 3),
			Pair::new(3, 3isize),
		];
		let values = &[
			1.0,
			2.0,
			3.0,
			4.0,
			5.0,
			f64::NAN,
			f64::NAN,
			f64::NAN,
			6.0,
			7.0f64,
		];
		let triplets = &[
			Triplet::new(0, 0, 1.0),
			Triplet::new(1, 2, 2.0),
			Triplet::new(0, 0, 3.0),
			Triplet::new(1, 1, 4.0),
			Triplet::new(0, 1, 5.0),
			Triplet::new(-1, 2, f64::NAN),
			Triplet::new(-2, 1, f64::NAN),
			Triplet::new(-3, -4, f64::NAN),
			Triplet::new(3, 3, 6.0),
			Triplet::new(3, 3isize, 7.0_f64),
		];
		{
			let mat =
				SymbolicSparseColMat::<usize>::try_new_from_nonnegative_indices(
					nrows, ncols, indices,
				);
			assert!(mat.is_ok());
			let (mat, order) = mat.unwrap();
			assert!(mat.nrows() == nrows);
			assert!(mat.ncols() == ncols);
			assert!(mat.col_ptr() == &[0, 1, 3, 4, 5]);
			assert!(mat.col_nnz() == None);
			assert!(mat.row_idx() == &[0, 0, 1, 1, 3]);
			let mat =
				SparseColMat::<_, f64>::new_from_argsort(mat, &order, values)
					.unwrap();
			assert!(mat.val() == &[1.0 + 3.0, 5.0, 4.0, 2.0, 6.0 + 7.0]);
		}
		{
			let mat =
				SparseColMat::<usize, _>::try_new_from_nonnegative_triplets(
					nrows, ncols, triplets,
				);
			assert!(mat.is_ok());
			let mat = mat.unwrap();
			assert!(mat.nrows() == nrows);
			assert!(mat.ncols() == ncols);
			assert!(mat.col_ptr() == &[0, 1, 3, 4, 5]);
			assert!(mat.col_nnz() == None);
			assert!(mat.row_idx() == &[0, 0, 1, 1, 3]);
			assert!(mat.val() == &[1.0 + 3.0, 5.0, 4.0, 2.0, 6.0 + 7.0]);
		}
		{
			let mat =
				SymbolicSparseRowMat::<usize>::try_new_from_nonnegative_indices(
					nrows, ncols, indices,
				);
			assert!(mat.is_ok());
			let (mat, order) = mat.unwrap();
			assert!(mat.nrows() == nrows);
			assert!(mat.ncols() == ncols);
			assert!(mat.row_ptr() == &[0, 2, 4, 4, 5, 5]);
			assert!(mat.row_nnz() == None);
			assert!(mat.col_idx() == &[0, 1, 1, 2, 3]);
			let mat =
				SparseRowMat::<_, f64>::new_from_argsort(mat, &order, values)
					.unwrap();
			assert!(mat.val() == &[1.0 + 3.0, 5.0, 4.0, 2.0, 6.0 + 7.0]);
		}
		{
			let mat =
				SparseRowMat::<usize, _>::try_new_from_nonnegative_triplets(
					nrows, ncols, triplets,
				);
			assert!(mat.is_ok());
			let mat = mat.unwrap();
			assert!(mat.nrows() == nrows);
			assert!(mat.ncols() == ncols);
			assert!(mat.row_ptr() == &[0, 2, 4, 4, 5, 5]);
			assert!(mat.row_nnz() == None);
			assert!(mat.col_idx() == &[0, 1, 1, 2, 3]);
			assert!(mat.val() == &[1.0 + 3.0, 5.0, 4.0, 2.0, 6.0 + 7.0]);
		}
	}
	#[test]
	fn test_from_indices_oob_row() {
		let nrows = 5;
		let ncols = 4;
		let indices = &[
			Pair::new(0, 0),
			Pair::new(1, 2),
			Pair::new(0, 0),
			Pair::new(1, 1),
			Pair::new(0, 1),
			Pair::new(3, 3),
			Pair::new(3, 3),
			Pair::new(5, 3usize),
		];
		let err =
			SymbolicSparseColMat::try_new_from_indices(nrows, ncols, indices);
		assert!(err.is_err());
		let err = err.unwrap_err();
		assert!(err == CreationError::OutOfBounds { row: 5, col: 3 });
	}
	#[test]
	fn test_from_indices_oob_col() {
		let nrows = 5;
		let ncols = 4;
		let indices = &[
			Pair::new(0, 0),
			Pair::new(1, 2),
			Pair::new(0, 0),
			Pair::new(1, 1),
			Pair::new(0, 1),
			Pair::new(3, 3),
			Pair::new(3, 3),
			Pair::new(2, 4usize),
		];
		let err =
			SymbolicSparseColMat::try_new_from_indices(nrows, ncols, indices);
		assert!(err.is_err());
		let err = err.unwrap_err();
		assert!(err == CreationError::OutOfBounds { row: 2, col: 4 });
	}
}