mirror of
https://github.com/ArashPartow/exprtk.git
synced 2025-07-08 03:09:03 +00:00
C++ Mathematical Expression Library (ExprTk) https://www.partow.net/programming/exprtk/index.html
This commit is contained in:
committed by
Arash Partow
parent
f46bffcd69
commit
cc1b800c2b
@ -3,7 +3,7 @@
|
||||
* C++ Mathematical Expression Toolkit Library *
|
||||
* *
|
||||
* Simple Example 16 *
|
||||
* Author: Arash Partow (1999-2023) *
|
||||
* Author: Arash Partow (1999-2024) *
|
||||
* URL: https://www.partow.net/programming/exprtk/index.html *
|
||||
* *
|
||||
* Copyright notice: *
|
||||
@ -11,6 +11,7 @@
|
||||
* permitted under the guidelines and in accordance with the *
|
||||
* most current version of the MIT License. *
|
||||
* https://www.opensource.org/licenses/MIT *
|
||||
* SPDX-License-Identifier: MIT *
|
||||
* *
|
||||
**************************************************************
|
||||
*/
|
||||
@ -31,24 +32,27 @@ void linear_least_squares()
|
||||
typedef exprtk::parser<T> parser_t;
|
||||
|
||||
const std::string linear_least_squares_program =
|
||||
" if (x[] == y[]) "
|
||||
" { "
|
||||
" beta := (sum(x * y) - sum(x) * sum(y) / x[]) / "
|
||||
" (sum(x^2) - sum(x)^2 / x[]); "
|
||||
" "
|
||||
" alpha := avg(y) - beta * avg(x); "
|
||||
" "
|
||||
" rmse := sqrt(sum((beta * x + alpha - y)^2) / y[]); "
|
||||
" } "
|
||||
" else "
|
||||
" { "
|
||||
" alpha := null; "
|
||||
" beta := null; "
|
||||
" rmse := null; "
|
||||
" } ";
|
||||
" if (x[] == y[]) "
|
||||
" { "
|
||||
" var mean_x := avg(x); "
|
||||
" var mean_y := avg(y); "
|
||||
" "
|
||||
" beta := sum((x - mean_x) * (y - mean_y)) / "
|
||||
" sum((x - mean_x)^2); "
|
||||
" "
|
||||
" alpha := mean_y - beta * mean_x; "
|
||||
" "
|
||||
" rmse := sqrt(sum((beta * x + alpha - y)^2) / y[]); "
|
||||
" } "
|
||||
" else "
|
||||
" { "
|
||||
" alpha := null; "
|
||||
" beta := null; "
|
||||
" rmse := null; "
|
||||
" } ";
|
||||
|
||||
T x[] = {T( 1), T( 2), T(3), T( 4), T( 5), T(6), T( 7), T( 8), T( 9), T(10)};
|
||||
T y[] = {T(8.7), T(6.8), T(6), T(5.6), T(3.8), T(3), T(2.4), T(1.7), T(0.4), T(-1)};
|
||||
T x[] = { T(1.0), T(2.0), T(3.0), T(4.0), T(5.0), T(6.0), T(7.0), T(8.0), T(9.0), T(10) };
|
||||
T y[] = { T(8.7), T(6.8), T(6.0), T(5.6), T(3.8), T(3.0), T(2.4), T(1.7), T(0.4), T(-1) };
|
||||
|
||||
T alpha = T(0);
|
||||
T beta = T(0);
|
||||
@ -65,7 +69,11 @@ void linear_least_squares()
|
||||
expression.register_symbol_table(symbol_table);
|
||||
|
||||
parser_t parser;
|
||||
parser.compile(linear_least_squares_program,expression);
|
||||
if (!parser.compile(linear_least_squares_program,expression))
|
||||
{
|
||||
printf("error: %s\n",parser.error().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
expression.value();
|
||||
|
||||
|
Reference in New Issue
Block a user