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
//! SVE result container
use crate::error::{Error, require_nonzero_size, require_threshold};
use crate::matrix::Mat;
use crate::poly::PiecewiseLegendrePolyVector;
/// Result of Singular Value Expansion computation
#[derive(Debug, Clone)]
pub struct SVEResult {
/// Left singular functions (u)
pub(crate) u: PiecewiseLegendrePolyVector,
/// Singular values in non-increasing order
pub(crate) s: Vec<f64>,
/// Right singular functions (v)
pub(crate) v: PiecewiseLegendrePolyVector,
/// Accuracy parameter used for computation
pub(crate) epsilon: f64,
}
impl SVEResult {
/// Left singular functions, one per singular value
pub fn u(&self) -> &PiecewiseLegendrePolyVector {
&self.u
}
/// Singular values, in non-increasing order
pub fn s(&self) -> &[f64] {
&self.s
}
/// Right singular functions, one per singular value
pub fn v(&self) -> &PiecewiseLegendrePolyVector {
&self.v
}
/// Accuracy the expansion was computed to
pub fn epsilon(&self) -> f64 {
self.epsilon
}
/// Build an SVE from a kernel matrix discretized on the Gauss points of
/// `segments_x` x `segments_y`
///
/// `matrix[i][j]` is `sqrt(w_x[i]) * K(x[i], y[j]) * sqrt(w_y[j])` for the
/// piecewise Gauss-Legendre rules `gauss_x` and `gauss_y` of `n_gauss`
/// points per segment, so `matrix` has `n_gauss * n_segments` rows and
/// columns. The weights are divided out of the singular vectors before
/// they become piecewise Legendre polynomials.
///
/// # Errors
///
/// * The errors of [`compute_svd_dtensor`](crate::tsvd::compute_svd_dtensor):
/// [`Error::EmptyInput`], [`Error::NonFiniteInput`] and
/// [`Error::DecompositionFailed`]
/// * [`Error::InvalidParameter`] if `matrix` does not have one row
/// (column) per Gauss point of `segments_x` (`segments_y`), or if a
/// segment array has fewer than 2 entries
/// * The errors of [`Self::new`], in particular [`Error::EmptyInput`] for
/// a matrix of rank 0, which has no singular functions
pub fn from_discretized_matrix<T: crate::numeric::CustomNumeric + 'static>(
matrix: &Mat<T>,
gauss_x: &crate::gauss::Rule<T>,
gauss_y: &crate::gauss::Rule<T>,
segments_x: &[f64],
segments_y: &[f64],
n_gauss: usize,
epsilon: f64,
) -> Result<Self, Error> {
require_gauss_point_count(matrix.shape().0, segments_x, n_gauss, "rows")?;
require_gauss_point_count(matrix.shape().1, segments_y, n_gauss, "columns")?;
let (u, s, v) = crate::tsvd::compute_svd_dtensor(matrix)?;
// The matrix carries the weights: divide them out of the singular
// vectors before they become polynomials.
let u_unweighted = crate::sve::utils::remove_weights(&u, gauss_x.w.as_slice(), true);
let v_unweighted = crate::sve::utils::remove_weights(&v, gauss_y.w.as_slice(), true);
let u_f64 = Mat::<f64>::from_fn(u_unweighted.dims(), |idx| u_unweighted[idx].to_f64());
let v_f64 = Mat::<f64>::from_fn(v_unweighted.dims(), |idx| v_unweighted[idx].to_f64());
let gauss_rule_f64 = crate::gauss::legendre::<f64>(n_gauss);
let u_polys =
crate::sve::utils::svd_to_polynomials(&u_f64, segments_x, &gauss_rule_f64, n_gauss)?;
let v_polys =
crate::sve::utils::svd_to_polynomials(&v_f64, segments_y, &gauss_rule_f64, n_gauss)?;
let s_f64: Vec<f64> = s.iter().map(|sv| sv.to_f64()).collect();
// A matrix of rank 0 has no singular functions. The vectors are built
// through the field (`PiecewiseLegendrePolyVector::new` rejects an
// empty vector), so that `Self::new` reports the empty `s`.
Self::new(
PiecewiseLegendrePolyVector::from_polys_unchecked(u_polys),
s_f64,
PiecewiseLegendrePolyVector::from_polys_unchecked(v_polys),
epsilon,
)
}
/// [`Self::from_discretized_matrix`] for a centrosymmetric kernel, whose
/// even and odd parts are discretized on the half domains
/// `segments_x` x `segments_y` and extended to `[-xmax, xmax]` and
/// `[-ymax, ymax]`
///
/// A block of rank 0 (the odd part of a kernel that is even in y, say)
/// contributes no singular function; only both blocks being empty is an
/// error.
///
/// # Errors
///
/// * The errors of [`Self::from_discretized_matrix`] for each block,
/// except that an empty block is accepted
/// * The errors of the crate-internal `extend_to_full_domain` and
/// `merge_results`, in particular [`Error::EmptyInput`] if both blocks
/// are empty
#[allow(clippy::too_many_arguments)]
pub fn from_discretized_matrices_centrosymmetric(
even: &Mat<f64>,
odd: &Mat<f64>,
gauss_x: &crate::gauss::Rule<f64>,
gauss_y: &crate::gauss::Rule<f64>,
segments_x: &[f64],
segments_y: &[f64],
n_gauss: usize,
xmax: f64,
ymax: f64,
epsilon: f64,
) -> Result<Self, Error> {
use crate::kernel::SymmetryType;
use crate::sve::utils::{extend_to_full_domain, merge_results, svd_to_polynomials};
for matrix in [even, odd] {
require_gauss_point_count(matrix.shape().0, segments_x, n_gauss, "rows")?;
require_gauss_point_count(matrix.shape().1, segments_y, n_gauss, "columns")?;
}
let gauss_rule_f64 = crate::gauss::legendre::<f64>(n_gauss);
let block = |matrix: &Mat<f64>, symmetry: SymmetryType| {
let (u, s, v) = crate::tsvd::compute_svd_dtensor(matrix)?;
let u_unweighted = crate::sve::utils::remove_weights(&u, gauss_x.w.as_slice(), true);
let v_unweighted = crate::sve::utils::remove_weights(&v, gauss_y.w.as_slice(), true);
let u_polys = svd_to_polynomials(&u_unweighted, segments_x, &gauss_rule_f64, n_gauss)?;
let v_polys = svd_to_polynomials(&v_unweighted, segments_y, &gauss_rule_f64, n_gauss)?;
let u_full = extend_to_full_domain(u_polys, symmetry, xmax)?;
let v_full = extend_to_full_domain(v_polys, symmetry, ymax)?;
Ok::<_, Error>((
PiecewiseLegendrePolyVector::from_polys_unchecked(u_full),
s,
PiecewiseLegendrePolyVector::from_polys_unchecked(v_full),
))
};
let result_even = block(even, SymmetryType::Even)?;
let result_odd = block(odd, SymmetryType::Odd)?;
merge_results(result_even, result_odd, epsilon)
}
/// Create a new SVEResult
///
/// # Errors
///
/// * [`Error::EmptyInput`] if `s` is empty
/// * [`Error::InvalidParameter`] if `u` or `v` does not have one function
/// per singular value, a singular value is not positive, the singular
/// values are not non-increasing, or `epsilon` is not in [0, 1)
/// * [`Error::NonFiniteInput`] if a singular value is NaN or infinite
pub fn new(
u: PiecewiseLegendrePolyVector,
s: Vec<f64>,
v: PiecewiseLegendrePolyVector,
epsilon: f64,
) -> Result<Self, Error> {
let result = Self { u, s, v, epsilon };
result.check()?;
Ok(result)
}
/// Check the invariants that [`Self::new`] guarantees. The fields are
/// public, so [`Self::part`] checks them again.
fn check(&self) -> Result<(), Error> {
let n = self.s.len();
if n == 0 {
return Err(Error::EmptyInput { name: "s" });
}
for (name, funcs) in [("u", &self.u), ("v", &self.v)] {
let len = funcs.get_polys().len();
if len != n {
return Err(Error::InvalidParameter {
name,
value: format!("{len} functions"),
reason: format!("must have one function per singular value ({n})"),
});
}
}
for (i, &x) in self.s.iter().enumerate() {
if !x.is_finite() {
return Err(Error::NonFiniteInput {
name: "s",
index: vec![i],
value: x,
});
}
if x <= 0.0 {
return Err(Error::InvalidParameter {
name: "s",
value: format!("{x:?} at index {i}"),
reason: "singular values must be positive".to_string(),
});
}
if i > 0 && x > self.s[i - 1] {
return Err(Error::InvalidParameter {
name: "s",
value: format!("{x:?} at index {i}, after {:?}", self.s[i - 1]),
reason: "singular values must be non-increasing".to_string(),
});
}
}
require_threshold("epsilon", Some(self.epsilon))
}
/// Extract a subset of the SVE result based on epsilon and max_size
///
/// # Arguments
///
/// * `eps` - Relative threshold for singular values (default: self.epsilon)
/// * `max_size` - Maximum number of singular values to keep
///
/// # Returns
///
/// Tuple of (u_subset, s_subset, v_subset)
///
/// # Errors
///
/// * [`Error::InvalidParameter`] if `eps` is not in [0, 1) (0 keeps every
/// singular value) or `max_size` is `Some(0)`
/// * The errors of [`Self::new`] and [`PiecewiseLegendrePolyVector::new`]
/// if the (public) fields break their invariants
pub fn part(
&self,
eps: Option<f64>,
max_size: Option<usize>,
) -> Result<
(
PiecewiseLegendrePolyVector,
Vec<f64>,
PiecewiseLegendrePolyVector,
),
Error,
> {
self.check()?;
require_threshold("eps", eps)?;
require_nonzero_size("max_size", max_size)?;
let eps = eps.unwrap_or(self.epsilon);
let threshold = eps * self.s[0];
let mut cut = 0;
for &val in self.s.iter() {
if val >= threshold {
cut += 1;
} else {
break;
}
}
if let Some(max) = max_size {
cut = cut.min(max);
}
// Extract subsets
let u_part = PiecewiseLegendrePolyVector::new(self.u.get_polys()[..cut].to_vec())?;
let s_part = self.s[..cut].to_vec();
let v_part = PiecewiseLegendrePolyVector::new(self.v.get_polys()[..cut].to_vec())?;
Ok((u_part, s_part, v_part))
}
}
/// The rows (columns) of a discretized kernel matrix are the Gauss points of
/// the segments: `n_gauss` per segment.
///
/// # Errors
/// * [`Error::InvalidParameter`] if `segments` has fewer than 2 entries or
/// `len` is not `n_gauss * (segments.len() - 1)`
fn require_gauss_point_count(
len: usize,
segments: &[f64],
n_gauss: usize,
what: &'static str,
) -> Result<(), Error> {
if segments.len() < 2 {
return Err(Error::InvalidParameter {
name: "segments",
value: format!("{} boundaries", segments.len()),
reason: "must have at least 2 entries".to_string(),
});
}
let expected = n_gauss * (segments.len() - 1);
if len != expected {
return Err(Error::InvalidParameter {
name: "matrix",
value: format!("{len} {what}"),
reason: format!("must have one per Gauss point of the segments ({expected})"),
});
}
Ok(())
}