Skip to content

Fix input types, improve readability #369

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

Merged
merged 15 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "KernelFunctions"
uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
version = "0.10.18"
version = "0.10.19"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
6 changes: 6 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ kernelpdmat
nystrom
NystromFact
```

## Optional Utilities
If the [Kronecker.jl](https://github.com/MichielStock/Kronecker.jl) package is loaded, KernelFunctions also provides the following functions.
Copy link
Member

Choose a reason for hiding this comment

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

It's always provided but it doesn't work without Kronecker.jl?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is in via Requires, so if I understand correctly it is not available without Kronecker.jl?

Copy link
Member Author

Choose a reason for hiding this comment

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

Have reformulated, is it clearer now?

```@docs
kronecker_kernelmatrix
```
28 changes: 18 additions & 10 deletions src/matrix/kernelkroneckermat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,39 @@ end
"""
@inline iskroncompatible(κ::Kernel) = false # Default return for kernels

function _kernelmatrix_kroneckerjl_helper(::MOInputIsotopicByFeatures, Kfeatures, Koutputs)
function _kernelmatrix_kroneckerjl_helper(
::Type{<:MOInputIsotopicByFeatures}, Kfeatures, Koutputs
)
return Kronecker.kronecker(Kfeatures, Koutputs)
end

function _kernelmatrix_kroneckerjl_helper(::MOInputIsotopicByOutputs, Kfeatures, Koutputs)
function _kernelmatrix_kroneckerjl_helper(
::Type{<:MOInputIsotopicByOutputs}, Kfeatures, Koutputs
)
return Kronecker.kronecker(Koutputs, Kfeatures)
end

"""
kronecker_kernelmatrix(k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::MOI, y::MOI)
where {MOI<:IsotopicMOInputsUnion}

Requires Kronecker.jl: Computes the `kernelmatrix` for the `IndependentMOKernel` and the `IntrinsicCoregionMOKernel`, but returns a lazy kronecker product. This object can be very efficiently inverted or decomposed. Also for details on `kernelmatrix` see its documentation.
"""
function kronecker_kernelmatrix(
k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel},
x::IsotopicMOInputsUnion,
y::IsotopicMOInputsUnion,
)
k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::MOI, y::MOI
) where {MOI<:IsotopicMOInputsUnion}
@assert x.out_dim == y.out_dim
Kfeatures = kernelmatrix(k.kernel, x.x, y.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
return _kernelmatrix_kroneckerjl_helper(x, Kfeatures, Koutputs)
return _kernelmatrix_kroneckerjl_helper(MOI, Kfeatures, Koutputs)
end

function kronecker_kernelmatrix(
k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::IsotopicMOInputsUnion
)
k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::MOI
) where {MOI<:IsotopicMOInputsUnion}
Kfeatures = kernelmatrix(k.kernel, x.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
return _kernelmatrix_kroneckerjl_helper(x, Kfeatures, Koutputs)
return _kernelmatrix_kroneckerjl_helper(MOI, Kfeatures, Koutputs)
end

function kronecker_kernelmatrix(
Expand Down
15 changes: 6 additions & 9 deletions src/mokernels/independent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,22 @@ end
_mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim)

function kernelmatrix(
k::IndependentMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion
)
k::IndependentMOKernel, x::MOI, y::MOI
) where {MOI<:IsotopicMOInputsUnion}
@assert x.out_dim == y.out_dim
Kfeatures = kernelmatrix(k.kernel, x.x, y.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs)
return _kernelmatrix_kron_helper(MOI, Kfeatures, Koutputs)
end

if VERSION >= v"1.6"
function kernelmatrix!(
K::AbstractMatrix,
k::IndependentMOKernel,
x::IsotopicMOInputsUnion,
y::IsotopicMOInputsUnion,
)
K::AbstractMatrix, k::IndependentMOKernel, x::MOI, y::MOI
) where {MOI<:IsotopicMOInputsUnion}
@assert x.out_dim == y.out_dim
Kfeatures = kernelmatrix(k.kernel, x.x, y.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs)
return _kernelmatrix_kron_helper!(K, MOI, Kfeatures, Koutputs)
end
end

Expand Down
15 changes: 6 additions & 9 deletions src/mokernels/intrinsiccoregion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,22 @@ function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim)
end

function kernelmatrix(
k::IntrinsicCoregionMOKernel, x::IsotopicMOInputsUnion, y::IsotopicMOInputsUnion
)
k::IntrinsicCoregionMOKernel, x::MOI, y::MOI
) where {MOI<:IsotopicMOInputsUnion}
@assert x.out_dim == y.out_dim
Kfeatures = kernelmatrix(k.kernel, x.x, y.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
return _kernelmatrix_kron_helper(x, Kfeatures, Koutputs)
return _kernelmatrix_kron_helper(MOI, Kfeatures, Koutputs)
end

if VERSION >= v"1.6"
function kernelmatrix!(
K::AbstractMatrix,
k::IntrinsicCoregionMOKernel,
x::IsotopicMOInputsUnion,
y::IsotopicMOInputsUnion,
)
K::AbstractMatrix, k::IntrinsicCoregionMOKernel, x::MOI, y::MOI
) where {MOI<:IsotopicMOInputsUnion}
@assert x.out_dim == y.out_dim
Kfeatures = kernelmatrix(k.kernel, x.x, y.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
return _kernelmatrix_kron_helper!(K, x, Kfeatures, Koutputs)
return _kernelmatrix_kron_helper!(K, MOI, Kfeatures, Koutputs)
end
end

Expand Down
12 changes: 8 additions & 4 deletions src/mokernels/mokernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ Abstract type for kernels with multiple outpus.
"""
abstract type MOKernel <: Kernel end

function _kernelmatrix_kron_helper(::MOInputIsotopicByFeatures, Kfeatures, Koutputs)
function _kernelmatrix_kron_helper(::Type{<:MOInputIsotopicByFeatures}, Kfeatures, Koutputs)
return kron(Kfeatures, Koutputs)
end

function _kernelmatrix_kron_helper(::MOInputIsotopicByOutputs, Kfeatures, Koutputs)
function _kernelmatrix_kron_helper(::Type{<:MOInputIsotopicByOutputs}, Kfeatures, Koutputs)
return kron(Koutputs, Kfeatures)
end

if VERSION >= v"1.6"
function _kernelmatrix_kron_helper!(K, ::MOInputIsotopicByFeatures, Kfeatures, Koutputs)
function _kernelmatrix_kron_helper!(
K, ::Type{<:MOInputIsotopicByFeatures}, Kfeatures, Koutputs
)
return kron!(K, Kfeatures, Koutputs)
end

function _kernelmatrix_kron_helper!(K, ::MOInputIsotopicByOutputs, Kfeatures, Koutputs)
function _kernelmatrix_kron_helper!(
K, ::Type{<:MOInputIsotopicByOutputs}, Kfeatures, Koutputs
)
return kron!(K, Koutputs, Kfeatures)
end
end