nettrash-a 0.1.29

Algorithms in pure Rust with no dependencies: 23 sorting algorithms and a default sort; arbitrary-precision integers with Karatsuba, Toom-Cook and NTT multiplication, Newton division, GCD, roots, modular powers, primes and modular square roots; elliptic curves over prime fields with secp256k1, P-256, P-384, P-521, Curve25519 and Ed25519; complex numbers; matrices of floats, complex numbers or integers with LU, QR, Cholesky, SVD, Schur and eigendecompositions, exp, sqrt and log, and exact integer determinants and inverses. Not constant-time.
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
//! Matrices: a [`Matrix<T>`] is a rectangular array of elements of the type `T`, which is `f32`,
//! `f64`, [`Complex<f32>`](crate::math::complex::Complex),
//! [`Complex<f64>`](crate::math::complex::Complex) or [`Number`](crate::math::numbers::Number).

mod basic;
mod blas;
mod cholesky;
mod eigen;
mod element;
mod exact;
mod functions;
mod gemm;
mod hermitian;
mod householder;
mod lu;
mod ops;
mod properties;
mod qr;
mod schur;
mod svd;
mod tridiagonal;

pub use cholesky::Cholesky;
pub use eigen::{Eigen, HermitianEigen};
pub use element::{Element, Scalar};
pub use lu::Lu;
pub use properties::Norm;
pub use qr::Qr;
pub use schur::Schur;
pub use svd::Svd;

use core::fmt;
use core::ops::{Index, IndexMut};

/// A matrix of `rows × cols` elements of the type `T`, held column after column in one vector.
///
/// # Examples
///
/// ```
/// use a::math::matrix::Matrix;
///
/// let a = Matrix::from_rows(&[[1.0, 2.0], [3.0, 4.0]]);
/// let b = Matrix::identity(2);
/// assert_eq!(&a * &b, a);
/// assert_eq!(a[(1, 0)], 3.0);
/// assert_eq!(a.transpose(), Matrix::from_rows(&[[1.0, 3.0], [2.0, 4.0]]));
/// ```
#[derive(Clone, PartialEq, Eq, Hash, Default)]
pub struct Matrix<T> {
    /// The number of rows.
    rows: usize,
    /// The number of columns.
    cols: usize,
    /// The elements, column after column.
    data: Vec<T>,
}

/// The number of elements of a `rows × cols` matrix, which a release build would otherwise let
/// wrap around and leave a matrix of nonempty dimensions with no elements.
///
/// # Panics
///
/// Panics if it overflows `usize`.
#[track_caller]
fn size(rows: usize, cols: usize) -> usize {
    rows.checked_mul(cols)
        .expect("the matrix has more elements than a vector can hold")
}

impl<T> Matrix<T> {
    /// The matrix of `rows × cols` elements given column after column.
    ///
    /// # Panics
    ///
    /// Panics if `data` doesn't hold `rows · cols` elements.
    #[track_caller]
    pub fn from_column_major(rows: usize, cols: usize, data: Vec<T>) -> Self {
        assert_eq!(
            Some(data.len()),
            rows.checked_mul(cols),
            "the data doesn't hold rows × cols elements"
        );
        Matrix { rows, cols, data }
    }

    /// The matrix of `rows × cols` elements given row after row.
    ///
    /// # Panics
    ///
    /// Panics if `data` doesn't hold `rows · cols` elements.
    #[track_caller]
    pub fn from_row_major(rows: usize, cols: usize, data: Vec<T>) -> Self {
        Matrix::from_column_major(cols, rows, data).into_transpose()
    }

    /// The matrix whose element in row `i` and column `j` is `f(i, j)`, computed column after
    /// column.
    ///
    /// # Panics
    ///
    /// Panics if `rows · cols` overflows `usize`.
    #[track_caller]
    pub fn from_fn(rows: usize, cols: usize, mut f: impl FnMut(usize, usize) -> T) -> Self {
        let mut data = Vec::with_capacity(size(rows, cols));
        for j in 0..cols {
            // A column at a time, from a range whose length is known, so that the vector is not
            // checked for room at each element.
            data.extend((0..rows).map(|i| f(i, j)));
        }
        Matrix { rows, cols, data }
    }

    /// The number of rows.
    #[inline]
    pub fn rows(&self) -> usize {
        self.rows
    }

    /// The number of columns.
    #[inline]
    pub fn cols(&self) -> usize {
        self.cols
    }

    /// `(rows, cols)`.
    #[inline]
    pub fn shape(&self) -> (usize, usize) {
        (self.rows, self.cols)
    }

    /// Whether it has as many rows as columns.
    #[inline]
    pub fn is_square(&self) -> bool {
        self.rows == self.cols
    }

    /// Whether it has no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// The element in row `i` and column `j`, or `None` outside the matrix.
    #[inline]
    pub fn get(&self, i: usize, j: usize) -> Option<&T> {
        if i < self.rows && j < self.cols {
            Some(&self.data[i + j * self.rows])
        } else {
            None
        }
    }

    /// The element in row `i` and column `j`, to change, or `None` outside the matrix.
    #[inline]
    pub fn get_mut(&mut self, i: usize, j: usize) -> Option<&mut T> {
        if i < self.rows && j < self.cols {
            Some(&mut self.data[i + j * self.rows])
        } else {
            None
        }
    }

    /// Column `j`.
    ///
    /// # Panics
    ///
    /// Panics if `j` is not below the number of columns.
    #[inline]
    #[track_caller]
    pub fn column(&self, j: usize) -> &[T] {
        assert!(j < self.cols, "the column is outside the matrix");
        &self.data[j * self.rows..(j + 1) * self.rows]
    }

    /// Column `j`, to change.
    ///
    /// # Panics
    ///
    /// Panics if `j` is not below the number of columns.
    #[inline]
    #[track_caller]
    pub fn column_mut(&mut self, j: usize) -> &mut [T] {
        assert!(j < self.cols, "the column is outside the matrix");
        &mut self.data[j * self.rows..(j + 1) * self.rows]
    }

    /// The elements of row `i`, from the left.
    ///
    /// # Panics
    ///
    /// Panics if `i` is not below the number of rows.
    #[track_caller]
    pub fn row(&self, i: usize) -> impl ExactSizeIterator<Item = &T> + '_ {
        assert!(i < self.rows, "the row is outside the matrix");
        (0..self.cols).map(move |j| &self.data[i + j * self.rows])
    }

    /// The elements, column after column.
    #[inline]
    pub fn as_slice(&self) -> &[T] {
        &self.data
    }

    /// The elements, column after column, to change.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        &mut self.data
    }

    /// The vector of the elements, column after column.
    #[inline]
    pub fn into_vec(self) -> Vec<T> {
        self.data
    }

    /// The matrix of `f` of each element.
    pub fn map<U>(&self, f: impl FnMut(&T) -> U) -> Matrix<U> {
        Matrix {
            rows: self.rows,
            cols: self.cols,
            data: self.data.iter().map(f).collect(),
        }
    }

    /// The transpose, moving the elements.
    fn into_transpose(self) -> Self {
        let (rows, cols) = (self.rows, self.cols);
        if rows <= 1 || cols <= 1 {
            return Matrix {
                rows: cols,
                cols: rows,
                data: self.data,
            };
        }
        let mut slots: Vec<Option<T>> = self.data.into_iter().map(Some).collect();
        Matrix::from_fn(cols, rows, |i, j| slots[j + i * rows].take().unwrap())
    }
}

impl<T: Clone> Matrix<T> {
    /// The matrix with the given rows.
    ///
    /// # Panics
    ///
    /// Panics if the rows are not all of the same length.
    #[track_caller]
    pub fn from_rows<R: AsRef<[T]>>(rows: &[R]) -> Self {
        let cols = rows.first().map_or(0, |r| r.as_ref().len());
        assert!(
            rows.iter().all(|r| r.as_ref().len() == cols),
            "the rows are not all of the same length"
        );
        Matrix::from_fn(rows.len(), cols, |i, j| rows[i].as_ref()[j].clone())
    }

    /// The matrix with the given columns.
    ///
    /// # Panics
    ///
    /// Panics if the columns are not all of the same length.
    #[track_caller]
    pub fn from_columns<C: AsRef<[T]>>(columns: &[C]) -> Self {
        let rows = columns.first().map_or(0, |c| c.as_ref().len());
        assert!(
            columns.iter().all(|c| c.as_ref().len() == rows),
            "the columns are not all of the same length"
        );
        let data = columns
            .iter()
            .flat_map(|c| c.as_ref().iter().cloned())
            .collect();
        Matrix {
            rows,
            cols: columns.len(),
            data,
        }
    }

    /// The elements of the main diagonal, from the top left.
    pub fn diagonal(&self) -> Vec<T> {
        (0..self.rows.min(self.cols))
            .map(|i| self.data[i + i * self.rows].clone())
            .collect()
    }
}

impl<T: Element> Matrix<T> {
    /// The `rows × cols` matrix of zeros.
    ///
    /// # Panics
    ///
    /// Panics if `rows · cols` overflows `usize`.
    #[track_caller]
    pub fn zeros(rows: usize, cols: usize) -> Self {
        // Filled at once, rather than element by element: for floats the allocator's zeroed
        // pages, which took 1.4 µs for `256 × 256` against 69 by `from_fn`.
        Matrix {
            rows,
            cols,
            data: vec![T::zero(); size(rows, cols)],
        }
    }

    /// The `n × n` identity matrix.
    pub fn identity(n: usize) -> Self {
        Matrix::from_diagonal(&vec![T::one(); n])
    }

    /// The square matrix with `diagonal` on its main diagonal and zeros elsewhere.
    pub fn from_diagonal(diagonal: &[T]) -> Self {
        let n = diagonal.len();
        let mut a = Matrix::zeros(n, n);
        for (i, d) in diagonal.iter().enumerate() {
            a.data[i + i * n] = d.clone();
        }
        a
    }
}

impl<T> Index<(usize, usize)> for Matrix<T> {
    type Output = T;

    /// The element in row `i` and column `j`.
    ///
    /// # Panics
    ///
    /// Panics outside the matrix.
    #[inline]
    #[track_caller]
    fn index(&self, (i, j): (usize, usize)) -> &T {
        assert!(
            i < self.rows && j < self.cols,
            "the index is outside the matrix"
        );
        &self.data[i + j * self.rows]
    }
}

impl<T> IndexMut<(usize, usize)> for Matrix<T> {
    /// The element in row `i` and column `j`, to change.
    ///
    /// # Panics
    ///
    /// Panics outside the matrix.
    #[inline]
    #[track_caller]
    fn index_mut(&mut self, (i, j): (usize, usize)) -> &mut T {
        assert!(
            i < self.rows && j < self.cols,
            "the index is outside the matrix"
        );
        &mut self.data[i + j * self.rows]
    }
}

impl<T: fmt::Display> fmt::Display for Matrix<T> {
    /// Writes the rows on lines of their own, each element as `Display` writes it with the
    /// formatter's precision, right-aligned in its column, the columns two spaces apart.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let text: Vec<String> = self
            .data
            .iter()
            .map(|x| match f.precision() {
                Some(p) => format!("{x:.p$}"),
                None => format!("{x}"),
            })
            .collect();
        let widths: Vec<usize> = (0..self.cols)
            .map(|j| {
                let column = &text[j * self.rows..(j + 1) * self.rows];
                column.iter().map(|s| s.chars().count()).max().unwrap_or(0)
            })
            .collect();
        for i in 0..self.rows {
            if i > 0 {
                writeln!(f)?;
            }
            for (j, width) in widths.iter().enumerate() {
                let gap = if j == 0 { "" } else { "  " };
                write!(f, "{gap}{:>width$}", text[i + j * self.rows])?;
            }
        }
        Ok(())
    }
}

impl<T: fmt::Debug> fmt::Debug for Matrix<T> {
    /// Writes the rows as lists: `[[1, 2], [3, 4]]`.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list()
            .entries((0..self.rows).map(|i| DebugRow(self, i)))
            .finish()
    }
}

/// A row of a matrix for [`fmt::Debug`].
struct DebugRow<'a, T>(&'a Matrix<T>, usize);

impl<T: fmt::Debug> fmt::Debug for DebugRow<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.0.row(self.1)).finish()
    }
}