Skip to content

Commit 35585a8

Browse files
committed
Add second-derivative functions to interface
1 parent 211b675 commit 35585a8

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

docs/src/implementer_guide.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ They are just listed here to help readers figure out the code structure:
2929
- `derivative` calls `jacobian`
3030
- `gradient` calls `jacobian`
3131
- `hessian` calls `jacobian` and `gradient`
32+
- `second_derivative` calls `derivative`
3233
- `value_and_jacobian` calls `jacobian`
3334
- `value_and_derivative` calls `value_and_jacobian`
3435
- `value_and_gradient` calls `value_and_jacobian`
3536
- `value_and_hessian` calls `jacobian` and `gradient`
37+
- `value_and_second_derivative` calls `second_derivative`
3638
- `value_gradient_and_hessian` calls `value_and_jacobian` and `gradient`
39+
- `value_and_derivatives` calls `value_and_derivative` and `second_derivative`
3740
- `pushforward_function` calls `jacobian`
3841
- `value_and_pushforward_function` calls `pushforward_function`
3942
- `pullback_function` calls `value_and_pullback_function`

docs/src/user_guide.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,27 @@ AbstractDifferentiation.HigherOrderBackend
5353

5454
## Derivatives
5555

56-
The following list of functions can be used to request the derivative, gradient, Jacobian or Hessian without the function value.
56+
The following list of functions can be used to request the derivative, gradient, Jacobian, second derivative or Hessian without the function value.
5757

5858
```@docs
5959
AbstractDifferentiation.derivative
6060
AbstractDifferentiation.gradient
6161
AbstractDifferentiation.jacobian
62+
AbstractDifferentiation.second_derivative
6263
AbstractDifferentiation.hessian
6364
```
6465

6566
## Value and derivatives
6667

67-
The following list of functions can be used to request the function value along with its derivative, gradient, Jacobian or Hessian. You can also request the function value, its gradient and Hessian for single-input functions.
68+
The following list of functions can be used to request the function value along with its derivative, gradient, Jacobian, second derivative, or Hessian. You can also request the function value, its derivative (or its gradient) and its second derivative (or Hessian) for single-input functions.
6869

6970
```@docs
7071
AbstractDifferentiation.value_and_derivative
7172
AbstractDifferentiation.value_and_gradient
7273
AbstractDifferentiation.value_and_jacobian
74+
AbstractDifferentiation.value_and_second_derivative
7375
AbstractDifferentiation.value_and_hessian
76+
AbstractDifferentiation.value_and_derivatives
7477
AbstractDifferentiation.value_gradient_and_hessian
7578
```
7679

src/AbstractDifferentiation.jl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ function jacobian(ab::HigherOrderBackend, f, xs...)
8585
return jacobian(lowest(ab), f, xs...)
8686
end
8787

88+
"""
89+
AD.second_derivative(ab::AD.AbstractBackend, f, xs...)
90+
91+
Compute the second derivative of `f` with respect to the input `x` using the backend `ab`.
92+
93+
The function returns a single value because `second_derivative` currently only supports a single input.
94+
"""
95+
function second_derivative(ab::AbstractBackend, f, x)
96+
if x isa Tuple
97+
# only support computation of second derivative for functions with single input argument
98+
x = only(x)
99+
end
100+
return derivative(second_lowest(ab), x -> begin
101+
d = derivative(lowest(ab), f, x)
102+
return d[1] # derivative returns a tuple
103+
end, x)
104+
end
105+
88106
"""
89107
AD.hessian(ab::AD.AbstractBackend, f, x)
90108
@@ -139,12 +157,23 @@ function value_and_jacobian(ab::AbstractBackend, f, xs...)
139157
return value, jacs
140158
end
141159

160+
"""
161+
AD.value_and_second_derivative(ab::AD.AbstractBackend, f, x)
162+
163+
Return the tuple `(v, d2)` of the function value `v = f(x)` and the second derivatives `d = AD.derivative(ab, f, x)` and `d2 = AD.hessian(ab, f, x)`.
164+
165+
See also [`AbstractDifferentiation.second_derivative`](@ref)
166+
"""
167+
function value_and_second_derivative(ab::AbstractBackend, f, x)
168+
return f(x), second_derivative(ab, f, x)
169+
end
170+
142171
"""
143172
AD.value_and_hessian(ab::AD.AbstractBackend, f, x)
144173
145174
Return the tuple `(v, H)` of the function value `v = f(x)` and the Hessian `H = AD.hessian(ab, f, x)`.
146175
147-
See also [`AbstractDifferentiation.hessian`](@ref).
176+
See also [`AbstractDifferentiation.hessian`](@ref).
148177
"""
149178
function value_and_hessian(ab::AbstractBackend, f, x)
150179
if x isa Tuple
@@ -176,6 +205,15 @@ function value_and_hessian(ab::HigherOrderBackend, f, x)
176205
return value, hess
177206
end
178207

208+
"""
209+
AD.value_and_derivatives(ab::AD.AbstractBackend, f, x)
210+
211+
Return the tuple `(v, d, d2)` of the function value `v = f(x)` and the first and second derivatives `d = AD.derivative(ab, f, x)` and `d2 = AD.second_derivative(ab, f, x)`.
212+
"""
213+
function value_and_derivatives(ab::AbstractBackend, f, x)
214+
return value_and_derivative(ab, f, x)..., second_derivative(ab, f, x)[1]
215+
end
216+
179217
"""
180218
AD.value_gradient_and_hessian(ab::AD.AbstractBackend, f, x)
181219

0 commit comments

Comments
 (0)