Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
taylor_example.cpp
Go to the documentation of this file.
1 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Sacado Package
7 // Copyright (2006) Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // This library is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as
14 // published by the Free Software Foundation; either version 2.1 of the
15 // License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 // USA
26 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27 // (etphipp@sandia.gov).
28 //
29 // ***********************************************************************
30 // @HEADER
31 
32 // taylor_example
33 //
34 // usage:
35 // taylor_example
36 //
37 // output:
38 // prints the results of computing a single Taylor series expansion of
39 // the solution to:
40 //
41 // dx/dt = 1 + x^2, x(0) = 1.0;
42 //
43 // The exact solution is x(t) = tan(t + pi/4)
44 
45 #include <iostream>
46 
47 #include "Sacado_No_Kokkos.hpp"
48 
49 // Function implementing RHS of ODE
50 template <typename ScalarT>
51 void func(ScalarT& f, const ScalarT& x) {
52  f = 1.0 + x*x;
53 }
54 
55 int main(int argc, char **argv)
56 {
57  double x0 = 1.0; // Initial condition
58  int deg = 40; // Degree of Taylor series solution
59 
60  Sacado::Tay::Taylor<double> x = x0; // Taylor polynomial for independent
61  Sacado::Tay::Taylor<double> f; // Taylor polynomial for dependent
62 
63  // Reserve space for degree deg coefficients
64  x.reserve(deg);
65 
66  // Compute Taylor series solution to dx/dt = f(x)
67  for (int k=0; k<deg; k++) {
68  func(f, x);
69 
70  // Set next coefficient
71  x.resize(k+1, true);
72 
73  // x_{k+1} = f_k / (k+1)
74  x.fastAccessCoeff(k+1) = f.coeff(k) / (k+1);
75  }
76 
77  // Print Taylor series solution
78  std::cout << "Taylor series solution = " << std::endl
79  << x << std::endl;
80 
81  // Compute Taylor series expansion of solution x(t) = tan(t+pi/4)
82  double pi = std::atan(1.0)*4.0;
84  t.fastAccessCoeff(0) = pi/4.0;
85  t.fastAccessCoeff(1) = 1.0;
87 
88  // Print expansion of solution
89  std::cout << "Exact expansion = " << std::endl
90  << u << std::endl;
91 
92  // Compute maximum relative error
93  double max_err = 0.0;
94  double err = 0.0;
95  for (int k=0; k<=deg; k++) {
96  err = std::fabs(x.coeff(k) - u.coeff(k)) / (1.0 + fabs(u.coeff(k)));
97  if (err > max_err) max_err = err;
98  }
99  std::cout << "Maximum relative error = " << max_err << std::endl;
100 
101  double tol = 1.0e-12;
102  if (max_err < tol){
103  std::cout << "\nExample passed!" << std::endl;
104  return 0;
105  }
106  else {
107  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
108  return 1;
109  }
110 }
111 
112 
113 
void resize(int d, bool keep_coeffs)
Resize polynomial to degree d.
void f()
atan(expr.val())
T & fastAccessCoeff(int i)
Returns degree i term without bounds checking.
int main()
Definition: ad_example.cpp:191
tan(expr.val())
const T * coeff() const
Returns Taylor coefficient array.
void reserve(int d)
Reserve space for a degree d polynomial.
const double tol
const T func(int n, T *x)
Definition: ad_example.cpp:49
fabs(expr.val())