OpcUaCanOpen
CANopen OPC-UA server
exprtk_simple_example_16.cpp
Go to the documentation of this file.
1 /*
2  **************************************************************
3  * C++ Mathematical Expression Toolkit Library *
4  * *
5  * Simple Example 16 *
6  * Author: Arash Partow (1999-2019) *
7  * URL: http://www.partow.net/programming/exprtk/index.html *
8  * *
9  * Copyright notice: *
10  * Free use of the Mathematical Expression Toolkit Library is *
11  * permitted under the guidelines and in accordance with the *
12  * most current version of the MIT License. *
13  * http://www.opensource.org/licenses/MIT *
14  * *
15  **************************************************************
16 */
17 
18 
19 #include <cstdio>
20 #include <cstdlib>
21 #include <string>
22 
23 #include "exprtk.hpp"
24 
25 
26 template <typename T>
28 {
32 
33  std::string linear_least_squares_program =
34  " if (x[] == y[]) "
35  " { "
36  " beta := (sum(x * y) - sum(x) * sum(y) / x[]) / "
37  " (sum(x^2) - sum(x)^2 / x[]); "
38  " "
39  " alpha := avg(y) - beta * avg(x); "
40  " "
41  " rmse := sqrt(sum((beta * x + alpha - y)^2) / y[]); "
42  " } "
43  " else "
44  " { "
45  " alpha := null; "
46  " beta := null; "
47  " rmse := null; "
48  " } ";
49 
50  T x[] = {T( 1), T( 2), T(3), T( 4), T( 5), T(6), T( 7), T( 8), T( 9), T(10)};
51  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)};
52 
53  T alpha = T(0);
54  T beta = T(0);
55  T rmse = T(0);
56 
57  symbol_table_t symbol_table;
58  symbol_table.add_variable("alpha",alpha);
59  symbol_table.add_variable("beta" ,beta );
60  symbol_table.add_variable("rmse" ,rmse );
61  symbol_table.add_vector ("x" ,x );
62  symbol_table.add_vector ("y" ,y );
63 
64  expression_t expression;
65  expression.register_symbol_table(symbol_table);
66 
68  parser.compile(linear_least_squares_program,expression);
69 
70  expression.value();
71 
72  printf("alpha: %15.12f\n",alpha);
73  printf("beta: %15.12f\n",beta );
74  printf("rmse: %15.12f\n",rmse );
75  printf("y = %15.12fx + %15.12f\n",beta,alpha);
76 }
77 
78 int main()
79 {
80  linear_least_squares<double>();
81  return 0;
82 }