Sacado Package Browser (Single Doxygen Collection)
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
example
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;
83
Sacado::Tay::Taylor<double>
t(deg);
84
t.
fastAccessCoeff
(0) = pi/4.0;
85
t.
fastAccessCoeff
(1) = 1.0;
86
Sacado::Tay::Taylor<double>
u =
std::tan
(t);
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
fabs
fabs(expr.val())
Sacado_No_Kokkos.hpp
main
int main()
Definition
ad_example.cpp:191
Sacado::Tay::Taylor
Taylor polynomial class.
Definition
Sacado_Tay_Taylor.hpp:52
Sacado::Tay::Taylor::reserve
void reserve(int d)
Reserve space for a degree d polynomial.
Definition
Sacado_Tay_TaylorImp.hpp:188
Sacado::Tay::Taylor::fastAccessCoeff
T & fastAccessCoeff(int i)
Returns degree i term without bounds checking.
Definition
Sacado_Tay_Taylor.hpp:191
Sacado::Tay::Taylor::coeff
const T * coeff() const
Returns Taylor coefficient array.
Definition
Sacado_Tay_Taylor.hpp:181
Sacado::Tay::Taylor::resize
void resize(int d, bool keep_coeffs)
Resize polynomial to degree d.
Definition
Sacado_Tay_TaylorImp.hpp:169
std::atan
ATanExprType< T >::expr_type atan(const Expr< T > &expr)
Definition
Sacado_Tay_CacheTaylorOps.hpp:1838
std::tan
TanExprType< T >::expr_type tan(const Expr< T > &expr)
Definition
Sacado_Tay_CacheTaylorOps.hpp:1778
func
void func(ScalarT &f, const ScalarT &x)
Definition
taylor_example.cpp:51
tol
const double tol
Definition
tradoptest_01.cpp:61
Generated by
1.17.0