Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Polynomial_UnitTest.cpp
Go to the documentation of this file.
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 
43 
44 #include "Teuchos_Polynomial.hpp"
45 #include "Teuchos_Array.hpp"
46 
48 using Teuchos::as;
49 using Teuchos::Array;
50 using Teuchos::RCP;
51 using Teuchos::rcp;
52 
53 
54 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, create ) {
55  Polynomial<double> P(0,1.0);
56  TEST_EQUALITY_CONST( P.degree(), 0 );
57 }
58 
59 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, degrees ) {
60  for (unsigned int degree=0 ; degree<10 ; ++degree) {
61  Polynomial<double> P(degree,1.0);
62  TEST_EQUALITY_CONST( P.degree(), degree );
63  }
64 }
65 
66 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, coeffs ) {
67  unsigned int degree = 10;
68  Polynomial<double> P(degree,20.0);
69  for (unsigned int d=0 ; d <= degree ; ++d) {
70  P.setCoefficient(d,d*1.0);
71  }
72  for (unsigned int d=0 ; d <= degree ; ++d) {
73  TEST_EQUALITY_CONST( *(P.getCoefficient(d)), d*1.0 );
74  }
75 }
76 
77 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, coeffsPtr ) {
78  unsigned int degree = 10;
79  Polynomial<double> P(degree);
80  for (unsigned int d=0 ; d <= degree ; ++d) {
81  RCP<double> coeffPtr = rcp(new double(d*1.0));
82  P.setCoefficientPtr(d,coeffPtr);
83  }
84  for (unsigned int d=0 ; d <= degree ; ++d) {
85  TEST_EQUALITY_CONST( *(P.getCoefficient(d)), d*1.0 );
86  }
87 }
88 
89 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, RCPcoeffs ) {
90  int degree = 10;
91  Polynomial<double> P(degree,20.0);
92  for (int d=0 ; d <= degree ; ++d) {
93  P.setCoefficient(d,d*1.0);
94  }
95  RCP<const double> constCoeff = rcp(new double);
96  constCoeff = P.getCoefficient(8);
97  TEST_EQUALITY_CONST( *constCoeff, 8.0 );
98 
99  RCP<double> coeff = rcp(new double);
100  coeff = P.getCoefficient(4);
101  TEST_EQUALITY_CONST( *coeff, 4.0 );
102 
103 }
104 
105 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, evaluate ) {
106  int degree = 2;
107  Polynomial<double> P(degree,0.0);
108  P.setCoefficient(0,-1.0);
109  P.setCoefficient(1, 0.0);
110  P.setCoefficient(2, 1.0);
111  int numTests = 11;
112  Array<double> testValues(numTests);
113  for (int i=0 ; i<numTests ; ++i) {
114  testValues[i] = (i-5);
115  }
116  Array<double> polyValues(numTests);
117  Array<double> polyDotValues(numTests);
118  for (int i=0 ; i<numTests ; ++i) {
119  polyValues[i] = pow(testValues[i],2.0)-1.0;
120  polyDotValues[i] = 2*testValues[i];
121  }
122  for (int i=0 ; i<numTests ; ++i ) {
123  double x_out;
124  double x_dot_out;
125  P.evaluate(testValues[i], &x_out, &x_dot_out );
126  TEST_EQUALITY( x_out, polyValues[i] );
127  TEST_EQUALITY( x_dot_out, polyDotValues[i] );
128  }
129 }
130 
131 #ifdef TEUCHOS_DEBUG
132 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, errors ) {
133  {
134  unsigned int degree = 2;
135  const Polynomial<double> constP(degree,20.0);
136  RCP<const double> constCoeff = rcp(new double);
137  TEST_THROW( constCoeff = constP.getCoefficient(3), std::out_of_range );
138  }
139  {
140  unsigned int degree = 2;
141  Polynomial<double> P(degree,20.0);
142  RCP<double> coeff = rcp(new double);
143  TEST_THROW( coeff = P.getCoefficient(3), std::out_of_range );
144  }
145  {
146  unsigned int degree = 2;
147  Polynomial<double> P(degree,20.0);
148  unsigned int i = 3;
149  const double coeff = 5.0;
150  TEST_THROW( P.setCoefficient(i,coeff), std::out_of_range );
151  }
152  {
153  unsigned int degree = 2;
154  Polynomial<double> P(degree);
155  unsigned int i = 0;
156  const double coeff = 5.0;
157  TEST_THROW( P.setCoefficient(i,coeff), std::runtime_error );
158  }
159  {
160  unsigned int degree = 2;
161  Polynomial<double> P(degree);
162  unsigned int i = 3;
163  RCP<double> coeff = rcp(new double(5.0));
164  TEST_THROW( P.setCoefficientPtr(i,coeff), std::out_of_range );
165  }
166  {
167  unsigned int degree = 2;
168  Polynomial<double> P(degree);
169  double t = 2.5;
170  double x;
171  double x_dot;
172  TEST_THROW( P.evaluate(t,&x,&x_dot), std::runtime_error );
173  }
174 }
175 #endif // TEUCHOS_DEBUG
176 
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
void setCoefficient(unsigned int i, const CoeffT &v)
Set coefficient i to c.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
void setCoefficientPtr(unsigned int i, const Teuchos::RCP< CoeffT > &c_ptr)
Set pointer for coefficient i to c_ptr. DANGEROUS!
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Lightweight container class to represent a simple polynomial.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Templated array class derived from the STL std::vector.
Teuchos::RCP< CoeffT > getCoefficient(unsigned int i)
Return ref-count pointer to coefficient i.
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.
unsigned int degree() const
Return degree of polynomial.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...