Skip to content

Commit 7ce1c39

Browse files
theogfgithub-actions[bot]st--
authored
Add docstring to kernelkronmat and small corrections (#377)
* Gives a docstring to kernelkronmat * Add elements to the `Conditional Utilities` parts * Reformulation of the docstring * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Formatting * Fix typo and error uniformisation * Formatting * Change to ArugmentError * Adapt tests * Splitted docstring and corrected type output * Included comments * Update src/matrix/kernelkroneckermat.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Patch bump * Update src/matrix/kernelkroneckermat.jl Co-authored-by: st-- <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: st-- <[email protected]>
1 parent cd26d09 commit 7ce1c39

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "KernelFunctions"
22
uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
3-
version = "0.10.25"
3+
version = "0.10.26"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

docs/src/api.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ To find out more about the background, read this [review of kernels for vector-v
8484

8585
KernelFunctions also provides miscellaneous utility functions.
8686
```@docs
87-
kernelpdmat
8887
nystrom
8988
NystromFact
9089
```
@@ -96,4 +95,11 @@ To keep the dependencies of KernelFunctions lean, some functionality is only ava
9695
[*https://github.com/MichielStock/Kronecker.jl*](https://github.com/MichielStock/Kronecker.jl)
9796
```@docs
9897
kronecker_kernelmatrix
98+
kernelkronmat
9999
```
100+
101+
### PDMats.jl
102+
[*https://github.com/JuliaStats/PDMats.jl*](https://github.com/JuliaStats/PDMats.jl)
103+
```@docs
104+
kernelpdmat
105+
```

src/matrix/kernelkroneckermat.jl

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,58 @@ using .Kronecker: Kronecker
66
export kernelkronmat
77
export kronecker_kernelmatrix
88

9-
function kernelkronmat::Kernel, X::AbstractVector, dims::Int)
10-
@assert iskroncompatible(κ) "The chosen kernel is not compatible for kroenecker matrices (see [`iskroncompatible`](@ref))"
11-
k = kernelmatrix(κ, X)
12-
return Kronecker.kronecker(k, dims)
9+
@doc raw"""
10+
kernelkronmat(κ::Kernel, X::AbstractVector{<:Real}, dims::Int) -> KroneckerPower
11+
12+
Return a `KroneckerPower` matrix on the `D`-dimensional input grid constructed by ``\otimes_{i=1}^D X``,
13+
where `D` is given by `dims`.
14+
15+
!!! warning
16+
17+
Require `Kronecker.jl` and for `iskroncompatible(κ)` to return `true`.
18+
"""
19+
function kernelkronmat::Kernel, X::AbstractVector{<:Real}, dims::Int)
20+
checkkroncompatible(κ)
21+
K = kernelmatrix(κ, X)
22+
return Kronecker.kronecker(K, dims)
1323
end
1424

15-
function kernelkronmat(
16-
κ::Kernel, X::AbstractVector{<:AbstractVector}; obsdim::Int=defaultobs
17-
)
18-
@assert iskroncompatible(κ) "The chosen kernel is not compatible for Kronecker matrices"
25+
@doc raw"""
26+
27+
kernelkronmat(κ::Kernel, X::AbstractVector{<:AbstractVector}) -> KroneckerProduct
28+
29+
Returns a `KroneckerProduct` matrix on the grid built with the collection of vectors ``\{X_i\}_{i=1}^D``: ``\otimes_{i=1}^D X_i``.
30+
31+
!!! warning
32+
33+
Requires `Kronecker.jl` and for `iskroncompatible(κ)` to return `true`.
34+
"""
35+
function kernelkronmat::Kernel, X::AbstractVector{<:AbstractVector})
36+
checkkroncompatible(κ)
1937
Ks = kernelmatrix.(κ, X)
20-
return K = reduce(Kronecker.:, Ks)
38+
return reduce(Kronecker.:, Ks)
2139
end
2240

23-
"""
24-
To be compatible with kroenecker constructions the kernel must satisfy
25-
the property : for x,x' ∈ ℜᴰ
26-
k(x,x') = ∏ᵢᴰ k(xᵢ,x'ᵢ)
41+
@doc raw"""
42+
iskroncompatible(k::Kernel)
43+
44+
Determine whether kernel `k` is compatible with Kronecker constructions such as [`kernelkronmat`](@ref)
45+
46+
The function returns `false` by default. If `k` is compatible it must satisfy for all ``x, x' \in \mathbb{R}^D`:
47+
```math
48+
k(x, x') = \prod_{i=1}^D k(x_i, x'_i).
49+
```
2750
"""
2851
@inline iskroncompatible::Kernel) = false # Default return for kernels
2952

53+
function checkkroncompatible::Kernel)
54+
return iskroncompatible(κ) || throw(
55+
ArgumentError(
56+
"The chosen kernel is not compatible for Kronecker matrices (see [`iskroncompatible`](@ref))",
57+
),
58+
)
59+
end
60+
3061
function _kernelmatrix_kroneckerjl_helper(
3162
::Type{<:MOInputIsotopicByFeatures}, Kfeatures, Koutputs
3263
)

test/matrix/kernelkroneckermat.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

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

1111
@testset "lazy kernelmatrix" begin
1212
rng = MersenneTwister(123)

0 commit comments

Comments
 (0)