Teuchos - Trilinos Tools Package
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
numerics
src
Teuchos_Polynomial.hpp
1
// @HEADER
2
// ***********************************************************************
3
//
4
// Teuchos: Common Tools Package
5
// Copyright (2004) Sandia Corporation
6
//
7
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8
// license for use of this work by or on behalf of the U.S. Government.
9
//
10
// Redistribution and use in source and binary forms, with or without
11
// modification, are permitted provided that the following conditions are
12
// met:
13
//
14
// 1. Redistributions of source code must retain the above copyright
15
// notice, this list of conditions and the following disclaimer.
16
//
17
// 2. Redistributions in binary form must reproduce the above copyright
18
// notice, this list of conditions and the following disclaimer in the
19
// documentation and/or other materials provided with the distribution.
20
//
21
// 3. Neither the name of the Corporation nor the names of the
22
// contributors may be used to endorse or promote products derived from
23
// this software without specific prior written permission.
24
//
25
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38
//
39
// ***********************************************************************
40
// @HEADER
41
42
#ifndef TEUCHOS_POLYNOMIAL_HPP
43
#define TEUCHOS_POLYNOMIAL_HPP
44
45
#include "Teuchos_PolynomialDecl.hpp"
46
#include "
Teuchos_ScalarTraits.hpp
"
47
48
template
<
typename
CoeffT>
49
Teuchos::Polynomial<CoeffT>::Polynomial
(
unsigned
int
deg,
50
const
CoeffT& cloneCoeff,
51
unsigned
int
reserve) :
52
d
(deg)
53
{
54
if
(reserve >
d
)
55
sz
= reserve+1;
56
else
57
sz
=
d
+1;
58
59
coeff
.resize(
sz
);
60
for
(
unsigned
int
i=0; i<
sz
; i++)
61
coeff
[i] =
PolynomialTraits<CoeffT>::clone
(cloneCoeff);
62
}
63
64
template
<
typename
CoeffT>
65
Teuchos::Polynomial<CoeffT>::Polynomial
(
unsigned
int
deg,
66
unsigned
int
reserve) :
67
d
(deg)
68
{
69
if
(reserve >
d
)
70
sz
= reserve+1;
71
else
72
sz
=
d
+1;
73
74
coeff
.resize(
sz
);
75
}
76
77
template
<
typename
CoeffT>
78
Teuchos::Polynomial<CoeffT>::~Polynomial
()
79
{
80
}
81
82
template
<
typename
CoeffT>
83
void
84
Teuchos::Polynomial<CoeffT>::setDegree
(
unsigned
int
deg)
85
{
86
d
= deg;
87
if
(
d
+1 >
sz
) {
88
coeff
.resize(
d
+1);
89
if
(
coeff
[0] != Teuchos::null) {
90
for
(
unsigned
int
i=
sz
; i<
d
+1; i++)
91
coeff
[i] =
PolynomialTraits<CoeffT>::clone
(*
coeff
[0]);
92
}
93
sz
=
d
+1;
94
}
95
}
96
97
template
<
typename
CoeffT>
98
Teuchos::RCP<CoeffT>
99
Teuchos::Polynomial<CoeffT>::getCoefficient
(
unsigned
int
i)
100
{
101
#ifdef TEUCHOS_DEBUG
102
TEUCHOS_TEST_FOR_EXCEPTION
(i >
d
,
103
std::out_of_range,
104
"Polynomial<CoeffT>::getCoefficient(i): "
<<
105
"Error, coefficient i = "
<< i <<
106
" is not in range, degree = "
<<
d
<<
"."
);
107
#endif
108
return
coeff
[i];
109
}
110
111
template
<
typename
CoeffT>
112
Teuchos::RCP<const CoeffT>
113
Teuchos::Polynomial<CoeffT>::getCoefficient
(
unsigned
int
i)
const
114
{
115
#ifdef TEUCHOS_DEBUG
116
TEUCHOS_TEST_FOR_EXCEPTION
(i >
d
,
117
std::out_of_range,
118
"Polynomial<CoeffT>::getCoefficient(i): "
<<
119
"Error, coefficient i = "
<< i <<
120
" is not in range, degree = "
<<
d
<<
"."
);
121
#endif
122
return
coeff
[i];
123
}
124
125
template
<
typename
CoeffT>
126
void
127
Teuchos::Polynomial<CoeffT>::setCoefficient
(
unsigned
int
i,
const
CoeffT& v)
128
{
129
#ifdef TEUCHOS_DEBUG
130
TEUCHOS_TEST_FOR_EXCEPTION
(i >
d
,
131
std::out_of_range,
132
"Polynomial<CoeffT>::setCoefficient(i,v): "
<<
133
"Error, coefficient i = "
<< i <<
134
" is not in range, degree = "
<<
d
<<
"."
);
135
TEUCHOS_TEST_FOR_EXCEPTION
(
coeff
[i] == Teuchos::null,
136
std::runtime_error,
137
"Polynomial<CoeffT>::setCoefficient(i,v): "
<<
138
"Error, coefficient i = "
<< i <<
" is null!"
);
139
#endif
140
PolynomialTraits<CoeffT>::copy
(v,
coeff
[i].
get
());
141
}
142
143
template
<
typename
CoeffT>
144
void
145
Teuchos::Polynomial<CoeffT>::setCoefficientPtr
(
146
unsigned
int
i,
147
const
Teuchos::RCP<CoeffT>
& v)
148
{
149
#ifdef TEUCHOS_DEBUG
150
TEUCHOS_TEST_FOR_EXCEPTION
(i >
d
,
151
std::out_of_range,
152
"Polynomial<CoeffT>::setCoefficientPtr(i,v): "
<<
153
"Error, coefficient i = "
<< i <<
154
" is not in range, degree = "
<<
d
<<
"."
);
155
#endif
156
coeff
[i] = v;
157
}
158
159
template
<
typename
CoeffT>
160
void
161
Teuchos::Polynomial<CoeffT>::evaluate
(
162
typename
Teuchos::Polynomial<CoeffT>::scalar_type
& t,
163
CoeffT* x, CoeffT* xdot)
const
164
{
165
bool
evaluate_xdot = (xdot != NULL);
166
167
#ifdef TEUCHOS_DEBUG
168
for
(
unsigned
int
i=0; i<=
d
; i++)
169
TEUCHOS_TEST_FOR_EXCEPTION
(
coeff
[i] == Teuchos::null,
170
std::runtime_error,
171
"Polynomial<CoeffT>::evaluate(): "
<<
172
"Error, coefficient i = "
<< i <<
" is null!"
);
173
#endif
174
175
// Initialize x, xdot with coeff[d]
176
PolynomialTraits<CoeffT>::copy
(*
coeff
[
d
], x);
177
if
(evaluate_xdot) {
178
if
(
d
> 0)
179
PolynomialTraits<CoeffT>::copy
(*
coeff
[
d
], xdot);
180
else
181
PolynomialTraits<CoeffT>::assign
(
182
xdot,
183
Teuchos::ScalarTraits<scalar_type>::zero
());
184
}
185
186
// If this is a degree 0 polynomial, we're done
187
if
(
d
== 0)
188
return
;
189
190
for
(
int
k=
d
-1; k>=0; --k) {
191
// compute x = coeff[k] + t*x
192
PolynomialTraits<CoeffT>::update
(x, *
coeff
[k], t);
193
194
// compute xdot = x + t*xdot
195
if
(evaluate_xdot && k > 0)
196
PolynomialTraits<CoeffT>::update
(xdot, *x, t);
197
}
198
}
199
200
#endif
// TEUCHOS_VECTOR_POLYNOMIAL_HPP
Teuchos_ScalarTraits.hpp
Defines basic traits for the scalar field type.
Teuchos::ParameterList::get
T & get(ParameterList &l, const std::string &name)
A shorter name for getParameter().
Definition
Teuchos_ParameterList.hpp:1320
Teuchos::PolynomialTraits::assign
static void assign(coeff_type *y, const scalar_type &alpha)
Assign a scalar to a coefficient.
Definition
Teuchos_PolynomialTraits.hpp:78
Teuchos::PolynomialTraits::update
static void update(coeff_type *y, const coeff_type &x, const scalar_type &beta)
y = x + beta*y
Definition
Teuchos_PolynomialTraits.hpp:83
Teuchos::PolynomialTraits::copy
static void copy(const coeff_type &x, coeff_type *y)
Copy a coefficient.
Definition
Teuchos_PolynomialTraits.hpp:73
Teuchos::PolynomialTraits::clone
static Teuchos::RCP< coeff_type > clone(const coeff_type &c)
Clone a coefficient.
Definition
Teuchos_PolynomialTraits.hpp:68
Teuchos::Polynomial::setCoefficient
void setCoefficient(unsigned int i, const CoeffT &v)
Set coefficient i to c.
Definition
Teuchos_Polynomial.hpp:127
Teuchos::Polynomial::getCoefficient
Teuchos::RCP< CoeffT > getCoefficient(unsigned int i)
Return ref-count pointer to coefficient i.
Definition
Teuchos_Polynomial.hpp:99
Teuchos::Polynomial::scalar_type
Teuchos::PolynomialTraits< coeff_type >::scalar_type scalar_type
Typename of scalars.
Definition
Teuchos_PolynomialDecl.hpp:76
Teuchos::Polynomial::sz
unsigned int sz
Size of polynomial (may be > d).
Definition
Teuchos_PolynomialDecl.hpp:154
Teuchos::Polynomial::~Polynomial
~Polynomial()
Destructor.
Definition
Teuchos_Polynomial.hpp:78
Teuchos::Polynomial::evaluate
void evaluate(typename Teuchos::Polynomial< CoeffT >::scalar_type &t, CoeffT *x, CoeffT *xdot=NULL) const
Evaluate polynomial and possibly its derivative at time t.
Definition
Teuchos_Polynomial.hpp:161
Teuchos::Polynomial::setDegree
void setDegree(unsigned int deg)
Set degree of polynomial to deg.
Definition
Teuchos_Polynomial.hpp:84
Teuchos::Polynomial::coeff
std::vector< Teuchos::RCP< CoeffT > > coeff
Vector of polynomial coefficients.
Definition
Teuchos_PolynomialDecl.hpp:160
Teuchos::Polynomial::d
unsigned int d
Degree of polynomial.
Definition
Teuchos_PolynomialDecl.hpp:151
Teuchos::Polynomial::Polynomial
Polynomial(unsigned int deg, const CoeffT &cloneCoeff, unsigned int reserve=0)
Create a polynomial of degree deg.
Definition
Teuchos_Polynomial.hpp:49
Teuchos::Polynomial::setCoefficientPtr
void setCoefficientPtr(unsigned int i, const Teuchos::RCP< CoeffT > &c_ptr)
Set pointer for coefficient i to c_ptr. DANGEROUS!
Definition
Teuchos_Polynomial.hpp:145
Teuchos::RCP
Smart reference counting pointer class for automatic garbage collection.
Definition
Teuchos_RCPDecl.hpp:429
TEUCHOS_TEST_FOR_EXCEPTION
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Definition
Teuchos_TestForException.hpp:178
Teuchos::ScalarTraits::zero
static T zero()
Returns representation of zero for this scalar type.
Definition
Teuchos_ScalarTraitsDecl.hpp:134
Generated by
1.17.0