sparse-ir-basis 0.13.0

The IR basis of sparse-ir: kernels, the singular value expansion and FiniteTempBasis
Documentation
//! 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::kernel::{AbstractKernel, CentrosymmKernel, KernelProperties, SymmetryType};
use crate::matrix::Mat;
use crate::numeric::CustomNumeric;
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: Mat<T>,
    /// 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: Mat<T>,
        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: Mat<T>, 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) -> Mat<T> {
        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 = Mat::<T>::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 = Mat::<T>::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 = Mat::<T>::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,
    )
}

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