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

This commit is contained in:
Arash Partow 2016-10-11 12:46:39 +11:00
parent 24c9e17e8c
commit fc1e23a811
2 changed files with 218 additions and 118 deletions

View File

@ -34923,116 +34923,6 @@ namespace exprtk
return true; return true;
} }
namespace rtl
{
namespace io
{
namespace details
{
template <typename T>
inline void print_type(const std::string& fmt,
const T v,
exprtk::details::numeric::details::real_type_tag)
{
printf(fmt.c_str(),v);
}
template <typename T>
struct print_impl
{
typedef typename igeneric_function<T>::generic_type generic_type;
typedef typename igeneric_function<T>::parameter_list_t parameter_list_t;
typedef typename generic_type::scalar_view scalar_t;
typedef typename generic_type::vector_view vector_t;
typedef typename generic_type::string_view string_t;
static void process(const std::string& scalar_format, parameter_list_t parameters)
{
for (std::size_t i = 0; i < parameters.size(); ++i)
{
generic_type& gt = parameters[i];
typename exprtk::details::numeric::details::number_type<T>::type num_type;
switch (gt.type)
{
case generic_type::e_scalar : print_type(scalar_format,scalar_t(gt)(),num_type);
break;
case generic_type::e_vector : {
vector_t vector(gt);
for (std::size_t x = 0; x < vector.size(); ++x)
{
print_type(scalar_format,vector[x],num_type);
if ((x + 1) < vector.size())
printf(" ");
}
}
break;
case generic_type::e_string : printf("%s",to_str(string_t(gt)).c_str());
break;
default : continue;
}
}
}
};
} // namespace exprtk::rtl::io::details
template <typename T>
struct print : public exprtk::igeneric_function<T>
{
typedef typename igeneric_function<T>::parameter_list_t parameter_list_t;
using exprtk::igeneric_function<T>::operator();
print(const std::string& scalar_format = "%10.5f")
: scalar_format_(scalar_format)
{
exprtk::enable_zero_parameters(*this);
}
inline T operator()(parameter_list_t parameters)
{
details::print_impl<T>::process(scalar_format_,parameters);
return T(0);
}
std::string scalar_format_;
};
template <typename T>
struct println : public exprtk::igeneric_function<T>
{
typedef typename igeneric_function<T>::parameter_list_t parameter_list_t;
using exprtk::igeneric_function<T>::operator();
println(const std::string& scalar_format = "%10.5f")
: scalar_format_(scalar_format)
{
exprtk::enable_zero_parameters(*this);
}
inline T operator()(parameter_list_t parameters)
{
details::print_impl<T>::process(scalar_format_,parameters);
printf("\n");
return T(0);
}
std::string scalar_format_;
};
} // namespace exprtk::rtl::io
}
} }
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
@ -35052,7 +34942,6 @@ namespace exprtk
namespace exprtk namespace exprtk
{ {
class timer class timer
{ {
public: public:
@ -35148,6 +35037,142 @@ namespace exprtk
} // namespace exprtk } // namespace exprtk
#ifndef exprtk_disable_rtl_io
namespace exprtk
{
namespace rtl { namespace io { namespace details
{
template <typename T>
inline void print_type(const std::string& fmt,
const T v,
exprtk::details::numeric::details::real_type_tag)
{
printf(fmt.c_str(),v);
}
template <typename T>
struct print_impl
{
typedef typename igeneric_function<T>::generic_type generic_type;
typedef typename igeneric_function<T>::parameter_list_t parameter_list_t;
typedef typename generic_type::scalar_view scalar_t;
typedef typename generic_type::vector_view vector_t;
typedef typename generic_type::string_view string_t;
typedef typename exprtk::details::numeric::details::number_type<T>::type num_type;
static void process(const std::string& scalar_format, parameter_list_t parameters)
{
for (std::size_t i = 0; i < parameters.size(); ++i)
{
generic_type& gt = parameters[i];
switch (gt.type)
{
case generic_type::e_scalar : print(scalar_format,scalar_t(gt));
break;
case generic_type::e_vector : print(scalar_format,vector_t(gt));
break;
case generic_type::e_string : print(string_t(gt));
break;
default : continue;
}
}
}
static inline void print(const std::string& scalar_format, const scalar_t& s)
{
print_type(scalar_format,s(),num_type());
}
static inline void print(const std::string& scalar_format, const vector_t& v)
{
for (std::size_t i = 0; i < v.size(); ++i)
{
print_type(scalar_format,v[i],num_type());
if ((i + 1) < v.size())
printf(" ");
}
}
static inline void print(const string_t& s)
{
printf("%s",to_str(s).c_str());
}
};
} // namespace exprtk::rtl::io::details
template <typename T>
struct print : public exprtk::igeneric_function<T>
{
typedef typename igeneric_function<T>::parameter_list_t parameter_list_t;
using exprtk::igeneric_function<T>::operator();
print(const std::string& scalar_format = "%10.5f")
: scalar_format_(scalar_format)
{
exprtk::enable_zero_parameters(*this);
}
inline T operator()(parameter_list_t parameters)
{
details::print_impl<T>::process(scalar_format_,parameters);
return T(0);
}
std::string scalar_format_;
};
template <typename T>
struct println : public exprtk::igeneric_function<T>
{
typedef typename igeneric_function<T>::parameter_list_t parameter_list_t;
using exprtk::igeneric_function<T>::operator();
println(const std::string& scalar_format = "%10.5f")
: scalar_format_(scalar_format)
{
exprtk::enable_zero_parameters(*this);
}
inline T operator()(parameter_list_t parameters)
{
details::print_impl<T>::process(scalar_format_,parameters);
printf("\n");
return T(0);
}
std::string scalar_format_;
};
template <typename T>
struct package
{
print <T> p;
println<T> pl;
bool register_package(exprtk::symbol_table<T>& symtab)
{
if (!symtab.add_function("print" ,p))
return false;
else if (!symtab.add_function("println" ,pl))
return false;
else
return true;
}
};
} // namespace exprtk::rtl::io
} // namespace exprtk::rtl
} // namespace exprtk
#endif
#ifndef exprtk_disable_rtl_io_file #ifndef exprtk_disable_rtl_io_file
#include <fstream> #include <fstream>
namespace exprtk namespace exprtk
@ -35184,6 +35209,7 @@ namespace exprtk
{ {
file_name.clear(); file_name.clear();
delete stream; delete stream;
return false; return false;
} }
else else
@ -35199,6 +35225,7 @@ namespace exprtk
{ {
file_name.clear(); file_name.clear();
delete stream; delete stream;
return false; return false;
} }
else else
@ -35214,6 +35241,7 @@ namespace exprtk
{ {
file_name.clear(); file_name.clear();
delete stream; delete stream;
return false; return false;
} }
else else
@ -35299,8 +35327,6 @@ namespace exprtk
case e_rdwrt : return (!!std::getline(*reinterpret_cast<std::fstream* >(stream_ptr),s)); case e_rdwrt : return (!!std::getline(*reinterpret_cast<std::fstream* >(stream_ptr),s));
default : return false; default : return false;
} }
return true;
} }
bool eof() bool eof()

View File

@ -2668,7 +2668,81 @@ via the 'unknown symbol resolver' mechanism.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[20 - EXPRTK NOTES] [20 - RUNTIME LIBRARY PACKAGES]
ExprTk contains a set of simple extensions, that provide
functionalities beyond basic numerical calculations. Currently the
available packages are:
+---+--------------------+-----------------------------------+
| # | Package Name | Namespace/Type |
+---+--------------------+-----------------------------------+
| 1 | Basic I/O | exprtk::rtl::io::package<T> |
| 2 | File I/O | exprtk::rtl::io::file::package<T> |
| 3 | Vector Operations | exprtk::rtl::vecops::package<T> |
+---+--------------------+-----------------------------------+
In order to make the features of a specific package available within
an expression, an instance of the package must be added to the
expression's associated symbol table. In the following example, the
file I/O package is made available for the given expression:
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
exprtk::rtl::io::file::package<T> fileio_package;
std::string expression_string =
" var file_name := 'file.txt'; "
" var stream := null; "
" "
" stream := open(file_name,'w'); "
" "
" write(stream,'Hello world....\n'); "
" "
" close(stream); "
" ";
symbol_table_t symbol_table;
symbol_table.add_package(fileio_package);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_string,expression);
expression.value();
(1) Basic I/O functions:
(a) print
(b) println
(2) File I/O functions:
(a) open (b) close
(c) write (d) read
(e) getline (f) eof
(3) Vector Operations functions:
(a) all_true (b) all_false
(c) any_true (d) any_false
(e) count (f) copy
(g) rotate-left (h) rotate-right
(i) shift-left (j) shift-right
(k) sort (l) sumk
(m) axpy (n) axpby
(o) axpyz (p) axpbyz
(q) axpbz (r) dot
(s) dotk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[21 - EXPRTK NOTES]
The following is a list of facts and suggestions one may want to take The following is a list of facts and suggestions one may want to take
into account when using ExprTk: into account when using ExprTk:
@ -2869,7 +2943,7 @@ into account when using ExprTk:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[21 - SIMPLE EXPRTK EXAMPLE] [22 - SIMPLE EXPRTK EXAMPLE]
The following is a simple yet complete example demonstrating typical The following is a simple yet complete example demonstrating typical
usage of the ExprTk Library. The example instantiates a symbol table usage of the ExprTk Library. The example instantiates a symbol table
object, adding to it three variables named x, y and z, and a custom object, adding to it three variables named x, y and z, and a custom
@ -2974,7 +3048,7 @@ int main()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[22 - BUILD OPTIONS] [23 - BUILD OPTIONS]
When building ExprTk there are a number of defines that will enable or When building ExprTk there are a number of defines that will enable or
disable certain features and capabilities. The defines can either be disable certain features and capabilities. The defines can either be
part of a compiler command line switch or scoped around the include to part of a compiler command line switch or scoped around the include to
@ -3041,7 +3115,7 @@ error.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[23 - FILES] [24 - FILES]
The source distribution of ExprTk is comprised of the following set of The source distribution of ExprTk is comprised of the following set of
files: files:
@ -3071,7 +3145,7 @@ files:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[24 - LANGUAGE STRUCTURE] [25 - LANGUAGE STRUCTURE]
+-------------------------------------------------------------+ +-------------------------------------------------------------+
|00 - If Statement | |00 - If Statement |
| | | |