|
25 | 25 |
|
26 | 26 | DiffPt(x; partial=()) = DiffPt{length(x)}(x, partial) # convenience constructor
|
27 | 27 |
|
| 28 | +""" |
| 29 | + partial(fun, idx) |
| 30 | +
|
| 31 | +Return ∂ᵢf where |
| 32 | + f = fun |
| 33 | + i = idx |
| 34 | +""" |
| 35 | +function partial(fun, idx) |
| 36 | + return x -> FD.derivative(0) do dx |
| 37 | + y = similar(x) |
| 38 | + y = copyto!(y, x) |
| 39 | + y[idx] += dx |
| 40 | + fun(y) |
| 41 | + end |
| 42 | +end |
| 43 | + |
28 | 44 | """
|
29 |
| -Take the partial derivative of a function `fun` with input dimesion `dim`. |
30 |
| -If partials=(i,j), then (∂ᵢ∂ⱼ fun) is returned. |
| 45 | + partial(fun, indices...) |
| 46 | +
|
| 47 | +Return the partial derivative with respect to all indices, e.g. |
| 48 | +```julia |
| 49 | +partial(f, i, j) # = ∂ᵢ∂ⱼf |
| 50 | +``` |
31 | 51 | """
|
32 |
| -function partial(fun, dim, partials=()) |
33 |
| - if !isnothing(local next = iterate(partials)) |
34 |
| - idx, state = next |
35 |
| - return partial( |
36 |
| - x -> FD.derivative(0) do dx |
37 |
| - fun(x .+ dx * OneHotVector(idx, dim)) |
38 |
| - end, dim, Base.rest(partials, state) |
39 |
| - ) |
40 |
| - end |
41 |
| - return fun |
| 52 | +function partial(fun, indices...) |
| 53 | + idx, state = iterate(indices) |
| 54 | + return partial(partial(fun, idx), Base.rest(indices, state)...) |
42 | 55 | end
|
43 | 56 |
|
44 | 57 | """
|
@@ -74,7 +87,20 @@ then julia would not know whether to use
|
74 | 87 | `(::SpecialKernel)(x,y)` or `(::Kernel)(x::DiffPt, y::DiffPt)`
|
75 | 88 | ```
|
76 | 89 | =#
|
77 |
| -for T in [SimpleKernel, Kernel] #subtypes(Kernel) |
| 90 | +for T in [ |
| 91 | + SimpleKernel, |
| 92 | + Kernel, |
| 93 | + ZeroKernel, |
| 94 | + NeuralNetworkKernel, |
| 95 | + NeuralKernelNetwork, |
| 96 | + GibbsKernel, |
| 97 | + WienerKernel, |
| 98 | + WienerKernel{2}, |
| 99 | + TransformedKernel, |
| 100 | + KernelSum, |
| 101 | + NormalizedKernel, |
| 102 | + KernelTensorProduct |
| 103 | + ] #subtypes(Kernel) |
78 | 104 | (k::T)(x::DiffPt{Dim}, y::DiffPt{Dim}) where {Dim} = _evaluate(k, x, y)
|
79 | 105 | (k::T)(x::DiffPt{Dim}, y) where {Dim} = _evaluate(k, x, DiffPt(y))
|
80 | 106 | (k::T)(x, y::DiffPt{Dim}) where {Dim} = _evaluate(k, DiffPt(x), y)
|
|
0 commit comments