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

This commit is contained in:
Arash Partow 2012-01-28 11:18:35 +00:00
parent d4df1f2266
commit a420147e7a
2 changed files with 42 additions and 13 deletions

View File

@ -955,14 +955,14 @@ namespace exprtk
} }
} }
template<typename T> template <typename T>
inline T process(const operator_type operation, const T& arg) inline T process(const operator_type operation, const T& arg)
{ {
typename details::number_type<T>::type num_type; typename details::number_type<T>::type num_type;
return details::process_impl<T>(operation,arg,num_type); return details::process_impl<T>(operation,arg,num_type);
} }
template<typename T> template <typename T>
inline T process(const operator_type operation, const T& arg0, const T& arg1) inline T process(const operator_type operation, const T& arg0, const T& arg1)
{ {
typename details::number_type<T>::type num_type; typename details::number_type<T>::type num_type;
@ -1008,7 +1008,7 @@ namespace exprtk
} }
}; };
template<typename T> template <typename T>
inline bool branch_deletable(expression_node<T>* expr) inline bool branch_deletable(expression_node<T>* expr)
{ {
return expression_node<T>::e_variable != expr->type(); return expression_node<T>::e_variable != expr->type();
@ -1656,8 +1656,7 @@ namespace exprtk
cleanup_branches<N>(branch_); cleanup_branches<N>(branch_);
} }
template <std::size_t NumBranches>
template<std::size_t NumBranches>
void init_branches(expression_ptr (&b)[NumBranches]) void init_branches(expression_ptr (&b)[NumBranches])
{ {
for (std::size_t i = 0; i < NumBranches; ++i) for (std::size_t i = 0; i < NumBranches; ++i)
@ -1951,7 +1950,7 @@ namespace exprtk
} // namespace details } // namespace details
template<typename T> template <typename T>
class ifunction class ifunction
{ {
public: public:
@ -2324,8 +2323,8 @@ namespace exprtk
add_variable("inf",infinity,true); add_variable("inf",infinity,true);
} }
template<typename Allocator, template <typename Allocator,
template<typename,typename> class Sequence> template <typename,typename> class Sequence>
inline std::size_t get_variable_list(Sequence<std::pair<std::string,T>,Allocator>& vlist) inline std::size_t get_variable_list(Sequence<std::pair<std::string,T>,Allocator>& vlist)
{ {
std::size_t count = 0; std::size_t count = 0;
@ -2788,6 +2787,7 @@ namespace exprtk
ptr_t* p_; ptr_t* p_;
private: private:
scoped_delete<Type,N>& operator=(const scoped_delete<Type,N>&); scoped_delete<Type,N>& operator=(const scoped_delete<Type,N>&);
}; };
@ -3253,7 +3253,7 @@ namespace exprtk
return synthesize_expression<sf4_node_t,4>(operation,branch); return synthesize_expression<sf4_node_t,4>(operation,branch);
} }
template<std::size_t N> template <std::size_t N>
inline expression_node_ptr function(typename function_node_t::ifunction* f, expression_node_ptr (&b)[N]) inline expression_node_ptr function(typename function_node_t::ifunction* f, expression_node_ptr (&b)[N])
{ {
expression_node_ptr result = synthesize_expression<function_node_t,N>(f,b); expression_node_ptr result = synthesize_expression<function_node_t,N>(f,b);

View File

@ -784,6 +784,7 @@ inline bool run_test1()
std::cout << "run_test1() - Error: " << parser.error() << "\tExpression: " << expression_string << std::endl; std::cout << "run_test1() - Error: " << parser.error() << "\tExpression: " << expression_string << std::endl;
return false; return false;
} }
expression.value();
return true; return true;
} }
@ -997,6 +998,22 @@ inline bool run_test6()
"x", "y", "z", "w", "u", "x", "y", "z", "w", "u",
"x + y + z + w + u", "x + y + z + w + u",
"x + y / z * w ^ u", "x + y / z * w ^ u",
"x:=1.1", "y:=2.2", "z:=3.3", "w:=4.4", "u:=5.5",
"x<-1.1", "y<-2.2", "z<-3.3", "w<-4.4", "u<-5.5",
"x:=x+1.1", "y:=y+2.2", "z:=z+3.3", "w:=w+4.4", "u:=u+5.5",
"x<-x+1.1", "y<-y+2.2", "z<-z+3.3", "w<-w+4.4", "u<-u+5.5",
"x:=1.1+x", "y:=2.2+y", "z:=3.3+z", "w:=4.4+w", "u:=5.5+u",
"x<-1.1+x", "y<-2.2+y", "z<-3.3+z", "w<-4.4+w", "u<-5.5+u",
"x:=(x <= 1.1)",
"y:=(2.2 >= y)",
"z:=(3.3 and z)",
"w:=(4.4 or w)",
"u:=(u xor 5.5)",
"x<-(x <= 1.1)",
"y<-(2.2 >= y)",
"z<-(3.3 and z)",
"w<-(4.4 or w)",
"u<-(u xor 5.5)",
"min(x,y) + min(x,y,z) + min(x,y,z,w) + min(x,y,z,w,y)", "min(x,y) + min(x,y,z) + min(x,y,z,w) + min(x,y,z,w,y)",
"max(x,y) + max(x,y,z) + max(x,y,z,w) + max(x,y,z,w,y)", "max(x,y) + max(x,y,z) + max(x,y,z,w) + max(x,y,z,w,y)",
"avg(x,y)", "avg(x,y)",
@ -1144,12 +1161,24 @@ inline bool run_test7()
std::cout << "run_test7() - Error: " << parser.error() << "\tExpression: " << expression_string << std::endl; std::cout << "run_test7() - Error: " << parser.error() << "\tExpression: " << expression_string << std::endl;
return false; return false;
} }
T result = expression.value();
const T pi = T(3.14159265358979323846); const T pi = T(3.14159265358979323846);
if (not_equal<T>(result,(T(1)+(std::sin(x*pi)*(y/T(2)))/T(3)) * 10.0,0.000001))
T result = expression.value();
T expected = mf(sin(x*pi),y/2) +
mf(sin(x*pi),y/2) +
mf(sin(x*pi),y/2) +
mf(sin(x*pi),y/2) +
mf(sin(x*pi),y/2) +
mf(sin(x*pi),y/2) +
mf(sin(x*pi),y/2) +
mf(sin(x*pi),y/2) +
mf(sin(x*pi),y/2) +
mf(sin(x*pi),y/2);
if (not_equal<T>(result,expected,0.0000001))
{ {
printf("run_test7() - Error Expected: %19.15f\tResult: %19.15f\n", printf("run_test7() - Error Expected: %19.15f\tResult: %19.15f\n",
T(1)+(std::sin(x*pi)*(y/T(2)))/T(3), expected,
result); result);
return false; return false;
} }
@ -1170,4 +1199,4 @@ int main()
run_test7<double>() run_test7<double>()
) )
? 0 : 1; ? 0 : 1;
} }