EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH

Real Expressions

To facilitate the definition of interdependent quantities, any real value and integer value can be entered as an arithmetic expression. When a value used in an expression is redefined by the user or changed in a matching process, the expression is reevaluated. Expression definitions may be entered in any order. MAD evaluates them in the correct order before it performs any computation. At evaluation time all operands used must have values assigned.

An expression is built from a combination of operator and operand, and it may contain random generators.

Operators in Arithmetic Expressions

An expression can be formed using the following operators:

Arithmetic operators

Ordinary functions

random number generators

in the above cases, "x" can be any expression, i.e. contain other functions, variable or constant expressions. To initialize the MAD-X random generator use the Eoption command.

table access functions

Operands in Arithmetic Expressions

An expression may contain the following operands:

Literal constants

Numerical values are entered like FORTRAN constants. Real values are accepted in INTEGER or REAL format. The use of a decimal exponent, marked by the letter D or E, is permitted.

Examples:

1, 10.35, 5E3, 314.1592E-2

Symbolic constants

MAD recognizes some mathematical and physical constants. Their names must not be used for user-defined labels.

Additional symbolic constants may be defined to simplify their repeated use in statements and expressions.

CONST name=constant-expression;
defines a real constant with the name given. An existing symbolic constant can be redefined, but it cannot change in a matching procedure.

Example:

const in = 0.0254;

Table 1: Predefined Symbolic Constants in MAD
mad name symbol value used unit
pi pi 4 * atan(1) 1
twopi 2 pi 2 * pi 1
degrad 180/pi 180 / pi deg/rad
raddeg pi/180 180 / pi rad/deg
e e exp(1) 1
emass m_e .510998902*10(-3) GeV
pmass m_p .938271998 GeV
mumass m_mu .1056583568 GeV
clight c 2.99792458*10**8 m/s
qelect elem. charge 1.602176462e-19 As

Parameter labels

Often a set of numerical values depends on a common variable parameter. Such a parameter must be defined as a global parameter. A global parameter always has a current value; however, this value may be re-evaluated or not, depending on the parameter definition:
x = a;
x is set to the current value of a and not changed, even if a changes. This makes assignments such as
x = x + 1;
perfectly valid (this replaces the old SET instruction). The definition of the deferred expression
x := a;
assign the current value of a to x every time x is used, i.e. it is re-evaluated using the latest value of a; therefore,
x := x + 1;
results in an infinite loop (!) when x is used (error abort). Of course, the following definitions are equivalent:
x = 0.1;
x := 0.1;

When such a parameter is used in an expression, MAD uses the current value of the parameter if the expression is deferred:

Example:

x:=1.0;
d1: drift,l = x;
d2: drift,l := 2.0 - x;
When the value of X is changed, the length of the drift d1 remains unchanged, that of d2 is recalculated.

Element or command attributes

In arithmetic expressions the attributes of physical elements or commands can occur as operands. They are named respectively by
element-name->attribute-name
command-name->attribute-name
Values are assigned to attributes in element definitions or commands.

Example:

D1: DRIFT,L=1.0;
D2: DRIFT,L=2.0-D1->L;
D1->L refers to the length L of the drift space D1.

Expressions and Random Values

The definition of random machine imperfections requires evaluation of expressions containing random functions. These are evaluated like any other expression when the expression is used, i.e. only once if a "=" assignment refers to it, or every time if the assignment is ":="; this latter case is used by the error generation routines.

Example:

a := 3*ranf();
Every time a is used, it gets a random value assigned from a uniform distribution between 0 and 3.
error:  ealign,range,dx:=sigma*gauss();
All elements in range are assigned independent random displacements sampled from a Gaussian distribution with standard deviation sigma.

hansg, May 8, 2001