single_svdlib/
utils.rs

1use ndarray::{Array1, Array2};
2use num_traits::{Float, FromPrimitive, One, Zero};
3use std::fmt::Debug;
4use std::iter::Sum;
5use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
6use single_utilities::traits::FloatOpsTS;
7
8pub fn determine_chunk_size(nrows: usize) -> usize {
9    let num_threads = rayon::current_num_threads();
10
11    let min_rows_per_thread = 16;
12    let desired_chunks_per_thread = 4;
13
14    let target_total_chunks = num_threads * desired_chunks_per_thread;
15    let chunk_size = nrows.div_ceil(target_total_chunks);
16
17    chunk_size.max(min_rows_per_thread)
18}
19
20pub trait SMat<T: Float>: Sync {
21    fn nrows(&self) -> usize;
22    fn ncols(&self) -> usize;
23    fn nnz(&self) -> usize;
24    fn svd_opa(&self, x: &[T], y: &mut [T], transposed: bool); // y = A*x
25}
26
27/// Singular Value Decomposition Components
28///
29/// # Fields
30/// - d:  Dimensionality (rank), the number of rows of both `ut`, `vt` and the length of `s`
31/// - ut: Transpose of left singular vectors, the vectors are the rows of `ut`
32/// - s:  Singular values (length `d`)
33/// - vt: Transpose of right singular vectors, the vectors are the rows of `vt`
34/// - diagnostics: Computational diagnostics
35#[derive(Debug, Clone, PartialEq)]
36pub struct SvdRec<T: Float> {
37    pub d: usize,
38    pub u: Array2<T>,
39    pub s: Array1<T>,
40    pub vt: Array2<T>,
41    pub diagnostics: Diagnostics<T>,
42}
43
44/// Computational Diagnostics
45///
46/// # Fields
47/// - non_zero:  Number of non-zeros in the matrix
48/// - dimensions: Number of dimensions attempted (bounded by matrix shape)
49/// - iterations: Number of iterations attempted (bounded by dimensions and matrix shape)
50/// - transposed:  True if the matrix was transposed internally
51/// - lanczos_steps: Number of Lanczos steps performed
52/// - ritz_values_stabilized: Number of ritz values
53/// - significant_values: Number of significant values discovered
54/// - singular_values: Number of singular values returned
55/// - end_interval: left, right end of interval containing unwanted eigenvalues
56/// - kappa: relative accuracy of ritz values acceptable as eigenvalues
57/// - random_seed: Random seed provided or the seed generated
58#[derive(Debug, Clone, PartialEq)]
59pub struct Diagnostics<T: Float> {
60    pub non_zero: usize,
61    pub dimensions: usize,
62    pub iterations: usize,
63    pub transposed: bool,
64    pub lanczos_steps: usize,
65    pub ritz_values_stabilized: usize,
66    pub significant_values: usize,
67    pub singular_values: usize,
68    pub end_interval: [T; 2],
69    pub kappa: T,
70    pub random_seed: u32,
71}
72
73pub trait SvdFloat:
74    FloatOpsTS
75{
76    fn eps() -> Self;
77    fn eps34() -> Self;
78    fn compare(a: Self, b: Self) -> bool;
79}
80
81impl SvdFloat for f32 {
82    fn eps() -> Self {
83        f32::EPSILON
84    }
85
86    fn eps34() -> Self {
87        f32::EPSILON.powf(0.75)
88    }
89
90    fn compare(a: Self, b: Self) -> bool {
91        (b - a).abs() < f32::EPSILON
92    }
93}
94
95impl SvdFloat for f64 {
96    fn eps() -> Self {
97        f64::EPSILON
98    }
99
100    fn eps34() -> Self {
101        f64::EPSILON.powf(0.75)
102    }
103
104    fn compare(a: Self, b: Self) -> bool {
105        (b - a).abs() < f64::EPSILON
106    }
107}