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
xis a real number, then it is initialized to \(x+\epsilon\). Ifxis 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:
dualifxisrealor[dom] multidualifxis[dom] real.
- proc initdual(x: [?D] ?t, v: [D] ?s)
Given a vector
xand 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
fatx.- 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
derivativea lambda as followsproc f(x) { return x**2 + 2*x + 1; } var dfx = derivative(proc(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
fatx.- 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
gradienta 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(proc(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
fatx.- 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
fmust be a concrete function, if it’s written as a generic function, you can passjacobiana 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(proc(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
fatxin 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
fatxand vectorv.