Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
polynomial.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file numeric/polynomial.cc
11 /// @brief Classes for polynomial evaluation functions
12 /// @author Matthew O'Meara (mattjomeara@gmail.com
13 
14 
15 // Unit Headers
16 #include <numeric/polynomial.hh>
17 
18 // Utility headers
19 #include <utility/vector1.hh>
21 
22 // Numeric headers
23 #include <numeric/types.hh>
24 
25 // C++ headers
26 #include <iostream>
27 #include <string>
28 #include <cmath>
29 
30 #ifdef SERIALIZATION
31 // Utility serialization headers
32 #include <utility/vector1.srlz.hh>
34 
35 // Cereal headers
36 #include <cereal/access.hpp>
37 #include <cereal/types/polymorphic.hpp>
38 #include <cereal/types/string.hpp>
39 #endif // SERIALIZATION
40 
41 namespace numeric {
42 
43 using std::string;
44 using std::ostream;
45 using utility::vector1;
46 
47 /// @brief ctor
49  string const & polynomial_name,
50  Real const xmin,
51  Real const xmax,
52  Real const min_val,
53  Real const max_val,
54  Real const root1,
55  Real const root2,
56  Size degree,
57  vector1< Real > const & coefficients):
58  polynomial_name_(polynomial_name),
59  xmin_(xmin), xmax_(xmax), min_val_(min_val), max_val_(max_val), root1_(root1), root2_(root2),
60  degree_(degree),
61  coefficients_(coefficients)
62 {
64 }
65 
67  utility::pointer::ReferenceCount( src ),
68  polynomial_name_(src.polynomial_name_),
69  xmin_(src.xmin_), xmax_(src.xmax_), root1_(src.root1_), root2_(src.root2_),
70  degree_(src.degree_),
71  coefficients_(src.coefficients_)
72 {
74 }
75 
77 
78 void
80 {
81  if ( xmin_ > xmax_ ) {
82  std::stringstream msg;
83  msg << "Polnomial_1d is badly formed because (xmin: '" << xmin_ << "') > (xmax: '" << xmax_ << "')";
84  throw utility::excn::EXCN_Msg_Exception(msg.str() );
85  }
86 
87  if ( coefficients_.size() == 0 ) {
88  std::stringstream msg;
89  msg << "Polnomial_1d is badly formed because no coefficients were provided";
90  throw utility::excn::EXCN_Msg_Exception(msg.str() );
91  }
92 
93  if ( coefficients_.size() != degree_ ) {
94  std::stringstream msg;
95  msg << "Polnomial_1d is badly formed because the degree was given to be '" << degree_ << "' while '" << coefficients_.size() << "' coefficients were provided, while they should be equal.";
96  throw utility::excn::EXCN_Msg_Exception(msg.str() );
97  }
98 
99 
100 }
101 
102 string
104 {
105  return polynomial_name_;
106 }
107 
108 Real
110 {
111  return xmin_;
112 }
113 
114 Real
116 {
117  return xmax_;
118 }
119 
120 Real
122 {
123  return min_val_;
124 }
125 
126 Real
128 {
129  return max_val_;
130 }
131 
132 Real
134 {
135  return root1_;
136 }
137 
138 Real
140 {
141  return root2_;
142 }
143 
144 Size
146 {
147  return degree_;
148 }
149 
150 vector1< Real > const &
152 {
153  return coefficients_;
154 }
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 ///
158 /// @brief evaluate the polynomial and its derivative.
159 ///
160 /// @details
161 ///
162 /// @param variable - [in] - evaluate polynomial(value)
163 /// @param value - [out] - returned output
164 /// @param deriv - [out] - returned output
165 ///
166 /// @global_read
167 ///
168 /// @global_write
169 ///
170 /// @remarks
171 /// Note the coefficients must be in reverse order: low to high
172 ///
173 /// Polynomial value and derivative using Horner's rule
174 /// value = Sum_(i = 1,...,N) [ coeff_i * variable^(i-1) ]
175 /// deriv = Sum_(i = 2,...,N) [ ( i - 1 ) * coeff_i * variable^(i-2) ]
176 /// JSS: Horner's rule for evaluating polynomials is based on rewriting the polynomial as:
177 /// JSS: p(x) = a0 + x*(a1 + x*(a2 + x*(... x*(aN)...)))
178 /// JSS: or value_k = a_k + x*value_k+1 for k = N-1 to 0
179 /// JSS: and the derivative is
180 /// JSS: deriv_k = value_k+1 + deriv_k+1 for k = N-1 to 1
181 ///
182 /// @references
183 ///
184 /// @author Jack Snoeyink
185 /// @author Matthew O'Meara
186 ///
187 /////////////////////////////////////////////////////////////////////////////////
188 void
190  double const variable,
191  double & value,
192  double & deriv) const
193 {
194  if ( variable <= xmin_ ) {
195  value = min_val_;
196  deriv = 0.0;
197  return;
198  }
199  if ( variable >= xmax_ ) {
200  value = max_val_;
201  deriv = 0.0;
202  return;
203  }
204 
205  value = coefficients_[1];
206  deriv = 0.0;
207  for ( Size i=2; i <= degree_; i++ ) {
208  (deriv *= variable) += value;
209  (value *= variable) += coefficients_[i];
210  }
211 }
212 
213 double
214 Polynomial_1d::eval( double const variable )
215 {
216  if ( variable <= xmin_ ) {
217  return min_val_;
218  }
219  if ( variable >= xmax_ ) {
220  return max_val_;
221  }
222  Real value = coefficients_[1];
223  for ( Size i=2; i <= degree_; ++i ) {
224  ( value *= variable ) += coefficients_[i];
225  }
226  return value;
227 }
228 
229 
230 ostream &
231 operator<< ( ostream & out, const Polynomial_1d & poly ){
232  poly.show( out );
233  return out;
234 }
235 
236 void
237 Polynomial_1d::show( ostream & out ) const{
238  out << polynomial_name_ << " "
239  << "domain:(" << xmin_ << "," << xmax_ << ") "
240  << "out_of_range_vals:(" << min_val_ << "," << max_val_ << ") "
241  << "roots:[" << root1_ << "," << root2_ << "] "
242  << "degree:" << degree_ << " "
243  << "y=";
244  for ( Size i=1; i <= degree_; ++i ) {
245  if ( i >1 ) {
246  if ( coefficients_[i] > 0 ) {
247  out << "+";
248  } else if ( coefficients_[i] < 0 ) {
249  out << "-";
250  } else {
251  continue;
252  }
253  }
254  out << std::abs(coefficients_[i]);
255  if ( degree_-i >1 ) {
256  out << "x^" << degree_-i;
257  } else if ( degree_-i == 1 ) {
258  out << "x";
259  } else { }
260  }
261 }
262 
263 } // namespace
264 
265 #ifdef SERIALIZATION
266 
267 /// @brief Default constructor required by cereal to deserialize this class
269 
270 /// @brief Automatically generated serialization method
271 template< class Archive >
272 void
273 numeric::Polynomial_1d::save( Archive & arc ) const {
274  arc( CEREAL_NVP( polynomial_name_ ) ); // std::string
275  arc( CEREAL_NVP( xmin_ ) ); // Real
276  arc( CEREAL_NVP( xmax_ ) ); // Real
277  arc( CEREAL_NVP( min_val_ ) ); // Real
278  arc( CEREAL_NVP( max_val_ ) ); // Real
279  arc( CEREAL_NVP( root1_ ) ); // Real
280  arc( CEREAL_NVP( root2_ ) ); // Real
281  arc( CEREAL_NVP( degree_ ) ); // Size
282  arc( CEREAL_NVP( coefficients_ ) ); // utility::vector1<Real>
283 }
284 
285 /// @brief Automatically generated deserialization method
286 template< class Archive >
287 void
288 numeric::Polynomial_1d::load( Archive & arc ) {
289  arc( polynomial_name_ ); // std::string
290  arc( xmin_ ); // Real
291  arc( xmax_ ); // Real
292  arc( min_val_ ); // Real
293  arc( max_val_ ); // Real
294  arc( root1_ ); // Real
295  arc( root2_ ); // Real
296  arc( degree_ ); // Size
297  arc( coefficients_ ); // utility::vector1<Real>
298 }
299 
300 SAVE_AND_LOAD_SERIALIZABLE( numeric::Polynomial_1d );
301 CEREAL_REGISTER_TYPE( numeric::Polynomial_1d )
302 
303 CEREAL_REGISTER_DYNAMIC_INIT( numeric_polynomial )
304 #endif // SERIALIZATION
virtual ~Polynomial_1d()
Definition: polynomial.cc:76
platform::Size Size
Definition: types.hh:42
Real max_val() const
Definition: polynomial.cc:127
Polynomial_1d(std::string const &polynomial_name, Real const xmin, Real const xmax, Real const min_val, Real const max_val, Real const root1, Real const root2, Size degree, utility::vector1< Real > const &coefficients)
ctor
Definition: polynomial.cc:48
common derived classes for thrown exceptions
Size degree() const
Definition: polynomial.cc:145
std::string polynomial_name_
Definition: polynomial.hh:105
Real root2() const
Definition: polynomial.cc:139
member1 value
Definition: Tag.cc:296
Commons serlialization routines.
double eval(double const variable)
just evaluate the polynomial, w/o derivatives
Definition: polynomial.cc:214
T abs(T const &x)
std::abs( x ) == | x |
Definition: Fmath.hh:295
std::vector with 1-based indexing
Definition: vector1.fwd.hh:44
Real xmax() const
Definition: polynomial.cc:115
rosetta project type declarations. Should be kept updated with core/types.hh. This exists because num...
utility::vector1< Real > const & coefficients() const
Definition: polynomial.cc:151
double Real
Definition: types.hh:39
Polynomial evaluation class.
void operator()(double const variable, double &value, double &deriv) const
Evaluate the polynomial and its derivative.
Definition: polynomial.cc:189
utility::vector1< Real > coefficients_
Definition: polynomial.hh:113
Real xmin() const
Definition: polynomial.cc:109
vector1: std::vector with 1-based indexing
struct numeric::kinematic_closure::p poly
Real min_val() const
Definition: polynomial.cc:121
std::string name() const
Definition: polynomial.cc:103
std::ostream & operator<<(std::ostream &stream, BodyPosition< T > const &p)
stream << BodyPosition output operator
Serlialization routines for vector1s.
void check_invariants() const
Definition: polynomial.cc:79
def load
Definition: IO.py:5
Real root1() const
Definition: polynomial.cc:133
void show(std::ostream &out) const
Definition: polynomial.cc:237