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 May 30, 2014
1 parent 6b86ec0 commit 1bf1073
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 38 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ BUILD_LIST+=exprtk_simple_example_09
BUILD_LIST+=exprtk_simple_example_10
BUILD_LIST+=exprtk_simple_example_11
BUILD_LIST+=exprtk_simple_example_12
BUILD_LIST+=exprtk_simple_example_13

all: $(BUILD_LIST)

Expand Down Expand Up @@ -81,6 +82,9 @@ exprtk_simple_example_11: exprtk_simple_example_11.cpp exprtk.hpp
exprtk_simple_example_12: exprtk_simple_example_12.cpp exprtk.hpp
$(COMPILER) $(OPTIONS) exprtk_simple_example_12 exprtk_simple_example_12.cpp $(LINKER_OPT)

exprtk_simple_example_13: exprtk_simple_example_13.cpp exprtk.hpp
$(COMPILER) $(OPTIONS) exprtk_simple_example_13 exprtk_simple_example_13.cpp $(LINKER_OPT)

pgo: exprtk_test.cpp exprtk_benchmark.cpp exprtk.hpp
$(COMPILER) $(BASE_OPTIONS) -O3 -march=native -fprofile-generate -o exprtk_benchmark exprtk_benchmark.cpp $(LINKER_OPT)
./exprtk_benchmark
Expand All @@ -101,6 +105,7 @@ strip_bin:
strip -s exprtk_simple_example_10
strip -s exprtk_simple_example_11
strip -s exprtk_simple_example_12
strip -s exprtk_simple_example_13

valgrind_check:
valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_test_valgrind.log -v ./exprtk_test
Expand All @@ -117,6 +122,7 @@ valgrind_check:
valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_10_valgrind.log -v ./exprtk_simple_example_10
valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_11_valgrind.log -v ./exprtk_simple_example_11
valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_12_valgrind.log -v ./exprtk_simple_example_12
valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_13_valgrind.log -v ./exprtk_simple_example_13

clean:
rm -f core.* *~ *.o *.bak *stackdump gmon.out *.gcda *.gcno *.gcnor *.gch
11 changes: 10 additions & 1 deletion exprtk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13030,7 +13030,11 @@ namespace exprtk
next_token();
break;
}
else if (!token_is(seperator))

bool is_next_until = peek_token_is(token_t::e_symbol) &&
peek_token_is("until");

if (!token_is(seperator) && is_next_until)
{
set_error(
make_error(parser_error::e_syntax,
Expand Down Expand Up @@ -15324,6 +15328,11 @@ namespace exprtk
return (lexer_.peek_next_token().type == ttype);
}

inline bool peek_token_is(const std::string& s)
{
return (details::imatch(lexer_.peek_next_token().value,s));
}

template <typename Type>
class expression_generator
{
Expand Down
2 changes: 1 addition & 1 deletion exprtk_simple_example_01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void trig_function()
exprtk::parser<T> parser;
parser.compile(expression_string,expression);

for (x = T(-5.0); x <= T(+5.0); x += 0.001)
for (x = T(-5); x <= T(+5); x += T(0.001))
{
T y = expression.value();
printf("%19.15f\t%19.15f\n",x,y);
Expand Down
22 changes: 10 additions & 12 deletions exprtk_simple_example_04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,23 @@ void fibonacci()
compositor_t compositor;

compositor
.add("fibonacci_impl",
"switch "
"{ "
" case x == 0 : 0; "
" case x == 1 : 1; "
" default : "
.add("fibonacci",
" var w := 0; "
" var y := 0; "
" var z := 1; "
" switch "
" { "
" case x == 0 : 0; "
" case x == 1 : 1; "
" default : "
" while ((x -= 1) > 0) "
" { "
" w := z; "
" z := z + y; "
" y := w; "
" z "
" }; "
"} ",
"x","y","z","w");

compositor
.add("fibonacci",
"fibonacci_impl(x,0,1,0)",
" } ",
"x");

T x = T(0);
Expand Down
38 changes: 17 additions & 21 deletions exprtk_simple_example_10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,25 @@ void newton_sqrt()

compositor_t compositor(symbol_table);

compositor
.add("newton_sqrt_impl",
"switch "
"{ "
" case x < 0 : -inf; "
" case x == 0 : 0; "
" case x == 1 : 1; "
" default: "
" ~{ "
" z := 100; "
" y := x / 2; "
" repeat "
" y := (1 / 2) * (y + (x / y)); "
" if (equal(y * y,x)) "
" break[y]; "
" until ((z -= 1) <= 0); "
" }; "
"} ",
"x","y","z");

compositor
.add("newton_sqrt",
"newton_sqrt_impl(x,0,0)","x");
" switch "
" { "
" case x < 0 : -inf; "
" case x == 0 : 0; "
" case x == 1 : 1; "
" default: "
" ~{ "
" var z := 100; "
" var y := x / 2; "
" repeat "
" y := (1 / 2) * (y + (x / y)); "
" if (equal(y * y,x)) "
" break[y]; "
" until ((z -= 1) <= 0); "
" }; "
" } ",
"x");

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

Expand Down
3 changes: 1 addition & 2 deletions exprtk_simple_example_11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void square_wave2()
typedef exprtk::parser<T> parser_t;

std::string wave_program =
" r := 0; "
" var r := 0; "
" for(i := 0; i < 1000; i += 1) "
" { "
" r += (1 / (2i + 1)) * sin((4i + 2) * pi * f * t); "
Expand All @@ -52,7 +52,6 @@ void square_wave2()
expression.register_symbol_table(symbol_table);

parser_t parser;
parser.enable_unknown_symbol_resolver();

parser.compile(wave_program,expression);

Expand Down
98 changes: 98 additions & 0 deletions exprtk_simple_example_13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 13 *
* Author: Arash Partow (1999-2014) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the Common Public License. *
* http://www.opensource.org/licenses/cpl1.0.php *
* *
**************************************************************
*/


#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
#include "exprtk.hpp"


template<typename T>
void savitzky_golay_filter()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

std::string gsfilter_program =
" var weight[9] := "
" { "
" -21, 14, 39, "
" 54, 59, 54, "
" 39, 14, -21 "
" }; "
" "
" var lower_bound := trunc(weight[] / 2); "
" var upper_bound := v_in[] - lower_bound; "
" "
" if (v_in[] >= weight[]) "
" { "
" for (i := lower_bound; i < upper_bound; i += 1) "
" { "
" v_out[i] := 0; "
" for (j := 0; j < weight[]; j += 1) "
" { "
" v_out[i] += weight[j] * v_in[i + j]; "
" }; "
" v_out[i] /= 231; "
" }; "
" } ";

const std::size_t n = 1024;

std::vector<T> v_in;
std::vector<T> v_out;

const T pi = T(3.141592653589793238462);

srand(time(0));

// Generate a signal with noise.
for (T t = T(-5); t <= T(+5); t += T(10.0 / n))
{
T noise = T(0.5 * (rand() / (RAND_MAX + 1.0) - 0.5));
v_in.push_back(sin(2.0 * pi * t) + noise);
}

v_out.resize(v_in.size());

symbol_table_t symbol_table;
symbol_table.add_vector("v_in" , v_in);
symbol_table.add_vector("v_out",v_out);

expression_t expression;
expression.register_symbol_table(symbol_table);

parser_t parser;

parser.compile(gsfilter_program,expression);

expression.value();

for (std::size_t i = 0; i < v_out.size(); ++i)
{
printf("%10.6f\t%10.6f\n",v_in[i],v_out[i]);
}
}

int main()
{
savitzky_golay_filter<double>();
return 0;
}
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ example defines a vararg function called 'boo':
For the above denoted custom functions to be used in an expression, an
instance of each function needs to be registered with a symbol_table
that has been associated with the expression instance. The following
demonstrations how all the pieces are put together:
demonstrates how all the pieces are put together:

typedef exprtk::symbol_table<double> symbol_table_t;
typedef exprtk::expression<double> expression_t;
Expand Down Expand Up @@ -1242,6 +1242,7 @@ int main()
(14) exprtk_simple_example_10.cpp
(15) exprtk_simple_example_11.cpp
(16) exprtk_simple_example_12.cpp
(17) exprtk_simple_example_13.cpp



Expand Down

0 comments on commit 1bf1073

Please sign in to comment.