From 935ddf0bc7edd3da0d69773c0dc75f9b1b716309 Mon Sep 17 00:00:00 2001 From: Joshua Lampert Date: Sun, 5 May 2024 17:51:51 +0200 Subject: [PATCH 1/2] add Base.:+ and Base.:* for kernels --- src/kernels/special_kernel.jl | 14 ++++++++++---- test/test_unit.jl | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/kernels/special_kernel.jl b/src/kernels/special_kernel.jl index 21b476a5..c1c00a1d 100644 --- a/src/kernels/special_kernel.jl +++ b/src/kernels/special_kernel.jl @@ -1,5 +1,5 @@ @doc raw""" - TransformationKernel(kernel, transformation) + TransformationKernel{Dim}(kernel, transformation) Given a base `kernel` and a bijective `transformation` function, construct a new kernel that applies the transformation to both arguments ``x`` and ``y``, @@ -7,7 +7,9 @@ i.e., the new kernel ``K_T`` is given by ```math K_T(x, y) = K(Tx, Ty), ``` -where ``K`` is the base kernel and ``T`` the transformation. +where ``K`` is the base `kernel` and ``T`` the transformation, i.e. if ``K`` +is a kernel of dimension ``d``, ``T`` is a function from dimension `Dim` to ``d``, +where `Dim` is the dimension of the new kernel. """ struct TransformationKernel{Dim, Kernel, Transformation} <: AbstractKernel{Dim} kernel::Kernel @@ -33,7 +35,7 @@ end order(kernel::TransformationKernel) = order(kernel.kernel) @doc raw""" - ProductKernel(kernels) + ProductKernel{Dim}(kernels) Given a vector of `kernels`, construct a new kernel that multiplies the results of the component kernels, i.e., the new kernel ``K`` is given by @@ -76,8 +78,10 @@ end # TODO: Is that correct in general? order(kernel::ProductKernel) = maximum(order.(kernel.kernels)) +Base.:*(k1::AbstractKernel, k2::AbstractKernel) = ProductKernel{dim(k1)}([k1, k2]) + @doc raw""" - SumKernel(kernels) + SumKernel{Dim}(kernels) Given a vector of `kernels`, construct a new kernel that sums the results of the component kernels, i.e., the new kernel ``K`` is given by @@ -119,3 +123,5 @@ end # TODO: Is that correct in general? order(kernel::SumKernel) = minimum(order.(kernel.kernels)) + +Base.:+(k1::AbstractKernel, k2::AbstractKernel) = SumKernel{dim(k2)}([k1, k2]) diff --git a/test/test_unit.jl b/test/test_unit.jl index 177845b0..bdbb811a 100644 --- a/test/test_unit.jl +++ b/test/test_unit.jl @@ -138,12 +138,14 @@ using Plots @test_nowarn display(kernel11) @test order(kernel11) == 1 @test isapprox(kernel11(x, y), kernel1(x, y) * kernel2(x, y)) + @test isapprox((kernel1 * kernel2)(x, y), kernel1(x, y) * kernel2(x, y)) kernel12 = @test_nowarn SumKernel{2}([kernel1, kernel2]) @test_nowarn println(kernel12) @test_nowarn display(kernel12) @test order(kernel12) == 0 @test isapprox(kernel12(x, y), kernel1(x, y) + kernel2(x, y)) + @test isapprox((kernel1 + kernel2)(x, y), kernel1(x, y) + kernel2(x, y)) end @testset "NodeSet" begin From 3893b62079dfbebdcaa322e1851533bdd1da4521 Mon Sep 17 00:00:00 2001 From: Joshua Lampert Date: Sun, 5 May 2024 17:59:52 +0200 Subject: [PATCH 2/2] add to docstring --- src/kernels/special_kernel.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/kernels/special_kernel.jl b/src/kernels/special_kernel.jl index c1c00a1d..c5fdb53e 100644 --- a/src/kernels/special_kernel.jl +++ b/src/kernels/special_kernel.jl @@ -44,6 +44,7 @@ results of the component kernels, i.e., the new kernel ``K`` is given by ``` where ``K_i`` are the component kernels and ``n`` the number of kernels. Note that all component kernels need to have the same [`dim`](@ref). +A `ProductKernel` can also be constructed using the `*` operator. """ struct ProductKernel{Dim} <: AbstractKernel{Dim} kernels::Vector{AbstractKernel} @@ -90,6 +91,7 @@ results of the component kernels, i.e., the new kernel ``K`` is given by ``` where ``K_i`` are the component kernels and ``n`` the number of kernels. Note that all component kernels need to have the same [`dim`](@ref). +A `SumKernel` can also be constructed using the `+` operator. """ struct SumKernel{Dim} <: AbstractKernel{Dim} kernels::Vector{AbstractKernel}