sparse-ir 0.8.4

Rust implementation of SparseIR functionality
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Kernel matrix discretization for SparseIR
//!
//! This module provides functionality to discretize kernels using Gauss quadrature
//! rules and store them as matrices for numerical computation.

use crate::gauss::Rule;
use crate::interpolation2d::Interpolate2D;
use crate::kernel::{AbstractKernel, CentrosymmKernel, KernelProperties, SymmetryType};
use crate::numeric::CustomNumeric;
use mdarray::DTensor;
use std::fmt::Debug;

/// This structure stores a discrete kernel matrix along with the corresponding
/// Gauss quadrature rules for x and y coordinates. This enables easy application
/// of weights for SVE computation and maintains the relationship between matrix
/// elements and their corresponding quadrature points.
#[derive(Debug, Clone)]
pub struct DiscretizedKernel<T> {
    /// Discrete kernel matrix
    pub matrix: DTensor<T, 2>,
    /// Gauss quadrature rule for x coordinates
    pub gauss_x: Rule<T>,
    /// Gauss quadrature rule for y coordinates
    pub gauss_y: Rule<T>,
    /// X-axis segment boundaries (from SVEHints)
    pub segments_x: Vec<T>,
    /// Y-axis segment boundaries (from SVEHints)
    pub segments_y: Vec<T>,
}

impl<T: CustomNumeric + Clone> DiscretizedKernel<T> {
    /// Create a new DiscretizedKernel
    pub fn new(
        matrix: DTensor<T, 2>,
        gauss_x: Rule<T>,
        gauss_y: Rule<T>,
        segments_x: Vec<T>,
        segments_y: Vec<T>,
    ) -> Self {
        Self {
            matrix,
            gauss_x,
            gauss_y,
            segments_x,
            segments_y,
        }
    }

    /// Create a new DiscretizedKernel without segments (legacy)
    pub fn new_legacy(matrix: DTensor<T, 2>, gauss_x: Rule<T>, gauss_y: Rule<T>) -> Self {
        Self {
            matrix,
            gauss_x: gauss_x.clone(),
            gauss_y: gauss_y.clone(),
            segments_x: vec![gauss_x.a, gauss_x.b],
            segments_y: vec![gauss_y.a, gauss_y.b],
        }
    }

    /// Delegate to matrix methods
    pub fn is_empty(&self) -> bool {
        self.matrix.is_empty()
    }

    pub fn nrows(&self) -> usize {
        self.matrix.shape().0
    }

    pub fn ncols(&self) -> usize {
        self.matrix.shape().1
    }

    pub fn iter(&self) -> impl Iterator<Item = &T> {
        self.matrix.iter()
    }

    /// Apply weights for SVE computation
    ///
    /// This applies the square root of Gauss weights to the matrix,
    /// which is required before performing SVD for SVE computation.
    /// The original matrix remains unchanged.
    pub fn apply_weights_for_sve(&self) -> DTensor<T, 2> {
        let mut weighted_matrix = self.matrix.clone();
        let shape = *weighted_matrix.shape();

        // Apply square root of x-direction weights to rows
        for i in 0..self.gauss_x.x.len() {
            let weight_sqrt = self.gauss_x.w[i].sqrt();
            for j in 0..shape.1 {
                weighted_matrix[[i, j]] = weighted_matrix[[i, j]] * weight_sqrt;
            }
        }

        // Apply square root of y-direction weights to columns
        for j in 0..self.gauss_y.x.len() {
            let weight_sqrt = self.gauss_y.w[j].sqrt();
            for i in 0..shape.0 {
                weighted_matrix[[i, j]] = weighted_matrix[[i, j]] * weight_sqrt;
            }
        }

        weighted_matrix
    }

    /// Remove weights from matrix (inverse of apply_weights_for_sve)
    pub fn remove_weights_from_sve(&mut self) {
        let shape = *self.matrix.shape();

        // Remove weights from U matrix (x-direction)
        for i in 0..self.gauss_x.x.len() {
            let weight_sqrt = self.gauss_x.w[i].sqrt();
            for j in 0..shape.1 {
                self.matrix[[i, j]] = self.matrix[[i, j]] / weight_sqrt;
            }
        }

        // Remove weights from V matrix (y-direction)
        for j in 0..self.gauss_y.x.len() {
            let weight_sqrt = self.gauss_y.w[j].sqrt();
            for i in 0..shape.0 {
                self.matrix[[i, j]] = self.matrix[[i, j]] / weight_sqrt;
            }
        }
    }

    /// Get the number of Gauss points in x direction
    pub fn n_gauss_x(&self) -> usize {
        self.gauss_x.x.len()
    }

    /// Get the number of Gauss points in y direction
    pub fn n_gauss_y(&self) -> usize {
        self.gauss_y.x.len()
    }
}

/// Compute matrix from Gauss quadrature rules with segments from SVEHints
///
/// This function evaluates the kernel at all combinations of Gauss points
/// and returns a DiscretizedKernel containing the matrix, quadrature rules, and segments.
pub fn matrix_from_gauss_with_segments<
    T: CustomNumeric + Clone + Send + Sync,
    K: CentrosymmKernel + KernelProperties,
    H: crate::kernel::SVEHints<T>,
>(
    kernel: &K,
    gauss_x: &Rule<T>,
    gauss_y: &Rule<T>,
    symmetry: SymmetryType,
    hints: &H,
) -> DiscretizedKernel<T> {
    let segments_x = hints.segments_x();
    let segments_y = hints.segments_y();

    // TODO: Fix range checking for composite Gauss rules
    // For now, skip range checking to allow testing
    /*
    // Check that Gauss points are within [0, xmax] and [0, ymax]
    let kernel_xmax = kernel.xmax();
    let kernel_ymax = kernel.ymax();
    let tolerance = 1e-12;

    // Check x points are in [0, xmax]
    for &x in &gauss_x.x {
        let x_f64 = x.to_f64();
        assert!(
            x_f64 >= -tolerance && x_f64 <= kernel_xmax + tolerance,
            "Gauss x point {} is outside [0, {}]", x_f64, kernel_xmax
        );
    }

    // Check y points are in [0, ymax]
    for &y in &gauss_y.x {
        let y_f64 = y.to_f64();
        assert!(
            y_f64 >= -tolerance && y_f64 <= kernel_ymax + tolerance,
            "Gauss y point {} is outside [0, {}]", y_f64, kernel_ymax
        );
    }
    */

    let n = gauss_x.x.len();
    let m = gauss_y.x.len();
    let mut result = DTensor::<T, 2>::from_elem([n, m], T::zero());

    // Evaluate kernel at all combinations of Gauss points
    for i in 0..n {
        for j in 0..m {
            let x = gauss_x.x[i];
            let y = gauss_y.x[j];
            result[[i, j]] = kernel.compute_reduced(x, y, symmetry);
        }
    }

    DiscretizedKernel::new(
        result,
        gauss_x.clone(),
        gauss_y.clone(),
        segments_x,
        segments_y,
    )
}

/// Compute matrix from Gauss quadrature rules (legacy version without segments)
///
/// This function evaluates the kernel at all combinations of Gauss points
/// and returns a DiscretizedKernel containing the matrix and quadrature rules.
pub fn matrix_from_gauss<T: CustomNumeric + Clone, K: CentrosymmKernel + KernelProperties>(
    kernel: &K,
    gauss_x: &Rule<T>,
    gauss_y: &Rule<T>,
    symmetry: SymmetryType,
) -> DiscretizedKernel<T> {
    // Check that Gauss points are within [0, xmax] and [0, ymax]
    let kernel_xmax = kernel.xmax();
    let kernel_ymax = kernel.ymax();
    let tolerance = 1e-12;

    // Check x points are in [0, xmax]
    for &x in &gauss_x.x {
        let x_f64 = x.to_f64();
        assert!(
            x_f64 >= -tolerance && x_f64 <= kernel_xmax + tolerance,
            "Gauss x point {} is outside [0, {}]",
            x_f64,
            kernel_xmax
        );
    }

    // Check y points are in [0, ymax]
    for &y in &gauss_y.x {
        let y_f64 = y.to_f64();
        assert!(
            y_f64 >= -tolerance && y_f64 <= kernel_ymax + tolerance,
            "Gauss y point {} is outside [0, {}]",
            y_f64,
            kernel_ymax
        );
    }

    let n = gauss_x.x.len();
    let m = gauss_y.x.len();
    let mut result = DTensor::<T, 2>::from_elem([n, m], T::zero());

    // Evaluate kernel at all combinations of Gauss points
    for i in 0..n {
        for j in 0..m {
            let x = gauss_x.x[i];
            let y = gauss_y.x[j];

            // Use T type directly for kernel computation
            // Note: gauss_x and gauss_y should already be scaled to [0, 1] interval
            result[[i, j]] = kernel.compute_reduced(x, y, symmetry);
        }
    }

    DiscretizedKernel::new_legacy(result, gauss_x.clone(), gauss_y.clone())
}

/// Compute matrix from Gauss quadrature rules for non-centrosymmetric kernels
///
/// This function evaluates the kernel directly at all combinations of Gauss points
/// without exploiting symmetry. It works with the full domain [-xmax, xmax] × [-ymax, ymax].
///
/// # Arguments
///
/// * `kernel` - The kernel implementing AbstractKernel
/// * `gauss_x` - Gauss quadrature rule for x coordinates (full domain)
/// * `gauss_y` - Gauss quadrature rule for y coordinates (full domain)
/// * `hints` - SVE hints providing segment information
///
/// # Returns
///
/// DiscretizedKernel containing the matrix, quadrature rules, and segments
pub fn matrix_from_gauss_noncentrosymmetric<
    T: CustomNumeric + Clone + Send + Sync,
    K: AbstractKernel + KernelProperties,
    H: crate::kernel::SVEHints<T>,
>(
    kernel: &K,
    gauss_x: &Rule<T>,
    gauss_y: &Rule<T>,
    hints: &H,
) -> DiscretizedKernel<T> {
    let segments_x = hints.segments_x();
    let segments_y = hints.segments_y();

    let n = gauss_x.x.len();
    let m = gauss_y.x.len();
    let mut result = DTensor::<T, 2>::from_elem([n, m], T::zero());

    // Evaluate kernel directly at all combinations of Gauss points
    for i in 0..n {
        for j in 0..m {
            let x = gauss_x.x[i];
            let y = gauss_y.x[j];

            // Direct kernel evaluation (no symmetry exploitation)
            result[[i, j]] = kernel.compute(x, y);
        }
    }

    DiscretizedKernel::new(
        result,
        gauss_x.clone(),
        gauss_y.clone(),
        segments_x,
        segments_y,
    )
}

/// 2D interpolation kernel for efficient evaluation at arbitrary points
///
/// This structure manages a grid of Interpolate2D objects for piecewise
/// polynomial interpolation across the entire kernel domain.
#[derive(Debug, Clone)]
pub struct InterpolatedKernel<T> {
    /// X-axis segment boundaries (from SVEHints)
    pub segments_x: Vec<T>,
    /// Y-axis segment boundaries (from SVEHints)
    pub segments_y: Vec<T>,
    /// Domain boundaries
    pub domain_x: (T, T),
    pub domain_y: (T, T),

    /// Interpolators for each cell ((segments_x.len()-1) × (segments_y.len()-1))
    pub interpolators: DTensor<Interpolate2D<T>, 2>,

    /// Number of cells (for efficiency)
    pub n_cells_x: usize,
    pub n_cells_y: usize,
}

impl<T: CustomNumeric + Debug + Clone + 'static> InterpolatedKernel<T> {
    /// Create InterpolatedKernel from kernel and segments
    ///
    /// This function creates a grid of Interpolate2D objects, one for each
    /// cell defined by the segments. Each cell uses independent Gauss rules
    /// and kernel evaluation for optimal interpolation.
    ///
    /// # Arguments
    /// * `kernel` - Kernel to interpolate
    /// * `segments_x` - X-axis segment boundaries
    /// * `segments_y` - Y-axis segment boundaries
    /// * `gauss_per_cell` - Number of Gauss points per cell (e.g., 4 for degree 3)
    /// * `symmetry` - Symmetry type for kernel evaluation
    ///
    /// # Returns
    /// New InterpolatedKernel instance
    pub fn from_kernel_and_segments<K: CentrosymmKernel + KernelProperties>(
        kernel: &K,
        segments_x: Vec<T>,
        segments_y: Vec<T>,
        gauss_per_cell: usize,
        symmetry: SymmetryType,
    ) -> Self {
        let n_cells_x = segments_x.len() - 1;
        let n_cells_y = segments_y.len() - 1;

        // Create interpolators for each cell
        let mut interpolators = Vec::new();

        // Create interpolator for each cell independently
        for i in 0..n_cells_x {
            for j in 0..n_cells_y {
                // Create Gauss rules for this cell
                let cell_gauss_x = crate::gauss::legendre_generic::<T>(gauss_per_cell)
                    .reseat(segments_x[i], segments_x[i + 1]);
                let cell_gauss_y = crate::gauss::legendre_generic::<T>(gauss_per_cell)
                    .reseat(segments_y[j], segments_y[j + 1]);

                // Evaluate kernel at Gauss points in this cell
                let mut cell_values =
                    DTensor::<T, 2>::from_elem([gauss_per_cell, gauss_per_cell], T::zero());
                for k in 0..gauss_per_cell {
                    for l in 0..gauss_per_cell {
                        let x = cell_gauss_x.x[k];
                        let y = cell_gauss_y.x[l];
                        let kernel_val = kernel.compute_reduced(x, y, symmetry);
                        cell_values[[k, l]] = kernel_val;
                    }
                }

                // Create Interpolate2D for this cell
                interpolators.push(Interpolate2D::new(
                    &cell_values,
                    &cell_gauss_x,
                    &cell_gauss_y,
                ));
            }
        }

        // Convert Vec to DTensor
        let interpolators_array =
            DTensor::<Interpolate2D<T>, 2>::from_fn([n_cells_x, n_cells_y], |idx| {
                interpolators[idx[0] * n_cells_y + idx[1]].clone()
            });

        Self {
            segments_x: segments_x.clone(),
            segments_y: segments_y.clone(),
            domain_x: (segments_x[0], segments_x[segments_x.len() - 1]),
            domain_y: (segments_y[0], segments_y[segments_y.len() - 1]),
            interpolators: interpolators_array,
            n_cells_x,
            n_cells_y,
        }
    }

    /// Find the cell containing point (x, y) using binary search
    ///
    /// # Arguments
    /// * `x` - x-coordinate
    /// * `y` - y-coordinate
    ///
    /// # Returns
    /// Some((i, j)) if point is in domain, None otherwise
    pub fn find_cell(&self, x: T, y: T) -> Option<(usize, usize)> {
        let i = self.binary_search_segments(&self.segments_x, x)?;
        let j = self.binary_search_segments(&self.segments_y, y)?;
        Some((i, j))
    }

    /// Binary search for segment containing a value
    fn binary_search_segments(&self, segments: &[T], value: T) -> Option<usize> {
        if value < segments[0] || value > segments[segments.len() - 1] {
            return None;
        }

        let mut left = 0;
        let mut right = segments.len() - 1;

        while left < right {
            let mid = (left + right) / 2;
            if segments[mid] <= value && value < segments[mid + 1] {
                return Some(mid);
            } else if value < segments[mid] {
                right = mid;
            } else {
                left = mid + 1;
            }
        }

        // Handle edge case where value equals the last segment
        if value == segments[segments.len() - 1] {
            Some(segments.len() - 2)
        } else {
            None
        }
    }

    /// Evaluate interpolated kernel at point (x, y)
    ///
    /// # Arguments
    /// * `x` - x-coordinate
    /// * `y` - y-coordinate
    ///
    /// # Returns
    /// Interpolated kernel value at (x, y)
    ///
    /// # Panics
    /// Panics if (x, y) is outside the interpolation domain
    pub fn evaluate(&self, x: T, y: T) -> T {
        let (i, j) = self
            .find_cell(x, y)
            .expect("Point is outside interpolation domain");

        self.interpolators[[i, j]].evaluate(x, y)
    }

    /// Get domain boundaries
    pub fn domain(&self) -> ((T, T), (T, T)) {
        (self.domain_x, self.domain_y)
    }

    /// Get number of cells in x direction
    pub fn n_cells_x(&self) -> usize {
        self.n_cells_x
    }

    /// Get number of cells in y direction
    pub fn n_cells_y(&self) -> usize {
        self.n_cells_y
    }
}

#[cfg(test)]
#[path = "kernelmatrix_tests.rs"]
mod tests;