Skip to content

Commit 8f95f02

Browse files
committed
Add second-derivative functions to interface
1 parent 211b675 commit 8f95f02

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

docs/src/implementer_guide.md

+3
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

+5-2
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, second derivative, gradient, Jacobian 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, second derivative, gradient, Jacobian or Hessian. You can also request the function value, its gradient and 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

+35-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ 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 derivatives of `f` with respect to the inputs `xs` using the backend `ab`.
92+
93+
The function returns a `Tuple` of second derivatives, one for each element in `xs`.
94+
"""
95+
function second_derivative(ab::AbstractBackend, f, xs...)
96+
return derivative(second_lowest(ab), x -> begin
97+
d = derivative(lowest(ab), f, x)
98+
return d[1] # derivative returns a tuple
99+
end, xs...)
100+
end
101+
88102
"""
89103
AD.hessian(ab::AD.AbstractBackend, f, x)
90104
@@ -139,12 +153,23 @@ function value_and_jacobian(ab::AbstractBackend, f, xs...)
139153
return value, jacs
140154
end
141155

156+
"""
157+
AD.value_and_second_derivative(ab::AD.AbstractBackend, f, x)
158+
159+
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)`.
160+
161+
See also [`AbstractDifferentiation.second_derivative`](@ref)
162+
"""
163+
function value_and_second_derivative(ab::AbstractBackend, f, x)
164+
return f(x), second_derivative(ab, f, x)[1]
165+
end
166+
142167
"""
143168
AD.value_and_hessian(ab::AD.AbstractBackend, f, x)
144169
145170
Return the tuple `(v, H)` of the function value `v = f(x)` and the Hessian `H = AD.hessian(ab, f, x)`.
146171
147-
See also [`AbstractDifferentiation.hessian`](@ref).
172+
See also [`AbstractDifferentiation.hessian`](@ref).
148173
"""
149174
function value_and_hessian(ab::AbstractBackend, f, x)
150175
if x isa Tuple
@@ -176,6 +201,15 @@ function value_and_hessian(ab::HigherOrderBackend, f, x)
176201
return value, hess
177202
end
178203

204+
"""
205+
AD.value_and_derivatives(ab::AD.AbstractBackend, f, x)
206+
207+
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)`.
208+
"""
209+
function value_and_derivatives(ab::AbstractBackend, f, x)
210+
return value_and_derivative(ab, f, x)..., second_derivative(ab, f, x)[1]
211+
end
212+
179213
"""
180214
AD.value_gradient_and_hessian(ab::AD.AbstractBackend, f, x)
181215

0 commit comments

Comments
 (0)