-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add transformation kernels * format * remove unnecessary begin ... end
- Loading branch information
1 parent
96bc4ff
commit dfd2c42
Showing
8 changed files
with
136 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using KernelInterpolation | ||
using Plots | ||
|
||
# function to interpolate | ||
f(x) = x[1] + x[2]^2 < 0.0 ? 1.0 : 0.1 | ||
|
||
x_min = -1.0 | ||
x_max = 1.0 | ||
n = 80 | ||
nodeset = random_hypercube(n, x_min, x_max; dim = 2) | ||
values = f.(nodeset) | ||
|
||
kernel = Matern12Kernel{dim(nodeset)}() | ||
trafo(x) = [x[1] + x[2]^2, 0.0] | ||
trafo_kernel = TransformationKernel{2}(kernel, trafo) | ||
itp = interpolate(nodeset, values, trafo_kernel) | ||
itp_base = interpolate(nodeset, values, kernel) | ||
|
||
many_nodes = homogeneous_hypercube(20, x_min, x_max; dim = 2) | ||
|
||
abs_diff_trafo = abs.(itp.(many_nodes) .- f.(many_nodes)) | ||
abs_diff = abs.(itp_base.(many_nodes) .- f.(many_nodes)) | ||
|
||
l1_error_trafo = sum(abs_diff_trafo) | ||
l1_error = sum(abs_diff) | ||
linf_error_trafo = maximum(abs_diff_trafo) | ||
linf_error = maximum(abs_diff) | ||
|
||
@show l1_error_trafo | ||
@show l1_error | ||
@show linf_error_trafo | ||
@show linf_error | ||
|
||
plot(layout = (1, 3)) | ||
plot!(many_nodes, trafo_kernel, subplot = 1, st = :surface, cbar = false, | ||
c = cgrad(:grays, rev = true), camera = (0, 90), xguide = "x", yguide = "y") | ||
|
||
plot!(many_nodes, itp, subplot = 2) | ||
plot!(many_nodes, f, subplot = 2) | ||
|
||
plot!(many_nodes, itp_base, subplot = 3) | ||
plot!(many_nodes, f, subplot = 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
AbstractKernel | ||
An abstract supertype of kernels. | ||
""" | ||
abstract type AbstractKernel{Dim} end | ||
|
||
""" | ||
dim(kernel) | ||
Return the dimension of a kernel, i.e. the size of the input vector. | ||
""" | ||
dim(kernel::AbstractKernel{Dim}) where {Dim} = Dim | ||
|
||
""" | ||
get_name(kernel::AbstractKernel) | ||
Returns the canonical, human-readable name for the given system of equations. | ||
""" | ||
get_name(kernel::AbstractKernel) = string(nameof(typeof(kernel))) * "{" * | ||
string(dim(kernel)) * "}" | ||
|
||
include("radialsymmetric_kernel.jl") | ||
include("special_kernel.jl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
@doc raw""" | ||
TransformationKernel(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. | ||
""" | ||
struct TransformationKernel{Dim, Kernel, Transformation} <: AbstractKernel{Dim} | ||
kernel::Kernel | ||
trafo::Transformation | ||
end | ||
|
||
function TransformationKernel{Dim}(kernel, transformation) where {Dim} | ||
return TransformationKernel{Dim, typeof(kernel), typeof(transformation)}(kernel, | ||
transformation) | ||
end | ||
|
||
function (kernel::TransformationKernel)(x, y) | ||
@assert length(x) == length(y) | ||
K = kernel.kernel | ||
T = kernel.trafo | ||
return K(T(x), T(y)) | ||
end | ||
|
||
function Base.show(io::IO, kernel::TransformationKernel{Dim}) where {Dim} | ||
return print(io, "TransformationKernel{", Dim, "}(kernel = ", kernel.kernel, ")") | ||
end | ||
|
||
order(kernel::TransformationKernel) = order(kernel.kernel) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters