To compute an arbitrary expression on arrays of real, use evalformula.
set a to createarray 10000 range {0, 2*pi}
set s2 to evalformula "2*cos(x)*sin(x)" with {x:a}
evalformula may mix arrays of real and numbers.
evalformula "poly*factor" with {poly:thePolygon, factor:10.0}
-
The expression may include logical operators, yet all values remain real numbers: true expressions evaluate to 1.0, false expressions evaluate to 0.0. Conversely when implied in a logical expression any non null number is considered as true (1.0).
-
The direct parameter of evalformula may be a sequence of elementary expressions, provided they are separated with semi-colon ;. You can also use return as the separator, so in complex cases when the expression may require several intermediate steps you can easily use the contents of a text window as the expression. When you use such a composite construct evalformula will return the result of the last (rightmost) expression, for instance the examples below return 2.0.
evalformula "a=1;a+1"
evalformula "a=1
a+1"
-
Instead of one expression you can pass a list of expressions to evalformula: evalformula will return a list of arrays of real.
-
Here is the list of the operators and functions that evalformula supports.
- operators (higher precedence first)
- - (unary)
- * /, + -, ^ (exponentiation)
- >= > <= <== != (equal - not equal), & (logical AND), | (logical OR), ! (boolean not, evaluates to 1 if the operand is 0 and to 0 otherwise).
- algebra
- sqr, sqrt, hypot, pow
- abs, ceil, floor, trunc, max, min
- mod (modulo), remainder
- isnan (test whether the quantity is a ``NAN''. NAN = Not A Number.)
- transcendental
- ln or log (neperian), log10, exp
- cos, sin, tan, acos, asin, atan, atan2
- cosh, sinh, tanh, acosh, asinh, atanh
- erf, erfc, gamma, lgamma
-
functions on arrays
- norm and norm2, respectively the euclidian norm and its square (numbers),
- sum and runsum, respectively the sum (a number) and the running sum (an array),
- count, the number of elements (a number).
evalformula ("atan2(y,x)") with {y:sqrt (3), x:1}
-- 1.047197551197 (that is, pi/3)
evalformula ("norm(cos(x))/sqrt(count(x))") with {x:randomarray 10000 range {0, 2 * pi}}
-- 0.701118610115 (that is, ~1/sqrt(2))
|