@@ -4,22 +4,26 @@ Basic ForwardDiff API
44Derivatives of :math: `f(x) : \mathbb {R} \to \mathbb {R}^{n_1 } \times \dots \times \mathbb {R}^{n_k}`
55--------------------------------------------------------------------------------------------------
66
7- Use ``ForwardDiff.derivative `` to differentiate functions of the form ``f(::Real)::Real `` and ``f(::Real)::AbstractArray ``.
7+ Use ``ForwardDiff.derivative `` to differentiate functions of the form ``f(::Real... )::Real `` and ``f(::Real... )::AbstractArray ``.
88
99.. function :: ForwardDiff.derivative!(out, f, x)
1010
11- Compute :math: `f'(x)`, storing the output in ``out ``.
11+ Compute :math: `f'(x)`, storing the output in ``out ``. If ``x `` is a ``Tuple ``,
12+ then ``f `` will be called as ``f(x...) `` and the derivatives with respect to
13+ each element in `x ` will be stored in the respective element of ``out `` (which
14+ should also be a ``Tuple ``).
1215
1316.. function :: ForwardDiff.derivative(f, x)
1417
15- Compute and return :math: `f'(x)`.
18+ Compute and return :math: `f'(x)`. If ``x `` is a ``Tuple ``, ``f `` will be
19+ called as ``f(x...) ``, and a ``Tuple `` of derivatives will be returned.
1620
1721Gradients of :math: `f(x) : \mathbb {R}^{n_1 } \times \dots \times \mathbb {R}^{n_k} \to \mathbb {R}`
1822------------------------------------------------------------------------------------------------
1923
2024Use ``ForwardDiff.gradient `` to differentiate functions of the form ``f(::AbstractArray)::Real ``.
2125
22- .. function :: ForwardDiff.gradient!(out, f, x, cfg = ForwardDiff.GradientConfig(x))
26+ .. function :: ForwardDiff.gradient!(out, f, x, cfg = ForwardDiff.GradientConfig(f, x))
2327
2428 Compute :math: `\nabla f(\vec {x})`, storing the output in ``out ``. It is highly advised
2529 to preallocate ``cfg `` yourself (see the `AbstractConfig
@@ -34,23 +38,23 @@ Jacobians of :math:`f(x) : \mathbb{R}^{n_1} \times \dots \times \mathbb{R}^{n_k}
3438
3539Use ``ForwardDiff.jacobian `` to differentiate functions of the form ``f(::AbstractArray)::AbstractArray ``.
3640
37- .. function :: ForwardDiff.jacobian!(out, f, x, cfg = ForwardDiff.JacobianConfig(x))
41+ .. function :: ForwardDiff.jacobian!(out, f, x, cfg = ForwardDiff.JacobianConfig(f, x))
3842
3943 Compute :math: `\mathbf {J}(f)(\vec {x})`, storing the output in ``out ``. It is highly
4044 advised to preallocate ``cfg `` yourself (see the `AbstractConfig
4145 <basic_api.html#the-abstractconfig-types> `_ section below).
4246
43- .. function :: ForwardDiff.jacobian!(out, f!, y, x, cfg = ForwardDiff.JacobianConfig(y, x))
47+ .. function :: ForwardDiff.jacobian!(out, f!, y, x, cfg = ForwardDiff.JacobianConfig(f!, y, x))
4448
4549 Compute :math: `\mathbf {J}(f)(\vec {x})`, where :math: `f(\vec {x})` can be called as
4650 ``f!(y, x) `` such that the output of :math: `f(\vec {x})` is stored in ``y ``. The output
4751 matrix is stored in ``out ``.
4852
49- .. function :: ForwardDiff.jacobian(f, x, cfg = ForwardDiff.JacobianConfig(x))
53+ .. function :: ForwardDiff.jacobian(f, x, cfg = ForwardDiff.JacobianConfig(f, x))
5054
5155 Compute and return :math: `\mathbf {J}(f)(\vec {x})`.
5256
53- .. function :: ForwardDiff.jacobian(f!, y, x, cfg = ForwardDiff.JacobianConfig(y, x))
57+ .. function :: ForwardDiff.jacobian(f!, y, x, cfg = ForwardDiff.JacobianConfig(f!, y, x))
5458
5559 Compute and return :math: `\mathbf {J}(f)(\vec {x})`, where :math: `f(\vec {x})` can be
5660 called as ``f!(y, x) `` such that the output of :math: `f(\vec {x})` is stored in ``y ``.
@@ -60,13 +64,13 @@ Hessians of :math:`f(x) : \mathbb{R}^{n_1} \times \dots \times \mathbb{R}^{n_k}
6064
6165Use ``ForwardDiff.hessian `` to perform second-order differentiation on functions of the form ``f(::AbstractArray)::Real ``.
6266
63- .. function :: ForwardDiff.hessian!(out, f, x, cfg = ForwardDiff.HessianConfig(x))
67+ .. function :: ForwardDiff.hessian!(out, f, x, cfg = ForwardDiff.HessianConfig(f, x))
6468
6569 Compute :math: `\mathbf {H}(f)(\vec {x})`, storing the output in ``out ``. It is highly
6670 advised to preallocate ``cfg `` yourself (see the `AbstractConfig
6771 <basic_api.html#the-abstractconfig-types> `_ section below).
6872
69- .. function :: ForwardDiff.hessian(f, x, cfg = ForwardDiff.HessianConfig(x))
73+ .. function :: ForwardDiff.hessian(f, x, cfg = ForwardDiff.HessianConfig(f, x))
7074
7175 Compute and return :math: `\mathbf {H}(f)(\vec {x})`.
7276
@@ -77,66 +81,63 @@ For the sake of convenience and performance, all "extra" information used by For
7781API methods is bundled up in the ``ForwardDiff.AbstractConfig `` family of types. Theses
7882types allow the user to easily feed several different parameters to ForwardDiff's API
7983methods, such as `chunk size <advanced_usage.html#configuring-chunk-size >`_, work buffers,
80- multithreading configurations, and perturbation seed configurations.
84+ and perturbation seed configurations.
8185
8286ForwardDiff's basic API methods will allocate these types automatically by default,
8387but you can drastically reduce memory usage if you preallocate them yourself.
8488
85- Note that for all constructors below, the chunk size ``N `` may be explictly provided as a
86- type parameter, or omitted, in which case ForwardDiff will automatically select a chunk size
87- for you. However, it is highly recomended to `specify the chunk size manually when possible
89+ Note that for all constructors below, the chunk size ``N `` may be explictly provided,
90+ or omitted, in which case ForwardDiff will automatically select a chunk size for you.
91+ However, it is highly recomended to `specify the chunk size manually when possible
8892<advanced_usage.html#configuring-chunk-size> `_.
8993
90- .. function :: ForwardDiff.GradientConfig{N}(x)
94+ Note also that configurations constructed for a specific function ``f `` cannot
95+ be reused to differentiate other functions (though can be reused to differentiate
96+ ``f `` at different values). To construct a configuration which can be reused to
97+ differentiate any function, you can pass ``nothing `` as the function argument.
98+ While this is more flexible, this decreases ForwardDiff's ability to catch
99+ and prevent `perturbation confusion `_.
91100
92- Construct a ``GradientConfig `` instance based on the type and shape of the input vector
93- ``x ``. The returned ``GradientConfig `` instance contains all the work buffers required
94- by ForwardDiff's gradient/Jacobian methods. If taking the Jacobian of a target function
95- with the form ``f!(y, x) ``, use the constructor ``ForwardDiff.GradientConfig{N}(y, x) ``
96- instead.
101+ .. function :: ForwardDiff.GradientConfig(f, x, chunk::ForwardDiff.Chunk{N} = Chunk(x))
102+
103+ Construct a ``GradientConfig `` instance based on the type of ``f `` and
104+ type/shape of the input vector ``x ``. The returned ``GradientConfig ``
105+ instance contains all the work buffers required by ForwardDiff's gradient
106+ methods.
97107
98108 This constructor does not store/modify ``x ``.
99109
100- .. function :: ForwardDiff.JacobianConfig{N}(x )
110+ .. function :: ForwardDiff.JacobianConfig(f, x, chunk::ForwardDiff.Chunk {N} = Chunk(x) )
101111
102- Exactly like ``ForwardDiff.GradientConfig{N}(x) ``, but returns a `JacobianConfig `
103- instead.
112+ Exactly like the ``GradientConfig `` constructor, but returns a ``JacobianConfig `` instead.
104113
105- .. function :: ForwardDiff.JacobianConfig{N}( y, x)
114+ .. function :: ForwardDiff.JacobianConfig(f!, y, x, chunk::ForwardDiff.Chunk{N} = Chunk(x) )
106115
107- Construct a ``JacobianConfig `` instance based on the type and shape of the output vector
108- ``y `` and the input vector ``x ``. The returned ``JacobianConfig `` instance contains all
109- the work buffers required by ``ForwardDiff.jacobian ``/``ForwardDiff.jacobian! `` with a
110- target function of the form ``f!(y, x) ``.
116+ Construct a ``JacobianConfig `` instance based on the type of ``f! ``, and the
117+ types/shapes of the output vector ``y `` and the input vector ``x ``. The
118+ returned ``JacobianConfig `` instance contains all the work buffers required
119+ by ``ForwardDiff.jacobian ``/``ForwardDiff.jacobian! `` when the target
120+ function takes the form ``f!(y, x) ``.
111121
112122 This constructor does not store/modify ``y `` or ``x ``.
113123
114- .. function :: ForwardDiff.HessianConfig{N}(x )
124+ .. function :: ForwardDiff.HessianConfig(f, x, chunk::ForwardDiff.Chunk {N} = Chunk(x) )
115125
116- Construct a ``HessianConfig `` instance based on the type and shape of the input vector
117- ``x ``. The returned ``HessianConfig `` instance contains all the work buffers required
118- by ForwardDiff's Hessian methods. If using
119- ``ForwardDiff.hessian!(out::DiffBase.DiffResult, args... ) ``, use the constructor
120- ``ForwardDiff.HessianConfig{N}( out, x) `` instead.
126+ Construct a ``HessianConfig `` instance based on the type of `` f `` and
127+ type/shape of the input vector ``x ``. The returned ``HessianConfig `` instance contains
128+ all the work buffers required by ForwardDiff's Hessian methods. If using
129+ ``ForwardDiff.hessian!(out::DiffBase.DiffResult, f, x ) ``, use the constructor
130+ ``ForwardDiff.HessianConfig(f, out, x, chunk ) `` instead.
121131
122132 This constructor does not store/modify ``x ``.
123133
124- .. function :: ForwardDiff.HessianConfig{N}( out::DiffBase.DiffResult, x)
134+ .. function :: ForwardDiff.HessianConfig(f, out::DiffBase.DiffResult, x, chunk::ForwardDiff.Chunk{N} = Chunk(x) )
125135
126- Construct an ``HessianConfig `` instance based on the type and shape of the storage in
127- ``out `` and the input vector ``x ``. The returned ``HessianConfig `` instance contains
128- all the work buffers required by `` ForwardDiff.hessian!(out::DiffBase.DiffResult,
129- args...) ``.
136+ Construct an ``HessianConfig `` instance based on the type of `` f ``, types/ storage
137+ in ``out ``, and type/shape of the input vector ``x ``. The returned ``HessianConfig ``
138+ instance contains all the work buffers required by
139+ `` ForwardDiff.hessian!(out::DiffBase.DiffResult, args...) ``.
130140
131141 This constructor does not store/modify ``out `` or ``x ``.
132142
133- .. function :: ForwardDiff.MultithreadConfig(cfg::AbstractConfig)
134-
135- Wrap the given ``cfg `` in a ``MultithreadConfig `` instance, which can then be passed to
136- gradient or Hessian methods in order to enable experimental multithreading. Jacobian
137- methods do not yet support multithreading.
138-
139- Note that multithreaded ForwardDiff API methods will attempt to use all available
140- threads. In the future, once Julia exposes more fine-grained threading primitives,
141- a ``MultithreadConfig `` constructor may be added which takes in a user-provided subset
142- of thread IDs instead of using all available threads.
143+ .. _`perturbation confusion` : https://github.com/JuliaDiff/ForwardDiff.jl/issues/83
0 commit comments