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
// Teuchos: Common Tools Package
4
//
5
// Copyright 2004 NTESS and the Teuchos contributors.
6
// SPDX-License-Identifier: BSD-3-Clause
7
// *****************************************************************************
8
// @HEADER
9
10
#ifndef TEUCHOS_POLYNOMIAL_HPP
11
#define TEUCHOS_POLYNOMIAL_HPP
12
13
#include "Teuchos_PolynomialDecl.hpp"
14
#include "
Teuchos_ScalarTraits.hpp
"
15
16
template
<
typename
CoeffT>
17
Teuchos::Polynomial<CoeffT>::Polynomial
(
unsigned
int
deg,
18
const
CoeffT& cloneCoeff,
19
unsigned
int
reserve) :
20
d
(deg)
21
{
22
if
(reserve >
d
)
23
sz
= reserve+1;
24
else
25
sz
=
d
+1;
26
27
coeff
.resize(
sz
);
28
for
(
unsigned
int
i=0; i<
sz
; i++)
29
coeff
[i] =
PolynomialTraits<CoeffT>::clone
(cloneCoeff);
30
}
31
32
template
<
typename
CoeffT>
33
Teuchos::Polynomial<CoeffT>::Polynomial
(
unsigned
int
deg,
34
unsigned
int
reserve) :
35
d
(deg)
36
{
37
if
(reserve >
d
)
38
sz
= reserve+1;
39
else
40
sz
=
d
+1;
41
42
coeff
.resize(
sz
);
43
}
44
45
template
<
typename
CoeffT>
46
Teuchos::Polynomial<CoeffT>::~Polynomial
()
47
{
48
}
49
50
template
<
typename
CoeffT>
51
void
52
Teuchos::Polynomial<CoeffT>::setDegree
(
unsigned
int
deg)
53
{
54
d
= deg;
55
if
(
d
+1 >
sz
) {
56
coeff
.resize(
d
+1);
57
if
(
coeff
[0] != Teuchos::null) {
58
for
(
unsigned
int
i=
sz
; i<
d
+1; i++)
59
coeff
[i] =
PolynomialTraits<CoeffT>::clone
(*
coeff
[0]);
60
}
61
sz
=
d
+1;
62
}
63
}
64
65
template
<
typename
CoeffT>
66
Teuchos::RCP<CoeffT>
67
Teuchos::Polynomial<CoeffT>::getCoefficient
(
unsigned
int
i)
68
{
69
#ifdef TEUCHOS_DEBUG
70
TEUCHOS_TEST_FOR_EXCEPTION
(i >
d
,
71
std::out_of_range,
72
"Polynomial<CoeffT>::getCoefficient(i): "
<<
73
"Error, coefficient i = "
<< i <<
74
" is not in range, degree = "
<<
d
<<
"."
);
75
#endif
76
return
coeff
[i];
77
}
78
79
template
<
typename
CoeffT>
80
Teuchos::RCP<const CoeffT>
81
Teuchos::Polynomial<CoeffT>::getCoefficient
(
unsigned
int
i)
const
82
{
83
#ifdef TEUCHOS_DEBUG
84
TEUCHOS_TEST_FOR_EXCEPTION
(i >
d
,
85
std::out_of_range,
86
"Polynomial<CoeffT>::getCoefficient(i): "
<<
87
"Error, coefficient i = "
<< i <<
88
" is not in range, degree = "
<<
d
<<
"."
);
89
#endif
90
return
coeff
[i];
91
}
92
93
template
<
typename
CoeffT>
94
void
95
Teuchos::Polynomial<CoeffT>::setCoefficient
(
unsigned
int
i,
const
CoeffT& v)
96
{
97
#ifdef TEUCHOS_DEBUG
98
TEUCHOS_TEST_FOR_EXCEPTION
(i >
d
,
99
std::out_of_range,
100
"Polynomial<CoeffT>::setCoefficient(i,v): "
<<
101
"Error, coefficient i = "
<< i <<
102
" is not in range, degree = "
<<
d
<<
"."
);
103
TEUCHOS_TEST_FOR_EXCEPTION
(
coeff
[i] == Teuchos::null,
104
std::runtime_error,
105
"Polynomial<CoeffT>::setCoefficient(i,v): "
<<
106
"Error, coefficient i = "
<< i <<
" is null!"
);
107
#endif
108
PolynomialTraits<CoeffT>::copy
(v,
coeff
[i].
get
());
109
}
110
111
template
<
typename
CoeffT>
112
void
113
Teuchos::Polynomial<CoeffT>::setCoefficientPtr
(
114
unsigned
int
i,
115
const
Teuchos::RCP<CoeffT>
& v)
116
{
117
#ifdef TEUCHOS_DEBUG
118
TEUCHOS_TEST_FOR_EXCEPTION
(i >
d
,
119
std::out_of_range,
120
"Polynomial<CoeffT>::setCoefficientPtr(i,v): "
<<
121
"Error, coefficient i = "
<< i <<
122
" is not in range, degree = "
<<
d
<<
"."
);
123
#endif
124
coeff
[i] = v;
125
}
126
127
template
<
typename
CoeffT>
128
void
129
Teuchos::Polynomial<CoeffT>::evaluate
(
130
typename
Teuchos::Polynomial<CoeffT>::scalar_type
& t,
131
CoeffT* x, CoeffT* xdot)
const
132
{
133
bool
evaluate_xdot = (xdot != NULL);
134
135
#ifdef TEUCHOS_DEBUG
136
for
(
unsigned
int
i=0; i<=
d
; i++)
137
TEUCHOS_TEST_FOR_EXCEPTION
(
coeff
[i] == Teuchos::null,
138
std::runtime_error,
139
"Polynomial<CoeffT>::evaluate(): "
<<
140
"Error, coefficient i = "
<< i <<
" is null!"
);
141
#endif
142
143
// Initialize x, xdot with coeff[d]
144
PolynomialTraits<CoeffT>::copy
(*
coeff
[
d
], x);
145
if
(evaluate_xdot) {
146
if
(
d
> 0)
147
PolynomialTraits<CoeffT>::copy
(*
coeff
[
d
], xdot);
148
else
149
PolynomialTraits<CoeffT>::assign
(
150
xdot,
151
Teuchos::ScalarTraits<scalar_type>::zero
());
152
}
153
154
// If this is a degree 0 polynomial, we're done
155
if
(
d
== 0)
156
return
;
157
158
for
(
int
k=
d
-1; k>=0; --k) {
159
// compute x = coeff[k] + t*x
160
PolynomialTraits<CoeffT>::update
(x, *
coeff
[k], t);
161
162
// compute xdot = x + t*xdot
163
if
(evaluate_xdot && k > 0)
164
PolynomialTraits<CoeffT>::update
(xdot, *x, t);
165
}
166
}
167
168
#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:1308
Teuchos::Polynomial::setCoefficient
void setCoefficient(unsigned int i, const CoeffT &v)
Set coefficient i to c.
Definition
Teuchos_Polynomial.hpp:95
Teuchos::Polynomial::getCoefficient
Teuchos::RCP< CoeffT > getCoefficient(unsigned int i)
Return ref-count pointer to coefficient i.
Definition
Teuchos_Polynomial.hpp:67
Teuchos::Polynomial::scalar_type
Teuchos::PolynomialTraits< coeff_type >::scalar_type scalar_type
Typename of scalars.
Definition
Teuchos_PolynomialDecl.hpp:44
Teuchos::Polynomial::sz
unsigned int sz
Size of polynomial (may be > d).
Definition
Teuchos_PolynomialDecl.hpp:122
Teuchos::Polynomial::~Polynomial
~Polynomial()
Destructor.
Definition
Teuchos_Polynomial.hpp:46
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:129
Teuchos::Polynomial::setDegree
void setDegree(unsigned int deg)
Set degree of polynomial to deg.
Definition
Teuchos_Polynomial.hpp:52
Teuchos::Polynomial::coeff
std::vector< Teuchos::RCP< CoeffT > > coeff
Vector of polynomial coefficients.
Definition
Teuchos_PolynomialDecl.hpp:128
Teuchos::Polynomial::d
unsigned int d
Degree of polynomial.
Definition
Teuchos_PolynomialDecl.hpp:119
Teuchos::Polynomial::Polynomial
Polynomial(unsigned int deg, const CoeffT &cloneCoeff, unsigned int reserve=0)
Create a polynomial of degree deg.
Definition
Teuchos_Polynomial.hpp:17
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:113
Teuchos::PolynomialTraits::assign
static void assign(coeff_type *y, const scalar_type &alpha)
Assign a scalar to a coefficient.
Definition
Teuchos_PolynomialTraits.hpp:46
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:51
Teuchos::PolynomialTraits::copy
static void copy(const coeff_type &x, coeff_type *y)
Copy a coefficient.
Definition
Teuchos_PolynomialTraits.hpp:41
Teuchos::PolynomialTraits::clone
static Teuchos::RCP< coeff_type > clone(const coeff_type &c)
Clone a coefficient.
Definition
Teuchos_PolynomialTraits.hpp:36
Teuchos::RCP
Smart reference counting pointer class for automatic garbage collection.
Definition
Teuchos_RCPDecl.hpp:397
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:146
Teuchos::ScalarTraits::zero
static T zero()
Returns representation of zero for this scalar type.
Definition
Teuchos_ScalarTraitsDecl.hpp:102
Generated by
1.17.0