CPL
Page: (symbolic)Top, Next: (cpl)CHARbyCHAR, Prev: (parallel), Up: (cpl)Library Index

Symbolic differentiation

The symbolic.cpl library adds concurrent symbolic differentiation to CPL. Symbolic differentiation is seamlessly integrated in the compiler, and is performed at compile time: any desired symbolic derivatives are worked out during compilation, and inlined in the generated code. Contrary to most other symbolic-math environments, therefore, the purpose is not so much to generate user-readable derivatives as to generate (hopefully effective) inline code while keeping the visible code as close to pen-and-paper notation as possible. Symbolic manipulation operates upon and produces (cpl)INLINE FUNCTIONs. The INLINE FUNCTION declaration (with untyped arguments) operates similarly to #define:(cpl)C preprocessor: and is almost interchangeable with it, except that internally it places parentheses around arguments (thus guarding against operator precedence), and that its rhs must be a well-formed algebraic expression rather than an arbitrary string, and thus can end at ; like all CPL instructions, rather than at end-of-line only as for #define. An INLINE FUNCTION without arguments is also interchangeable with the == (cpl)Deferred assignment operator. Being a symbolic construction, an inline expression can contain yet undeclared variables; at the later position where the inline expression directly or indirectly appears in an executable statement, code for it is actually generated just as for ==. If all the required declarations have not appeared before this later point, a compilation error will turn up. The derivative of an INLINE FUNCTION with respect to a symbolic variable <svname> (which is not allowed to contain an underscore in its name) is denoted by either D_<svname>( <function name> ) or D( <function name> , <svname> ) When the latter form is used, <svname> may also be an array element (in the usual indexed notation). The derivative of an INLINE FUNCTION is another INLINE FUNCTION, and can be differentiated further, given a name or used in the definition of a new compound INLINE FUNCTION, or eventually appear in an executable statement. Higher derivatives and more general compound differential operators, which take an expression as their argument, can be defined in a similar syntax. For example: INLINE FUNCTION laplacian(f)=D_x(D_x(f))+D_y(D_y(f))+D_z(D_z(f)) A mechanism is provided to associate a derivative with a user-defined expression (or more generally to override automatic differentiation): if a variable named D_<svname>_<fname> is declared, it will be preferentially used as the value of the derivative of function <fname> with respect to variable <svname> wherever such differentiation is attempted. If no such variable is declared and <fname> was previously defined as an INLINE FUNCTION, its derivative will be automatically generated. If <fname> is yet undeclared, or was declared as an ordinary CPL VARIABLE (or CONSTANT) rather than as an INLINE FUNCTION, it will be treated as a constant the derivative of which is zero. As a side effect, variable and function names starting with D_ are reserved and should not be declared for other purposes when symbolic.cpl is in USE.