Skip to content

Add docstring to kernelkronmat and small corrections #377

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 17 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 13 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
8 changes: 7 additions & 1 deletion docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ To find out more about the background, read this [review of kernels for vector-v

KernelFunctions also provides miscellaneous utility functions.
```@docs
kernelpdmat
nystrom
NystromFact
```
Expand All @@ -96,4 +95,11 @@ To keep the dependencies of KernelFunctions lean, some functionality is only ava
[*https://github.com/MichielStock/Kronecker.jl*](https://github.com/MichielStock/Kronecker.jl)
```@docs
kronecker_kernelmatrix
kernelkronmat
```

### PDMats.jl
[*https://github.com/JuliaStats/PDMats.jl*](https://github.com/JuliaStats/PDMats.jl)
```@docs
kernelpdmat
```
57 changes: 44 additions & 13 deletions src/matrix/kernelkroneckermat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,58 @@ using .Kronecker: Kronecker
export kernelkronmat
export kronecker_kernelmatrix

function kernelkronmat(κ::Kernel, X::AbstractVector, dims::Int)
@assert iskroncompatible(κ) "The chosen kernel is not compatible for kroenecker matrices (see [`iskroncompatible`](@ref))"
k = kernelmatrix(κ, X)
return Kronecker.kronecker(k, dims)
@doc raw"""
kernelkronmat(κ::Kernel, X::AbstractVector{<:Real}, dims::Int) -> KroneckerPower

Return a `KroneckerPower` matrix on the `D`-dimensional input grid constructed by ``\otimes_{i=1}^D X``,
where `D` is given by `dims`.

!!! warning

Require `Kronecker.jl` and for `iskroncompatible(κ)` to return `true`.
"""
function kernelkronmat(κ::Kernel, X::AbstractVector{<:Real}, dims::Int)
checkkroncompatible(κ)
K = kernelmatrix(κ, X)
return Kronecker.kronecker(K, dims)
end

function kernelkronmat(
κ::Kernel, X::AbstractVector{<:AbstractVector}; obsdim::Int=defaultobs
)
@assert iskroncompatible(κ) "The chosen kernel is not compatible for Kronecker matrices"
@doc raw"""

kernelkronmat(κ::Kernel, X::AbstractVector{<:AbstractVector}) -> KroneckerProduct

Returns a `KroneckerProduct` matrix on the grid built with the collection of vectors ``\{X_i\}_{i=1}^D``: ``\otimes_{i=1}^D X_i``.

!!! warning

Require `Kronecker.jl` and for `iskroncompatible(κ)` to return `true`.
"""
function kernelkronmat(κ::Kernel, X::AbstractVector{<:AbstractVector})
checkkroncompatible(κ)
Ks = kernelmatrix.(κ, X)
return K = reduce(Kronecker.:⊗, Ks)
return reduce(Kronecker.:⊗, Ks)
end

"""
To be compatible with kroenecker constructions the kernel must satisfy
the property : for x,x' ∈ ℜᴰ
k(x,x') = ∏ᵢᴰ k(xᵢ,x'ᵢ)
@doc raw"""
iskroncompatible(k::Kernel)

Determine whether kernel `k` is compatible with Kronecker constructions such as [`kernelkronmat`](@ref)

The function returns `false` by default. If `k` is compatible it must satisfy for all ``x, x' \in \mathbb{R}^D`:
```math
k(x, x') = \prod_{i=1}^D k(x_i, x'_i).
```
"""
@inline iskroncompatible(κ::Kernel) = false # Default return for kernels

function checkkroncompatible(κ::Kernel)
iskroncompatible(κ) || throw(
ArgumentError(
"The chosen kernel is not compatible for Kronecker matrices (see [`iskroncompatible`](@ref))",
),
)
end

function _kernelmatrix_kroneckerjl_helper(
::Type{<:MOInputIsotopicByFeatures}, Kfeatures, Koutputs
)
Expand Down
2 changes: 1 addition & 1 deletion test/matrix/kernelkroneckermat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@test all(collect(kernelkronmat(k, collect(x), 2)) .≈ kernelmatrix(k, X; obsdim=1))
@test all(collect(kernelkronmat(k, [x, x])) .≈ kernelmatrix(k, X; obsdim=1))
@test_throws AssertionError kernelkronmat(LinearKernel(), collect(x), 2)
@test_throws ArgumentError kernelkronmat(LinearKernel(), collect(x), 2)

@testset "lazy kernelmatrix" begin
rng = MersenneTwister(123)
Expand Down