Functions for differentiation
- proc initdual(x: real)
Initializes the input to the appropriate dual number to evalute the derivative.
- Arguments
x :
real or [dom] real
– point where to evaluate the derivative- Returns
If
x
is a real number, then it is initialized to \(x+\epsilon\). Ifx
is a vector of reals, it is initialized to the vector of multiduals \(\begin{bmatrix}x_1+\epsilon_1\\\vdots\\x_n+\epsilon_n\end{bmatrix}\).- Return type
dual
ifx
isreal
or[dom] multidual
ifx
is[dom] real
.
- proc initdual(x: [?D] ?t, v: [D] ?s)
Given a vector
x
and a vectorv
, creates a vector of duals \([x_i + \epsilon v_i]\). Used to compute directional derivative and Jacobian-vector product (JVP).- Arguments
x :
[D] real
– point where to evaluate the directional derivative / JVPv :
[D] real
– direction
- Returns
Vector of dual numbers
- Return type
[D] dual
- proc derivative(f, x: real)
Evaluates the derivative of
f
atx
.- Arguments
f :
Function
– Function, note that this must be a concrete function.x :
real
– point at which the derivative is evaluated
- Returns
value of f’(x)
- Return type
real
Note that f must be a concrete function, if it’s written as a generic function, you can pass
derivative
a lambda as followsproc f(x) { return x**2 + 2*x + 1; } var dfx = derivative(lambda(x : dual){return f(x);}, 1.0); //outputs //4.0
- proc derivative(x: dual)
Extracts the derivative from a dual number.
- proc gradient(f, x: [?D] real)
Evaluates the gradient of
f
atx
.- Arguments
f :
Function
– Function, note that this must be a concrete function.x :
[ ] real
– point at which the gradient is evaluated
- Returns
value of \(\nabla f(x)\)
- Return type
[ ] real
Note that f must be a concrete function, if it’s written as a generic function, you can pass
gradient
a lambda as followsproc h(x) { return x[0] ** 2 + 3 * x[0] * x[1]; } type D = [0..#2] multidual; // domain for the lambda function var dh = gradient(lambda(x : D){return h(x);}, [1.0, 2.0]); //outputs //8.0 3.0
- proc gradient(x: multidual)
Extracts the gradient from a multidual number.
- proc jacobian(f, x: [?D])
Evaluates the jacobian of
f
atx
.- Arguments
f :
Function
– Function, note that this must be a concrete function.x :
[ ] real
– point at which the jacobian is evaluated
- Returns
value of \(J_f\)
- Return type
[Dout, Din] real
Note that
f
must be a concrete function, if it’s written as a generic function, you can passjacobian
a lambda as followsproc F(x) { return [x[0] ** 2 + x[1] + 1, x[0] + x[1] ** 2 + x[0] * x[1]]; } type D = [0..#2] multidual; // domain for the lambda function var Jf = jacobian(lambda(x : D){return F(x);}, [1.0, 2.0]); writeln(Jf, "\n"); //outputs //2.0 1.0 //3.0 5.0
- proc jacobian(x: [?D] multidual)
Extracts the Jacobian from an array of multidual numbers.
- proc value(x)
Extracts the function value.
- Arguments
x :
dual, multidual or [] multidual.
– result of computations using dual numbers.
- proc directionalDerivative(x: dual)
Extracts the directional derivative from a dual number.
- proc directionalDerivative(f, x: [?D], v: [D])
Computes the directional derivative of
f
atx
in the direction ofv
.
- proc jvp(x: [] dual)
Extracts the Jacobian-vector product from a vector of dual numbers.
- proc jvp(f, x: [?D], v: [D])
Computes the Jacobian-vector product of the Jacobian of
f
atx
and vectorv
.