1use rayon::iter::ParallelIterator;
2use nalgebra_sparse::na::{DMatrix, DVector};
3use ndarray::{Array1, Array2};
4use num_traits::{Float, Zero};
5use rayon::prelude::{IntoParallelIterator, IndexedParallelIterator};
6use single_utilities::traits::FloatOpsTS;
7use std::fmt::Debug;
8
9pub fn determine_chunk_size(nrows: usize) -> usize {
10 let num_threads = rayon::current_num_threads();
11
12 let min_rows_per_thread = 64;
13 let desired_chunks_per_thread = 4;
14
15 let target_total_chunks = num_threads * desired_chunks_per_thread;
16 let chunk_size = nrows.div_ceil(target_total_chunks);
17
18 chunk_size.max(min_rows_per_thread)
19}
20
21pub trait SMat<T: Float> {
22 fn nrows(&self) -> usize;
23 fn ncols(&self) -> usize;
24 fn nnz(&self) -> usize;
25 fn svd_opa(&self, x: &[T], y: &mut [T], transposed: bool); fn compute_column_means(&self) -> Vec<T>;
27 fn multiply_with_dense(&self, dense: &DMatrix<T>, result: &mut DMatrix<T>, transpose_self: bool);
28 fn multiply_with_dense_centered(&self, dense: &DMatrix<T>, result: &mut DMatrix<T>, transpose_self: bool, means: &DVector<T>);
29
30 fn multiply_transposed_by_dense(&self, q: &DMatrix<T>, result: &mut DMatrix<T>);
31 fn multiply_transposed_by_dense_centered(&self, q: &DMatrix<T>, result: &mut DMatrix<T>, means: &DVector<T>);
32}
33
34#[derive(Debug, Clone, PartialEq)]
43pub struct SvdRec<T: Float> {
44 pub d: usize,
45 pub u: Array2<T>,
46 pub s: Array1<T>,
47 pub vt: Array2<T>,
48 pub diagnostics: Diagnostics<T>,
49}
50
51#[derive(Debug, Clone, PartialEq)]
66pub struct Diagnostics<T: Float> {
67 pub non_zero: usize,
68 pub dimensions: usize,
69 pub iterations: usize,
70 pub transposed: bool,
71 pub lanczos_steps: usize,
72 pub ritz_values_stabilized: usize,
73 pub significant_values: usize,
74 pub singular_values: usize,
75 pub end_interval: [T; 2],
76 pub kappa: T,
77 pub random_seed: u32,
78}
79
80pub trait SvdFloat: FloatOpsTS {
81 fn eps() -> Self;
82 fn eps34() -> Self;
83 fn compare(a: Self, b: Self) -> bool;
84}
85
86impl SvdFloat for f32 {
87 fn eps() -> Self {
88 f32::EPSILON
89 }
90
91 fn eps34() -> Self {
92 f32::EPSILON.powf(0.75)
93 }
94
95 fn compare(a: Self, b: Self) -> bool {
96 (b - a).abs() < f32::EPSILON
97 }
98}
99
100impl SvdFloat for f64 {
101 fn eps() -> Self {
102 f64::EPSILON
103 }
104
105 fn eps34() -> Self {
106 f64::EPSILON.powf(0.75)
107 }
108
109 fn compare(a: Self, b: Self) -> bool {
110 (b - a).abs() < f64::EPSILON
111 }
112}