2013-04-14 07:01:14 +00:00
|
|
|
/*
|
|
|
|
**************************************************************
|
|
|
|
* C++ Mathematical Expression Toolkit Library *
|
|
|
|
* *
|
2024-01-01 00:00:00 +00:00
|
|
|
* Simple Example 08 *
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void composite()
|
|
|
|
{
|
2021-01-01 00:00:00 +00:00
|
|
|
typedef exprtk::symbol_table<T> symbol_table_t;
|
|
|
|
typedef exprtk::expression<T> expression_t;
|
|
|
|
typedef exprtk::parser<T> parser_t;
|
2024-01-01 00:00:00 +00:00
|
|
|
typedef exprtk::parser_error::type err_t;
|
2021-01-01 00:00:00 +00:00
|
|
|
typedef exprtk::function_compositor<T> compositor_t;
|
|
|
|
typedef typename compositor_t::function function_t;
|
2013-04-14 07:01:14 +00:00
|
|
|
|
|
|
|
T x = T(1);
|
|
|
|
T y = T(2);
|
|
|
|
|
2024-01-01 00:00:00 +00:00
|
|
|
compositor_t compositor;
|
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
symbol_table_t& symbol_table = compositor.symbol_table();
|
|
|
|
symbol_table.add_constants();
|
|
|
|
symbol_table.add_variable("x",x);
|
|
|
|
symbol_table.add_variable("y",y);
|
|
|
|
|
2024-01-01 00:00:00 +00:00
|
|
|
compositor.add(
|
|
|
|
function_t("f") // f(x) = sin(x / pi)
|
|
|
|
.var("x")
|
|
|
|
.expression( "sin(x / pi)" ));
|
2015-04-28 06:50:54 +00:00
|
|
|
|
2024-01-01 00:00:00 +00:00
|
|
|
compositor.add(
|
|
|
|
function_t("g") // g(x,y) = 3[f(x) + f(y)]
|
|
|
|
.vars("x", "y")
|
|
|
|
.expression( "3*[f(x) + f(y)]" ));
|
2013-04-14 07:01:14 +00:00
|
|
|
|
2016-09-26 00:50:06 +00:00
|
|
|
std::string expression_string = "g(1 + f(x), f(y) / 2)";
|
2013-04-14 07:01:14 +00:00
|
|
|
|
|
|
|
expression_t expression;
|
|
|
|
expression.register_symbol_table(symbol_table);
|
|
|
|
|
|
|
|
parser_t parser;
|
|
|
|
|
|
|
|
if (!parser.compile(expression_string,expression))
|
|
|
|
{
|
|
|
|
printf("Error: %s\tExpression: %s\n",
|
|
|
|
parser.error().c_str(),
|
|
|
|
expression_string.c_str());
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < parser.error_count(); ++i)
|
|
|
|
{
|
2024-01-01 00:00:00 +00:00
|
|
|
const err_t error = parser.get_error(i);
|
2016-10-18 02:17:34 +00:00
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
printf("Error: %02d Position: %02d Type: [%14s] Msg: %s\tExpression: %s\n",
|
|
|
|
static_cast<unsigned int>(i),
|
|
|
|
static_cast<unsigned int>(error.token.position),
|
|
|
|
exprtk::parser_error::to_str(error.mode).c_str(),
|
|
|
|
error.diagnostic.c_str(),
|
|
|
|
expression_string.c_str());
|
|
|
|
}
|
2014-11-03 10:36:41 +00:00
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-01 00:00:00 +00:00
|
|
|
const T result = expression.value();
|
2013-04-14 07:01:14 +00:00
|
|
|
|
|
|
|
printf("%s = %e\n", expression_string.c_str(), result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
composite<double>();
|
|
|
|
return 0;
|
|
|
|
}
|