2012-01-28 05:25:39 +00:00
|
|
|
C++ Mathematical Expression Toolkit Library
|
|
|
|
|
2013-04-23 11:30:49 +00:00
|
|
|
[00 - INTRODUCTION]
|
2013-04-04 09:45:19 +00:00
|
|
|
The C++ Mathematical Expression Toolkit Library (ExprTk) is a simple
|
|
|
|
to use, easy to integrate and extremely efficient mathematical
|
|
|
|
expression parsing and evaluation engine. The parsing engine supports
|
|
|
|
numerous forms of functional and logic processing semantics and is
|
2013-10-16 21:44:15 +00:00
|
|
|
easily extendible.
|
2012-01-28 05:25:39 +00:00
|
|
|
|
|
|
|
|
2012-05-16 22:14:45 +00:00
|
|
|
|
2013-04-23 11:30:49 +00:00
|
|
|
[01 - CAPABILITIES]
|
2013-04-27 03:55:23 +00:00
|
|
|
The ExprTk evaluator supports the following fundamental arithmetic
|
2012-05-07 19:18:29 +00:00
|
|
|
operations, functions and processes:
|
2012-05-05 11:07:37 +00:00
|
|
|
|
2013-03-14 21:38:09 +00:00
|
|
|
(0) Basic operators: +, -, *, /, %, ^
|
2012-05-05 11:07:37 +00:00
|
|
|
|
2013-12-08 22:30:01 +00:00
|
|
|
(1) Functions: abs, avg, ceil, clamp, equal, erf, erfc, exp,
|
|
|
|
expm1, floor, frac, log, log10, log1p, log2,
|
|
|
|
logn, max, min, mul, nequal, root, round,
|
|
|
|
roundn, sgn, sqrt, sum, trunc
|
2012-05-05 11:07:37 +00:00
|
|
|
|
2014-01-03 10:42:54 +00:00
|
|
|
(2) Trigonometry: acos, acosh, asin, asinh, atan, atanh, atan2,
|
|
|
|
cos, cosh, cot, csc, deg2grad, deg2rad, grad2deg,
|
|
|
|
hypot, rad2deg, sec, sin, sinh, tan, tanh
|
2013-07-16 22:21:06 +00:00
|
|
|
|
2013-03-14 21:38:09 +00:00
|
|
|
(3) Equalities &
|
2013-01-30 13:28:22 +00:00
|
|
|
Inequalities: =, ==, <>, !=, <, <=, >, >=
|
2012-05-05 11:07:37 +00:00
|
|
|
|
2013-04-04 09:45:19 +00:00
|
|
|
(4) Boolean logic: and, mand, mor, nand, nor, not, or, shl, shr,
|
|
|
|
xnor, xor, true, false
|
2012-05-05 11:07:37 +00:00
|
|
|
|
2013-04-13 08:26:57 +00:00
|
|
|
(5) Conditional,
|
|
|
|
Switch &
|
2013-07-08 22:32:08 +00:00
|
|
|
Loop statements: if-then-else, switch-case, while, repeat-until
|
2012-05-05 11:07:37 +00:00
|
|
|
|
2013-03-14 21:38:09 +00:00
|
|
|
(6) Assignment: :=
|
2013-01-30 13:28:22 +00:00
|
|
|
|
2013-03-14 21:38:09 +00:00
|
|
|
(7) String
|
2013-03-11 13:29:59 +00:00
|
|
|
processing: in, like, ilike
|
2013-01-30 13:28:22 +00:00
|
|
|
|
2013-03-14 21:38:09 +00:00
|
|
|
(8) Calculus: numerical integration and differentiation
|
2012-05-05 11:07:37 +00:00
|
|
|
|
2014-01-22 20:32:55 +00:00
|
|
|
(9) Optimisations: constant-folding and simple strength reduction
|
|
|
|
|
2012-05-05 11:07:37 +00:00
|
|
|
|
2012-05-16 22:14:45 +00:00
|
|
|
|
2013-04-23 11:30:49 +00:00
|
|
|
[02 - EXAMPLE EXPRESSIONS]
|
2013-07-08 22:32:08 +00:00
|
|
|
The following is a short listing of the types of mathematical
|
2012-05-05 11:07:37 +00:00
|
|
|
expressions that can be parsed and evaluated using the ExprTk library.
|
|
|
|
|
2014-02-09 09:20:15 +00:00
|
|
|
(01) sqrt(1 - (x^2))
|
|
|
|
(02) clamp(-1, sin(2 * pi * x) + cos(y / 2 * pi), +1)
|
|
|
|
(03) sin(2.34e-3 * x)
|
|
|
|
(04) if(((x + 2) == 3) and ((y + 5) <= 9),1 + w, 2 / z)
|
|
|
|
(05) inrange(-2,m,+2) == if(({-2 <= m} and [m <= +2]),1,0)
|
|
|
|
(06) ({1/1}*[1/2]+(1/3))-{1/4}^[1/5]+(1/6)-({1/7}+[1/8]*(1/9))
|
|
|
|
(07) a * exp(2.2 / 3.3 * t) + c
|
|
|
|
(08) z := x + sin(2.567 * pi / y)
|
|
|
|
(09) u := 2.123 * (pi * z) / (w := x + cos(y / pi))
|
|
|
|
(10) 2x + 3y + 4z + 5w == 2 * x + 3 * y + 4 * z + 5 * w
|
|
|
|
(11) 3(x + y) / 2.9 + 1.234e+12 == 3 * (x + y) / 2.9 + 1.234e+12
|
|
|
|
(12) (x + y)3.3 + 1 / 4.5 == (x + y) * 3.3 + 1 / 4.5
|
|
|
|
(13) (x + y)z + 1.1 / 2.7 == (x + y) * z + 1.1 / 2.7
|
|
|
|
(14) (sin(x / pi) cos(2y) + 1) == (sin(x / pi) * cos(2 * y) + 1)
|
|
|
|
(15) 75x^17 + 25.1x^5 - 35x^4 - 15.2x^3 + 40x^2 - 15.3x + 1
|
|
|
|
(16) if (avg(x,y) <= x + y, x - y, x * y) + 2.345 * pi / x
|
|
|
|
(17) fib_i := fib_i + (x := y + 0 * (fib_i := x + (y := fib_i)))
|
|
|
|
(18) while (x <= 100) { x := x + 1 }
|
|
|
|
(19) x <= 'abc123' and (y in 'AString') or ('1x2y3z' != z)
|
|
|
|
(20) (x like '*123*') or ('a123b' ilike y)
|
2012-05-05 11:07:37 +00:00
|
|
|
|
|
|
|
|
2012-05-16 22:14:45 +00:00
|
|
|
|
2013-04-23 11:30:49 +00:00
|
|
|
[03 - COPYRIGHT NOTICE]
|
2013-04-04 09:45:19 +00:00
|
|
|
Free use of the C++ Mathematical Expression Toolkit Library is
|
|
|
|
permitted under the guidelines and in accordance with the most current
|
|
|
|
version of the Common Public License.
|
2012-01-28 05:25:39 +00:00
|
|
|
|
|
|
|
http://www.opensource.org/licenses/cpl1.0.php
|
|
|
|
|
|
|
|
|
2012-05-16 22:14:45 +00:00
|
|
|
|
2013-04-23 11:30:49 +00:00
|
|
|
[04 - DOWNLOADS & UPDATES]
|
2013-04-04 09:45:19 +00:00
|
|
|
The most recent version of the C++ Mathematical Expression Toolkit
|
|
|
|
Library including all updates and tests can be found at the following
|
|
|
|
locations:
|
|
|
|
|
2014-02-09 09:20:15 +00:00
|
|
|
(1) http://www.partow.net/programming/exprtk/index.html
|
|
|
|
(2) svn checkout http://exprtk.googlecode.com/svn/ exprtk
|
2012-01-28 05:25:39 +00:00
|
|
|
|
2012-05-05 11:07:37 +00:00
|
|
|
|
2012-05-16 22:14:45 +00:00
|
|
|
|
2013-04-23 11:30:49 +00:00
|
|
|
[05 - INSTALLATION]
|
2013-04-04 09:45:19 +00:00
|
|
|
The header file exprtk.hpp should be placed in a project or system
|
|
|
|
include path (e.g: /usr/include/).
|
2012-01-28 05:25:39 +00:00
|
|
|
|
|
|
|
|
2012-05-16 22:14:45 +00:00
|
|
|
|
2013-04-23 11:30:49 +00:00
|
|
|
[06 - COMPILATION]
|
2014-02-09 09:20:15 +00:00
|
|
|
(1) For a complete build: make clean all
|
|
|
|
(2) For a PGO build: make clean pgo
|
|
|
|
(3) To strip executables: make strip_bin
|
|
|
|
(5) Execute valgrind check: make valgrind_check
|
2012-01-28 05:25:39 +00:00
|
|
|
|
|
|
|
|
2012-05-16 22:14:45 +00:00
|
|
|
|
2013-04-23 11:30:49 +00:00
|
|
|
[07 - COMPILER COMPATIBILITY]
|
2014-02-09 09:20:15 +00:00
|
|
|
(*) GNU Compiler Collection (4.1+)
|
|
|
|
(*) Intel C++ Compiler (9.x+)
|
|
|
|
(*) Clang/LLVM (1.1+)
|
|
|
|
(*) PGI C++ (10.x+)
|
|
|
|
(*) Microsoft Visual Studio C++ Compiler (8.1+)
|
|
|
|
(*) Comeau C++ Compiler (4.3+)
|
|
|
|
(*) IBM XL C/C++ (10.x+)
|
2012-01-28 05:25:39 +00:00
|
|
|
|
|
|
|
|
2012-05-16 22:14:45 +00:00
|
|
|
|
2013-04-23 11:30:49 +00:00
|
|
|
[08 - BUILT-IN OPERATIONS & FUNCTIONS]
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2013-04-27 03:55:23 +00:00
|
|
|
(0) Arithmetic Operators
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| OPERATOR | DEFINITION |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| + | Addition between x and y. (eg: x + y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| - | Subtraction between x and y. (eg: x - y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| * | Multiplication between x and y. (eg: x * y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| / | Division between x and y (eg: x / y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| % | Modulus of x with respect to y. (eg: x % y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| ^ | x to the power of y. (eg: x ^ y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| := | Assign the value of x to y. (eg: y := x) |
|
|
|
|
| | Where y is a variable type. |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-03-11 13:29:59 +00:00
|
|
|
|
|
|
|
(1) Equalities & Inequalities
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| OPERATOR | DEFINITION |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| == or = | True only if x is strictly equal to y. (eg: x == y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| <> or != | True only if x does not equal y. (eg: x <> y or x != y) |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| < | True only if x is less than y. (eg: x < y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| <= | True only if x is less than or equal to y. (eg: x <= y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| > | True only if x is greater than y. (eg: x > y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| >= | True only if x greater than or equal to y. (eg: x >= y) |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-03-11 13:29:59 +00:00
|
|
|
|
|
|
|
(2) Boolean Operations
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| OPERATOR | DEFINITION |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| true | True state or any value other than zero (typically 1). |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| false | False state, value of zero. |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| and | Logical AND, True only if x and y are both true. |
|
|
|
|
| | (eg: x and y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| mand | Multi-input logical AND, True only if all inputs are |
|
|
|
|
| | true. Left to right short-circuiting of expressions. |
|
|
|
|
| | (eg: mand(x > y,z < w,u or v,w and x)) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| mor | Multi-input logical OR, True if at least one of the |
|
|
|
|
| | inputs are true. Left to right short-circuiting of |
|
2013-07-16 22:21:06 +00:00
|
|
|
| | expressions. (eg: mor(x > y,z < w,u or v,w and x)) |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| nand | Logical NAND, True only if either x or y is false. |
|
|
|
|
| | (eg: x nand y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| nor | Logical NOR, True only if the result of x or y is false |
|
|
|
|
| | (eg: x nor y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| not | Logical NOT, Negate the logical sense of the input. |
|
|
|
|
| | (eg: not(x and y) == x nand y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| or | Logical OR, True if either x or y is true. (eg: x or y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| xor | Logical XOR, True only if the logical states of x and y |
|
|
|
|
| | differ. (eg: x xor y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| xnor | Logical XNOR, True iff the biconditional of x and y is |
|
|
|
|
| | satisfied. (eg: x xnor y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| & | Similar to AND but with left to right expression short |
|
|
|
|
| | circuiting optimisation. (eg: (x & y) == (y and x)) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| | | Similar to OR but with left to right expression short |
|
|
|
|
| | circuiting optimisation. (eg: (x | y) == (y or x)) |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-03-11 13:29:59 +00:00
|
|
|
|
|
|
|
(3) General Purpose Functions
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| FUNCTION | DEFINITION |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| abs | Absolute value of x. |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| avg | Average of all the inputs. |
|
|
|
|
| | (eg: avg(x,y,z,w,u,v) == (x + y + z + w + u + v) / 6) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| ceil | Smallest integer that is greater than or equal to x. |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| clamp | Clamp x in range between r0 and r1, where r0 < r1. |
|
|
|
|
| | (eg: clamp(r0,x,r1) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| equal | Equality test between x and y using normalized epsilon |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| erf | Error function of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| erfc | Complimentary error function of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| exp | e to the power of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-12 13:08:57 +00:00
|
|
|
| expm1 | e to the power of x minus 1, where x is very small. |
|
|
|
|
| | (eg: expm1(x)) |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-08 22:32:08 +00:00
|
|
|
| floor | Largest integer that is less than or equal to x. |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| frac | Fractional portion of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| hypot | Hypotenuse of x and y (eg: hypot(x,y) = sqrt(x*x + y*y))|
|
|
|
|
+----------+---------------------------------------------------------+
|
2014-01-14 20:13:18 +00:00
|
|
|
| iclamp | Inverse-clamp x outside of the range r0 and r1. Where |
|
|
|
|
| | r0 < r1. If x is within the range it will snap to the |
|
|
|
|
| | closest bound. (eg: iclamp(r0,x,r1) |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| log | Natural logarithm of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| log10 | Base 10 logarithm of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| log1p | Natural logarithm of 1 + x, where x is very small. |
|
|
|
|
| | (eg: log1p(x)) |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| log2 | Base 2 logarithm of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| logn | Base N logarithm of x (eg: logn(1235,8)) |
|
|
|
|
| | where n > 0 and is an integer. |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| max | Largest value of all the inputs. (eg: max(x,y,z,w,u,v)) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| min | Smallest value of all the inputs. (eg: min(x,y,z,w,u)) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| mul | Product of all the inputs. |
|
|
|
|
| | (eg: mul(x,y,z,w,u,v,t) == (x * y * z * w * u * v * t)) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| nequal | Not-equal test between x and y using normalized epsilon |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| root | Nth-Root of x (eg: root(x,3)) |
|
|
|
|
| | where n > 0 and is an integer. |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| round | Round x to the nearest integer. |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| roundn | Round x to n decimal places (eg: roundn(x,3)) |
|
|
|
|
| | where n > 0 and is an integer. |
|
|
|
|
| | (eg: roundn(1.2345678,4) == 1.2346) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| sgn | Sign of x, -1 where x < 0, +1 where x > 0, else zero. |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| sqrt | Square root of x, where x > 0 |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| sum | Sum of all the inputs. |
|
|
|
|
| | (eg: sum(x,y,z,w,u,v,t) == (x + y + z + w + u + v + t)) |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| trunc | Integer portion of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-03-11 13:29:59 +00:00
|
|
|
|
|
|
|
(4) Trigonometry Functions
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| FUNCTION | DEFINITION |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| acos | Arc cosine of x expressed in radians. Interval [-1,+1] |
|
|
|
|
+----------+---------------------------------------------------------+
|
2014-01-03 10:42:54 +00:00
|
|
|
| acosh | Inverse hyperbolic cosine of x expressed in radians. |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-08 22:32:08 +00:00
|
|
|
| asin | Arc sine of x expressed in radians. Interval [-1,+1] |
|
|
|
|
+----------+---------------------------------------------------------+
|
2014-01-03 10:42:54 +00:00
|
|
|
| asinh | Inverse hyperbolic sine of x expressed in radians. |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-08 22:32:08 +00:00
|
|
|
| atan | Arc tangent of x expressed in radians. Interval [-1,+1] |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| atan2 | Arc tangent of (x/y) expressed in radians. [-pi,+pi] |
|
|
|
|
| | eg: atan2(x,y) |
|
|
|
|
+----------+---------------------------------------------------------+
|
2014-01-03 10:42:54 +00:00
|
|
|
| atanh | Inverse hyperbolic tangent of x expressed in radians. |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| cos | Cosine of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| cosh | Hyperbolic cosine of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| cot | Cotangent of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| csc | Cosecant of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| sec | Secant of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| sin | Sine of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| sinh | Hyperbolic sine of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| tan | Tangent of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| tanh | Hyperbolic tangent of x. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| deg2rad | Convert x from degrees to radians. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| deg2grad | Convert x from degrees to gradians. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| rad2deg | Convert x from radians to degrees. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| grad2deg | Convert x from gradians to degrees. |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-03-11 13:29:59 +00:00
|
|
|
|
|
|
|
(5) String Processing
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| FUNCTION | DEFINITION |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-07-28 11:35:06 +00:00
|
|
|
| in | True only if x is a substring of y. |
|
2013-07-08 22:32:08 +00:00
|
|
|
| | (eg: x in y or 'abc' in 'abcdefgh') |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| like | True only if the string x matches the pattern y. |
|
|
|
|
| | Available wildcard characters are '*' and '?' denoting |
|
|
|
|
| | zero or more and zero or one matches respectively. |
|
|
|
|
| | (eg: x like y or 'abcdefgh' like 'a?d*h') |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| ilike | True only if the string x matches the pattern y in a |
|
|
|
|
| | case insensitive manner. Available wildcard characters |
|
|
|
|
| | are '*' and '?' denoting zero or more and zero or one |
|
|
|
|
| | matches respectively. |
|
|
|
|
| | (eg: x ilike y or 'a1B2c3D4e5F6g7H' ilike 'a?d*h') |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| [r0:r1] | The closed interval [r0,r1] of the specified string. |
|
|
|
|
| | eg: Given a string x with a value of 'abcdefgh' then: |
|
|
|
|
| | 0. x[1:4] == 'bcde' |
|
2013-10-16 21:44:15 +00:00
|
|
|
| | 1. x[ :5] == x[:5] == 'abcdef' |
|
|
|
|
| | 2. x[3: ] == x[3:] =='cdefgh' |
|
|
|
|
| | 3. x[ : ] == x[:] == 'abcdefgh' |
|
2013-07-08 22:32:08 +00:00
|
|
|
| | 4. x[4/2:3+2] == x[2:5] == 'cdef' |
|
|
|
|
| | |
|
2013-07-12 13:08:57 +00:00
|
|
|
| | Note: Both r0 and r1 are assumed to be integers, where |
|
|
|
|
| | r0 <= r1. They may also be the result of an expression, |
|
|
|
|
| | in the event they have fractional components truncation |
|
|
|
|
| | will be performed. (eg: 1.67 -> 1) |
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2013-04-27 03:55:23 +00:00
|
|
|
(6) Control Structures
|
2013-07-08 22:32:08 +00:00
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
|STRUCTURE | DEFINITION |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| if | If x is true then return y else return z. |
|
|
|
|
| | (eg: if(x, y, z) or if((x + 1) > 2y, z + 1, w / v)) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| switch | The first true case condition that is encountered will |
|
|
|
|
| | determine the result of the switch. If none of the case |
|
|
|
|
| | conditions hold true, the default action is assumed as |
|
|
|
|
| | the final return value. This is sometimes also known as |
|
|
|
|
| | a multi-way branch mechanism. |
|
|
|
|
| | eg: |
|
|
|
|
| | switch |
|
|
|
|
| | { |
|
|
|
|
| | case x > (y + z) : 2 * x / abs(y - z); |
|
2013-10-16 21:44:15 +00:00
|
|
|
| | case x < 3 : sin(x + y); |
|
2013-07-08 22:32:08 +00:00
|
|
|
| | default : 1 + x; |
|
|
|
|
| | } |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| while | The structure will repeatedly evaluate the internal |
|
|
|
|
| | statement(s) 'while' the condition is true. The final |
|
|
|
|
| | statement in the final iteration will be used as the |
|
|
|
|
| | return value of the loop. |
|
|
|
|
| | eg: |
|
|
|
|
| | while ((x := (x - 1)) > 0) |
|
|
|
|
| | { |
|
|
|
|
| | y := x + z; |
|
|
|
|
| | w := z + y; |
|
|
|
|
| | } |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| repeat/ | The structure will repeatedly evaluate the internal |
|
|
|
|
| until | statement(s) 'until' the condition is true. The final |
|
|
|
|
| | statement in the final iteration will be used as the |
|
|
|
|
| | return value of the loop. |
|
|
|
|
| | eg: |
|
|
|
|
| | repeat |
|
|
|
|
| | y := x + z; |
|
|
|
|
| | w := z + y; |
|
|
|
|
| | until ((x := (x - 1)) <= 0) |
|
|
|
|
+----------+---------------------------------------------------------+
|
|
|
|
| ~ | Evaluate each sub-expression, then return as the result |
|
|
|
|
| | the value of the last sub-expression. This is sometimes |
|
|
|
|
| | known as multiple sequence point evaluation. |
|
|
|
|
| | eg: |
|
|
|
|
| | ~(i := x + 1, j := y / z, k := sin(w/u)) == (sin(w/u))) |
|
|
|
|
| | ~{i := x + 1; j := y / z; k := sin(w/u)} == (sin(w/u))) |
|
|
|
|
+----------+---------------------------------------------------------+
|
2014-01-05 08:49:26 +00:00
|
|
|
| [*] | Evaluate any consequent for which its case statement is |
|
|
|
|
| | true. The return value will be either zero or the result|
|
|
|
|
| | of the last consequent to have been evaluated. |
|
|
|
|
| | eg: |
|
|
|
|
| | [*] |
|
|
|
|
| | { |
|
2014-01-14 20:13:18 +00:00
|
|
|
| | case (x + 1) > (y - 2) : x := z / 2 + sin(y / pi); |
|
|
|
|
| | case (x + 2) < abs(y + 3): w / 4 + min(5y,9); |
|
|
|
|
| | case (x + 3) = (y * 4) : y := abs(z / 6) + 7y; |
|
2014-01-05 08:49:26 +00:00
|
|
|
| | } |
|
|
|
|
+----------+---------------------------------------------------------+
|
2013-04-27 03:55:23 +00:00
|
|
|
|
2013-03-11 13:29:59 +00:00
|
|
|
|
|
|
|
|
2013-10-16 21:44:15 +00:00
|
|
|
[09 - COMPONENTS]
|
|
|
|
There are three primary components, that are specialized upon a given
|
|
|
|
numeric type, which make up the core of ExprTk. The components are as
|
|
|
|
follows:
|
|
|
|
|
2014-01-21 21:13:37 +00:00
|
|
|
1. Symbol Table exprtk::symbol_table<NumericType>
|
|
|
|
2. Expression exprtk::expression<NumericType>
|
|
|
|
3. Parser exprtk::parser<NumericType>
|
2013-10-16 21:44:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
(1) Symbol Table
|
|
|
|
A structure that is used to store references to variables, constants
|
2014-02-09 09:20:15 +00:00
|
|
|
and functions that are to be used within expressions. Furthermore in
|
|
|
|
the context of composited recursive functions the symbol table can
|
|
|
|
also be thought of as a simple representation of a stack specific for
|
|
|
|
the expression(s) that reference it.
|
|
|
|
|
2013-10-16 21:44:15 +00:00
|
|
|
|
|
|
|
(2) Expression
|
|
|
|
A structure that holds an AST for a specified expression and is used
|
|
|
|
to evaluate said expression. If a compiled Expression uses variables
|
|
|
|
or user defined functions, it will then also have an associated Symbol
|
|
|
|
Table, which will contain references to said variables, functions et
|
2014-01-03 10:42:54 +00:00
|
|
|
al. An example AST structure for the denoted expression is as follows:
|
|
|
|
|
|
|
|
Expression: z := (x + y^-2.345) * sin(pi / min(w - 7.3,v))
|
|
|
|
|
|
|
|
[Root]
|
|
|
|
|
|
|
|
|
[Assignment]
|
|
|
|
________/ \_____
|
|
|
|
/ \
|
|
|
|
Variable(z) [Multiply]
|
|
|
|
____________/ \___________
|
|
|
|
/ \
|
|
|
|
/ [Unary-Func(sin)]
|
|
|
|
[Addition] |
|
|
|
|
____/ \____ [Division]
|
|
|
|
/ \ ___/ \___
|
|
|
|
Variable(x) [Power] / \
|
|
|
|
______/ \______ Constant(pi) [Binary-Func(min)]
|
2014-01-21 21:13:37 +00:00
|
|
|
/ \ ____/ \____
|
|
|
|
Variable(y) [Negate] / \
|
|
|
|
| / Variable(v)
|
|
|
|
Constant(2.345) /
|
|
|
|
/
|
|
|
|
[Subtract]
|
|
|
|
____/ \____
|
|
|
|
/ \
|
|
|
|
Variable(w) Constant(7.3)
|
2013-10-16 21:44:15 +00:00
|
|
|
|
2014-02-09 09:20:15 +00:00
|
|
|
|
2013-10-16 21:44:15 +00:00
|
|
|
(3) Parser
|
|
|
|
A structure which takes as input a string representation of an
|
|
|
|
expression and attempts to compile said input with the result being an
|
|
|
|
instance of Expression. If an error is encountered during the
|
|
|
|
compilation process, the parser will stop compiling and return an
|
|
|
|
error status code, with a more detailed description of the error(s)
|
|
|
|
and its location within the input provided by the 'get_error'
|
|
|
|
interface.
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-09 09:20:15 +00:00
|
|
|
[10 - COMPILATION OPTIONS]
|
|
|
|
The exprtk::parser when being instantiated takes as input a set of
|
|
|
|
options to be used during the compilation process of expressions.
|
|
|
|
An example instantiation of exprtk::parser where only the joiner,
|
|
|
|
commutative and strength reduction options are enabled is as follows:
|
|
|
|
|
|
|
|
const std::size_t compile_options = e_joiner +
|
|
|
|
e_commutative_check +
|
|
|
|
e_strength_reduction;
|
|
|
|
|
|
|
|
exprtk::parser<NumericType> parser(compile_options);
|
|
|
|
|
|
|
|
|
|
|
|
Currently seven types of options are supported, and enabled by
|
|
|
|
default. The options and their explanations are as follows:
|
|
|
|
|
|
|
|
(1) Replacer (e_replacer)
|
|
|
|
Enable replacement of specific tokens with other tokens. For example
|
|
|
|
the token "true" of type symbol will be replaced with the numeric
|
|
|
|
token of value one.
|
|
|
|
|
|
|
|
(a) (x < y) == true ---> (x < y) == 1
|
|
|
|
(b) false == (x > y) ---> 0 == (x > y)
|
|
|
|
|
|
|
|
|
|
|
|
(2) Joiner (e_joiner)
|
|
|
|
Enable joining of multi-character operators that may have been
|
|
|
|
incorrectly disjoint in the string representation of the specified
|
|
|
|
expression. For example the consecutive tokens of ">" "=" will become
|
|
|
|
">=" representing the "greater than or equal to" operator. If not
|
|
|
|
properly resolved the original form will cause a compilation error.
|
|
|
|
The following is a listing of the scenarios that the joiner can
|
|
|
|
handle:
|
|
|
|
|
|
|
|
(a) ':' '=' ---> ':=' (assignment)
|
|
|
|
(b) '>' '=' ---> '>=' (gte)
|
|
|
|
(c) '<' '=' ---> '<=' (lte)
|
|
|
|
(d) '=' '=' ---> '==' (equal)
|
|
|
|
(e) '!' '=' ---> '!=' (not-equal)
|
|
|
|
(f) '<' '>' ---> '<>' (not-equal)
|
|
|
|
|
|
|
|
An example of the transformation that takes place is as follows:
|
|
|
|
|
|
|
|
(a) (x > = y) and (z ! = w) ---> (x >= y) and (z != w)
|
|
|
|
|
|
|
|
|
|
|
|
(3) Numeric Check (e_numeric_check)
|
|
|
|
Enable validation of tokens representing numeric types so as to catch
|
|
|
|
any errors prior to the costly process of the main compilation step
|
|
|
|
commencing.
|
|
|
|
|
|
|
|
|
|
|
|
(4) Bracket Check (e_bracket_check)
|
|
|
|
Enable the check for validating the ordering of brackets in the
|
|
|
|
specified expression.
|
|
|
|
|
|
|
|
|
|
|
|
(5) Sequence Check (e_sequence_check)
|
|
|
|
Enable the check for validating that sequences of either pairs or
|
|
|
|
triplets of tokens make sense. For example the following sequence of
|
|
|
|
tokens when encountered will raise an error:
|
|
|
|
|
|
|
|
(a) (x + * 3) ---> sequence error
|
|
|
|
|
|
|
|
|
|
|
|
(6) Commutative Check (e_commutative_check)
|
|
|
|
Enable the check that will transform sequences of pairs of tokens that
|
|
|
|
imply a multiplication operation. The following are some examples of
|
|
|
|
such transformations:
|
|
|
|
|
|
|
|
(a) 2x ---> 2 * x
|
|
|
|
(b) 25x^3 ---> 25 * x^3
|
|
|
|
(c) 3(x + 1) ---> 3 * (x + 1)
|
|
|
|
(d) (x + 1)4 ---> (x + 1) * 4
|
|
|
|
(e) 5foo(x,y) ---> 5 * foo(x,y)
|
|
|
|
(f) foo(x,y)6 + 1 ---> foo(x,y) * 6 + 1
|
|
|
|
|
|
|
|
|
|
|
|
(7) Strength Reduction Check (e_strength_reduction)
|
|
|
|
Enable the use of strength reduction optimisations during the
|
|
|
|
compilation process. In ExprTk strength reduction optimisations
|
|
|
|
predominantly involve transforming sub-expressions into other forms
|
|
|
|
that are algebraically equivalent yet less costly to compute. The
|
|
|
|
following are examples of the various transformations that can occur:
|
|
|
|
|
|
|
|
(a) (x / y) / z ---> x / (y * z)
|
|
|
|
(b) (x / y) / (z / w) ---> (x * w) / (y * z)
|
|
|
|
(c) (2 * x) - (2 * y) ---> 2 * (x - y)
|
|
|
|
(d) (2 / x) / (3 / y) ---> (2 / 3) / (x * y)
|
|
|
|
(e) (2 * x) * (3 * y) ---> (2 * 3) * (x * y)
|
|
|
|
|
|
|
|
Note:
|
|
|
|
When using strength reduction in conjunction with expressions whose
|
|
|
|
inputs or sub-expressions may result in values nearing either of the
|
|
|
|
bounds of the underlying numeric type (eg: double), there may be the
|
|
|
|
possibility of a decrease in the precision of results.
|
|
|
|
|
|
|
|
In the following example the given expression which represents an
|
|
|
|
attempt at computing the average between x and y will be transformed
|
|
|
|
as follows:
|
|
|
|
|
|
|
|
(x * 0.5) + (y * 0.5) ---> 0.5 * (x + y)
|
|
|
|
|
|
|
|
There may be situations where the above transformation will cause
|
|
|
|
numerical overflows and that the original form of the expression is
|
|
|
|
desired over the strength reduced form. In these situations it is best
|
|
|
|
to turn off strength reduction optimisations or to use a type with a
|
|
|
|
larger numerical bound.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[11 - SPECIAL FUNCTIONS]
|
2013-03-26 14:35:51 +00:00
|
|
|
The purpose of special functions in ExprTk is to provide compiler
|
|
|
|
generated equivalents of common mathematical expressions which can be
|
|
|
|
invoked by using the 'special function' syntax (eg: $f12(x,y,z) or
|
2013-07-08 22:32:08 +00:00
|
|
|
$f82(x,y,z,w)).
|
2012-05-11 22:18:14 +00:00
|
|
|
|
|
|
|
Special functions dramatically decrease the total evaluation time of
|
|
|
|
expressions which would otherwise have been written using the common
|
|
|
|
form by reducing the total number of nodes in the evaluation tree of
|
|
|
|
an expression and by also leveraging the compiler's ability to
|
|
|
|
correctly optimize such expressions for a given architecture.
|
|
|
|
|
2013-03-04 13:21:28 +00:00
|
|
|
3-Parameter 4-Parameter
|
|
|
|
+-------------+-------------+ +--------------+------------------+
|
|
|
|
| Prototype | Operation | | Prototype | Operation |
|
|
|
|
+-------------+-------------+ +--------------+------------------+
|
2014-01-21 21:13:37 +00:00
|
|
|
$f00(x,y,z) | (x + y) / z $f48(x,y,z,w) | x + ((y + z) / w)
|
|
|
|
$f01(x,y,z) | (x + y) * z $f49(x,y,z,w) | x + ((y + z) * w)
|
|
|
|
$f02(x,y,z) | (x + y) - z $f50(x,y,z,w) | x + ((y - z) / w)
|
|
|
|
$f03(x,y,z) | (x + y) + z $f51(x,y,z,w) | x + ((y - z) * w)
|
|
|
|
$f04(x,y,z) | (x - y) + z $f52(x,y,z,w) | x + ((y * z) / w)
|
|
|
|
$f05(x,y,z) | (x - y) / z $f53(x,y,z,w) | x + ((y * z) * w)
|
|
|
|
$f06(x,y,z) | (x - y) * z $f54(x,y,z,w) | x + ((y / z) + w)
|
|
|
|
$f07(x,y,z) | (x * y) + z $f55(x,y,z,w) | x + ((y / z) / w)
|
|
|
|
$f08(x,y,z) | (x * y) - z $f56(x,y,z,w) | x + ((y / z) * w)
|
|
|
|
$f09(x,y,z) | (x * y) / z $f57(x,y,z,w) | x - ((y + z) / w)
|
|
|
|
$f10(x,y,z) | (x * y) * z $f58(x,y,z,w) | x - ((y + z) * w)
|
|
|
|
$f11(x,y,z) | (x / y) + z $f59(x,y,z,w) | x - ((y - z) / w)
|
|
|
|
$f12(x,y,z) | (x / y) - z $f60(x,y,z,w) | x - ((y - z) * w)
|
|
|
|
$f13(x,y,z) | (x / y) / z $f61(x,y,z,w) | x - ((y * z) / w)
|
|
|
|
$f14(x,y,z) | (x / y) * z $f62(x,y,z,w) | x - ((y * z) * w)
|
|
|
|
$f15(x,y,z) | x / (y + z) $f63(x,y,z,w) | x - ((y / z) / w)
|
|
|
|
$f16(x,y,z) | x / (y - z) $f64(x,y,z,w) | x - ((y / z) * w)
|
|
|
|
$f17(x,y,z) | x / (y * z) $f65(x,y,z,w) | ((x + y) * z) - w
|
|
|
|
$f18(x,y,z) | x / (y / z) $f66(x,y,z,w) | ((x - y) * z) - w
|
|
|
|
$f19(x,y,z) | x * (y + z) $f67(x,y,z,w) | ((x * y) * z) - w
|
|
|
|
$f20(x,y,z) | x * (y - z) $f68(x,y,z,w) | ((x / y) * z) - w
|
|
|
|
$f21(x,y,z) | x * (y * z) $f69(x,y,z,w) | ((x + y) / z) - w
|
|
|
|
$f22(x,y,z) | x * (y / z) $f70(x,y,z,w) | ((x - y) / z) - w
|
|
|
|
$f23(x,y,z) | x - (y + z) $f71(x,y,z,w) | ((x * y) / z) - w
|
|
|
|
$f24(x,y,z) | x - (y - z) $f72(x,y,z,w) | ((x / y) / z) - w
|
|
|
|
$f25(x,y,z) | x - (y / z) $f73(x,y,z,w) | (x * y) + (z * w)
|
|
|
|
$f26(x,y,z) | x - (y * z) $f74(x,y,z,w) | (x * y) - (z * w)
|
|
|
|
$f27(x,y,z) | x + (y * z) $f75(x,y,z,w) | (x * y) + (z / w)
|
|
|
|
$f28(x,y,z) | x + (y / z) $f76(x,y,z,w) | (x * y) - (z / w)
|
|
|
|
$f29(x,y,z) | x + (y + z) $f77(x,y,z,w) | (x / y) + (z / w)
|
|
|
|
$f30(x,y,z) | x + (y - z) $f78(x,y,z,w) | (x / y) - (z / w)
|
|
|
|
$f31(x,y,z) | x * y^2 + z $f79(x,y,z,w) | (x / y) - (z * w)
|
|
|
|
$f32(x,y,z) | x * y^3 + z $f80(x,y,z,w) | x / (y + (z * w))
|
|
|
|
$f33(x,y,z) | x * y^4 + z $f81(x,y,z,w) | x / (y - (z * w))
|
|
|
|
$f34(x,y,z) | x * y^5 + z $f82(x,y,z,w) | x * (y + (z * w))
|
|
|
|
$f35(x,y,z) | x * y^6 + z $f83(x,y,z,w) | x * (y - (z * w))
|
|
|
|
$f36(x,y,z) | x * y^7 + z $f84(x,y,z,w) | x*y^2 + z*w^2
|
|
|
|
$f37(x,y,z) | x * y^8 + z $f85(x,y,z,w) | x*y^3 + z*w^3
|
|
|
|
$f38(x,y,z) | x * y^9 + z $f86(x,y,z,w) | x*y^4 + z*w^4
|
|
|
|
$f39(x,y,z) | x * log(y)+z $f87(x,y,z,w) | x*y^5 + z*w^5
|
|
|
|
$f40(x,y,z) | x * log(y)-z $f88(x,y,z,w) | x*y^6 + z*w^6
|
|
|
|
$f41(x,y,z) | x * log10(y)+z $f89(x,y,z,w) | x*y^7 + z*w^7
|
|
|
|
$f42(x,y,z) | x * log10(y)-z $f90(x,y,z,w) | x*y^8 + z*w^8
|
|
|
|
$f43(x,y,z) | x * sin(y)+z $f91(x,y,z,w) | x*y^9 + z*w^9
|
|
|
|
$f44(x,y,z) | x * sin(y)-z $f92(x,y,z,w) | (x and y) ? z : w
|
|
|
|
$f45(x,y,z) | x * cos(y)+z $f93(x,y,z,w) | (x or y) ? z : w
|
|
|
|
$f46(x,y,z) | x * cos(y)-z $f94(x,y,z,w) | (x < y) ? z : w
|
|
|
|
$f47(x,y,z) | x ? y : z $f95(x,y,z,w) | (x <= y) ? z : w
|
|
|
|
$f96(x,y,z,w) | (x > y) ? z : w
|
|
|
|
$f97(x,y,z,w) | (x >= y) ? z : w
|
|
|
|
$f98(x,y,z,w) | (x == y) ? z : w
|
2014-02-09 09:20:15 +00:00
|
|
|
$f99(x,y,z,w) | x*sin(y)+z*cos(w)
|
2012-05-16 22:14:45 +00:00
|
|
|
|
|
|
|
|
2012-05-01 12:43:33 +00:00
|
|
|
|
2014-02-09 09:20:15 +00:00
|
|
|
[12 - EXPRTK NOTES]
|
2013-04-04 09:45:19 +00:00
|
|
|
(00) Precision and performance of expression evaluations are the
|
|
|
|
dominant principles of the ExprTk library.
|
|
|
|
|
|
|
|
(01) Supported types are float, double and long double.
|
2013-03-14 21:38:09 +00:00
|
|
|
|
2013-04-04 09:45:19 +00:00
|
|
|
(02) Standard mathematical operator precedence is applied (BEDMAS).
|
2013-03-14 21:38:09 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(03) Results of expressions that are deemed as being 'valid' are to
|
|
|
|
exist within the set of Real numbers. All other results will be
|
2013-10-16 21:44:15 +00:00
|
|
|
of the value: Not-A-Number (NaN).
|
2013-07-28 11:35:06 +00:00
|
|
|
|
|
|
|
(04) Supported user defined types are numeric and string variables
|
2013-03-14 21:38:09 +00:00
|
|
|
and functions.
|
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(05) All variable and function names are case-insensitive.
|
2013-03-14 21:38:09 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(06) Variable and function names must begin with a letter
|
2013-04-04 09:45:19 +00:00
|
|
|
(A-Z or a-z), then can be comprised of any combination of
|
|
|
|
letters, digits and underscores. (eg: x, var1 or power_func99)
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(07) Expression lengths and sub-expression lists are limited only by
|
2013-07-08 22:32:08 +00:00
|
|
|
storage capacity.
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(08) The life-time of objects registered with or created from a
|
|
|
|
specific symbol-table must span at least the life-time of the
|
|
|
|
compiled expressions which utilize objects, such as variables,
|
|
|
|
of that symbol-table, otherwise the result will be undefined
|
|
|
|
behavior.
|
2013-04-01 09:30:28 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(09) Equal/Nequal are normalized equality routines, which use
|
2013-04-04 09:45:19 +00:00
|
|
|
epsilons of 0.0000000001 and 0.000001 for double and float
|
|
|
|
types respectively.
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(10) All trigonometric functions assume radian input unless
|
2013-03-14 21:38:09 +00:00
|
|
|
stated otherwise.
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(11) Expressions may contain white-space characters such as
|
2013-03-14 21:38:09 +00:00
|
|
|
space, tabs, new-lines, control-feed et al.
|
|
|
|
('\n', '\r', '\t', '\b', '\v', '\f')
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(12) Strings may be constructed from any letters, digits or special
|
2013-03-14 21:38:09 +00:00
|
|
|
characters such as (~!@#$%^&*()[]|=+ ,./?<>;:"`~_), and must
|
|
|
|
be enclosed with single-quotes.
|
2014-01-05 08:49:26 +00:00
|
|
|
eg: 'Frankly my dear, I do not give a damn!'
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(13) User defined normal functions can have up to 20 parameters,
|
2013-04-01 09:30:28 +00:00
|
|
|
where as user defined vararg-functions can have an unlimited
|
2013-03-31 23:56:44 +00:00
|
|
|
number of parameters.
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
(14) The inbuilt polynomial functions can be at most of degree 12.
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2014-01-22 20:32:55 +00:00
|
|
|
(15) Where appropriate constant folding optimisations may be
|
2013-03-14 21:38:09 +00:00
|
|
|
applied. (eg: The expression '2+(3-(x/y))' becomes '5-(x/y)')
|
2013-03-11 13:29:59 +00:00
|
|
|
|
2014-02-09 09:20:15 +00:00
|
|
|
(16) If the strength reduction compilation option has been enabled,
|
|
|
|
then where applicable strength reduction optimisations may be
|
|
|
|
applied.
|
2014-01-22 20:32:55 +00:00
|
|
|
|
|
|
|
(17) String processing capabilities are available by default.
|
2013-03-24 17:54:44 +00:00
|
|
|
To turn them off, the following needs to be defined at
|
|
|
|
compile time: exprtk_disable_string_capabilities
|
|
|
|
|
2014-01-22 20:32:55 +00:00
|
|
|
(18) Composited functions can call themselves or any other functions
|
2013-04-17 14:43:00 +00:00
|
|
|
that have been defined prior to their own definition.
|
2013-04-13 08:26:57 +00:00
|
|
|
|
2014-01-22 20:32:55 +00:00
|
|
|
(19) Recursive calls made from within composited functions will have
|
2013-07-12 13:08:57 +00:00
|
|
|
a stack size bound by the stack of the executing architecture.
|
2013-07-08 22:32:08 +00:00
|
|
|
|
2014-01-22 20:32:55 +00:00
|
|
|
(20) The entity relationship between symbol_table and an expression
|
2014-01-21 21:13:37 +00:00
|
|
|
is one-to-many. Hence the intended use case is to have a single
|
|
|
|
symbol table manage the variable and function requirements of
|
2014-02-09 09:20:15 +00:00
|
|
|
multiple expressions.
|
2014-01-21 21:13:37 +00:00
|
|
|
|
2014-01-22 20:32:55 +00:00
|
|
|
(21) The common use-case for an expression is to have it compiled
|
2014-01-21 21:13:37 +00:00
|
|
|
only once and then subsequently have it evaluated multiple
|
|
|
|
times. An extremely inefficient approach would be to recompile
|
|
|
|
an expression from its string form every time it requires
|
|
|
|
evaluating.
|
|
|
|
|
2014-01-22 20:32:55 +00:00
|
|
|
(22) The following are examples of compliant floating point value
|
2013-12-30 10:09:57 +00:00
|
|
|
representations:
|
2013-12-08 22:30:01 +00:00
|
|
|
(a) 12345 (b) -123.456
|
|
|
|
(c) +123.456e+12 (d) 123.456E-12
|
2013-12-30 10:09:57 +00:00
|
|
|
(e) +012.045e+07 (f) .1234
|
|
|
|
(g) 123.456f (h) -321.654E+3L
|
2013-12-08 22:30:01 +00:00
|
|
|
|
2014-01-22 20:32:55 +00:00
|
|
|
(23) Expressions may contain any of the following comment styles:
|
2013-03-26 14:20:20 +00:00
|
|
|
1. // .... \n
|
|
|
|
2. # .... \n
|
|
|
|
3. /* .... */
|
2013-03-24 17:54:44 +00:00
|
|
|
|
2013-03-11 13:29:59 +00:00
|
|
|
|
|
|
|
|
2014-02-09 09:20:15 +00:00
|
|
|
[13 - SIMPLE EXPRTK EXAMPLE]
|
2013-01-30 13:28:22 +00:00
|
|
|
--- snip ---
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "exprtk.hpp"
|
|
|
|
|
2013-04-01 09:30:28 +00:00
|
|
|
template <typename T>
|
|
|
|
struct myfunc : public exprtk::ifunction<T>
|
|
|
|
{
|
|
|
|
myfunc() : exprtk::ifunction<T>(2) {}
|
|
|
|
|
2013-12-30 10:09:57 +00:00
|
|
|
T operator()(const T& v1, const T& v2)
|
2013-04-01 09:30:28 +00:00
|
|
|
{
|
|
|
|
return T(1) + (v1 * v2) / T(3);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-30 13:28:22 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
typedef exprtk::symbol_table<double> symbol_table_t;
|
|
|
|
typedef exprtk::expression<double> expression_t;
|
|
|
|
typedef exprtk::parser<double> parser_t;
|
|
|
|
typedef exprtk::parser_error::type error_t;
|
|
|
|
|
2013-04-01 20:52:06 +00:00
|
|
|
std::string expression_str = "z := 2 myfunc([4+sin(x/pi)^3],y^2)";
|
2013-01-30 13:28:22 +00:00
|
|
|
|
|
|
|
double x = 1.1;
|
|
|
|
double y = 2.2;
|
|
|
|
double z = 3.3;
|
|
|
|
|
2013-04-01 09:30:28 +00:00
|
|
|
myfunc<double> mf;
|
|
|
|
|
2013-01-30 13:28:22 +00:00
|
|
|
symbol_table_t symbol_table;
|
|
|
|
symbol_table.add_constants();
|
|
|
|
symbol_table.add_variable("x",x);
|
|
|
|
symbol_table.add_variable("y",y);
|
|
|
|
symbol_table.add_variable("z",z);
|
2013-04-01 09:30:28 +00:00
|
|
|
symbol_table.add_function("myfunc",mf);
|
2013-01-30 13:28:22 +00:00
|
|
|
|
|
|
|
expression_t expression;
|
|
|
|
expression.register_symbol_table(symbol_table);
|
|
|
|
|
|
|
|
parser_t parser;
|
|
|
|
|
|
|
|
if (!parser.compile(expression_str,expression))
|
|
|
|
{
|
2014-02-09 09:20:15 +00:00
|
|
|
// A compilation error has occured. Attempt to
|
|
|
|
// print all errors to the stdout.
|
|
|
|
|
2013-01-30 13:28:22 +00:00
|
|
|
printf("Error: %s\tExpression: %s\n",
|
|
|
|
parser.error().c_str(),
|
|
|
|
expression_str.c_str());
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < parser.error_count(); ++i)
|
|
|
|
{
|
2014-02-09 09:20:15 +00:00
|
|
|
// Include the specific nature of each error
|
|
|
|
// and its position in the expression string.
|
|
|
|
|
2013-01-30 13:28:22 +00:00
|
|
|
error_t error = parser.get_error(i);
|
2013-10-16 21:44:15 +00:00
|
|
|
printf("Error: %02d Position: %02d "
|
|
|
|
"Type: [%s] "
|
|
|
|
"Message: %s "
|
|
|
|
"Expression: %s\n",
|
2013-01-30 13:28:22 +00:00
|
|
|
static_cast<int>(i),
|
|
|
|
static_cast<int>(error.token.position),
|
|
|
|
exprtk::parser_error::to_str(error.mode).c_str(),
|
|
|
|
error.diagnostic.c_str(),
|
|
|
|
expression_str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-02-09 09:20:15 +00:00
|
|
|
// Evaluate the expression and obtain its result.
|
|
|
|
|
2013-01-30 13:28:22 +00:00
|
|
|
double result = expression.value();
|
|
|
|
|
|
|
|
printf("Result: %10.5f\n",result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
--- snip ---
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-09 09:20:15 +00:00
|
|
|
[14 - FILES]
|
2012-01-28 05:25:39 +00:00
|
|
|
(00) Makefile
|
|
|
|
(01) readme.txt
|
|
|
|
(02) exprtk.hpp
|
|
|
|
(03) exprtk_test.cpp
|
2012-03-13 20:12:58 +00:00
|
|
|
(04) exprtk_benchmark.cpp
|
2013-04-23 11:30:49 +00:00
|
|
|
(05) exprtk_simple_example_01.cpp
|
|
|
|
(06) exprtk_simple_example_02.cpp
|
|
|
|
(07) exprtk_simple_example_03.cpp
|
|
|
|
(08) exprtk_simple_example_04.cpp
|
|
|
|
(09) exprtk_simple_example_05.cpp
|
|
|
|
(10) exprtk_simple_example_06.cpp
|
|
|
|
(11) exprtk_simple_example_07.cpp
|
|
|
|
(12) exprtk_simple_example_08.cpp
|
|
|
|
(13) exprtk_simple_example_09.cpp
|
|
|
|
(14) exprtk_simple_example_10.cpp
|