Skip to content

Commit

Permalink
Add _sum for KernelSum{<:AbstractVector} and associated tests (#547)
Browse files Browse the repository at this point in the history
* add sum for vectors and add tests for it

* Update test/kernels/kernelsum.jl
  • Loading branch information
theogf authored Feb 8, 2024
1 parent 13bd5cb commit 87623ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/kernels/kernelsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Base.length(k::KernelSum) = length(k.kernels)

_sum(f, ks::Tuple, args...) = f(first(ks), args...) + _sum(f, Base.tail(ks), args...)
_sum(f, ks::Tuple{Tx}, args...) where {Tx} = f(only(ks), args...)
_sum(f, ks::AbstractVector, args...) = sum(k -> f(k, args...), ks)

::KernelSum)(x, y) = _sum((k, x, y) -> k(x, y), κ.kernels, x, y)

Expand Down
11 changes: 7 additions & 4 deletions test/kernels/kernelsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
k1 = LinearKernel()
k2 = SqExponentialKernel()
k = KernelSum(k1, k2)
@test k == KernelSum([k1, k2]) == KernelSum((k1, k2))
kvec = KernelSum([k1, k2])
@test k == kvec == KernelSum((k1, k2))
for (_k1, _k2) in Iterators.product(
(k1, KernelSum((k1,)), KernelSum([k1])), (k2, KernelSum((k2,)), KernelSum([k2]))
)
@test k == _k1 + _k2
@test kvec == _k1 + _k2
end
@test length(k) == 2
@test repr(k) == (
@test length(k) == length(kvec) == 2
@test repr(k) ==
repr(kvec) ==
"Sum of 2 kernels:\n" *
"\tLinear Kernel (c = 0.0)\n" *
"\tSquared Exponential Kernel (metric = Euclidean(0.0))"
)

# Standardised tests.
test_interface(k, Float64)
test_interface(kvec, Float64)
test_interface(ConstantKernel(; c=1.5) + WhiteKernel(), Vector{String})
test_ADs(x -> KernelSum(SqExponentialKernel(), LinearKernel(; c=exp(x[1]))), rand(1))
test_interface_ad_perf(2.4, StableRNG(123456)) do c
Expand Down

2 comments on commit 87623ff

@theogf
Copy link
Member Author

@theogf theogf commented on 87623ff Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Version 0.10.62 already exists

Please sign in to comment.