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 Dec 31, 2019
1 parent e0e880c commit 7c9b237
Show file tree
Hide file tree
Showing 24 changed files with 1,708 additions and 771 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# **************************************************************
# * C++ Mathematical Expression Toolkit Library *
# * *
# * Author: Arash Partow (1999-2019) *
# * Author: Arash Partow (1999-2020) *
# * URL: http://www.partow.net/programming/exprtk/index.html *
# * *
# * Copyright notice: *
Expand Down
1,748 changes: 1,055 additions & 693 deletions exprtk.hpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion exprtk_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* ExprTk vs Native Benchmarks *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand Down
9 changes: 5 additions & 4 deletions exprtk_simple_example_01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 1 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand All @@ -29,7 +29,8 @@ void trig_function()
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)";
const std::string expression_string =
"clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";

T x;

Expand All @@ -45,8 +46,8 @@ void trig_function()

for (x = T(-5); x <= T(+5); x += T(0.001))
{
T y = expression.value();
printf("%19.15f\t%19.15f\n",x,y);
const T y = expression.value();
printf("%19.15f\t%19.15f\n", x, y);
}
}

Expand Down
23 changes: 12 additions & 11 deletions exprtk_simple_example_02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 2 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand All @@ -29,14 +29,15 @@ void square_wave()
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)+"
" (1 /9)*sin(18*pi*f*t)+(1/11)*sin(22*pi*f*t)+"
" (1/13)*sin(26*pi*f*t)+(1/15)*sin(30*pi*f*t)+"
" (1/17)*sin(34*pi*f*t)+(1/19)*sin(38*pi*f*t)+"
" (1/21)*sin(42*pi*f*t)+(1/23)*sin(46*pi*f*t)+"
" (1/25)*sin(50*pi*f*t)+(1/27)*sin(54*pi*f*t))";
const 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)+"
" (1 /9)*sin(18*pi*f*t)+(1/11)*sin(22*pi*f*t)+"
" (1/13)*sin(26*pi*f*t)+(1/15)*sin(30*pi*f*t)+"
" (1/17)*sin(34*pi*f*t)+(1/19)*sin(38*pi*f*t)+"
" (1/21)*sin(42*pi*f*t)+(1/23)*sin(46*pi*f*t)+"
" (1/25)*sin(50*pi*f*t)+(1/27)*sin(54*pi*f*t))";

static const T pi = T(3.141592653589793238462643383279502);

Expand All @@ -60,8 +61,8 @@ void square_wave()

for (t = (T(-2) * pi); t <= (T(+2) * pi); t += delta)
{
T result = expression.value();
printf("%19.15f\t%19.15f\n",t,result);
const T result = expression.value();
printf("%19.15f\t%19.15f\n", t, result);
}
}

Expand Down
7 changes: 4 additions & 3 deletions exprtk_simple_example_03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 3 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand All @@ -29,7 +29,8 @@ void polynomial()
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";
const std::string expression_string =
"25x^5 - 35x^4 - 15x^3 + 40x^2 - 15x + 1";

const T r0 = T(0);
const T r1 = T(1);
Expand All @@ -48,7 +49,7 @@ void polynomial()

for (x = r0; x <= r1; x += delta)
{
printf("%19.15f\t%19.15f\n",x,expression.value());
printf("%19.15f\t%19.15f\n", x, expression.value());
}
}

Expand Down
4 changes: 2 additions & 2 deletions exprtk_simple_example_04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 4 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand Down Expand Up @@ -73,7 +73,7 @@ void fibonacci()
{
x = static_cast<T>(i);

T result = expression.value();
const T result = expression.value();

printf("fibonacci(%3d) = %10.0f\n",
static_cast<int>(i),
Expand Down
6 changes: 3 additions & 3 deletions exprtk_simple_example_05.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 5 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand Down Expand Up @@ -50,7 +50,7 @@ void custom_function()
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

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

T x = T(1);
Expand All @@ -70,7 +70,7 @@ void custom_function()
parser_t parser;
parser.compile(expression_string,expression);

T result = expression.value();
const T result = expression.value();
printf("Result: %10.5f\n",result);
}

Expand Down
4 changes: 2 additions & 2 deletions exprtk_simple_example_06.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 6 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand All @@ -29,7 +29,7 @@ void vector_function()
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string expression_string =
const std::string expression_string =
" for (var i := 0; i < min(x[],y[],z[]); i += 1) "
" { "
" z[i] := 3sin(x[i]) + 2log(y[i]); "
Expand Down
6 changes: 3 additions & 3 deletions exprtk_simple_example_07.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 7 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand All @@ -29,7 +29,7 @@ void logic()
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

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

symbol_table_t symbol_table;
symbol_table.create_variable("A");
Expand All @@ -53,7 +53,7 @@ void logic()
symbol_table.get_variable("B")->ref() = T((i & 0x02) ? 1 : 0);
symbol_table.get_variable("C")->ref() = T((i & 0x04) ? 1 : 0);

int result = static_cast<int>(expression.value());
const int result = static_cast<int>(expression.value());

printf(" %d | %d | %d | %d | %d \n",
i,
Expand Down
6 changes: 3 additions & 3 deletions exprtk_simple_example_08.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 8 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand Down Expand Up @@ -65,7 +65,7 @@ void composite()

for (std::size_t i = 0; i < parser.error_count(); ++i)
{
err_t error = parser.get_error(i);
const err_t error = parser.get_error(i);

printf("Error: %02d Position: %02d Type: [%14s] Msg: %s\tExpression: %s\n",
static_cast<unsigned int>(i),
Expand All @@ -78,7 +78,7 @@ void composite()
return;
}

T result = expression.value();
const T result = expression.value();

printf("%s = %e\n", expression_string.c_str(), result);
}
Expand Down
8 changes: 4 additions & 4 deletions exprtk_simple_example_09.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 9 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand Down Expand Up @@ -133,9 +133,9 @@ void primes()
{
x = static_cast<T>(i);

T result1 = expression1.value();
T result2 = expression2.value();
T result3 = expression3.value();
const T result1 = expression1.value();
const T result2 = expression2.value();
const T result3 = expression3.value();

printf("%03d Result1: %c Result2: %c Result3: %c\n",
static_cast<unsigned int>(i),
Expand Down
6 changes: 3 additions & 3 deletions exprtk_simple_example_10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 10 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand Down Expand Up @@ -64,7 +64,7 @@ void newton_sqrt()
" } ",
"x"));

std::string expression_str = "newton_sqrt(x)";
const std::string expression_str = "newton_sqrt(x)";

expression_t expression;
expression.register_symbol_table(symbol_table);
Expand All @@ -76,7 +76,7 @@ void newton_sqrt()
{
x = static_cast<T>(i);

T result = expression.value();
const T result = expression.value();

printf("sqrt(%03d) - Result: %15.13f\tReal: %15.13f\n",
static_cast<unsigned int>(i),
Expand Down
8 changes: 4 additions & 4 deletions exprtk_simple_example_11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 11 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand All @@ -29,7 +29,7 @@ void square_wave2()
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string wave_program =
const std::string wave_program =
" var r := 0; "
" for (var i := 0; i < 1000; i += 1) "
" { "
Expand Down Expand Up @@ -59,8 +59,8 @@ void square_wave2()

for (t = (T(-2) * pi); t <= (T(+2) * pi); t += delta)
{
T result = expression.value();
printf("%19.15f\t%19.15f\n",t,result);
const T result = expression.value();
printf("%19.15f\t%19.15f\n", t, result);
}
}

Expand Down
4 changes: 2 additions & 2 deletions exprtk_simple_example_12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 12 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand All @@ -29,7 +29,7 @@ void bubble_sort()
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string bubblesort_program =
const std::string bubblesort_program =
" var upper_bound := v[]; "
" var swapped := false; "
" repeat "
Expand Down
4 changes: 2 additions & 2 deletions exprtk_simple_example_13.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 13 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand Down Expand Up @@ -31,7 +31,7 @@ void savitzky_golay_filter()
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string sgfilter_program =
const std::string sgfilter_program =
" var weight[9] := "
" { "
" -21, 14, 39, "
Expand Down
6 changes: 3 additions & 3 deletions exprtk_simple_example_14.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 14 *
* Author: Arash Partow (1999-2019) *
* Author: Arash Partow (1999-2020) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
Expand All @@ -28,7 +28,7 @@ void stddev_example()
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string stddev_program =
const std::string stddev_program =
" var x[25] := { "
" 1, 2, 3, 4, 5, "
" 6, 7, 8, 9, 10, "
Expand All @@ -44,7 +44,7 @@ void stddev_example()
parser_t parser;
parser.compile(stddev_program,expression);

T stddev = expression.value();
const T stddev = expression.value();

printf("stddev(1..25) = %10.6f\n",stddev);
}
Expand Down
Loading

0 comments on commit 7c9b237

Please sign in to comment.