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); }
26
27#[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#[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}