use crate::advanced::enhanced_kriging::{AnisotropicCovariance, TrendFunction};
use crate::advanced::kriging::CovarianceFunction;
use crate::error::{InterpolateError, InterpolateResult};
use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2};
use scirs2_core::numeric::{Float, FromPrimitive};
use std::fmt::{Debug, Display};
use std::marker::PhantomData;
use std::ops::{Add, Div, Mul, Sub};
type SparseComponents<F> = (Vec<(usize, usize)>, Vec<F>);
const DEFAULT_MAX_NEIGHBORS: usize = 50;
const DEFAULT_RADIUS_MULTIPLIER: f64 = 3.0;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FastKrigingMethod {
Local,
FixedRank(usize),
Tapering(f64),
HODLR(usize), }
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct FastKriging<F>
where
F: Float
+ FromPrimitive
+ Debug
+ Display
+ Div<Output = F>
+ Mul<Output = F>
+ Sub<Output = F>
+ Add<Output = F>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::RemAssign,
{
points: Array2<F>,
values: Array1<F>,
anisotropic_cov: AnisotropicCovariance<F>,
trend_fn: TrendFunction,
approx_method: FastKrigingMethod,
max_neighbors: usize,
radius_multiplier: F,
low_rank_components: Option<(Array2<F>, Array1<F>, Array2<F>)>,
sparse_components: Option<SparseComponents<F>>,
weights: Array1<F>,
basis_functions: Option<Array2<F>>,
trend_coeffs: Option<Array1<F>>,
optimize_parameters: bool,
compute_exact_variance: bool,
_phantom: PhantomData<F>,
}
#[derive(Debug, Clone)]
pub struct FastKrigingBuilder<F>
where
F: Float
+ FromPrimitive
+ Debug
+ Display
+ Div<Output = F>
+ Mul<Output = F>
+ Sub<Output = F>
+ Add<Output = F>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::RemAssign,
{
points: Option<Array2<F>>,
values: Option<Array1<F>>,
cov_fn: CovarianceFunction,
length_scales: Option<Array1<F>>,
sigma_sq: F,
nugget: F,
trend_fn: TrendFunction,
approx_method: FastKrigingMethod,
max_neighbors: usize,
radius_multiplier: F,
_phantom: PhantomData<F>,
}
#[derive(Debug, Clone)]
pub struct FastPredictionResult<F: Float> {
pub value: Array1<F>,
pub variance: Array1<F>,
pub method: FastKrigingMethod,
pub computation_time_ms: Option<f64>,
}
impl<F> Default for FastKrigingBuilder<F>
where
F: Float
+ FromPrimitive
+ Debug
+ Display
+ Add<Output = F>
+ Sub<Output = F>
+ Mul<Output = F>
+ Div<Output = F>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::RemAssign
+ 'static,
{
fn default() -> Self {
Self::new()
}
}
impl<F> FastKrigingBuilder<F>
where
F: Float
+ FromPrimitive
+ Debug
+ Display
+ Add<Output = F>
+ Sub<Output = F>
+ Mul<Output = F>
+ Div<Output = F>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::RemAssign
+ 'static,
{
pub fn new() -> Self {
Self {
points: None,
values: None,
cov_fn: CovarianceFunction::Matern52,
length_scales: None,
sigma_sq: F::from_f64(1.0).expect("Operation failed"),
nugget: F::from_f64(1e-6).expect("Operation failed"),
trend_fn: TrendFunction::Constant,
approx_method: FastKrigingMethod::Local,
max_neighbors: DEFAULT_MAX_NEIGHBORS,
radius_multiplier: F::from_f64(DEFAULT_RADIUS_MULTIPLIER).expect("Operation failed"),
_phantom: PhantomData,
}
}
pub fn points(mut self, points: Array2<F>) -> Self {
self.points = Some(points);
self
}
pub fn values(mut self, values: Array1<F>) -> Self {
self.values = Some(values);
self
}
pub fn covariance_function(mut self, covfn: CovarianceFunction) -> Self {
self.cov_fn = covfn;
self
}
pub fn length_scales(mut self, lengthscales: Array1<F>) -> Self {
self.length_scales = Some(lengthscales);
self
}
pub fn length_scale(mut self, lengthscale: F) -> Self {
self.sigma_sq = lengthscale;
self
}
pub fn sigma_sq(mut self, sigmasq: F) -> Self {
self.sigma_sq = sigmasq;
self
}
pub fn nugget(mut self, nugget: F) -> Self {
self.nugget = nugget;
self
}
pub fn trend_function(mut self, trendfn: TrendFunction) -> Self {
self.trend_fn = trendfn;
self
}
pub fn approximation_method(mut self, method: FastKrigingMethod) -> Self {
self.approx_method = method;
self
}
pub fn max_neighbors(mut self, maxneighbors: usize) -> Self {
self.max_neighbors = maxneighbors;
self
}
pub fn radius_multiplier(mut self, multiplier: F) -> Self {
self.radius_multiplier = multiplier;
self
}
pub fn build(self) -> InterpolateResult<FastKriging<F>> {
Err(InterpolateError::NotImplemented(
"FastKriging implementation is currently being refactored for improved performance and maintainability. Please use standard kriging instead for now.".to_string(),
))
}
}
impl<F> FastKriging<F>
where
F: Float
+ FromPrimitive
+ Debug
+ Display
+ Add<Output = F>
+ Sub<Output = F>
+ Mul<Output = F>
+ Div<Output = F>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::RemAssign
+ 'static,
{
pub fn builder() -> FastKrigingBuilder<F> {
FastKrigingBuilder::new()
}
pub fn predict(
&self,
_query_points: &ArrayView2<F>,
) -> InterpolateResult<FastPredictionResult<F>> {
Err(InterpolateError::NotImplemented(
"FastKriging implementation is currently being refactored for improved performance and maintainability. Please use standard kriging instead for now.".to_string(),
))
}
}
#[allow(dead_code)]
pub fn make_local_kriging<
F: Float
+ FromPrimitive
+ Debug
+ Display
+ Add<Output = F>
+ Sub<Output = F>
+ Mul<Output = F>
+ Div<Output = F>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::RemAssign
+ 'static,
>(
_points: &ArrayView2<F>,
_values: &ArrayView1<F>,
_cov_fn: CovarianceFunction,
_scale: F,
neighbors: usize,
) -> InterpolateResult<FastKriging<F>> {
Err(InterpolateError::NotImplemented(
"FastKriging implementation is currently being refactored for improved performance and maintainability. Please use standard kriging instead for now.".to_string(),
))
}
#[allow(dead_code)]
pub fn make_fixed_rank_kriging<
F: Float
+ FromPrimitive
+ Debug
+ Display
+ Add<Output = F>
+ Sub<Output = F>
+ Mul<Output = F>
+ Div<Output = F>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::RemAssign
+ 'static,
>(
_points: &ArrayView2<F>,
_values: &ArrayView1<F>,
_cov_fn: CovarianceFunction,
_scale: F,
rank: usize,
) -> InterpolateResult<FastKriging<F>> {
Err(InterpolateError::NotImplemented(
"FastKriging implementation is currently being refactored for improved performance and maintainability. Please use standard kriging instead for now.".to_string(),
))
}
#[allow(dead_code)]
pub fn make_tapered_kriging<
F: Float
+ FromPrimitive
+ Debug
+ Display
+ Add<Output = F>
+ Sub<Output = F>
+ Mul<Output = F>
+ Div<Output = F>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::RemAssign
+ 'static,
>(
_points: &ArrayView2<F>,
_values: &ArrayView1<F>,
_cov_fn: CovarianceFunction,
_scale: F,
range: F,
) -> InterpolateResult<FastKriging<F>> {
Err(InterpolateError::NotImplemented(
"FastKriging implementation is currently being refactored for improved performance and maintainability. Please use standard kriging instead for now.".to_string(),
))
}
#[allow(dead_code)]
pub fn make_hodlr_kriging<
F: Float
+ FromPrimitive
+ Debug
+ Display
+ Add<Output = F>
+ Sub<Output = F>
+ Mul<Output = F>
+ Div<Output = F>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::RemAssign
+ 'static,
>(
_points: &ArrayView2<F>,
_values: &ArrayView1<F>,
_cov_fn: CovarianceFunction,
_scale: F,
size: usize,
) -> InterpolateResult<FastKriging<F>> {
Err(InterpolateError::NotImplemented(
"FastKriging implementation is currently being refactored for improved performance and maintainability. Please use standard kriging instead for now.".to_string(),
))
}
#[allow(dead_code)]
pub fn select_approximation_method(_npoints: usize) -> FastKrigingMethod {
if _npoints < 500 {
FastKrigingMethod::Local
} else if _npoints < 5_000 {
FastKrigingMethod::FixedRank(50)
} else if _npoints < 50_000 {
FastKrigingMethod::Tapering(3.0)
} else {
FastKrigingMethod::HODLR(64)
}
}