|
1 | 1 | ## Pushforward |
2 | 2 |
|
3 | | -function DI.value_and_pushforward!!( |
4 | | - f, _dy::Number, ::AutoFiniteDiff{fdtype}, x, dx, extras::Nothing |
5 | | -) where {fdtype} |
| 3 | +function DI.pushforward(f, backend::AnyAutoFiniteDiff, x, dx, extras::Nothing) |
| 4 | + step(t::Number) = f(x .+ t .* dx) |
| 5 | + new_dy = finite_difference_derivative(step, zero(eltype(x)), fdtype(backend)) |
| 6 | + return new_dy |
| 7 | +end |
| 8 | + |
| 9 | +function DI.value_and_pushforward(f, backend::AnyAutoFiniteDiff, x, dx, extras::Nothing) |
6 | 10 | y = f(x) |
7 | | - step(t::Number)::Number = f(x .+ t .* dx) |
8 | | - new_dy = finite_difference_derivative(step, zero(eltype(dx)), fdtype, eltype(y), y) |
| 11 | + step(t::Number) = f(x .+ t .* dx) |
| 12 | + new_dy = finite_difference_derivative( |
| 13 | + step, zero(eltype(x)), fdtype(backend), eltype(y), y |
| 14 | + ) |
9 | 15 | return y, new_dy |
10 | 16 | end |
11 | 17 |
|
12 | | -function DI.value_and_pushforward!!( |
13 | | - f, dy::AbstractArray, ::AutoFiniteDiff{fdtype}, x, dx, extras::Nothing |
14 | | -) where {fdtype} |
| 18 | +## Derivative |
| 19 | + |
| 20 | +function DI.derivative(f, backend::AnyAutoFiniteDiff, x, extras::Nothing) |
| 21 | + return finite_difference_derivative(f, x, fdtype(backend)) |
| 22 | +end |
| 23 | + |
| 24 | +function DI.value_and_derivative(f, backend::AnyAutoFiniteDiff, x, extras::Nothing) |
15 | 25 | y = f(x) |
16 | | - step(t::Number)::AbstractArray = f(x .+ t .* dx) |
17 | | - finite_difference_gradient!( |
18 | | - dy, step, zero(eltype(dx)), fdtype, eltype(y), FUNCTION_NOT_INPLACE, y |
19 | | - ) |
20 | | - return y, dy |
| 26 | + return y, finite_difference_derivative(f, x, fdtype(backend), eltype(y), y) |
21 | 27 | end |
22 | 28 |
|
23 | | -function DI.value_and_pushforward( |
24 | | - f, ::AutoFiniteDiff{fdtype}, x, dx, extras::Nothing |
25 | | -) where {fdtype} |
| 29 | +## Gradient |
| 30 | + |
| 31 | +function DI.gradient(f, backend::AnyAutoFiniteDiff, x::Number, extras::Nothing) |
| 32 | + return DI.derivative(f, backend, x, extras) |
| 33 | +end |
| 34 | + |
| 35 | +function DI.value_and_gradient(f, backend::AnyAutoFiniteDiff, x::Number, extras::Nothing) |
| 36 | + return DI.value_and_derivative(f, backend, x, extras) |
| 37 | +end |
| 38 | + |
| 39 | +function DI.gradient(f, backend::AnyAutoFiniteDiff, x::AbstractArray, extras::Nothing) |
| 40 | + return finite_difference_gradient(f, x, fdtype(backend)) |
| 41 | +end |
| 42 | + |
| 43 | +function DI.value_and_gradient( |
| 44 | + f, backend::AnyAutoFiniteDiff, x::AbstractArray, extras::Nothing |
| 45 | +) |
26 | 46 | y = f(x) |
27 | | - step(t::Number) = f(x .+ t .* dx) |
28 | | - new_dy = finite_difference_derivative(step, zero(eltype(dx)), fdtype, eltype(y), y) |
29 | | - return y, new_dy |
| 47 | + return y, finite_difference_gradient(f, x, fdtype(backend), typeof(y), y) |
| 48 | +end |
| 49 | + |
| 50 | +function DI.gradient!!( |
| 51 | + f, grad, backend::AnyAutoFiniteDiff, x::AbstractArray, extras::Nothing |
| 52 | +) |
| 53 | + return finite_difference_gradient!(grad, f, x, fdtype(backend)) |
| 54 | +end |
| 55 | + |
| 56 | +function DI.value_and_gradient!!( |
| 57 | + f, grad, backend::AnyAutoFiniteDiff, x::AbstractArray, extras::Nothing |
| 58 | +) |
| 59 | + y = f(x) |
| 60 | + return y, finite_difference_gradient!(grad, f, x, fdtype(backend), typeof(y), y) |
| 61 | +end |
| 62 | + |
| 63 | +## Jacobian |
| 64 | + |
| 65 | +function DI.jacobian(f, backend::AnyAutoFiniteDiff, x, extras::Nothing) |
| 66 | + return finite_difference_jacobian(f, x, fdjtype(backend)) |
| 67 | +end |
| 68 | + |
| 69 | +function DI.value_and_jacobian(f, backend::AnyAutoFiniteDiff, x, extras::Nothing) |
| 70 | + y = f(x) |
| 71 | + return y, finite_difference_jacobian(f, x, fdjtype(backend), eltype(y), y) |
| 72 | +end |
| 73 | + |
| 74 | +function DI.jacobian!!(f, jac, backend::AnyAutoFiniteDiff, x, extras::Nothing) |
| 75 | + return DI.jacobian(f, backend, x, extras) |
| 76 | +end |
| 77 | + |
| 78 | +function DI.value_and_jacobian!!(f, jac, backend::AnyAutoFiniteDiff, x, extras::Nothing) |
| 79 | + return DI.value_and_jacobian(f, backend, x, extras) |
| 80 | +end |
| 81 | + |
| 82 | +## Hessian |
| 83 | + |
| 84 | +function DI.hessian(f, backend::AnyAutoFiniteDiff, x, extras::Nothing) |
| 85 | + return finite_difference_hessian(f, x, fdhtype(backend)) |
| 86 | +end |
| 87 | + |
| 88 | +function DI.hessian!!(f, hess, backend::AnyAutoFiniteDiff, x, extras::Nothing) |
| 89 | + return finite_difference_hessian!(hess, f, x, fdhtype(backend)) |
30 | 90 | end |
0 commit comments