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> |
---|
10 | EUROPEAN 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 | |
---|
15 | To facilitate the definition of interdependent quantities, |
---|
16 | any real value and integer value can be entered as an arithmetic expression. |
---|
17 | When a value used in an expression is redefined by the user |
---|
18 | or changed in a matching process, the expression is reevaluated. |
---|
19 | Expression definitions may be entered in any order. |
---|
20 | MAD evaluates them in the correct order before it performs |
---|
21 | any computation. |
---|
22 | At evaluation time all operands used must have values assigned. |
---|
23 | <p> |
---|
24 | An expression is built from a combination of |
---|
25 | <a href="#operator">operator</a> and |
---|
26 | <a href="#operand">operand</a>, |
---|
27 | and it may contain <a href="#random">random generators</a>. |
---|
28 | |
---|
29 | <h2><a name=operator> Operators in Arithmetic Expressions</a></h2> |
---|
30 | An 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, |
---|
59 | uniformly distributed in [0,1], |
---|
60 | <li>gauss() random number, |
---|
61 | gaussian distribution with unit standard deviation, |
---|
62 | <li>tgauss(x) random number, |
---|
63 | gaussian distribution with unit standard deviation, |
---|
64 | truncated at x standard deviations; |
---|
65 | </ul> |
---|
66 | in the above cases, "x" can be any expression, i.e. contain other |
---|
67 | functions, variable or constant expressions. To initialize the MAD-X |
---|
68 | random generator use the <a |
---|
69 | href="../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 |
---|
74 | summary table "summ". <li>table(x,y,z): accesses value of the named |
---|
75 | table column "z" for element "y" (first table row with that name) of |
---|
76 | table "x"; example: table(twiss,mb.12,betx) returns the beta_x at |
---|
77 | element mb.12 from the Twiss table "twiss". |
---|
78 | <font color=0000ff> When the element is called with its proper name, |
---|
79 | as in the example above, the value is returned at the first occurrence |
---|
80 | of the element of this name. If the value is needed at a occurrence |
---|
81 | number: NNN, then "[NNN]" has to be appended to the name, e.g. in the |
---|
82 | above example one obtains the betx of the 23th occurrence of the |
---|
83 | element "mb.12" by changing the example to: |
---|
84 | "table(twiss,mb.12[23],betx)". Mind you that the old, but little known, |
---|
85 | form: "table(twiss,mb.12->23,betx)" continues to work. Lastly, if NNN |
---|
86 | exceeds the maximum occurrence number the return value is arbitrarily |
---|
87 | small. |
---|
88 | </font> |
---|
89 | <font color=ff0000> |
---|
90 | <p>Beware: |
---|
91 | <li> Unnamed Drifts are not included in the table name database, so |
---|
92 | as to speed up the search for "real" elements. Therefore, those |
---|
93 | unnamed drifts cannot be found. However, named drifts can be searched |
---|
94 | for. |
---|
95 | <li> Due to the importance of finding elements in the table for a |
---|
96 | proper 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> |
---|
106 | Note 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> |
---|
111 | A typical example could look like this, in fact the square root of |
---|
112 | betx (user defined variable myvar) is added to the twiss table: |
---|
113 | <p> |
---|
114 | <font color=0000ff><pre> |
---|
115 | myvar := sqrt(table(twiss,betx)); |
---|
116 | select,flag=twiss,column=name,s,myvar,betx; |
---|
117 | twiss,file; |
---|
118 | </font></pre> |
---|
119 | <p> |
---|
120 | Or another arbitrary test case of adding k1l taken from the Twiss table: |
---|
121 | <p> |
---|
122 | Define macro: |
---|
123 | <p> |
---|
124 | <font color=0000ff><pre> |
---|
125 | mycrap(xx,yy,zz): macro = {myval = table(xx,yy,zz);}; |
---|
126 | </font></pre> |
---|
127 | <p> |
---|
128 | Use macro in loop: |
---|
129 | <p> |
---|
130 | <font color=0000ff><pre> |
---|
131 | i = 0; |
---|
132 | incval = 0; |
---|
133 | while (i < 100) { |
---|
134 | value,i; |
---|
135 | exec,mycrap(twiss,k1l,$i); |
---|
136 | incval = incval + myval; |
---|
137 | value,i,myval,incval; |
---|
138 | i = 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> |
---|
145 | FILL,TABLE=t,ROW=n; |
---|
146 | <p> |
---|
147 | fill a table row with the present variable values. If ROW is negative |
---|
148 | or missing a new row is created. If ROW is greater than the number of |
---|
149 | rows, the last row is selected without creating a new row. |
---|
150 | <p> |
---|
151 | <li> |
---|
152 | SETVARS,TABLE=t,ROW=n; |
---|
153 | <p> |
---|
154 | set variables according to the column names of the given table and |
---|
155 | the values of the given row. if ROW is negative, missing or greater |
---|
156 | than the number of rows, the last row is selected. |
---|
157 | <p> |
---|
158 | <li> |
---|
159 | An example can be found at: <a |
---|
160 | href="http://cern.ch/frs/mad-X_examples/special_features">Special |
---|
161 | Features</a> |
---|
162 | <p> |
---|
163 | <li> |
---|
164 | The length of a table can be determined by using the attribute |
---|
165 | "tablelength" via table("your_table_name", tablelength). This is |
---|
166 | useful 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> |
---|
172 | An expression may contain the following operands: |
---|
173 | |
---|
174 | <H3>Literal constants</H3> |
---|
175 | Numerical values are entered like FORTRAN constants. |
---|
176 | Real values are accepted in INTEGER or REAL format. |
---|
177 | The use of a decimal exponent, marked by the letter D or E, |
---|
178 | is permitted. |
---|
179 | <p> |
---|
180 | Examples: |
---|
181 | <pre> |
---|
182 | 1, 10.35, 5E3, 314.1592E-2 |
---|
183 | </pre> |
---|
184 | |
---|
185 | <H3><a name=constant>Symbolic constants</a></H3> |
---|
186 | MAD recognizes some |
---|
187 | <a href="#constant">mathematical and physical constants</a>. |
---|
188 | Their names must not be used for user-defined labels. |
---|
189 | <p> |
---|
190 | Additional symbolic constants may be defined to simplify |
---|
191 | their repeated use in statements and expressions. |
---|
192 | <pre>CONST name=constant-expression;</pre> |
---|
193 | defines a real constant with the name given. |
---|
194 | An existing symbolic constant can be redefined, |
---|
195 | but it cannot change in a matching procedure. |
---|
196 | <p> |
---|
197 | Example: |
---|
198 | <pre> |
---|
199 | const 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> |
---|
275 | Often a set of numerical values depends |
---|
276 | on a common variable parameter. |
---|
277 | Such a parameter must be defined as a |
---|
278 | <a href="parameter.html">global parameter</a>. |
---|
279 | A global parameter always has a current value; however, this value may |
---|
280 | be re-evaluated or not, depending on the parameter definition: |
---|
281 | <pre> |
---|
282 | x = a; |
---|
283 | </pre> |
---|
284 | x is set to the current value of a and not changed, even if a changes. |
---|
285 | This makes assignments such as |
---|
286 | <pre> |
---|
287 | x = x + 1; |
---|
288 | </pre> |
---|
289 | perfectly valid (this replaces the old SET instruction). |
---|
290 | The definition of the deferred expression |
---|
291 | <pre> |
---|
292 | x := a; |
---|
293 | </pre> |
---|
294 | assign the current value of a to x every time x is used, i.e. it is |
---|
295 | re-evaluated using the latest value of a; therefore, |
---|
296 | <pre> |
---|
297 | x := x + 1; |
---|
298 | </pre> |
---|
299 | results in an infinite loop (!) when x is used (error abort). |
---|
300 | Of course, the following definitions are equivalent: |
---|
301 | <pre> |
---|
302 | x = 0.1; |
---|
303 | x := 0.1; |
---|
304 | </pre> |
---|
305 | <p> |
---|
306 | When such a parameter is used in an expression, |
---|
307 | MAD uses the current value of the parameter if the expression is deferred: |
---|
308 | <p> |
---|
309 | Example: |
---|
310 | <pre> |
---|
311 | x:=1.0; |
---|
312 | d1: drift,l = x; |
---|
313 | d2: drift,l := 2.0 - x; |
---|
314 | </pre> |
---|
315 | When the value of X is changed, |
---|
316 | the length of the drift d1 remains unchanged, that of d2 is recalculated. |
---|
317 | |
---|
318 | <H3>Element or command attributes</H3> |
---|
319 | In arithmetic expressions the attributes of physical elements |
---|
320 | or commands can occur as operands. |
---|
321 | They are named respectively by |
---|
322 | <pre> |
---|
323 | element-name->attribute-name |
---|
324 | command-name->attribute-name |
---|
325 | </pre> |
---|
326 | Values are assigned to attributes in element definitions or commands. |
---|
327 | <p> |
---|
328 | Example: |
---|
329 | <pre> |
---|
330 | D1: DRIFT,L=1.0; |
---|
331 | D2: DRIFT,L=2.0-D1->L; |
---|
332 | </pre> |
---|
333 | D1->L refers to the length L of the drift space D1. |
---|
334 | |
---|
335 | <h2><a name=defer>Expressions and Random Values</a></h2> |
---|
336 | The definition of random machine imperfections requires evaluation |
---|
337 | of expressions containing random functions. |
---|
338 | These are evaluated like any other expression when the expression |
---|
339 | is used, i.e. only once if a "=" assignment refers to it, or every time |
---|
340 | if the assignment is ":="; this latter case is used by the error |
---|
341 | generation routines. |
---|
342 | <p> |
---|
343 | Example: |
---|
344 | <pre> |
---|
345 | a := 3*ranf(); |
---|
346 | </pre> |
---|
347 | Every time a is used, it gets a random value assigned from a uniform |
---|
348 | distribution between 0 and 3. |
---|
349 | <pre> |
---|
350 | error: ealign,range,dx:=sigma*gauss(); |
---|
351 | </pre> |
---|
352 | All elements in range are assigned independent random |
---|
353 | displacements sampled from a Gaussian distribution |
---|
354 | with standard deviation sigma. |
---|
355 | <p> |
---|
356 | |
---|
357 | <address> |
---|
358 | <a href="http://www.cern.ch/Hans.Grote/hansg_sign.html">hansg</a>, |
---|
359 | May 8, 2001 |
---|
360 | </address> |
---|
361 | |
---|
362 | </body> |
---|