This class implements the computation rules described in Dan Kalman's paper Doubly Recursive Multivariate Automatic Differentiation, Mathematics Magazine, vol. 75, no. 3, June 2002. However, in order to avoid performances bottlenecks, the recursive rules are "compiled" once in an unfold form. This class does this recursion unrolling and stores the computation rules as simple loops with pre-computed indirection arrays.
This class maps all derivative computation into single dimension arrays that hold the value and partial derivatives. The class does not hold these arrays, which remains under the responsibility of the caller. For each combination of number of free parameters and derivation order, only one compiler is necessary, and this compiler will be used to perform computations on all arrays provided to it, which can represent hundreds or thousands of different parameters kept together with all theur partial derivatives.
The arrays on which compilers operate contain only the partial derivatives together
with the 0th derivative, i.e. the value. The partial derivatives are stored in
a compiler-specific order, which can be retrieved using methods getPartialDerivativeIndex and getPartialDerivativeOrders(int). The value is guaranteed to be stored as the first element
(i.e. the getPartialDerivativeIndex method returns
0 when called with 0 for all derivation orders and getPartialDerivativeOrders returns an array filled with 0 when called with 0 as the index).
Note that the ordering changes with number of parameters and derivation order. For example given 2 parameters x and y, df/dy is stored at index 2 when derivation order is set to 1 (in this case the array has three elements: f, df/dx and df/dy). If derivation order is set to 2, then df/dy will be stored at index 3 (in this case the array has six elements: f, df/dx, df/dxdx, df/dy, df/dxdy and df/dydy).
Given this structure, users can perform some simple operations like adding, subtracting or multiplying constants and negating the elements by themselves, knowing if they want to mutate their array or create a new array. These simple operations are not provided by the compiler. The compiler provides only the more complex operations between several arrays.
This class is mainly used as the engine for scalar variable DerivativeStructure.
It can also be used directly to hold several variables in arrays for more complex data
structures. User can for example store a vector of n variables depending on three x, y
and z free parameters in one array as follows:
// parameter 0 is x, parameter 1 is y, parameter 2 is z
int parameters = 3;
DSCompiler compiler = DSCompiler.getCompiler(parameters, order);
int size = compiler.getSize();
// pack all elements in a single array
double[] array = new double[n * size];
for (int i = 0; i < n; ++i) {
// we know value is guaranteed to be the first element
array[i * size] = v[i];
// we don't know where first derivatives are stored, so we ask the compiler
array[i * size + compiler.getPartialDerivativeIndex(1, 0, 0) = dvOnDx[i][0];
array[i * size + compiler.getPartialDerivativeIndex(0, 1, 0) = dvOnDy[i][0];
array[i * size + compiler.getPartialDerivativeIndex(0, 0, 1) = dvOnDz[i][0];
// we let all higher order derivatives set to 0
}
Then in another function, user can perform some operations on all elements stored in the single array, such as a simple product of all variables:
// compute the product of all elements
double[] product = new double[size];
prod[0] = 1.0;
for (int i = 0; i < n; ++i) {
double[] tmp = product.clone();
compiler.multiply(tmp, 0, array, i * size, product, 0);
}
// value
double p = product[0];
// first derivatives
double dPdX = product[compiler.getPartialDerivativeIndex(1, 0, 0)];
double dPdY = product[compiler.getPartialDerivativeIndex(0, 1, 0)];
double dPdZ = product[compiler.getPartialDerivativeIndex(0, 0, 1)];
// cross derivatives (assuming order was at least 2)
double dPdXdX = product[compiler.getPartialDerivativeIndex(2, 0, 0)];
double dPdXdY = product[compiler.getPartialDerivativeIndex(1, 1, 0)];
double dPdXdZ = product[compiler.getPartialDerivativeIndex(1, 0, 1)];
double dPdYdY = product[compiler.getPartialDerivativeIndex(0, 2, 0)];
double dPdYdZ = product[compiler.getPartialDerivativeIndex(0, 1, 1)];
double dPdZdZ = product[compiler.getPartialDerivativeIndex(0, 0, 2)];
DerivativeStructure
| Modifier and Type | Field and Description |
|---|---|
| private static AtomicReference | compilers
Array of all compilers created so far. |
| private final int[][][] | compIndirection
Indirection arrays for function composition. |
| private final int[][] | derivativesIndirection
Indirection array for partial derivatives. |
| private final int[] | lowerIndirection
Indirection array of the lower derivative elements. |
| private final int[][][] | multIndirection
Indirection arrays for multiplication. |
| private final int | order
Derivation order. |
| private final int | parameters
Number of free parameters. |
| private final int[][] | sizes
Number of partial derivatives (including the single 0 order derivative element). |
| Access | Constructor and Description |
|---|---|
| private | DSCompiler(final int
number of free parameters parameters, final int derivation order order, final DSCompiler compiler for the value part valueCompiler, final DSCompiler compiler for the derivative part derivativeCompiler)Private constructor, reserved for the factory method |
| Modifier and Type | Method and Description |
|---|---|
| public void | acos(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
arc cosine the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute arc cosine of a derivative structure. |
| public void | acosh(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
inverse hyperbolic cosine the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute inverse hyperbolic cosine of a derivative structure. |
| public void | add(final double[]
array holding left hand side of addition lhs, final int offset of the left hand side in its array lhsOffset, final double[] array right hand side of addition rhs, final int offset of the right hand side in its array rhsOffset, final double[] array where result must be stored (it may be
one of the input arrays) result, final int offset of the result in its array resultOffset)Perform addition of two derivative structures. |
| public void | asin(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
arc sine the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute arc sine of a derivative structure. |
| public void | asinh(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
inverse hyperbolic sine the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute inverse hyperbolic sine of a derivative structure. |
| public void | atan(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
arc tangent the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute arc tangent of a derivative structure. |
| public void | atan2(final double[]
array holding the first operand y, final int offset of the first operand in its array yOffset, final double[] array holding the second operand x, final int offset of the second operand in its array xOffset, final double[] array where result must be stored (for
two arguments arc tangent the result array cannot
be the input array) result, final int offset of the result in its array resultOffset)Compute two arguments arc tangent of a derivative structure. |
| public void | atanh(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
inverse hyperbolic tangent the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute inverse hyperbolic tangent of a derivative structure. |
| public void | checkCompatibility(final DSCompiler
other compiler to check against instance compiler)Check rules set compatibility. |
| private static int[][][] | Returns: multiplication indirection arraynumber of free parameters parameters, final int derivation order order, final DSCompiler compiler for the value part valueCompiler, final DSCompiler compiler for the derivative part derivativeCompiler, final int[][] sizes array sizes, final int[][] derivatives indirection array derivativesIndirection)Compile the function composition indirection array. |
| private static int[][] | Returns: derivatives indirection arraynumber of free parameters parameters, final int derivation order order, final DSCompiler compiler for the value part valueCompiler, final DSCompiler compiler for the derivative part derivativeCompiler)Compile the derivatives indirection array. |
| private static int[] | Returns: lower derivatives indirection arraynumber of free parameters parameters, final int derivation order order, final DSCompiler compiler for the value part valueCompiler, final DSCompiler compiler for the derivative part derivativeCompiler)Compile the lower derivatives indirection array. |
| private static int[][][] | Returns: multiplication indirection arraynumber of free parameters parameters, final int derivation order order, final DSCompiler compiler for the value part valueCompiler, final DSCompiler compiler for the derivative part derivativeCompiler, final int[] lower derivatives indirection array lowerIndirection)Compile the multiplication indirection array. |
| private static int[][] | Returns: sizes arraynumber of free parameters parameters, final int derivation order order, final DSCompiler compiler for the value part valueCompiler)Compile the sizes array. |
| public void | compose(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array of value and derivatives of the function at
the current point (i.e. at f, final double[] operand[operandOffset]).array where result must be stored (for
composition the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute composition of a derivative structure by a function. |
| private static int | Returns: index of the partial derivative with the same characteristics in destination derivative structureindex of a partial derivative in source derivative structure index, final int number of free parameters in source derivative structure srcP, final int[][] derivatives indirection array for the source
derivative structure srcDerivativesIndirection, final int number of free parameters in destination derivative structure destP, final int derivation order in destination derivative structure destO, final int[][] sizes array for the destination derivative structure destSizes)Convert an index from one (parameters, order) structure to another. |
| public void | cos(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
cosine the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute cosine of a derivative structure. |
| public void | cosh(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
hyperbolic cosine the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute hyperbolic cosine of a derivative structure. |
| public void | divide(final double[]
array holding left hand side of division lhs, final int offset of the left hand side in its array lhsOffset, final double[] array right hand side of division rhs, final int offset of the right hand side in its array rhsOffset, final double[] array where result must be stored (for
division the result array cannot be one of
the input arrays) result, final int offset of the result in its array resultOffset)Perform division of two derivative structures. |
| public void | exp(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
exponential the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute exponential of a derivative structure. |
| public void | expm1(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
exponential the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute exp(x) - 1 of a derivative structure. |
| public static DSCompiler | Returns: cached rules setnumber of free parameters parameters, int derivation order order)Get the compiler for number of free parameters and order. |
| public int | |
| public int | |
| public int | Returns: index of the partial derivativederivation orders with respect to each parameter orders)Get the index of a partial derivative in the array. |
| private static int | Returns: index of the partial derivativenumber of free parameters parameters, final int derivation order order, final int[][] sizes array sizes, final int... derivation orders with respect to each parameter
(the lenght of this array must match the number of parameters) orders)Get the index of a partial derivative in an array. |
| public int[] | Returns: orders derivation orders with respect to each parameterof the partial derivative index)Get the derivation orders for a specific index in the array. |
| public int | Returns: array size required for holding partial derivatives dataGet the array size required for holding partial derivatives data. |
| public void | linearCombination(final double
first scale factor a1, final double[] first base (unscaled) component c1, final int offset of first operand in its array offset1, final double second scale factor a2, final double[] second base (unscaled) component c2, final int offset of second operand in its array offset2, final double[] array where result must be stored (it may be
one of the input arrays) result, final int offset of the result in its array resultOffset)Compute linear combination. |
| public void | linearCombination(final double
first scale factor a1, final double[] first base (unscaled) component c1, final int offset of first operand in its array offset1, final double second scale factor a2, final double[] second base (unscaled) component c2, final int offset of second operand in its array offset2, final double third scale factor a3, final double[] third base (unscaled) component c3, final int offset of third operand in its array offset3, final double[] array where result must be stored (it may be
one of the input arrays) result, final int offset of the result in its array resultOffset)Compute linear combination. |
| public void | linearCombination(final double
first scale factor a1, final double[] first base (unscaled) component c1, final int offset of first operand in its array offset1, final double second scale factor a2, final double[] second base (unscaled) component c2, final int offset of second operand in its array offset2, final double third scale factor a3, final double[] third base (unscaled) component c3, final int offset of third operand in its array offset3, final double fourth scale factor a4, final double[] fourth base (unscaled) component c4, final int offset of fourth operand in its array offset4, final double[] array where result must be stored (it may be
one of the input arrays) result, final int offset of the result in its array resultOffset)Compute linear combination. |
| public void | log(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
logarithm the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute natural logarithm of a derivative structure. |
| public void | log10(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
base 10 logarithm the result array cannot be the input array) result, final int offset of the result in its array resultOffset)Computes base 10 logarithm of a derivative structure. |
| public void | log1p(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
shifted logarithm the result array cannot be the input array) result, final int offset of the result in its array resultOffset)Computes shifted logarithm of a derivative structure. |
| public void | multiply(final double[]
array holding left hand side of multiplication lhs, final int offset of the left hand side in its array lhsOffset, final double[] array right hand side of multiplication rhs, final int offset of the right hand side in its array rhsOffset, final double[] array where result must be stored (for
multiplication the result array cannot be one of
the input arrays) result, final int offset of the result in its array resultOffset)Perform multiplication of two derivative structures. |
| public void | pow(final double
number to exponentiate a, final double[] array holding the power operand, final int offset of the power in its array operandOffset, final double[] array where result must be stored (for
power the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute power of a double to a derivative structure. |
| public void | pow(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double power to apply p, final double[] array where result must be stored (for
power the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute power of a derivative structure. |
| public void | pow(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final int power to apply n, final double[] array where result must be stored (for
power the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute integer power of a derivative structure. |
| public void | pow(final double[]
array holding the base x, final int offset of the base in its array xOffset, final double[] array holding the exponent y, final int offset of the exponent in its array yOffset, final double[] array where result must be stored (for
power the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute power of a derivative structure. |
| public void | remainder(final double[]
array holding left hand side of remainder lhs, final int offset of the left hand side in its array lhsOffset, final double[] array right hand side of remainder rhs, final int offset of the right hand side in its array rhsOffset, final double[] array where result must be stored (it may be
one of the input arrays) result, final int offset of the result in its array resultOffset)Perform remainder of two derivative structures. |
| public void | rootN(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final int order of the root n, final double[] array where result must be stored (for
nth root the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute nth root of a derivative structure. |
| public void | sin(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
sine the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute sine of a derivative structure. |
| public void | sinh(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
hyperbolic sine the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute hyperbolic sine of a derivative structure. |
| public void | subtract(final double[]
array holding left hand side of subtraction lhs, final int offset of the left hand side in its array lhsOffset, final double[] array right hand side of subtraction rhs, final int offset of the right hand side in its array rhsOffset, final double[] array where result must be stored (it may be
one of the input arrays) result, final int offset of the result in its array resultOffset)Perform subtraction of two derivative structures. |
| public void | tan(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
tangent the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute tangent of a derivative structure. |
| public void | tanh(final double[]
array holding the operand operand, final int offset of the operand in its array operandOffset, final double[] array where result must be stored (for
hyperbolic tangent the result array cannot be the input
array) result, final int offset of the result in its array resultOffset)Compute hyperbolic tangent of a derivative structure. |
| public double | Returns: value of the Taylor expansion at x + Δx, y + Δy, ...array holding the derivative structure ds, final int offset of the derivative structure in its array dsOffset, final double... parameters offsets (Δx, Δy, ...) delta)Evaluate Taylor expansion of a derivative structure. |