use std::borrow::Cow;
use std::fmt;
use std::marker::PhantomData;
use std::ops::{Add, Mul};
use std::sync::atomic::{AtomicU64, Ordering};
use crate::error::GprError;
use crate::kernel::leaf_params::LeafParams;
use crate::kernel::{
ConstantKernel, CustomKernel, KernelSpec, KernelTerm, MaternArdKernel, MaternKernel,
ParameterBinding, PeriodicKernel, RationalQuadraticArdKernel, RationalQuadraticKernel,
RbfArdKernel, RbfKernel, WhiteKernel,
};
use crate::kernel::{NoSupply, Supply};
use crate::param::Interval;
static NEXT_SLOT: AtomicU64 = AtomicU64::new(1);
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SlotId(u64);
impl SlotId {
pub(crate) fn fresh() -> Self {
Self(NEXT_SLOT.fetch_add(1, Ordering::Relaxed))
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SlotShape {
Scalar,
Ard(usize),
}
impl SlotShape {
pub(crate) fn blocks(self) -> usize {
match self {
Self::Scalar => 1,
Self::Ard(d) => d,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct SuppliedSpec {
pub(crate) slot: SlotId,
pub(crate) at: usize,
pub(crate) leaf: SuppliedLeafSpec,
}
impl SuppliedSpec {
pub(crate) fn shape(&self) -> SlotShape {
match &self.leaf {
SuppliedLeafSpec::Scalar(_) => SlotShape::Scalar,
SuppliedLeafSpec::Ard(leaf) => SlotShape::Ard(leaf.dims()),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum SuppliedLeafSpec {
Scalar(ScalarLeafSpec),
Ard(ArdLeafSpec),
}
macro_rules! with_distance_leaves {
($on:ident) => {
$on! {
scalar: Rbf = Rbf, Matern = Matern, Periodic = Periodic,
RationalQuadratic = RationalQuadratic, Custom = Custom;
ard: Rbf = RbfArd, Matern = MaternArd, RationalQuadratic = RationalQuadraticArd;
}
};
}
macro_rules! leaf_maps {
(
scalar: $($s:ident = $sk:ident),+;
ard: $($a:ident = $ak:ident),+;
) => {
impl SuppliedLeafSpec {
pub(crate) fn into_coordinate(self) -> KernelSpec {
match self {
$(Self::Scalar(ScalarLeafSpec::$s(k)) => KernelSpec::$sk(k),)+
$(Self::Ard(ArdLeafSpec::$a(k)) => KernelSpec::$ak(k),)+
}
}
pub(crate) fn from_coordinate(shape: SlotShape, spec: KernelSpec) -> Option<Self> {
match (shape, spec) {
$((SlotShape::Scalar, KernelSpec::$sk(k)) => {
Some(Self::Scalar(ScalarLeafSpec::$s(k)))
})+
$((SlotShape::Ard(_), KernelSpec::$ak(k)) => {
Some(Self::Ard(ArdLeafSpec::$a(k)))
})+
_ => None,
}
}
}
};
}
with_distance_leaves!(leaf_maps);
#[derive(Clone, Debug, PartialEq)]
pub enum ScalarLeafSpec {
Rbf(RbfKernel),
Matern(MaternKernel),
Periodic(PeriodicKernel),
RationalQuadratic(RationalQuadraticKernel),
Custom(CustomKernel),
}
#[derive(Clone, Debug, PartialEq)]
pub enum ArdLeafSpec {
Rbf(RbfArdKernel),
Matern(MaternArdKernel),
RationalQuadratic(RationalQuadraticArdKernel),
}
impl ArdLeafSpec {
pub(crate) fn dims(&self) -> usize {
match self {
Self::Rbf(leaf) => leaf.lengthscales().num_params(),
Self::Matern(leaf) => leaf.lengthscales().num_params(),
Self::RationalQuadratic(leaf) => leaf.lengthscales().num_params(),
}
}
}
macro_rules! each_leaf {
($value:expr, $leaf:ident => $body:expr) => {
match $value {
SuppliedLeafSpec::Scalar(ScalarLeafSpec::Rbf($leaf)) => $body,
SuppliedLeafSpec::Scalar(ScalarLeafSpec::Matern($leaf)) => $body,
SuppliedLeafSpec::Scalar(ScalarLeafSpec::Periodic($leaf)) => $body,
SuppliedLeafSpec::Scalar(ScalarLeafSpec::RationalQuadratic($leaf)) => $body,
SuppliedLeafSpec::Scalar(ScalarLeafSpec::Custom($leaf)) => $body,
SuppliedLeafSpec::Ard(ArdLeafSpec::Rbf($leaf)) => $body,
SuppliedLeafSpec::Ard(ArdLeafSpec::Matern($leaf)) => $body,
SuppliedLeafSpec::Ard(ArdLeafSpec::RationalQuadratic($leaf)) => $body,
}
};
}
impl LeafParams for SuppliedLeafSpec {
fn leaf_num_params(&self) -> usize {
each_leaf!(self, leaf => leaf.leaf_num_params())
}
fn write_leaf_params(&self, out: &mut [f64], offset: &mut usize) -> Result<(), GprError> {
each_leaf!(self, leaf => leaf.write_leaf_params(out, offset))
}
fn write_leaf_intervals(
&self,
out: &mut [Interval],
offset: &mut usize,
) -> Result<(), GprError> {
each_leaf!(self, leaf => leaf.write_leaf_intervals(out, offset))
}
fn apply_leaf_params(&mut self, params: &[f64], offset: &mut usize) -> Result<(), GprError> {
each_leaf!(self, leaf => leaf.apply_leaf_params(params, offset))
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ScalarDistance {
slot: SlotId,
}
impl ScalarDistance {
#[allow(
clippy::new_without_default,
reason = "every call makes a new slot, which a `Default` would hide"
)]
pub fn new() -> Self {
Self {
slot: SlotId::fresh(),
}
}
pub(crate) fn with_slot(slot: SlotId) -> Self {
Self { slot }
}
pub fn kernel(&self, leaf: impl ScalarDistanceLeaf) -> DistanceKernel {
DistanceKernel::leaf(SuppliedSpec {
slot: self.slot,
at: 0,
leaf: SuppliedLeafSpec::Scalar(leaf.into_leaf()),
})
}
pub fn from_vec(&self, d2: Vec<f64>) -> DistanceSource<'static> {
self.source(ScalarData::Values(Cow::Owned(d2)))
}
pub fn from_slice(&self, d2: &[f64]) -> DistanceSource<'static> {
self.source(ScalarData::Values(Cow::Owned(d2.to_vec())))
}
pub fn borrow<'a>(&self, d2: &'a [f64]) -> DistanceSource<'a> {
self.source(ScalarData::Values(Cow::Borrowed(d2)))
}
pub fn fill<'a>(&self, filler: &'a dyn DistanceFill) -> DistanceSource<'a> {
self.source(ScalarData::Fill(filler))
}
fn source<'a>(&self, data: ScalarData<'a>) -> DistanceSource<'a> {
DistanceSource {
slot: self.slot,
data: SourceData::Scalar(data),
tidy: Tidy::Exact,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ArdDistance {
slot: SlotId,
dims: usize,
}
impl ArdDistance {
pub fn from_leaf(leaf: impl ArdDistanceLeaf) -> (Self, DistanceKernel) {
let bands = Self {
slot: SlotId::fresh(),
dims: leaf.lengthscale_count(),
};
let kernel = bands.leaf(leaf);
(bands, kernel)
}
#[cfg(test)]
pub(crate) fn of_dims(dims: usize) -> Self {
Self {
slot: SlotId::fresh(),
dims,
}
}
pub(crate) fn with_slot(slot: SlotId, dims: usize) -> Self {
Self { slot, dims }
}
pub fn dims(&self) -> usize {
self.dims
}
pub fn kernel(&self, leaf: impl ArdDistanceLeaf) -> Result<DistanceKernel, GprError> {
let lengthscales = leaf.lengthscale_count();
if lengthscales != self.dims {
return Err(GprError::DimensionMismatch {
x_dim: self.dims,
expected_dim: lengthscales,
});
}
Ok(self.leaf(leaf))
}
fn leaf(&self, leaf: impl ArdDistanceLeaf) -> DistanceKernel {
DistanceKernel::leaf(SuppliedSpec {
slot: self.slot,
at: 0,
leaf: SuppliedLeafSpec::Ard(leaf.into_leaf()),
})
}
pub fn from_vecs(&self, d2: Vec<Vec<f64>>) -> DistanceSource<'static> {
self.source(ArdData::Blocks(d2))
}
pub fn from_slices(&self, d2: &[&[f64]]) -> DistanceSource<'static> {
self.source(ArdData::Blocks(
d2.iter().map(|block| block.to_vec()).collect(),
))
}
pub fn borrow<'a>(&self, d2: &'a [&'a [f64]]) -> DistanceSource<'a> {
self.source(ArdData::Slices(d2))
}
pub fn fill<'a>(&self, filler: &'a dyn DistanceFill) -> DistanceSource<'a> {
self.source(ArdData::Fill(filler))
}
fn source<'a>(&self, data: ArdData<'a>) -> DistanceSource<'a> {
DistanceSource {
slot: self.slot,
data: SourceData::Ard(self.dims, data),
tidy: Tidy::Exact,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum DistanceSlot {
Scalar(ScalarDistance),
Ard(ArdDistance),
}
impl DistanceSlot {
pub(crate) fn id(self) -> SlotId {
match self {
Self::Scalar(slot) => slot.slot,
Self::Ard(slot) => slot.slot,
}
}
pub(crate) fn shape(self) -> SlotShape {
match self {
Self::Scalar(_) => SlotShape::Scalar,
Self::Ard(slot) => SlotShape::Ard(slot.dims),
}
}
pub(crate) fn from_parts(slot: SlotId, shape: SlotShape) -> Self {
match shape {
SlotShape::Scalar => Self::Scalar(ScalarDistance::with_slot(slot)),
SlotShape::Ard(dims) => Self::Ard(ArdDistance::with_slot(slot, dims)),
}
}
}
mod sealed {
use crate::kernel::KernelSpec;
pub trait Leaf {
fn into_leaf(self) -> super::ScalarLeafSpec;
}
pub trait ArdLeaf {
fn into_leaf(self) -> super::ArdLeafSpec;
fn lengthscale_count(&self) -> usize;
}
pub trait Points {
const POINTS: bool;
}
pub trait Model: Sized {
const POINTS: bool;
type Supply: crate::kernel::Supply;
fn into_spec(self) -> KernelSpec<Self::Supply>;
fn from_spec(spec: KernelSpec<Self::Supply>) -> Self;
}
}
pub trait ScalarDistanceLeaf: sealed::Leaf {}
pub trait ArdDistanceLeaf: sealed::ArdLeaf {}
macro_rules! scalar_leaf {
($($leaf:ty => $variant:ident),*) => {$(
impl sealed::Leaf for $leaf {
fn into_leaf(self) -> ScalarLeafSpec {
ScalarLeafSpec::$variant(self)
}
}
impl ScalarDistanceLeaf for $leaf {}
)*};
}
scalar_leaf!(
RbfKernel => Rbf,
MaternKernel => Matern,
PeriodicKernel => Periodic,
RationalQuadraticKernel => RationalQuadratic,
CustomKernel => Custom
);
impl<K> sealed::Leaf for K
where
K: KernelTerm<f64> + KernelTerm<f32> + Clone + fmt::Debug + Send + Sync + 'static,
{
fn into_leaf(self) -> ScalarLeafSpec {
ScalarLeafSpec::Custom(CustomKernel::new(self))
}
}
impl<K> ScalarDistanceLeaf for K where
K: KernelTerm<f64> + KernelTerm<f32> + Clone + fmt::Debug + Send + Sync + 'static
{
}
macro_rules! ard_leaf {
($($leaf:ty => $variant:ident),*) => {$(
impl sealed::ArdLeaf for $leaf {
fn into_leaf(self) -> ArdLeafSpec {
ArdLeafSpec::$variant(self)
}
fn lengthscale_count(&self) -> usize {
self.lengthscales().num_params()
}
}
impl ArdDistanceLeaf for $leaf {}
)*};
}
ard_leaf!(
RbfArdKernel => Rbf,
MaternArdKernel => Matern,
RationalQuadraticArdKernel => RationalQuadratic
);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DistanceOnly;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct WithPoints;
pub trait PointUse: sealed::Points + Send + Sync + 'static {}
impl sealed::Points for DistanceOnly {
const POINTS: bool = false;
}
impl sealed::Points for WithPoints {
const POINTS: bool = true;
}
impl PointUse for DistanceOnly {}
impl PointUse for WithPoints {}
pub trait JoinPoints<Rhs: PointUse>: PointUse {
type Output: PointUse;
}
impl JoinPoints<DistanceOnly> for DistanceOnly {
type Output = DistanceOnly;
}
impl JoinPoints<WithPoints> for DistanceOnly {
type Output = WithPoints;
}
impl JoinPoints<DistanceOnly> for WithPoints {
type Output = WithPoints;
}
impl JoinPoints<WithPoints> for WithPoints {
type Output = WithPoints;
}
pub struct DistanceKernel<C: PointUse = DistanceOnly> {
spec: KernelSpec<SuppliedSpec>,
_points: PhantomData<C>,
}
impl<C: PointUse> Clone for DistanceKernel<C> {
fn clone(&self) -> Self {
Self {
spec: self.spec.clone(),
_points: PhantomData,
}
}
}
impl<C: PointUse> PartialEq for DistanceKernel<C> {
fn eq(&self, other: &Self) -> bool {
self.spec == other.spec
}
}
impl<C: PointUse> fmt::Debug for DistanceKernel<C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DistanceKernel")
.field("spec", &self.spec)
.finish()
}
}
impl DistanceKernel {
fn leaf(spec: SuppliedSpec) -> Self {
Self::from_spec(KernelSpec::Supplied(spec))
}
}
impl<C: PointUse> DistanceKernel<C> {
pub(crate) fn from_spec(mut spec: KernelSpec<SuppliedSpec>) -> Self {
number_leaves(&mut spec, &mut [Vec::new(), Vec::new()]);
Self {
spec,
_points: PhantomData,
}
}
pub fn num_params(&self) -> usize {
self.spec.num_params()
}
pub fn get_params(&self, out: &mut [f64]) -> Result<(), GprError> {
self.spec.get_params(out)
}
pub fn set_params(&mut self, params: &[f64]) -> Result<(), GprError> {
self.spec.set_params(params)
}
pub fn parameter_bindings(&self) -> Vec<ParameterBinding> {
self.spec.parameter_bindings()
}
pub fn slots(&self) -> Vec<DistanceSlot> {
spec_slots(&self.spec)
}
#[cfg(test)]
pub(crate) fn spec(&self) -> &KernelSpec<SuppliedSpec> {
&self.spec
}
}
pub(crate) fn spec_slots<S: Supply>(spec: &KernelSpec<S>) -> Vec<DistanceSlot> {
let mut out = Vec::new();
collect_slots(spec, &mut out);
out
}
fn collect_slots<S: Supply>(spec: &KernelSpec<S>, out: &mut Vec<DistanceSlot>) {
match spec {
KernelSpec::Supplied(leaf) => {
let leaf = S::spec(leaf);
match out.iter().find(|slot| slot.id() == leaf.slot) {
Some(seen) => debug_assert_eq!(seen.shape(), leaf.shape()),
None => out.push(DistanceSlot::from_parts(leaf.slot, leaf.shape())),
}
}
KernelSpec::Sum(left, right) | KernelSpec::Product(left, right) => {
collect_slots(left, out);
collect_slots(right, out);
}
_ => {}
}
}
fn number_leaves(spec: &mut KernelSpec<SuppliedSpec>, seen: &mut [Vec<SlotId>; 2]) {
match spec {
KernelSpec::Supplied(leaf) => {
let list = &mut seen[usize::from(matches!(leaf.shape(), SlotShape::Ard(_)))];
leaf.at = match list.iter().position(|&slot| slot == leaf.slot) {
Some(at) => at,
None => {
list.push(leaf.slot);
list.len() - 1
}
};
}
KernelSpec::Sum(left, right) | KernelSpec::Product(left, right) => {
number_leaves(left, seen);
number_leaves(right, seen);
}
_ => {}
}
}
impl From<KernelSpec> for DistanceKernel<WithPoints> {
fn from(spec: KernelSpec) -> Self {
Self::from_spec(spec.widen())
}
}
macro_rules! distance_ops {
($trait:ident, $method:ident, $variant:ident) => {
impl<C1, C2> $trait<DistanceKernel<C2>> for DistanceKernel<C1>
where
C1: JoinPoints<C2>,
C2: PointUse,
{
type Output = DistanceKernel<<C1 as JoinPoints<C2>>::Output>;
fn $method(self, rhs: DistanceKernel<C2>) -> Self::Output {
DistanceKernel::from_spec(KernelSpec::$variant(
Box::new(self.spec),
Box::new(rhs.spec),
))
}
}
impl<C: PointUse> $trait<KernelSpec> for DistanceKernel<C> {
type Output = DistanceKernel<WithPoints>;
fn $method(self, rhs: KernelSpec) -> Self::Output {
DistanceKernel::from_spec(KernelSpec::$variant(
Box::new(self.spec),
Box::new(rhs.widen()),
))
}
}
distance_ops!(@leaf $trait, $method, $variant, ConstantKernel);
distance_ops!(@leaf $trait, $method, $variant, WhiteKernel);
};
(@leaf $trait:ident, $method:ident, $variant:ident, $leaf:ty) => {
impl<C: PointUse> $trait<$leaf> for DistanceKernel<C> {
type Output = DistanceKernel<C>;
fn $method(self, rhs: $leaf) -> Self::Output {
DistanceKernel::from_spec(KernelSpec::$variant(
Box::new(self.spec),
Box::new(KernelSpec::from(rhs).widen()),
))
}
}
impl<C: PointUse> $trait<DistanceKernel<C>> for $leaf {
type Output = DistanceKernel<C>;
fn $method(self, rhs: DistanceKernel<C>) -> Self::Output {
DistanceKernel::from_spec(KernelSpec::$variant(
Box::new(KernelSpec::from(self).widen()),
Box::new(rhs.spec),
))
}
}
};
}
distance_ops!(Add, add, Sum);
distance_ops!(Mul, mul, Product);
pub trait ModelKernel: sealed::Model + Clone + fmt::Debug + Send + Sync + 'static {}
impl sealed::Model for KernelSpec {
const POINTS: bool = true;
type Supply = NoSupply;
fn into_spec(self) -> KernelSpec {
self
}
fn from_spec(spec: KernelSpec) -> Self {
spec
}
}
impl ModelKernel for KernelSpec {}
impl<C: PointUse> sealed::Model for DistanceKernel<C> {
const POINTS: bool = <C as sealed::Points>::POINTS;
type Supply = SuppliedSpec;
fn into_spec(self) -> KernelSpec<SuppliedSpec> {
self.spec
}
fn from_spec(spec: KernelSpec<SuppliedSpec>) -> Self {
Self::from_spec(spec)
}
}
impl<C: PointUse> ModelKernel for DistanceKernel<C> {}
pub trait PointKernel: ModelKernel {}
impl PointKernel for KernelSpec {}
impl PointKernel for DistanceKernel<WithPoints> {}
pub(crate) use sealed::Model as ModelKernelParts;
pub(crate) type SpecOf<K> = KernelSpec<<K as sealed::Model>::Supply>;
pub(crate) type CompiledOf<T, K> = crate::kernel::CompiledKernel<T, <K as sealed::Model>::Supply>;
pub trait DistanceFill: Send + Sync {
fn fill_column(&self, col: usize, rows: std::ops::Range<usize>, out: &mut [f64]);
}
pub struct DistanceSource<'a> {
pub(crate) slot: SlotId,
pub(crate) data: SourceData<'a>,
pub(crate) tidy: Tidy,
}
impl DistanceSource<'_> {
pub fn tidy(mut self, rel_tol: f64) -> Result<Self, GprError> {
if !(0.0..1.0).contains(&rel_tol) {
return Err(GprError::InvalidConfig {
reason: format!("tidy tolerance must be in [0, 1), got {rel_tol}"),
});
}
self.tidy = Tidy::Within(rel_tol);
Ok(self)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum Tidy {
Exact,
Within(f64),
}
impl fmt::Debug for DistanceSource<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let data = match &self.data {
SourceData::Scalar(ScalarData::Values(v)) => format!("{} values", v.len()),
SourceData::Ard(_, ArdData::Blocks(b)) => format!("{} blocks", b.len()),
SourceData::Ard(_, ArdData::Slices(b)) => format!("{} borrowed blocks", b.len()),
SourceData::Scalar(ScalarData::Fill(_)) | SourceData::Ard(_, ArdData::Fill(_)) => {
"fill".to_owned()
}
};
f.debug_struct("DistanceSource")
.field("slot", &self.slot)
.field("shape", &self.data.shape())
.field("data", &data)
.finish()
}
}
pub(crate) enum SourceData<'a> {
Scalar(ScalarData<'a>),
Ard(usize, ArdData<'a>),
}
pub(crate) enum ScalarData<'a> {
Values(Cow<'a, [f64]>),
Fill(&'a dyn DistanceFill),
}
pub(crate) enum ArdData<'a> {
Blocks(Vec<Vec<f64>>),
Slices(&'a [&'a [f64]]),
Fill(&'a dyn DistanceFill),
}
impl SourceData<'_> {
pub(crate) fn shape(&self) -> SlotShape {
match self {
Self::Scalar(_) => SlotShape::Scalar,
Self::Ard(dims, _) => SlotShape::Ard(*dims),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slots_are_distinct_and_ordered() {
let a = ScalarDistance::new();
let (b, ard) = ArdDistance::from_leaf(RbfArdKernel::new(&[1.0, 2.0]).expect("ell"));
assert_eq!(b.dims(), 2);
assert_ne!(a, ScalarDistance::new());
let k = a.kernel(RbfKernel::new(1.0).expect("ell")) * ard
+ a.kernel(MaternKernel::new(1.0, crate::kernel::MaternNu::FiveHalves).expect("ell"))
+ b.kernel(RbfArdKernel::new(&[3.0, 4.0]).expect("ell"))
.expect("dims");
assert_eq!(
k.slots(),
vec![DistanceSlot::Scalar(a), DistanceSlot::Ard(b)]
);
assert_eq!(k.num_params(), 6);
}
#[test]
fn a_further_ard_leaf_must_match_the_slot() {
let (b, _) = ArdDistance::from_leaf(RbfArdKernel::new(&[1.0, 2.0, 3.0]).expect("ell"));
assert!(matches!(
b.kernel(RbfArdKernel::new(&[1.0, 2.0]).expect("ell")),
Err(GprError::DimensionMismatch {
x_dim: 3,
expected_dim: 2
})
));
}
struct Zeros;
impl DistanceFill for Zeros {
fn fill_column(&self, _col: usize, _rows: std::ops::Range<usize>, out: &mut [f64]) {
out.fill(0.0);
}
}
#[test]
fn sources_and_kernels_describe_themselves() {
let a = ScalarDistance::new();
let b = ArdDistance::of_dims(2);
let (b0, b1) = ([0.0, 1.0], [0.0, 4.0]);
let shown = [
format!("{:?}", a.from_slice(&[0.0])),
format!("{:?}", b.from_slices(&[&b0, &b1])),
format!("{:?}", b.fill(&Zeros)),
];
assert!(shown[0].contains("1 values"));
assert!(shown[1].contains("2 blocks"));
assert!(shown[2].contains("fill"));
let rbf = RbfKernel::new(1.0).expect("ell");
let k = a.kernel(rbf);
assert_eq!(k.clone(), k);
assert!(format!("{k:?}").starts_with("DistanceKernel"));
assert_eq!(k.parameter_bindings().len(), k.num_params());
let mixed = DistanceKernel::from(KernelSpec::from(rbf)) * k.clone();
assert_eq!(mixed.slots(), k.slots());
let summed = DistanceKernel::from(KernelSpec::from(rbf)) + k;
assert_eq!(summed.num_params(), 2);
let coordinate: KernelSpec = KernelSpec::from(rbf) + rbf.into();
assert_eq!(coordinate.num_params(), 2);
}
}