Difference between revisions of "Expression Syntax"
(→Notes) |
|||
(24 intermediate revisions by the same user not shown) | |||
Line 24: | Line 24: | ||
</ul> | </ul> | ||
When there are multiple operators, the operations are combined with the usual operator precedence of <tt>^</tt> followed by <tt>*</tt> and <tt>/</tt> followed by <tt>+</tt> and <tt>-</tt>. When there are multiple operators of the same precedence, they are evaluated left to right.</li> | When there are multiple operators, the operations are combined with the usual operator precedence of <tt>^</tt> followed by <tt>*</tt> and <tt>/</tt> followed by <tt>+</tt> and <tt>-</tt>. When there are multiple operators of the same precedence, they are evaluated left to right. In string expressions, all top-level <tt>[op]</tt>'s must be for concatenation (<tt>&</tt>), although any <tt>[ATOMIC]</tt> can be a numeric expression enclosed in parentheses.</li> | ||
<li><tt>[ATOMIC]</tt> are the elements that are combined. The possible atomics are: | <li><tt>[ATOMIC]</tt> are the elements that are combined. The possible atomics are: | ||
Line 36: | Line 36: | ||
<li>A function of an expression (such as <tt>sin([EXPRESSION])</tt>) and the value of the <tt>[ATOMIC]</tt> is the value of the function applied to the value of the expression. The allowed functions are listed [[#Supported Functions|below]].</li> | <li>A function of an expression (such as <tt>sin([EXPRESSION])</tt>) and the value of the <tt>[ATOMIC]</tt> is the value of the function applied to the value of the expression. The allowed functions are listed [[#Supported Functions|below]].</li> | ||
<li>An "at" expression | <li>An "at" expression beginning in an <tt>@</tt> sign and containing specific items separated by periods, such as "<tt>@key.TopLeft.x</tt>" to evaluate to the x value of the keypoint named <tt>TopLeft</tt>. | ||
<ul> | <ul> | ||
Line 50: | Line 50: | ||
== Supported Functions == | == Supported Functions == | ||
=== Numeric Functions === | |||
The following numeric functions are supported in both [[NairnFEAMPM]] and [[NairnFEAMPMViz]]: | The following numeric functions are supported in both [[NairnFEAMPM]] and [[NairnFEAMPMViz]]: | ||
Line 63: | Line 65: | ||
<li><tt>cosh()</tt> - hyperbolic cosine</li> | <li><tt>cosh()</tt> - hyperbolic cosine</li> | ||
<li><tt>tanh()</tt> - hyperbolic tangent</li> | <li><tt>tanh()</tt> - hyperbolic tangent</li> | ||
<li><tt>erf()</tt> - error function</li> | |||
<li><tt>erfc()</tt> - error function complement</li> | |||
<li><tt>exp()</tt> - exponential</li> | <li><tt>exp()</tt> - exponential</li> | ||
<li><tt>log()</tt> - natural log</li> | <li><tt>log()</tt> - natural log</li> | ||
Line 69: | Line 73: | ||
<li><tt>abs()</tt> - absolute value</li> | <li><tt>abs()</tt> - absolute value</li> | ||
<li><tt>int()</tt> - integer part</li> | <li><tt>int()</tt> - integer part</li> | ||
<li><tt>sign()</tt> - 1 if | <li><tt>sign(x)</tt> - 1 if x is positive or 0 if it is negative</li> | ||
<li><tt> | <li><tt>sgn(x)</tt> - -1 if x <0, 0 if x=0, and +1 if x>0</li> | ||
</ | <li><tt>ramp(A,x)</tt> - 0 if x< 0, Ax if 0<x<1, A if x>1</li> | ||
<li><tt>cosramp(A,x)</tt> - 0 if x< 0, (A/2)(1-cos(πx)) if 0<x<1, A if x>1</li> | |||
<li><tt>box(A,x)</tt> - 0 if x< 0, A if 0<x<1, 0 if x>1</li> | |||
<li><tt>sinbox(A,x)</tt> - 0 if x< 0, A sin(πx) if 0<x<1, 0 if x>1</li> | |||
< | <li><tt>tri(x)</tt> - 1-|x| for -1 < x < 1, 0 otherwise</li> | ||
<li><tt> | <li><tt>mod(A,x)</tt> - remainder of A/x and workings with non-integer values (useful for creating [[User_Defined_Functions#Periodic_Boundary_Condition_Function|periodic functions]])</tt> | ||
<li><tt> | <li><tt>rand(x)</tt> - a random number between 0 and x.</li> | ||
<li><tt>cdfinv(x)</tt> - find inverse of the cumulative distribution function. For a random variable ''Z'' with mean ''m'' and standard deviation σ, the value of ''x'' such that the probability that ''Z''<''x'' is ''p'' is equal to ''x''=''m''+σ*<tt>cdfinv(p)</tt>.</li> | |||
</ul> | </ul> | ||
The following string functions are supported | === String Functions === | ||
Each string function takes a single string expression as an argument. Some functions embed sub-arguments delimited by back slashes within the evaluated single string expression. The following string functions are supported in both [[NairnFEAMPM]] and [[NairnFEAMPMViz]]: | |||
<ul> | <ul> | ||
Line 89: | Line 95: | ||
<li><tt>removeFirstWord()</tt> - string with first word removed.</li> | <li><tt>removeFirstWord()</tt> - string with first word removed.</li> | ||
<li><tt>removeLastWord()</tt> - string with last word removed.</li> | <li><tt>removeLastWord()</tt> - string with last word removed.</li> | ||
<li><tt>chars(c1\c2\string)</tt> - returns characters <tt>c1</tt> to <tt>c2</tt> of <tt>string</tt> (the first character is character 1). <tt>c1</tt> | <li><tt>trim()</tt> - string with leading and trailing white space removed.</li> | ||
<li><tt>word(w1\string)</tt> - | <li><tt>chars(c1\c2\string)</tt> - returns characters <tt>c1</tt> to <tt>c2</tt> (inclusive) of <tt>string</tt> (the first character is character 1). If omitted, <tt>c1</tt> and <tt>c2</tt> default to 1 and length of <tt>string</tt>, respectively. Thus <tt>char(c1\string)</tt> gets character 1 to the end and <tt>char(\c2\string)</tt> gets character 1 through <tt>c2</tt>. Either <tt>c1</tt> or <tt>c2</tt> can be <=0 to indicate character number relative to end of the string (''e.g.'', 0 is last character, -1 is next to last, ''etc.'')</li> | ||
<li><tt>word(w1\string)</tt> - rreturns word <tt>w1</tt> of <tt>string</tt>. If <tt>w1<=0</tt>, it specifies word number relative to end of the string (<i>e.g.</i>, 0 is last word, -1 is next to last, <i>etc.</i>).</li> | |||
<li><tt>offset(s1\c1\string)</tt> - returns offset of string <tt>s1</tt> in string at or after character <tt>c1</tt> (the first character is character 1) or returns 0 if string <tt>s1</tt> is not found. <tt>c1 can</tt> be omitted to find <tt>s1</tt> any place in <code>string</code>. The search is case insensitive.</li> | <li><tt>offset(s1\c1\string)</tt> - returns offset of string <tt>s1</tt> in string at or after character <tt>c1</tt> (the first character is character 1) or returns 0 if string <tt>s1</tt> is not found. <tt>c1 can</tt> be omitted to find <tt>s1</tt> any place in <code>string</code>. The search is case insensitive.</li> | ||
<li><tt>replace(s1\s2\string)</tt> - returns string where all occurrences of string <tt>s1</tt> in <tt>string</tt> are replaced with string <tt>s2</tt>. <tt>s2</tt> may be omitted to delete all occurrences of string <tt>s1</tt>.</li> | <li><tt>replace(s1\s2\string)</tt> - returns string where all occurrences of string <tt>s1</tt> in <tt>string</tt> are replaced with string <tt>s2</tt>. <tt>s2</tt> may be omitted to delete all occurrences of string <tt>s1</tt>.</li> | ||
Line 97: | Line 104: | ||
<li><tt>titleCase()</tt> - convert to string with capitalized words.</li> | <li><tt>titleCase()</tt> - convert to string with capitalized words.</li> | ||
</ul> | </ul> | ||
To omit first subargument in <tt>chars()</tt>, <tt>offset()</tt>, or <tt>replace()</tt>, start with a back slash (''e.g.'', <tt>chars(\c2\string)</tt>). To omit second argument, you can either omit it and its backslash (''e.g.'', <tt>chars(c1\string)</tt>), but if <tt>string</tt> might contain a backslash, it is better to just omit the subargument (''e.g.'', <tt>chars(c1\\string)</tt>). | |||
== Notes == | == Notes == | ||
Line 104: | Line 113: | ||
<li>An assignment command can be combined with an operator as in | <li>An assignment command can be combined with an operator as in | ||
<pre>#x [op] = [EXPRESSION] | <pre>#x [op]= [EXPRESSION] | ||
</pre> | </pre> | ||
Line 114: | Line 123: | ||
provided the variable is already a defined variable. It is an error if the variable has not previously been defined. | provided the variable is already a defined variable. It is an error if the variable has not previously been defined. | ||
</li> | </li> | ||
</ol> | </ol> |
Latest revision as of 14:26, 28 March 2023
The scripting language allows expressions to do calculations with variables.
Expression Definition
An expression combines any number of numbers, strings, variables, and functions with operators. An expression evaluates to a numeric or string value. The general, recursive definition of an expression is:
[EXPRESSION] = [sign] [ATOMIC] [op] [ATOMIC] [op] [ATOMIC] ... [op] [ATOMIC]
where
- [sign] is an optional leading sign of + or -
- [op] are operators that determine how the [ATOMIC]'s are combined. The options are:
- + for addition
- - for subtraction
- * for multiplication
- / for division
- ^ for raising to a power
- & for string concatenation
- [ATOMIC] are the elements that are combined. The possible atomics are:
- An unsigned number (23, 2.234, 3e2, 4.56e-3, ...).2
- A variable (such as #varname, #x[2], #z[#i][#j], etc.) and the value of the variable is used. The variable must have been previously defined.
- Text in quotes ("some text") and the value is the text after the quotes are removed.
- Unquoted text and the value is all the text between the operators after all spaces have been removed. To include spaces in text [ATOMIC]'s, the text must be quoted.
- Another expression in parenthesis or ([EXPRESSION]) and the value of the [ATOMIC] is the value of the included expression.
- A function of an expression (such as sin([EXPRESSION])) and the value of the [ATOMIC] is the value of the function applied to the value of the expression. The allowed functions are listed below.
- An "at" expression beginning in an @ sign and containing specific items separated by periods, such as "@key.TopLeft.x" to evaluate to the x value of the keypoint named TopLeft.
- The available "at" expressions are defined elsewhere (for example, see Keypoint Command and Path Command).
- Any of the period-delimited items can be a variable (such as @key.#kname.x) and the contents of #kname will replace that item in the expression.
- Variables used as items can only be a single variable and not an expression. They also cannot be an array variable with a variable index (e.g., it can be #n[3] but cannot be #n[#i]). Hint: if you need to index an array with a variable, you can define simple variable first (e.g., #ni=#n[#i]) and then use that in an expression (e.g., @key.#ni.y).
Supported Functions
Numeric Functions
The following numeric functions are supported in both NairnFEAMPM and NairnFEAMPMViz:
- sin() - sine of argument in radians
- cos() - cosine of argument in radians
- tan() - tangent of argument in radians
- asin() - inverse sine with result in radians
- acos() - inverse cosine with result in radians
- atan() - inverse tangent with result in radians
- sinh() - hyperbolic sine
- cosh() - hyperbolic cosine
- tanh() - hyperbolic tangent
- erf() - error function
- erfc() - error function complement
- exp() - exponential
- log() - natural log
- log10() - log base 10
- sqrt() - square root
- abs() - absolute value
- int() - integer part
- sign(x) - 1 if x is positive or 0 if it is negative
- sgn(x) - -1 if x <0, 0 if x=0, and +1 if x>0
- ramp(A,x) - 0 if x< 0, Ax if 0<x<1, A if x>1
- cosramp(A,x) - 0 if x< 0, (A/2)(1-cos(πx)) if 0<x<1, A if x>1
- box(A,x) - 0 if x< 0, A if 0<x<1, 0 if x>1
- sinbox(A,x) - 0 if x< 0, A sin(πx) if 0<x<1, 0 if x>1
- tri(x) - 1-|x| for -1 < x < 1, 0 otherwise
- mod(A,x) - remainder of A/x and workings with non-integer values (useful for creating periodic functions)
- rand(x) - a random number between 0 and x.
- cdfinv(x) - find inverse of the cumulative distribution function. For a random variable Z with mean m and standard deviation σ, the value of x such that the probability that Z<x is p is equal to x=m+σ*cdfinv(p).
String Functions
Each string function takes a single string expression as an argument. Some functions embed sub-arguments delimited by back slashes within the evaluated single string expression. The following string functions are supported in both NairnFEAMPM and NairnFEAMPMViz:
- length() - number of characters in a string.
- words() - number of words in a string.
- firstWord() - first word in a string.
- lastWord() - last word in a string.
- removeFirstWord() - string with first word removed.
- removeLastWord() - string with last word removed.
- trim() - string with leading and trailing white space removed.
- chars(c1\c2\string) - returns characters c1 to c2 (inclusive) of string (the first character is character 1). If omitted, c1 and c2 default to 1 and length of string, respectively. Thus char(c1\string) gets character 1 to the end and char(\c2\string) gets character 1 through c2. Either c1 or c2 can be <=0 to indicate character number relative to end of the string (e.g., 0 is last character, -1 is next to last, etc.)
- word(w1\string) - rreturns word w1 of string. If w1<=0, it specifies word number relative to end of the string (e.g., 0 is last word, -1 is next to last, etc.).
- offset(s1\c1\string) - returns offset of string s1 in string at or after character c1 (the first character is character 1) or returns 0 if string s1 is not found. c1 can be omitted to find s1 any place in
string
. The search is case insensitive. - replace(s1\s2\string) - returns string where all occurrences of string s1 in string are replaced with string s2. s2 may be omitted to delete all occurrences of string s1.
- upperCase() - convert string to upper case.
- lowerCase() - convert string to lower case.
- titleCase() - convert to string with capitalized words.
To omit first subargument in chars(), offset(), or replace(), start with a back slash (e.g., chars(\c2\string)). To omit second argument, you can either omit it and its backslash (e.g., chars(c1\string)), but if string might contain a backslash, it is better to just omit the subargument (e.g., chars(c1\\string)).
Notes
- An assignment command can be combined with an operator as in
#x [op]= [EXPRESSION]
which is equivalent to
#x = #x [op] ([EXPRESSION])
provided the variable is already a defined variable. It is an error if the variable has not previously been defined.