2013-04-14 07:01:14 +00:00
|
|
|
/*
|
|
|
|
**************************************************************
|
|
|
|
* C++ Mathematical Expression Toolkit Library *
|
|
|
|
* *
|
2024-01-01 00:00:00 +00:00
|
|
|
* Simple Example 03 *
|
|
|
|
* Author: Arash Partow (1999-2024) *
|
2023-01-01 02:18:35 +00:00
|
|
|
* URL: https://www.partow.net/programming/exprtk/index.html *
|
2013-04-14 07:01:14 +00:00
|
|
|
* *
|
|
|
|
* Copyright notice: *
|
|
|
|
* Free use of the Mathematical Expression Toolkit Library is *
|
|
|
|
* permitted under the guidelines and in accordance with the *
|
2017-02-21 21:44:24 +00:00
|
|
|
* most current version of the MIT License. *
|
2023-01-01 02:18:35 +00:00
|
|
|
* https://www.opensource.org/licenses/MIT *
|
2024-01-01 00:00:00 +00:00
|
|
|
* SPDX-License-Identifier: MIT *
|
2013-04-14 07:01:14 +00:00
|
|
|
* *
|
|
|
|
**************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
2020-01-01 00:00:00 +00:00
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
#include "exprtk.hpp"
|
|
|
|
|
|
|
|
|
2014-07-15 13:09:08 +00:00
|
|
|
template <typename T>
|
2013-04-14 07:01:14 +00:00
|
|
|
void polynomial()
|
|
|
|
{
|
2015-03-28 12:21:55 +00:00
|
|
|
typedef exprtk::symbol_table<T> symbol_table_t;
|
2021-01-01 00:00:00 +00:00
|
|
|
typedef exprtk::expression<T> expression_t;
|
|
|
|
typedef exprtk::parser<T> parser_t;
|
2015-03-28 12:21:55 +00:00
|
|
|
|
2020-01-01 00:00:00 +00:00
|
|
|
const std::string expression_string =
|
2024-01-01 00:00:00 +00:00
|
|
|
"25x^5 - 35x^4 - 15x^3 + 40x^2 - 15x + 1";
|
2014-11-03 10:36:41 +00:00
|
|
|
|
2017-05-05 02:16:16 +00:00
|
|
|
const T r0 = T(0);
|
|
|
|
const T r1 = T(1);
|
|
|
|
T x = T(0);
|
2013-04-14 07:01:14 +00:00
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
|
2013-09-12 13:28:40 +00:00
|
|
|
const T delta = T(1.0 / 100.0);
|
2014-11-03 10:36:41 +00:00
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
for (x = r0; x <= r1; x += delta)
|
|
|
|
{
|
2020-01-01 00:00:00 +00:00
|
|
|
printf("%19.15f\t%19.15f\n", x, expression.value());
|
2013-04-14 07:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
polynomial<double>();
|
|
|
|
return 0;
|
|
|
|
}
|