stator
A math, geometry, and utility library
symbolic_example.cpp
Go to the documentation of this file.
1 
6 /*
7  Copyright (C) 2015 Marcus N Campbell Bannerman <[email protected]>
8 
9  This file is part of stator.
10 
11  stator is free software: you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  stator is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with stator. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include <iostream>
27 
28 int main(int argc, char **argv) {
29  using namespace sym;
30  //The default variable is the letter x
31  Var<> x;
32 
33  //But you can specify your own variables by letter:
34  Var<vidx<'y'> > y;
35 
36  //Or by index:
37  Var<vidx<42>> v42;
38  //We have to catch symbolic expressions using "auto" as their type is very complex
39  auto f1 = x * x + sin(y);
40 
41  std::cout << f1 << std::endl;
42  //Output: ((x × x) + sin(y))
43  auto f1_xsub = sub(f1, x = y + 2);
44 
45  std::cout << f1_xsub << std::endl;
46  //Output: (((y + 2) × (y + 2)) + sin(y))
47  std::cout << sub(f1_xsub, y = 3.14159265359) << std::endl;
48  //Output: 26.436...
49  auto f2 = (3-3) * x;
50  std::cout << f2 << std::endl;
51  //Output: (0 × x)
52  C<1, 2> half;
53  C<2> two;
54 
55  //Compile-time rational arithmetic!
56  auto three = half + half + two;
57 
58  //The variable three is of type "C<3>", thus it must be computed at compile time.
59  assert((std::is_same<decltype(three), C<3> >::value)); //does not fail
60 
61  std::cout << three << std::endl;
62  //Output: C<3>()
63  auto f3 = (C<1>() - C<1>()) * sin(x);
64 
65  std::cout << f3 << std::endl;
66  //Output: C<0>()
67  auto f4 = C<1>() * sin(x);
68  std::cout << f4 << std::endl;
69  //Output: sin(x)
70  auto f5 = cos(2 * x);
71  auto df5_dx = derivative(f5, x);
72 
73  std::cout << df5_dx << std::endl;
74  //Output: (-2 × sin((2 × x)))
75  //A taylor series of sin(2x) around zero
76  std::cout << taylor_series<3>(sin(C<2>() * x), C<0>(), x) << std::endl;
77  //Output: (x × (C<2>() + ((x ^ C<2>()) × C<-4,3>())))
78  std::cout << x * x * x * x * x << std::endl;
79  //Output: ((((x × x) × x) × x) × x)
80 
81  std::cout << simplify(x * x * x * x * x) << std::endl;
82  //Output: (x ^ C<5>()) (which is equivalent to "pow(x, C<5>())")
83 
84  auto f6 = taylor_series<3>(sin(C<2>() * x), C<0>(), x);
85  std::cout << f6 << std::endl;
86  //Output: (x × (C<2>() + ((x ^ C<2>()) × C<-4,3>())))
87 
88  auto f6_poly = expand(f6);
89  std::cout << f6_poly << std::endl;
90  //Output: P(-1.33333 × x³ + 2 × x)
91 
92  std::cout << f6_poly[0] << std::endl;
93  //Output: 0
94  std::cout << f6_poly[3] << std::endl;
95  //Output: -1.33333
96  //Then we can analyse its real roots!
97  auto f6_roots = solve_real_roots(f6_poly);
98 
99  std::cout << f6_roots << std::endl;
100  //Output: StackVector{ -1.22474 0 1.22474 }
101 
102  //Extracting the number of roots
103  std::cout << f6_roots.size() << std::endl;
104  //Output: 3
105 
106  //Extracting a value of a root
107  std::cout << f6_roots[2] << std::endl;
108  //Output: 1.22474
109  Eigen::Matrix<double, 1, 3> r{1.0, 2.0, 3.0};
110  Eigen::Matrix<double, 1, 3> v{1.0, 0.5, 0.1};
111  auto fvec = r + x * v;
112  std::cout << sub(fvec, x = 12) << std::endl;
113  //Output: 13 8 4.2
114 
115  {//Quick example
116 
117  Var<vidx<'x'> > x; //Create a variable "x"
118  Var<vidx<'y'> > y; //Create a variable "y"
119 
120  auto f = sin(x) + 2 * cos(x); //Define a function f(x)
121 
122  //Perform a substitution
123  auto g = sub(f, x = y*y); //g(y)=f(y^2)
124 
125  //Print the function to screen
126  std::cout << g << std::endl;
127  //Output: (sin((y × y)) + (2 × cos((y × y))))
128 
129  //Evaluate the function
130  double a = sub(g, y = 2.3); // a = g(2.3)
131  std::cout << a << std::endl;
132  //Output: 0.254279
133  auto dg_dy = derivative(g, y); // dg/dy
134  std::cout << dg_dy << std::endl;
135  //Output: (((y + y) × cos((y × y))) + (2 × ((C<-1>() × (y + y)) × sin((y × y)))))
136  //Take a 5th order taylor series of the derivative around y=0
137  auto poly = expand(taylor_series<5>(dg_dy, C<0>(), y));
138  std::cout << poly << std::endl;
139  //Output: P(-1 × y^5 + -4 × y³ + 2 × y)
140 
141  //Calculate the real roots of this 5th order polynomial
142  std::cout << solve_real_roots(poly) << std::endl;
143  //Output: StackVector{ -0.67044 0.67044 }
144  Expr f_rt = "sin(x) + 2 * cos(x)";
145  Expr g_rt = sub(f_rt, x=y*y);
146  a = simplify(sub(g_rt, y = 2.3)).as<double>(); // a = g(2.3)
147  Expr dg_dy_rt = derivative(g_rt, y);
148  }//End of the example
149 }
T as() const
Definition: runtime.hpp:360
Symbolic representation of a variable.
Definition: symbolic.hpp:94
auto cos(const C< num, den > &a) -> STATOR_AUTORETURN(cos_Cimpl(a, detail::select_overload
Definition: constants.hpp:186
A class representing a compile-time rational constant (i.e., std::ratio).
Definition: constants.hpp:31
Main header for the stator::symbolic library.
auto expand(const T &a) -> STATOR_AUTORETURN(simplify< ExpandConfig >(a))
A variant of simplify that expands into Polynomial types aggressively.
int main(int argc, char **argv)
StackVector< double, 0 > solve_real_roots(Null f)
Definition: symbolic.hpp:217
Expr simplify(const Expr &f)
Definition: runtime.hpp:511
auto sin(const C< num, den > &a) -> STATOR_AUTORETURN(sin_Cimpl(a, detail::select_overload
Definition: constants.hpp:183
The generic holder/smart pointer for a runtime Abstract Syntax Tree (AST) (expression).
Definition: runtime.hpp:68
The stator symbolic math library.
auto sub(BinaryOp< LHS, Op, RHS > f, Relation< Var, Arg > x) -> STATOR_AUTORETURN_BYVALUE(Op::apply(sub(f._l, x), sub(f._r, x)))
auto derivative(const Expression &)
Performs a symbolic derivative on the expression.