Skip to content

Commit

Permalink
C++ Mathematical Expression Library (ExprTk) http://www.partow.net/pr…
Browse files Browse the repository at this point in the history
  • Loading branch information
ArashPartow committed Mar 28, 2015
1 parent 73b1e4c commit 53b2977
Show file tree
Hide file tree
Showing 8 changed files with 3,914 additions and 2,891 deletions.
1,744 changes: 1,310 additions & 434 deletions exprtk.hpp

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions exprtk_simple_example_01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@
template <typename T>
void trig_function()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string expression_string = "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";
T x;
exprtk::symbol_table<T> symbol_table;

symbol_table_t symbol_table;
symbol_table.add_variable("x",x);
symbol_table.add_constants();

exprtk::expression<T> expression;
expression_t expression;
expression.register_symbol_table(symbol_table);

exprtk::parser<T> parser;
parser_t parser;
parser.compile(expression_string,expression);

for (x = T(-5); x <= T(+5); x += T(0.001))
Expand Down
10 changes: 7 additions & 3 deletions exprtk_simple_example_02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
template <typename T>
void square_wave()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string expr_string = "a*(4/pi)*"
"((1 /1)*sin( 2*pi*f*t)+(1 /3)*sin( 6*pi*f*t)+"
" (1 /5)*sin(10*pi*f*t)+(1 /7)*sin(14*pi*f*t)+"
Expand All @@ -39,16 +43,16 @@ void square_wave()
T t = T(0);
T a = T(10);

exprtk::symbol_table<T> symbol_table;
symbol_table_t symbol_table;
symbol_table.add_variable("f",f);
symbol_table.add_variable("t",t);
symbol_table.add_variable("a",a);
symbol_table.add_constants();

exprtk::expression<T> expression;
expression_t expression;
expression.register_symbol_table(symbol_table);

exprtk::parser<T> parser;
parser_t parser;
parser.compile(expr_string,expression);

const T delta = (T(4) * pi) / T(1000);
Expand Down
10 changes: 7 additions & 3 deletions exprtk_simple_example_03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@
template <typename T>
void polynomial()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string expression_string = "25x^5 - 35x^4 - 15x^3 + 40x^2 - 15x + 1";

T r0 = T(0);
T r1 = T(1);
T x = T(0);

exprtk::symbol_table<T> symbol_table;
symbol_table_t symbol_table;
symbol_table.add_variable("x",x);

exprtk::expression<T> expression;
expression_t expression;
expression.register_symbol_table(symbol_table);

exprtk::parser<T> parser;
parser_t parser;
parser.compile(expression_string,expression);

const T delta = T(1.0 / 100.0);
Expand Down
10 changes: 7 additions & 3 deletions exprtk_simple_example_05.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ struct myfunc : public exprtk::ifunction<T>
template <typename T>
void custom_function()
{
typedef exprtk::expression<T> expression_t;
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string expression_string = "myfunc(sin(x * pi),y / 2)";

T x = T(1);
T y = T(2);
myfunc<T> mf;

exprtk::symbol_table<T> symbol_table;
symbol_table_t symbol_table;
symbol_table.add_variable("x",x);
symbol_table.add_variable("y",y);
symbol_table.add_function("myfunc",mf);
Expand All @@ -52,7 +56,7 @@ void custom_function()
expression_t expression;
expression.register_symbol_table(symbol_table);

exprtk::parser<T> parser;
parser_t parser;
parser.compile(expression_string,expression);

T result = expression.value();
Expand Down
9 changes: 6 additions & 3 deletions exprtk_simple_example_07.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@
template <typename T>
void logic()
{
typedef exprtk::expression<T> expression_t;
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string expression_string = "not(A and B) or C";

exprtk::symbol_table<T> symbol_table;
symbol_table_t symbol_table;
symbol_table.create_variable("A");
symbol_table.create_variable("B");
symbol_table.create_variable("C");

expression_t expression;
expression.register_symbol_table(symbol_table);

exprtk::parser<T> parser;
parser_t parser;
parser.compile(expression_string,expression);

printf(" # | A | B | C | %s\n"
Expand Down
Loading

0 comments on commit 53b2977

Please sign in to comment.