Diffcode Overview
This toolbox enables Scilab code differentiation using operators and primitive functions overloading.
Given a Scilab code computing a variable y depending on a variable x and a direction dx it allow evaluation of y together with the directional derivative Grad(y)*dx.
It is far from complete, but supports all basic computations including matrix inversion.
Supported operators:
// Access insertion extraction // Comparison < > <= >= == <> // Arithmetic + - * .* \ .\ / ./ ^ .^ .*. [] '
Supported primitives functions:
// Elementary functions abs acos acosh asin asinh atan atanh cos cosh cotg exp log log10 sin sinh sqrt tan tanh // Conversion string // Matrix functions cumsum cumprod conj det eye inv matrix maxi mini ones pinv prod size sum trace zeros
It is easy to add new overloading functions in the macros directory.
In the following example, we compute of sin(t^2) and its derivative. The expression depends on only one variable so the direction is set to 1.
It may happen that the function that we use is not
defined for automatic differentiation.
In this case, a message error is printed, stating that
some function is undefined.
In the following example, we compute the matrix-sign of
a value-derivative pair.
The signm
function apparently internaly
uses the schur
function,
which has not been defined for automatic differentiation.
-->vdv = diffcode_der(0,1) vdv = TDFFCD: ======= v: 0 dv: 1 -->signm(vdv) !--error 246 Function not defined for given argument type(s), check arguments or define function %TDFFCD_schur for overloading. at line 24 of function called by : signm(vdv) | ![]() | ![]() |
In this case, all we have to do is to create the missing functions, by implementing the usual differentiation rules. This may be difficult for high level functions, but is straightforward for most common functions.
"The matrix cookbook", Kaare Brandt Petersen, Michael Syskind Pedersen, 2008
"Matrix Differential Calculus with Applications in Statistics and Econometrics", Jan R. Magnus, Heinz Neudecker, 2007
"Symbolic matrix derivatives", Paul S. Dwyer, M.S. Macphail, Ann. Math. Statist. Volume 19, Number 4 (1948), 517-534.