source: PSPA/madxPSPA/doc/usrguide/Introduction/expression.html @ 430

Last change on this file since 430 was 430, checked in by touze, 11 years ago

import madx-5.01.00

File size: 10.1 KB
Line 
1<head>
2<title>Expressions</title>
3<!-- Changed by: Chris ISELIN, 27-Mar-1997 -->
4<!-- Changed by: Hans Grote, 25-Sep-2002 -->
5</head>
6
7<body bgcolor="#ffffff">
8
9<center>
10EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
11<IMG SRC="http://cern.ch/madx/icons/mx7_25.gif" align=right>
12<h2><a name=expression> Real Expressions</a></h2>
13</center>
14
15To facilitate the definition of interdependent quantities,
16any real value and integer value can be entered as an arithmetic expression.
17When a value used in an expression is redefined by the user
18or changed in a matching process, the expression is reevaluated.
19Expression definitions may be entered in any order.
20MAD evaluates them in the correct order before it performs
21any computation.
22At evaluation time all operands used must have values assigned.
23<p>
24An expression is built from a combination of
25<a href="#operator">operator</a> and
26<a href="#operand">operand</a>,
27and it may contain <a href="#random">random generators</a>.
28
29<h2><a name=operator> Operators in Arithmetic Expressions</a></h2>
30An expression can be formed using the following operators:
31<H3>Arithmetic operators</H3>
32<UL>
33<LI>+ Addition,
34<LI>- Subtraction,
35<LI>* Multiplication,
36<LI>/ Division,
37<LI>^ Exponentiation.
38</UL>
39<H3><a name=function>Ordinary functions</a></H3>
40<UL>
41<li>sqrt(x) square root,
42<li>log(x) natural logarithm,
43<li>log10(x) logarithm base 10,
44<li>exp(x) exponential,
45<li>sin(x) trigonometric sine,
46<li>cos(x) trigonometric cosine,
47<li>tan(x) trigonometric tangent,
48<li>asin(x) arc sine,
49<li>acos(x) arc cosine,
50<li>atan(x) arc tangent,
51<li>sinh(x) hyperbolic sine,
52<li>cosh(x) hyperbolic cosine,
53<li>tanh(x) hyperbolic tangent,
54<li>abs(x) absolute value;
55</ul>
56<h3><a name=random>random number generators</a></h3>
57<ul>
58<li>ranf() random number,
59uniformly distributed in [0,1],
60<li>gauss() random number,
61gaussian distribution with unit standard deviation,
62<li>tgauss(x) random number,
63gaussian distribution with unit standard deviation,
64truncated at x standard deviations;
65</ul>
66in the above cases, "x" can be any expression, i.e. contain other
67functions, variable or constant expressions. To initialize the MAD-X
68random generator use the <a
69href="../error/error_option.html#EOPTION">Eoption command</a>.
70<h3><a name=table>table access functions</a></h3>
71<ul>
72<li>table(x,z): accesses value of the named table column "z" of table
73"x"; example: table(summ,q1) returns the hor. tune of the Twiss
74summary table "summ". <li>table(x,y,z): accesses value of the named
75table column "z" for element "y" (first table row with that name) of
76table "x"; example: table(twiss,mb.12,betx) returns the beta_x at
77element mb.12 from the Twiss table "twiss".
78<font color=0000ff> When the element is called with its proper name,
79as in the example above, the value is returned at the first occurrence
80of the element of this name. If the value is needed at a occurrence
81number: NNN, then "[NNN]" has to be appended to the name, e.g. in the
82above example one obtains the betx of the 23th occurrence of the
83element "mb.12" by changing the example to:
84"table(twiss,mb.12[23],betx)". Mind you that the old, but little known,
85form: "table(twiss,mb.12->23,betx)" continues to work. Lastly, if NNN
86exceeds the maximum occurrence number the return value is arbitrarily
87small.
88</font>
89<font color=ff0000>
90<p>Beware:
91<li> Unnamed Drifts are not included in the table name database, so
92as to speed up the search for "real" elements. Therefore, those
93unnamed drifts cannot be found. However, named drifts can be searched
94for.
95<li> Due to the importance of finding elements in the table for a
96proper functioning of the MAD-X runs, the programs throws a
97"fatal_error" if an element cannot be found in the table.
98</font>
99 <p> There is a second option of this function with 3 entries
100 <li>table(x,z,N_row): accesses the value of the named table column
101 "z" at the "N_row" number of rows of table "x" (row numbers start at
102 1); example: table(twiss,betx,370) returns the beta_x at row number
103 "370" of the Twiss table "twiss". The return value is zero if "N_row"
104 is outside of the allowed range.
105<p>
106Note that "N_row" has to be a <font color=ff0000>number</font> and not a
107<font color=ff0000>variable</font>. However, the <a
108 href="../control/special.html#macro">Macro facility</a> in MAD-X
109 allows one to use a variable instead.
110<p>
111A typical example could look like this, in fact the square root of
112betx (user defined variable myvar) is added to the twiss table:
113<p>
114<font color=0000ff><pre>
115myvar := sqrt(table(twiss,betx));
116select,flag=twiss,column=name,s,myvar,betx;
117twiss,file;
118</font></pre>
119<p>
120Or another arbitrary test case of adding k1l taken from the Twiss table:
121<p>
122Define macro:
123<p>
124<font color=0000ff><pre>
125mycrap(xx,yy,zz): macro = {myval = table(xx,yy,zz);};
126</font></pre>
127<p>
128Use macro in loop:
129<p>
130<font color=0000ff><pre>
131i = 0;
132incval = 0;
133while (i < 100) {
134value,i;
135exec,mycrap(twiss,k1l,$i);
136incval = incval + myval;
137value,i,myval,incval;               
138i = i + 1;
139}
140</font></pre>
141
142<H3><font color=ff0000>Features as of Version 3_03_50</font></H3>
143<li>
144<p>
145FILL,TABLE=t,ROW=n;
146<p>
147fill a table row with the present variable values. If ROW is negative
148or missing a new row is created. If ROW is greater than the number of
149rows, the last row is selected without creating a new row.
150<p>
151<li>
152  SETVARS,TABLE=t,ROW=n;
153<p>
154set variables according to the column names of the given table and
155the values of the given row. if ROW is negative, missing or greater
156than the number of rows, the last row is selected.
157<p>
158<li>
159An example can be found at: <a
160href="http://cern.ch/frs/mad-X_examples/special_features">Special
161Features</a>
162<p>
163<li>
164The length of a table can be determined by using the attribute
165"tablelength" via table("your_table_name", tablelength). This is
166useful when creating a table from existing ones. See an example at:
167<a href="../Introduction/select.html#screate">user table II</a> 
168<p>
169
170</ul>
171<h2><a name=operand> Operands in Arithmetic Expressions</a></h2>
172An expression may contain the following operands:
173
174<H3>Literal constants</H3>
175Numerical values are entered like FORTRAN constants.
176Real values are accepted in INTEGER or REAL format.
177The use of a decimal exponent, marked by the letter D or E,
178is permitted.
179<p>
180Examples:
181<pre>
1821, 10.35, 5E3, 314.1592E-2
183</pre>
184
185<H3><a name=constant>Symbolic constants</a></H3>
186MAD recognizes some
187<a href="#constant">mathematical and physical constants</a>.
188Their names must not be used for user-defined labels.
189<p>
190Additional symbolic constants may be defined to simplify
191their repeated use in statements and expressions.
192<pre>CONST name=constant-expression;</pre>
193defines a real constant with the name given.
194An existing symbolic constant can be redefined,
195but it cannot change in a matching procedure.
196<p>
197Example:
198<pre>
199const in = 0.0254;
200</pre>
201<p>
202<a name=constant>
203<table cols = 4 border=4 frame=box align=center rules=all>
204<caption><b>Table 1:</b> Predefined Symbolic Constants in MAD
205<tr>
206  <td><b>mad name</b></td>
207  <td><b>symbol</b></td>
208  <td><b>value used</b></td>
209  <td><b>unit</b></td>
210</tr>
211<tr>
212  <td>pi</td>
213  <td>pi</td>
214  <td>4 * atan(1)</td>
215  <td>1</td>
216</tr>
217<tr>
218  <td>twopi</td>
219  <td>2 pi</td>
220  <td>2 * pi</td>
221  <td>1</td>
222</tr>
223<tr>
224  <td>degrad</td>
225  <td>180/pi</td>
226  <td>180 / pi</td>
227  <td>deg/rad</td>
228</tr>
229<tr>
230  <td>raddeg</td>
231  <td>pi/180</td>
232  <td>180 / pi</td>
233  <td>rad/deg</td>
234</tr>
235<tr>
236  <td>e</td>
237  <td>e</td>
238  <td>exp(1)</td>
239  <td>1</td>
240</tr>
241<tr>
242  <td>emass</td>
243  <td>m_e</td>
244  <td>.510998902*10(-3)</td>
245  <td>GeV</td>
246</tr>
247<tr>
248  <td>pmass</td>
249  <td>m_p</td>
250  <td>.938271998</td>
251  <td>GeV</td>
252</tr>
253<tr>
254  <td>mumass</td>
255  <td>m_mu</td>
256  <td>.1056583568</td>
257  <td>GeV</td>
258</tr>
259<tr>
260  <td>clight</td>
261  <td>c</td>
262  <td>2.99792458*10**8</td>
263  <td>m/s</td>
264</tr>
265<tr>
266  <td>qelect</td>
267  <td>elem. charge</td>
268  <td>1.602176462e-19</td>
269  <td>As</td>
270</tr>
271</table>
272</a>
273
274<H3>Parameter labels</H3>
275Often a set of numerical values depends
276on a common variable parameter.
277Such a parameter must be defined as a
278<a href="parameter.html">global parameter</a>.
279A global parameter always has a current value; however, this value may
280be re-evaluated or not, depending on the parameter definition:
281<pre>
282x = a;
283</pre>
284x is set to the current value of a and not changed, even if a changes.
285This makes assignments such as
286<pre>
287x = x + 1;
288</pre>
289perfectly valid (this replaces the old SET instruction).
290The definition of the deferred expression
291<pre>
292x := a;
293</pre>
294assign the current value of a to x every time x is used, i.e. it is
295re-evaluated using the latest value of a; therefore,
296<pre>
297x := x + 1;
298</pre>
299results in an infinite loop (!) when x is used (error abort).
300Of course, the following definitions are equivalent:
301<pre>
302x = 0.1;
303x := 0.1;
304</pre>
305<p>
306When such a parameter is used in an expression,
307MAD uses the current value of the parameter if the expression is deferred:
308<p>
309Example:
310<pre>
311x:=1.0;
312d1: drift,l = x;
313d2: drift,l := 2.0 - x;
314</pre>
315When the value of X is changed,
316the length of the drift d1 remains unchanged, that of d2 is recalculated.
317
318<H3>Element or command attributes</H3>
319In arithmetic expressions the attributes of physical elements
320or commands can occur as operands.
321They are named respectively by
322<pre>
323element-name->attribute-name
324command-name->attribute-name
325</pre>
326Values are assigned to attributes in element definitions or commands.
327<p>
328Example:
329<pre>
330D1: DRIFT,L=1.0;
331D2: DRIFT,L=2.0-D1->L;
332</pre>
333D1->L refers to the length L of the drift space D1.
334
335<h2><a name=defer>Expressions and Random Values</a></h2>
336The definition of random machine imperfections requires evaluation
337of expressions containing random functions.
338These are evaluated like any other expression when the expression
339is used, i.e. only once if a "=" assignment refers to it, or every time
340if the assignment is ":="; this latter case is used by the error
341generation routines.
342<p>
343Example:
344<pre>
345a := 3*ranf();
346</pre>
347Every time a is used, it gets a random value assigned from a uniform
348distribution between 0 and 3.
349<pre>
350error:  ealign,range,dx:=sigma*gauss();
351</pre>
352All elements in range are assigned independent random
353displacements sampled from a Gaussian distribution
354with standard deviation sigma.
355<p>
356
357<address>
358<a href="http://www.cern.ch/Hans.Grote/hansg_sign.html">hansg</a>,
359May 8, 2001
360</address>
361
362</body>
Note: See TracBrowser for help on using the repository browser.