Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Base.:+ and Base.:* for kernels #45

Merged
merged 2 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/kernels/special_kernel.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
@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``,
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
Expand All @@ -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
Expand All @@ -42,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}
Expand Down Expand Up @@ -76,8 +79,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
Expand All @@ -86,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}
Expand Down Expand Up @@ -119,3 +125,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])
2 changes: 2 additions & 0 deletions test/test_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading