use crate::error::GprError;
use crate::kernel::{
ConstantKernel, CustomKernel, KernelScalar, LinearKernel, MaternArdKernel, MaternKernel,
PeriodicKernel, RationalQuadraticArdKernel, RationalQuadraticKernel, RbfArdKernel, RbfKernel,
WhiteKernel,
};
use crate::param::Interval;
pub(crate) trait LeafParams {
fn leaf_num_params(&self) -> usize;
fn write_leaf_params(&self, out: &mut [f64], offset: &mut usize) -> Result<(), GprError>;
fn write_leaf_intervals(
&self,
out: &mut [Interval],
offset: &mut usize,
) -> Result<(), GprError>;
fn apply_leaf_params(&mut self, params: &[f64], offset: &mut usize) -> Result<(), GprError>;
}
macro_rules! apply_slice {
() => {
fn apply_leaf_params(
&mut self,
params: &[f64],
offset: &mut usize,
) -> Result<(), GprError> {
let n = self.num_params();
self.set_params(¶ms[*offset..*offset + n])?;
*offset += n;
Ok(())
}
};
}
macro_rules! one_param {
($leaf:ty, $log:ident) => {
impl LeafParams for $leaf {
fn leaf_num_params(&self) -> usize {
1
}
fn write_leaf_params(
&self,
out: &mut [f64],
offset: &mut usize,
) -> Result<(), GprError> {
out[*offset] = self.$log();
*offset += 1;
Ok(())
}
fn write_leaf_intervals(
&self,
out: &mut [Interval],
offset: &mut usize,
) -> Result<(), GprError> {
out[*offset] = self.bounds();
*offset += 1;
Ok(())
}
apply_slice!();
}
};
}
one_param!(RbfKernel, log_lengthscale);
one_param!(MaternKernel, log_lengthscale);
one_param!(ConstantKernel, log_constant);
one_param!(LinearKernel, log_variance);
one_param!(WhiteKernel, log_variance);
macro_rules! ard_only {
($leaf:ty) => {
impl LeafParams for $leaf {
fn leaf_num_params(&self) -> usize {
self.num_params()
}
fn write_leaf_params(
&self,
out: &mut [f64],
offset: &mut usize,
) -> Result<(), GprError> {
let n = self.num_params();
out[*offset..*offset + n].copy_from_slice(self.log_lengthscales());
*offset += n;
Ok(())
}
fn write_leaf_intervals(
&self,
out: &mut [Interval],
offset: &mut usize,
) -> Result<(), GprError> {
self.lengthscales().write_intervals(out, offset);
Ok(())
}
apply_slice!();
}
};
}
ard_only!(RbfArdKernel);
ard_only!(MaternArdKernel);
impl LeafParams for PeriodicKernel {
fn leaf_num_params(&self) -> usize {
2
}
fn write_leaf_params(&self, out: &mut [f64], offset: &mut usize) -> Result<(), GprError> {
out[*offset] = self.log_lengthscale();
out[*offset + 1] = self.log_period();
*offset += 2;
Ok(())
}
fn write_leaf_intervals(
&self,
out: &mut [Interval],
offset: &mut usize,
) -> Result<(), GprError> {
out[*offset] = self.lengthscale_bounds();
out[*offset + 1] = self.period_bounds();
*offset += 2;
Ok(())
}
apply_slice!();
}
impl LeafParams for RationalQuadraticKernel {
fn leaf_num_params(&self) -> usize {
2
}
fn write_leaf_params(&self, out: &mut [f64], offset: &mut usize) -> Result<(), GprError> {
out[*offset] = self.log_lengthscale();
out[*offset + 1] = self.log_alpha();
*offset += 2;
Ok(())
}
fn write_leaf_intervals(
&self,
out: &mut [Interval],
offset: &mut usize,
) -> Result<(), GprError> {
out[*offset] = self.lengthscale_bounds();
out[*offset + 1] = self.alpha_bounds();
*offset += 2;
Ok(())
}
apply_slice!();
}
impl LeafParams for RationalQuadraticArdKernel {
fn leaf_num_params(&self) -> usize {
self.num_params()
}
fn write_leaf_params(&self, out: &mut [f64], offset: &mut usize) -> Result<(), GprError> {
let n = self.lengthscales().num_params();
out[*offset..*offset + n].copy_from_slice(self.log_lengthscales());
out[*offset + n] = self.log_alpha();
*offset += n + 1;
Ok(())
}
fn write_leaf_intervals(
&self,
out: &mut [Interval],
offset: &mut usize,
) -> Result<(), GprError> {
self.lengthscales().write_intervals(out, offset);
out[*offset] = self.alpha_bounds();
*offset += 1;
Ok(())
}
apply_slice!();
}
impl<T: KernelScalar> LeafParams for CustomKernel<T> {
fn leaf_num_params(&self) -> usize {
self.num_params()
}
fn write_leaf_params(&self, out: &mut [f64], offset: &mut usize) -> Result<(), GprError> {
self.write_params(out, offset)
}
fn write_leaf_intervals(
&self,
out: &mut [Interval],
offset: &mut usize,
) -> Result<(), GprError> {
self.write_intervals(out, offset)
}
fn apply_leaf_params(&mut self, params: &[f64], offset: &mut usize) -> Result<(), GprError> {
self.apply_params(params, offset)
}
}