2013-04-14 07:01:14 +00:00
|
|
|
/*
|
|
|
|
**************************************************************
|
|
|
|
* C++ Mathematical Expression Toolkit Library *
|
|
|
|
* *
|
|
|
|
* Simple Example 1 *
|
2015-01-08 10:35:36 +00:00
|
|
|
* Author: Arash Partow (1999-2015) *
|
2013-04-14 07:01:14 +00:00
|
|
|
* 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 <string>
|
|
|
|
#include "exprtk.hpp"
|
|
|
|
|
|
|
|
|
2014-07-15 13:09:08 +00:00
|
|
|
template <typename T>
|
2013-04-14 07:01:14 +00:00
|
|
|
void trig_function()
|
|
|
|
{
|
2015-03-28 12:21:55 +00:00
|
|
|
typedef exprtk::symbol_table<T> symbol_table_t;
|
|
|
|
typedef exprtk::expression<T> expression_t;
|
|
|
|
typedef exprtk::parser<T> parser_t;
|
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
std::string expression_string = "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";
|
|
|
|
T x;
|
2015-03-28 12:21:55 +00:00
|
|
|
|
|
|
|
symbol_table_t symbol_table;
|
2013-04-14 07:01:14 +00:00
|
|
|
symbol_table.add_variable("x",x);
|
|
|
|
symbol_table.add_constants();
|
|
|
|
|
2015-03-28 12:21:55 +00:00
|
|
|
expression_t expression;
|
2013-04-14 07:01:14 +00:00
|
|
|
expression.register_symbol_table(symbol_table);
|
|
|
|
|
2015-03-28 12:21:55 +00:00
|
|
|
parser_t parser;
|
2013-04-14 07:01:14 +00:00
|
|
|
parser.compile(expression_string,expression);
|
|
|
|
|
2014-05-30 21:38:57 +00:00
|
|
|
for (x = T(-5); x <= T(+5); x += T(0.001))
|
2013-04-14 07:01:14 +00:00
|
|
|
{
|
|
|
|
T y = expression.value();
|
|
|
|
printf("%19.15f\t%19.15f\n",x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
trig_function<double>();
|
|
|
|
return 0;
|
|
|
|
}
|