Skip to main content
Kernel composition operations allow you to build complex kernel functions from simpler ones while preserving positive semi-definiteness properties required for valid kernels.

Overview

All composition operations preserve the positive semi-definite (PSD) property, ensuring the resulting kernels are valid for use in kernel methods like SVM.

SumKernel

Sum of two kernels representing the direct sum of their Reproducing Kernel Hilbert Spaces (RKHS). Formula: k(x, y) = k₁(x, y) + k₂(x, y) Mathematical property: The sum of two PSD kernels is PSD. Corresponds to the direct sum H = H₁ ⊕ H₂.

Constructor

k1
KernelFunction
First kernel function
k2
KernelFunction
Second kernel function

Methods

operator()

Evaluates the sum kernel.
x
const Vector&
First input vector
y
const Vector&
Second input vector
return
double
Sum of kernel evaluations: k₁(x, y) + k₂(x, y)

Example

#include <mlpp/classifiers/kernel/kernel_composition.hpp>
#include <mlpp/classifiers/kernel/rkhs_kernels.hpp>

using namespace mlpp::classifiers::kernel;

// Combine linear and RBF kernels
KernelFunction linear(std::make_unique<LinearKernel>());
KernelFunction rbf(std::make_unique<RBFKernel>(0.5));

SumKernel sum(linear, rbf);
KernelFunction combined(std::make_unique<SumKernel>(linear, rbf));

Vector x = {1.0, 2.0, 3.0};
Vector y = {1.5, 2.5, 3.5};

double result = combined(x, y);  // linear(x,y) + rbf(x,y)

ProductKernel

Product of two kernels inducing the tensor product RKHS. Formula: k(x, y) = k₁(x, y) · k₂(x, y) Mathematical property: The product of two PSD kernels is PSD.

Constructor

k1
KernelFunction
First kernel function
k2
KernelFunction
Second kernel function

Methods

operator()

Evaluates the product kernel.
x
const Vector&
First input vector
y
const Vector&
Second input vector
return
double
Product of kernel evaluations: k₁(x, y) · k₂(x, y)

Example

#include <mlpp/classifiers/kernel/kernel_composition.hpp>
#include <mlpp/classifiers/kernel/rkhs_kernels.hpp>

using namespace mlpp::classifiers::kernel;

// Create product of two polynomial kernels
KernelFunction poly1(std::make_unique<PolynomialKernel>(1.0, 0.0, 2));
KernelFunction poly2(std::make_unique<PolynomialKernel>(1.0, 1.0, 3));

KernelFunction product(std::make_unique<ProductKernel>(poly1, poly2));

Vector x = {1.0, 2.0};
Vector y = {3.0, 4.0};

double result = product(x, y);  // poly1(x,y) * poly2(x,y)

ScaledKernel

Scalar multiplication of a kernel function. Formula: k(x, y) = α · k(x, y), where α ≥ 0 Mathematical property: Scalar multiplication by a non-negative constant preserves PSD.

Constructor

scale
double
Scale factor α (must be non-negative)
kernel
KernelFunction
Base kernel function to scale

Methods

operator()

Evaluates the scaled kernel.
x
const Vector&
First input vector
y
const Vector&
Second input vector
return
double
Scaled kernel evaluation: α · k(x, y)

Example

#include <mlpp/classifiers/kernel/kernel_composition.hpp>
#include <mlpp/classifiers/kernel/rkhs_kernels.hpp>

using namespace mlpp::classifiers::kernel;

// Scale an RBF kernel by 2.5
KernelFunction rbf(std::make_unique<RBFKernel>(0.5));
KernelFunction scaled(std::make_unique<ScaledKernel>(2.5, rbf));

Vector x = {1.0, 2.0, 3.0};
Vector y = {1.5, 2.5, 3.5};

double result = scaled(x, y);  // 2.5 * rbf(x, y)

ExponentialKernel

Exponentiated kernel transformation (advanced construction). Formula: k(x, y) = exp(k₀(x, y)) Mathematical property: Only PSD if the base kernel k₀ is conditionally PSD. This is provided as an advanced construction and should be used with care.

Constructor

base
KernelFunction
Base kernel function k₀

Methods

operator()

Evaluates the exponentiated kernel.
x
const Vector&
First input vector
y
const Vector&
Second input vector
return
double
Exponentiated kernel value: exp(k₀(x, y))

Example

#include <mlpp/classifiers/kernel/kernel_composition.hpp>
#include <mlpp/classifiers/kernel/rkhs_kernels.hpp>

using namespace mlpp::classifiers::kernel;

// Exponentiate a linear kernel
KernelFunction linear(std::make_unique<LinearKernel>());
KernelFunction exponential(std::make_unique<ExponentialKernel>(linear));

Vector x = {0.1, 0.2};
Vector y = {0.3, 0.4};

double result = exponential(x, y);  // exp(linear(x, y))

Complex compositions

You can combine multiple composition operations to create sophisticated kernel functions:
#include <mlpp/classifiers/kernel/kernel_composition.hpp>
#include <mlpp/classifiers/kernel/rkhs_kernels.hpp>

using namespace mlpp::classifiers::kernel;

// Create a complex kernel: 2.0 * (linear + rbf) * polynomial
KernelFunction linear(std::make_unique<LinearKernel>());
KernelFunction rbf(std::make_unique<RBFKernel>(0.5));
KernelFunction poly(std::make_unique<PolynomialKernel>(1.0, 0.0, 2));

// First, sum linear and RBF
KernelFunction sum(std::make_unique<SumKernel>(linear, rbf));

// Multiply by polynomial
KernelFunction product(std::make_unique<ProductKernel>(sum, poly));

// Scale by 2.0
KernelFunction final_kernel(std::make_unique<ScaledKernel>(2.0, product));

// Use in your SVM or kernel method
Vector x = {1.0, 2.0, 3.0};
Vector y = {1.5, 2.5, 3.5};

double k = final_kernel(x, y);

Design notes

Value semantics

All kernel composition classes work with KernelFunction, which provides value semantics (copyable and movable) while maintaining polymorphic behavior.

PSD preservation

  • SumKernel: Always preserves PSD
  • ProductKernel: Always preserves PSD
  • ScaledKernel: Preserves PSD if scale ≥ 0
  • ExponentialKernel: Preserves PSD only under certain conditions on the base kernel

RKHS interpretation

  • Sum: Direct sum of Hilbert spaces H₁ ⊕ H₂
  • Product: Tensor product of Hilbert spaces H₁ ⊗ H₂
  • Scaled: Scaled inner product space
  • Exponential: Advanced transformation with complex RKHS structure

Build docs developers (and LLMs) love