10. SYMDIFF¶
10.1. Overview¶
SYMDIFF
is a tool capable of evaluating derivatives of symbolic expressions. Using a natural syntax, it is possible to manipulate symbolic equations in order to aid derivation of equations for a variety of applications. It has been tailored for use within DEVSIM
.
10.2. Syntax¶
10.2.1. Variables and numbers¶
Variables and numbers are the basic building blocks for expressions. A variable is defined as any sequence of characters beginning with a letter and followed by letters, integer digits, and the _
character. Note that the letters are case sensitive so that a
and {A} are not the same variable. Any other characters are considered to be either mathematical operators or invalid, even if there is no space between the character and the rest of the variable name.
Examples of valid variable names are:
a, dog, var1, var_2
Numbers can be integer or floating point. Scientific notation is accepted as a valid syntax. For example:
1.0, 1.0e2, 3.4E4
10.2.2. Basic expressions¶
Expression 
Description 


Parenthesis for changing precedence 

Unary Plus 

Unary Minus 

Logical Not 

Exponentiation 

Multiplication 

Division 

Addition 

Subtraction 

Test Less 

Test Less Equal 

Test Greater 

Test Greater Equal 

Test Equality 

Test Inequality 

Logical And 

Logical Or 

Independent Variable 

Integer or decimal number 
In Table 10.1, the basic syntax for the language is presented. An expression may be composed of variables and numbers tied together with mathematical operations. Order of operations is from bottom to top in order of increasing precedence. Operators with the same level of precedence are contained within horizontal lines.
In the expression a + b * c
, the multiplication will be performed before the addition. In order to override this precedence, parenthesis may be used. For example, in (a + b) * c
, the addition operation is performed before the multiplication.
The logical operators are based on non zero values being true and zero values being false. The test operators are evaluate the numerical values and result in 0 for false and 1 for true.
It is important to note since values are based on double precision arithmetic, testing for equality with values other than 0.0 may yield unexpected results.
10.2.3. Functions¶
Function 
Description 


Inverse Hyperbolic Cosine 

Inverse Hyperbolic Sine 

Inverse Hyperbolic Tangent 

Hyperbolic Cosine 

Hyperbolic Sine 

Hyperbolic Tangent 

Bernoulli Function 

derivative of Bernoulli function 



exponent 

if test is true, then evaluate exp1, otherwise exp2 

if test is true, then evaluate exp, otherwise 0 

natural log 

maximum of the two arguments 

minimum of the two arguments 

take exp1 to the power of exp2 

sign function 

unit step function 

Extended precision addition of arguments 

Extended precision addition of arguments 

maximum of all the values over the entire region or interface 

minimum of all the values over the entire region or interface 

sum of all the values over the entire region or interface 
Function 
Description 


complementary error function 

derivative of complementary error function 

inverse complementary error function 

derivative of inverse complementary error function 

error function 

derivative error function 

inverse error function 

derivative of inverse error function 
Function 
Description 


Fermi Integral 

derivative of Fermi Integral 

inverse of the Fermi Integral 

derivative of InvFermi Integral 

GaussFermi Integral 


Derivative of GaussFermi Integral with respect to first argument 

Inverse GaussFermi Integral 

Derivative of Inverse GaussFermi Integral with respect to first argument 
In Table 10.2 are the built in functions of SYMDIFF
. Note that the pow
function uses the ,
operator to separate arguments. In addition an expression like pow(a,b+y)
is equivalent to an expression like a^(b+y)
. Both exp
and log
are provided since many derivative expressions can be expressed in terms of these two functions. It is possible to nest expressions within functions and viceversa. Table 10.3 lists the error functions, derivatives, and inverses. Table 10.4 lists the Fermi functions, and are based on the JoyceDixon Approximation [4]. The GaussFermi functions are listed in Table 10.5, based on [7].
10.2.4. Commands¶
Command 
Description 


Take derivative of 

Expand out all multiplications into a sum of products 

Print description of commands 

Get constant factor 

Get sign as 

Simplify as much as possible 

substitute 

Get value without constant scaling 

Get unsigned value 
Commands are shown in Table 10.6. While they appear to have the same form as functions, they are special in the sense that they manipulate expressions and are never present in the expression which results. For example, note the result of the following command
> diff(a*b, b)
a
10.2.5. User functions¶
Command 
Description 


Clears the name of a user function 

declare function name taking dummy arguments 

declare function name taking arguments 
Commands for specifying and manipulating user functions are listed in Table 10.7. They are used in order to define new user function, as well as the derivatives of the functions with respect to the user variables. For example, the following expression defines a function named f
which takes one argument.
> define(f(x), 0.5*x)
The list after the function protoype is used to define the derivatives with respect to each of the independent variables. Once defined, the function may be used in any other expression. In additions the any expression can be used as an arguments. For example:
> diff(f(x*y),x)
((0.5 * (x * y)) * y)
> simplify((0.5 * (x * y)) * y)
(0.5 * x * (y^2))
The chain rule is applied to ensure that the derivative is correct. This can be expressed as
The declare
command is required when the derivatives of two user functions are based on one another. For example:
> declare(cos(x))
cos(x)
> define(sin(x),cos(x))
sin(x)
> define(cos(x),sin(x))
cos(x)
When declared, a functions derivatives are set to 0, unless specified with a define command. It is now possible to use these expressions as desired.
> diff(sin(cos(x)),x)
(cos(cos(x)) * (sin(x)))
> simplify(cos(cos(x)) * (sin(x)))
(cos(cos(x)) * sin(x))
10.2.6. Macro assignment¶
The use of macro assignment allows the substitution of expressions into new expressions. Every time a command is successfully used, the resulting expression is assigned to a special macro definition, $_
.
In this example, the result of the each command is substituted into the next.
> a+b
(a + b)
> $_b
((a + b)  b)
> simplify($_)
a
In addition to the default macro definition, it is possible to specify a variable identifier by using the $
character followed by an alphanumeric string beginning with a letter. In addition to letters and numbers, a _
character may be used as well. A macro which has not previously assigned will implicitly use 0
as its value.
This example demonstrates the use of macro assignment.
> $a1 = a + b
(a + b)
> $a2 = a  b
(a  b)
> simplify($a1+$a2)
(2 * a)
10.3. Invoking SYMDIFF from DEVSIM¶
10.3.1. Equation parser¶
The devsim.symdiff()
should be used when defining new functions to the parser. Since you do not specify regions or interfaces, it considers all strings as being independent variables, as opposed to models. Model Commands presents commands which have the concepts of models. A ;
should be used to separate each statement.
This is a sample invocation from DEVSIM
% symdiff(expr="subst(dog * cat, dog, bear)")
(bear * cat)
10.3.2. Evaluating external math¶
The devsim.register_function()
is used to evaluate functions declared or defined within SYMDIFF
. A Python
procedure may then be used taking the same number of arguments. For example:
from math import cos
from math import sin
symdiff(expr="declare(sin(x))")
symdiff(expr="define(cos(x), sin(x))")
symdiff(expr="define(sin(x), cos(x))")
register_function(name="cos", nargs=1)
register_function(name="sin", nargs=1)
The cos
and sin
function may then be used for model evaluation. For improved efficiency, it is possible to create procedures written in C or C++ and load them into Python
.
10.3.3. Models¶
When used withing the model commands discussed in Model Commands, DEVSIM
has been extended to recognize model names in the expressions. In this situation, the derivative of a model named, model
, with respect to another model, variable
, is then model:variable
.
During the element assembly process, DEVSIM
evaluates all models of an equation together. While the expressions in models and their derivatives are independent, the software uses a caching scheme to ensure that redundant calculations are not performed. It is recommended, however, that users developing their own models investigate creating intermediate models in order to improve their understanding of the equations that they wish to be assembled.