C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html

This commit is contained in:
Arash Partow 2012-05-03 21:23:37 +10:00
parent 920a11ee6e
commit d7b199bdbd
3 changed files with 119 additions and 5 deletions

View File

@ -34,18 +34,17 @@ exprtk_benchmark: exprtk_benchmark.cpp exprtk.hpp
$(COMPILER) $(OPTIONS) exprtk_benchmark exprtk_benchmark.cpp $(LINKER_OPT)
pgo: exprtk_test.cpp exprtk_benchmark.cpp exprtk.hpp
$(COMPILER) $(BASE_OPTIONS) -O3 -march=native -fprofile-generate -o exprtk_test exprtk_test.cpp $(LINKER_OPT)
$(COMPILER) $(BASE_OPTIONS) -O3 -march=native -fprofile-generate -o exprtk_benchmark exprtk_benchmark.cpp $(LINKER_OPT)
./exprtk_test
./exprtk_benchmark
$(COMPILER) $(BASE_OPTIONS) -O3 -march=native -fprofile-use -o exprtk_test exprtk_test.cpp $(LINKER_OPT)
$(COMPILER) $(BASE_OPTIONS) -O3 -march=native -fprofile-use -o exprtk_benchmark exprtk_benchmark.cpp $(LINKER_OPT)
strip_bin:
strip -s exprtk_test
strip -s exprtk_benchmark
valgrind_check:
valgrind --leak-check=full --show-reachable=yes --track-origins=yes ./exprtk_test
valgrind --leak-check=full --show-reachable=yes --track-origins=yes ./exprtk_benchmark
clean:
rm -f core.* *~ *.o *.bak *stackdump gmon.out *.gcda *.gcno *.gcnor *.gch

View File

@ -6873,6 +6873,101 @@ namespace exprtk
return std::numeric_limits<T>::quiet_NaN();
}
/*
Note: The following 'compute' routines are very simple helpers,
for quickly setting up the required pieces of code in order to
evaluate an expression. By virtue of how they the operate there
will be an overhead with regards to their setup and teardown and
hence should not be used in time critical sections of code.
Furthermore they only assume a small sub set of variables - no
string variables or user defined functions.
*/
template <typename T>
inline bool compute(const std::string& expression_string, T& result)
{
//No variables
symbol_table<T> symbol_table;
symbol_table.add_constants();
expression<T> expression;
parser<T> parser;
if (parser.compile(expression_string,expression))
{
result = expression.value();
return true;
}
else
return false;
}
template <typename T>
inline bool compute(const std::string& expression_string,
const T& x,
T& result)
{
//Only 'x'
static const std::string x_var("x");
symbol_table<T> symbol_table;
symbol_table.add_constants();
symbol_table.add_variable("x",x);
expression<T> expression;
parser<T> parser;
if (parser.compile(expression_string,expression))
{
result = expression.value();
return true;
}
else
return false;
}
template <typename T>
inline bool compute(const std::string& expression_string,
const T&x, const T& y,
T& result)
{
//Only 'x' and 'y'
static const std::string x_var("x");
static const std::string y_var("y");
symbol_table<T> symbol_table;
symbol_table.add_constants();
symbol_table.add_variable("x",x);
symbol_table.add_variable("y",y);
expression<T> expression;
parser<T> parser;
if (parser.compile(expression_string,expression))
{
result = expression.value();
return true;
}
else
return false;
}
template <typename T>
inline bool compute(const std::string& expression_string,
const T& x, const T& y, const T& z,
T& result)
{
//Only 'x', 'y' or 'z'
static const std::string x_var("x");
static const std::string y_var("y");
static const std::string z_var("z");
symbol_table<T> symbol_table;
symbol_table.add_constants();
symbol_table.add_variable(x_var,x);
symbol_table.add_variable(y_var,y);
symbol_table.add_variable(z_var,z);
expression<T> expression;
parser<T> parser;
if (parser.compile(expression_string,expression))
{
result = expression.value();
return true;
}
else
return false;
}
template <typename T>
inline bool pgo_primer()
{
@ -7035,6 +7130,26 @@ namespace exprtk
else if (details::numeric::nequal(details::numeric::fast_exp<T,38>::result(v),std::pow(v,T(38.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,39>::result(v),std::pow(v,T(39.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,40>::result(v),std::pow(v,T(40.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,41>::result(v),std::pow(v,T(41.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,42>::result(v),std::pow(v,T(42.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,43>::result(v),std::pow(v,T(43.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,44>::result(v),std::pow(v,T(44.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,45>::result(v),std::pow(v,T(45.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,46>::result(v),std::pow(v,T(46.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,47>::result(v),std::pow(v,T(47.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,48>::result(v),std::pow(v,T(48.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,49>::result(v),std::pow(v,T(49.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,50>::result(v),std::pow(v,T(50.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,51>::result(v),std::pow(v,T(51.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,52>::result(v),std::pow(v,T(52.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,53>::result(v),std::pow(v,T(53.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,54>::result(v),std::pow(v,T(54.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,55>::result(v),std::pow(v,T(55.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,56>::result(v),std::pow(v,T(56.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,57>::result(v),std::pow(v,T(57.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,58>::result(v),std::pow(v,T(58.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,59>::result(v),std::pow(v,T(59.0)))) return false;
else if (details::numeric::nequal(details::numeric::fast_exp<T,60>::result(v),std::pow(v,T(60.0)))) return false;
else
return true;
}

View File

@ -30,7 +30,7 @@ const std::string expression_list[] = {
"(y + x / y) * (x - y / x)",
"x / ((x + y) * (x - y)) / y",
"1 - ((x * y) + (y / x)) - 3",
"1.1x^1 + 2.2y^2 - 3.3x^3 + 4.4y^4 - 5.5x^5 + 6.6y^6",
"1.1x^1 + 2.2y^2 - 3.3x^3 + 4.4y^15 - 5.5x^23 + 6.6y^55",
"sin(2 * x) + cos(pi / y)",
"1 - sin(2 * x) + cos(pi / y)",
"sqrt(1 - sin(2 * x) + cos(pi / y) / 3)",
@ -114,7 +114,7 @@ template <typename T> inline T func02(const T& x, const T& y) { return (T(2.0) *
template <typename T> inline T func03(const T& x, const T& y) { return (y + x / y) * (x - y / x); }
template <typename T> inline T func04(const T& x, const T& y) { return x / ((x + y) * (x - y)) / y; }
template <typename T> inline T func05(const T& x, const T& y) { return T(1.0) - ((x * y) + (y / x)) - T(3.0); }
template <typename T> inline T func06(const T& x, const T& y) { return (1.1*pow(x,T(1.0))+2.2*pow(y,T(2.0))-3.3*pow(x,T(3.0))+4.4*pow(y,T(4.0))-5.5*pow(x,T(5.0))+6.6*pow(y,T(6.0))); }
template <typename T> inline T func06(const T& x, const T& y) { return (1.1*pow(x,T(1.0))+2.2*pow(y,T(2.0))-3.3*pow(x,T(3.0))+4.4*pow(y,T(15.0))-5.5*pow(x,T(23.0))+6.6*pow(y,T(55.0))); }
template <typename T> inline T func07(const T& x, const T& y) { return std::sin(T(2.0) * x) + std::cos(pi / y); }
template <typename T> inline T func08(const T& x, const T& y) { return T(1.0) - std::sin(2.0 * x) + std::cos(pi / y); }
template <typename T> inline T func09(const T& x, const T& y) { return std::sqrt(T(1.0) - std::sin(T(2.0) * x) + std::cos(pi / y) / T(3.0)); }