use crate::gauss::Rule;
use crate::kernel::{AbstractKernel, CentrosymmKernel, KernelProperties, SymmetryType};
use crate::matrix::Mat;
use crate::numeric::CustomNumeric;
use std::fmt::Debug;
#[derive(Debug, Clone)]
pub struct DiscretizedKernel<T> {
pub matrix: Mat<T>,
pub gauss_x: Rule<T>,
pub gauss_y: Rule<T>,
pub segments_x: Vec<T>,
pub segments_y: Vec<T>,
}
impl<T: CustomNumeric + Clone> DiscretizedKernel<T> {
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,
}
}
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],
}
}
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()
}
pub fn apply_weights_for_sve(&self) -> Mat<T> {
let mut weighted_matrix = self.matrix.clone();
let shape = *weighted_matrix.shape();
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;
}
}
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
}
pub fn remove_weights_from_sve(&mut self) {
let shape = *self.matrix.shape();
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;
}
}
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;
}
}
}
pub fn n_gauss_x(&self) -> usize {
self.gauss_x.x.len()
}
pub fn n_gauss_y(&self) -> usize {
self.gauss_y.x.len()
}
}
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();
let n = gauss_x.x.len();
let m = gauss_y.x.len();
let mut result = Mat::<T>::from_elem([n, m], T::zero());
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,
)
}
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> {
let kernel_xmax = kernel.xmax();
let kernel_ymax = kernel.ymax();
let tolerance = 1e-12;
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
);
}
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());
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_legacy(result, gauss_x.clone(), gauss_y.clone())
}
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());
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(x, y);
}
}
DiscretizedKernel::new(
result,
gauss_x.clone(),
gauss_y.clone(),
segments_x,
segments_y,
)
}
#[cfg(test)]
#[path = "kernelmatrix_tests.rs"]
mod tests;