C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html
This commit is contained in:
parent
6b86ec0deb
commit
1bf1073cd8
6
Makefile
6
Makefile
|
@ -36,6 +36,7 @@ BUILD_LIST+=exprtk_simple_example_09
|
||||||
BUILD_LIST+=exprtk_simple_example_10
|
BUILD_LIST+=exprtk_simple_example_10
|
||||||
BUILD_LIST+=exprtk_simple_example_11
|
BUILD_LIST+=exprtk_simple_example_11
|
||||||
BUILD_LIST+=exprtk_simple_example_12
|
BUILD_LIST+=exprtk_simple_example_12
|
||||||
|
BUILD_LIST+=exprtk_simple_example_13
|
||||||
|
|
||||||
all: $(BUILD_LIST)
|
all: $(BUILD_LIST)
|
||||||
|
|
||||||
|
@ -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
|
exprtk_simple_example_12: exprtk_simple_example_12.cpp exprtk.hpp
|
||||||
$(COMPILER) $(OPTIONS) exprtk_simple_example_12 exprtk_simple_example_12.cpp $(LINKER_OPT)
|
$(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
|
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)
|
$(COMPILER) $(BASE_OPTIONS) -O3 -march=native -fprofile-generate -o exprtk_benchmark exprtk_benchmark.cpp $(LINKER_OPT)
|
||||||
./exprtk_benchmark
|
./exprtk_benchmark
|
||||||
|
@ -101,6 +105,7 @@ strip_bin:
|
||||||
strip -s exprtk_simple_example_10
|
strip -s exprtk_simple_example_10
|
||||||
strip -s exprtk_simple_example_11
|
strip -s exprtk_simple_example_11
|
||||||
strip -s exprtk_simple_example_12
|
strip -s exprtk_simple_example_12
|
||||||
|
strip -s exprtk_simple_example_13
|
||||||
|
|
||||||
valgrind_check:
|
valgrind_check:
|
||||||
valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_test_valgrind.log -v ./exprtk_test
|
valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_test_valgrind.log -v ./exprtk_test
|
||||||
|
@ -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_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_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_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:
|
clean:
|
||||||
rm -f core.* *~ *.o *.bak *stackdump gmon.out *.gcda *.gcno *.gcnor *.gch
|
rm -f core.* *~ *.o *.bak *stackdump gmon.out *.gcda *.gcno *.gcnor *.gch
|
||||||
|
|
11
exprtk.hpp
11
exprtk.hpp
|
@ -13030,7 +13030,11 @@ namespace exprtk
|
||||||
next_token();
|
next_token();
|
||||||
break;
|
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(
|
set_error(
|
||||||
make_error(parser_error::e_syntax,
|
make_error(parser_error::e_syntax,
|
||||||
|
@ -15324,6 +15328,11 @@ namespace exprtk
|
||||||
return (lexer_.peek_next_token().type == ttype);
|
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>
|
template <typename Type>
|
||||||
class expression_generator
|
class expression_generator
|
||||||
{
|
{
|
||||||
|
|
|
@ -36,7 +36,7 @@ void trig_function()
|
||||||
exprtk::parser<T> parser;
|
exprtk::parser<T> parser;
|
||||||
parser.compile(expression_string,expression);
|
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();
|
T y = expression.value();
|
||||||
printf("%19.15f\t%19.15f\n",x,y);
|
printf("%19.15f\t%19.15f\n",x,y);
|
||||||
|
|
|
@ -32,12 +32,15 @@ void fibonacci()
|
||||||
compositor_t compositor;
|
compositor_t compositor;
|
||||||
|
|
||||||
compositor
|
compositor
|
||||||
.add("fibonacci_impl",
|
.add("fibonacci",
|
||||||
"switch "
|
" var w := 0; "
|
||||||
"{ "
|
" var y := 0; "
|
||||||
" case x == 0 : 0; "
|
" var z := 1; "
|
||||||
" case x == 1 : 1; "
|
" switch "
|
||||||
" default : "
|
" { "
|
||||||
|
" case x == 0 : 0; "
|
||||||
|
" case x == 1 : 1; "
|
||||||
|
" default : "
|
||||||
" while ((x -= 1) > 0) "
|
" while ((x -= 1) > 0) "
|
||||||
" { "
|
" { "
|
||||||
" w := z; "
|
" w := z; "
|
||||||
|
@ -45,12 +48,7 @@ void fibonacci()
|
||||||
" y := w; "
|
" y := w; "
|
||||||
" z "
|
" z "
|
||||||
" }; "
|
" }; "
|
||||||
"} ",
|
" } ",
|
||||||
"x","y","z","w");
|
|
||||||
|
|
||||||
compositor
|
|
||||||
.add("fibonacci",
|
|
||||||
"fibonacci_impl(x,0,1,0)",
|
|
||||||
"x");
|
"x");
|
||||||
|
|
||||||
T x = T(0);
|
T x = T(0);
|
||||||
|
|
|
@ -39,29 +39,25 @@ void newton_sqrt()
|
||||||
|
|
||||||
compositor_t compositor(symbol_table);
|
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
|
compositor
|
||||||
.add("newton_sqrt",
|
.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)";
|
std::string expression_str = "newton_sqrt(x)";
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ void square_wave2()
|
||||||
typedef exprtk::parser<T> parser_t;
|
typedef exprtk::parser<T> parser_t;
|
||||||
|
|
||||||
std::string wave_program =
|
std::string wave_program =
|
||||||
" r := 0; "
|
" var r := 0; "
|
||||||
" for(i := 0; i < 1000; i += 1) "
|
" for(i := 0; i < 1000; i += 1) "
|
||||||
" { "
|
" { "
|
||||||
" r += (1 / (2i + 1)) * sin((4i + 2) * pi * f * t); "
|
" r += (1 / (2i + 1)) * sin((4i + 2) * pi * f * t); "
|
||||||
|
@ -52,7 +52,6 @@ void square_wave2()
|
||||||
expression.register_symbol_table(symbol_table);
|
expression.register_symbol_table(symbol_table);
|
||||||
|
|
||||||
parser_t parser;
|
parser_t parser;
|
||||||
parser.enable_unknown_symbol_resolver();
|
|
||||||
|
|
||||||
parser.compile(wave_program,expression);
|
parser.compile(wave_program,expression);
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -968,7 +968,7 @@ example defines a vararg function called 'boo':
|
||||||
For the above denoted custom functions to be used in an expression, an
|
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
|
instance of each function needs to be registered with a symbol_table
|
||||||
that has been associated with the expression instance. The following
|
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::symbol_table<double> symbol_table_t;
|
||||||
typedef exprtk::expression<double> expression_t;
|
typedef exprtk::expression<double> expression_t;
|
||||||
|
@ -1242,6 +1242,7 @@ int main()
|
||||||
(14) exprtk_simple_example_10.cpp
|
(14) exprtk_simple_example_10.cpp
|
||||||
(15) exprtk_simple_example_11.cpp
|
(15) exprtk_simple_example_11.cpp
|
||||||
(16) exprtk_simple_example_12.cpp
|
(16) exprtk_simple_example_12.cpp
|
||||||
|
(17) exprtk_simple_example_13.cpp
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue