Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
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>
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>
79 {
80 }
81 
82 template <typename CoeffT>
83 void
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>
100 {
101 #ifdef TEUCHOS_DEBUG
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>
114 {
115 #ifdef TEUCHOS_DEBUG
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
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
146  unsigned int i,
147  const Teuchos::RCP<CoeffT>& v)
148 {
149 #ifdef TEUCHOS_DEBUG
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
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
182  xdot,
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)
197  }
198 }
199 
200 #endif // TEUCHOS_VECTOR_POLYNOMIAL_HPP
Teuchos::PolynomialTraits< coeff_type >::scalar_type scalar_type
Typename of scalars.
static void assign(coeff_type *y, const scalar_type &alpha)
Assign a scalar to a coefficient.
static void update(coeff_type *y, const coeff_type &x, const scalar_type &beta)
y = x + beta*y
void setDegree(unsigned int deg)
Set degree of polynomial to deg.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
unsigned int sz
Size of polynomial (may be &gt; d)
Polynomial(unsigned int deg, const CoeffT &cloneCoeff, unsigned int reserve=0)
Create a polynomial of degree deg.
This structure defines some basic traits for a scalar field type.
void setCoefficient(unsigned int i, const CoeffT &v)
Set coefficient i to c.
void setCoefficientPtr(unsigned int i, const Teuchos::RCP< CoeffT > &c_ptr)
Set pointer for coefficient i to c_ptr. DANGEROUS!
Teuchos::RCP< CoeffT > getCoefficient(unsigned int i)
Return ref-count pointer to coefficient i.
std::vector< Teuchos::RCP< CoeffT > > coeff
Vector of polynomial coefficients.
Traits class for polynomial coefficients in Teuchos::Polynomial.
Defines basic traits for the scalar field type.
Smart reference counting pointer class for automatic garbage collection.
void evaluate(typename Teuchos::Polynomial< CoeffT >::scalar_type &t, CoeffT *x, CoeffT *xdot=NULL) const
Evaluate polynomial and possibly its derivative at time t.
static void copy(const coeff_type &x, coeff_type *y)
Copy a coefficient.
unsigned int d
Degree of polynomial.