API reference
Public
ImplicitDifferentiation — Module
ImplicitDifferentiationA Julia package for automatic differentiation of implicit functions.
Its main export is the type ImplicitFunction.
Main entry point
ImplicitDifferentiation.ImplicitFunction — Type
ImplicitFunctionWrapper for an implicit function defined by a solver and a set of conditions which the solution satisfies.
An ImplicitFunction object behaves like a function, with the following signature:
y, z = (implicit::ImplicitFunction)(x, args...)The first output y is differentiable with respect to the first argument x, while the second output z (a byproduct of the solve) and the following positional arguments args are considered constant.
When a derivative is queried, the Jacobian of y(x) is computed using the implicit function theorem applied to the conditions c(x, y) (we ignore z for concision):
∂₂c(x, y(x)) * ∂y(x) = -∂₁c(x, y(x))This requires solving a linear system A * J = -B where A = ∂₂c, B = ∂₁c and J = ∂y.
Constructor
ImplicitFunction(
solver,
conditions;
representation=OperatorRepresentation(),
linear_solver=IterativeLinearSolver(),
backends=nothing,
strict=Val(true),
)Positional arguments
solver: a callable returning(x, args...) -> (y, z)wherezis an arbitrary byproduct of the solve. Bothxandymust be subtypes ofAbstractArray, whilezandargscan be anything.conditions: a callable returning a vector of optimality conditions(x, y, z, args...) -> c, must be compatible with automatic differentiation.
Keyword arguments
representation: defines how the partial JacobianAof the conditions with respect to the output is represented. It can be eitherMatrixRepresentationorOperatorRepresentation.linear_solver: specifies how the linear systemA * J = -Bwill be solved in the implicit function theorem. It must be a subtype ofAbstractSolver.backends::AbstractADType: specifies how theconditionswill be differentiated with respect toxandy. It can be either,nothing, which means that the external autodiff system will be used, or a named tuple(; x=AutoSomething(), y=AutoSomethingElse())of backend objects from ADTypes.jl.strict::Val: specifies whether preparation inside DifferentiationInterface.jl should enforce a strict match between the primal variables and the provided tangents.
Settings
ImplicitDifferentiation.MatrixRepresentation — Type
MatrixRepresentationSpecify that the matrix A involved in the implicit function theorem should be represented explicitly, with all its coefficients.
See also
ImplicitDifferentiation.OperatorRepresentation — Type
OperatorRepresentationSpecify that the matrix A involved in the implicit function theorem should be represented lazily, as a function.
See also
ImplicitDifferentiation.AbstractSolver — Type
AbstractSolverAbstract supertype for linear solvers to be used within an ImplicitFunction.
Interface
The only requirement on any concrete subtype MySolver is that it should be callable, with the following signature:
(s::MySolver)(A, Aᵀ, x0, b)This method should solve the linear system Ax = b, possibly using the transpose Aᵀ or starting from an initial point x0. Depending on the representation chosen for the ImplicitFunction, the arguments A and Aᵀ might be either matrix-like or functional objects.
Implementations
This package provides three default implementations:
ImplicitDifferentiation.IterativeLinearSolver — Type
IterativeLinearSolver <: AbstractSolverSpecify that linear systems Ax = b should be solved with an iterative method.
Can only be used when the solver and the conditions both output AbstractArrays with the same type and length.
See also
ImplicitDifferentiation.IterativeLeastSquaresSolver — Type
IterativeLeastSquaresSolver <: AbstractSolverSpecify that linear systems Ax = b should be solved with an iterative least-squares method.
Can be used when the solver and the conditions output AbstractArrays with different types or different lengths.
See also
ImplicitDifferentiation.DirectLinearSolver — Type
DirectLinearSolver <: AbstractSolverSpecify that linear systems Ax = b should be solved with a direct method.
Can only be used when the solver and the conditions both output an AbstractVector.
Additionally, this solver requires a MatrixRepresentation of the matrix A. To do so, use the representation keyword of the ImplicitFunction constructor :
f = ImplicitFunction(
forward,
conditions;
solver = DirectLinearSolver(),
representation = MatrixRepresentation()
)See also
Internals
ImplicitDifferentiation.ImplicitFunctionPreparation — Type
ImplicitFunctionPreparationFields
prep_A: preparation forA(derivative of conditions with respect toy) in forward modeprep_Aᵀ: preparation forA(derivative of conditions with respect toy) in reverse modeprep_B: preparation forB(derivative of conditions with respect tox) in forward modeprep_Bᵀ: preparation forB(derivative of conditions with respect tox) in reverse mode
ImplicitDifferentiation.Switch12 — Type
Switch12Represent a function which behaves like f, except that the first and second arguments are switched: f(a1, a2, a3) = b becomes g(a2, a1, a3) = f(a1, a2, a3)
ImplicitDifferentiation.prepare_implicit — Method
prepare_implicit(
mode::ADTypes.AbstractMode,
implicit::ImplicitFunction,
x_prep,
args_prep...;
strict=Val(true)
)Uses the preparation mechanism from DifferentiationInterface.jl to speed up subsequent differentiated calls to implicit(x, args...) where (x, args...) are similar to (x_prep, args_prep...).
The mode argument is an object from ADTypes.jl that specifies whether the preparation should target ForwardMode, ReverseMode or both (ForwardOrReverseMode).