Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Polynom.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009, 2005 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __POLYNOM_HH__
27 #define __POLYNOM_HH__
28 
29 #include "Base/Common/BIASpragmaStart.hh"
30 #include <vector>
31 
32 #include "Base/Debug/Debug.hh"
33 #include "Base/Debug/Error.hh"
34 
35 
36 namespace BIAS{
37  /**@class Polynom
38  @ingroup g_math
39  @brief class for Polynoms of arbitary order
40 
41  class Polynom represents a Polynom of arbitary order, where
42  ith element is the coefficient of the ith exponent
43 
44  @author Marcel Lilienthal,June 2005; Christian Beder, August 2007
45  */
46  class Polynom : public std::vector<double>,public Debug
47  {
48  public:
49  Polynom(double d=0.0):std::vector<double>(1,0.0){
50  SetOrder(0);
51  (*this)[0] = d;
52  };
53 
54  Polynom(const Polynom& toCopy){
55  operator=(toCopy);
56  };
57 
58  // ~Polynom(){
59  // std::cout << this << std::endl;
60  //if((*this) == NULL)
61  // std::cout << "what" << std::endl;
62  // }
63 
64  /**
65  sets the order of polynom to i
66  new generated coefficients are 0
67 
68  @author Marcel Lilienthal
69  */
70  inline void SetOrder(unsigned int i){
71  order_=i;
72  this->resize(i+1,0.0);
73  };
74 
75 
76  /**
77  removes zero leading coefficients
78  */
79  void Prune() {
80  int deg=GetOrder();
81  while ((*this)[deg]==0) {
82  deg--;
83  if (deg==-1) {
84  SetOrder(0);
85  return;
86  }
87  }
88  std::vector<double> tmp(deg+1);
89  for (int i=0; i<=deg; i++) {
90  tmp[i] = (*this)[i];
91  }
92  SetOrder(deg);
93  for (int i=0; i<=deg; i++) {
94  (*this)[i] = tmp[i];
95  }
96  }
97 
98  double Evaluate(double const x) const {
99  double res=0;
100  double xx=1;
101  for (unsigned int i=0; i<=order_; i++) {
102  res += (*this)[i]*xx;
103  xx *= x;
104  }
105  return res;
106  }
107 
108  inline unsigned int GetOrder() const { return order_; };
109 
110  //operators
111  inline Polynom& operator=(const Polynom& a);
112  inline Polynom& operator=(const double& d);
113  inline Polynom& operator+=(const Polynom& a);
114  inline Polynom& operator+=(const double& d);
115  inline Polynom& operator-=(const Polynom& a);
116  inline Polynom& operator-=(const double& d);
117  inline Polynom& operator*=(const Polynom& a);
118  inline Polynom& operator*=(const double& d);
119  inline Polynom& operator/=(const Polynom& a);
120  inline Polynom& operator/=(const double& d);
121  inline bool operator==(const Polynom& a) const;
122  inline bool operator!=(const Polynom& a) const;
123 
124  protected:
125  unsigned int order_;
126  };//class
127 
128  inline Polynom& Polynom::operator=(const Polynom& a){
129  if (this != &a){
130  SetOrder(a.order_);
131  for(unsigned int i=0;i<=order_; i++){
132  (*this)[i]=a[i];
133  }
134  }
135  return *this;
136  }
137 
138  inline Polynom& Polynom::operator=(const double& d){
139  SetOrder(0);
140  (*this)[0]=d;
141  return *this;
142  }
143 
145  if ( a.order_>order_ )
146  SetOrder(a.order_);
147  for(unsigned int i=0; i<=a.order_; i++){
148  (*this)[i]+=a[i];
149  }
150  return *this;
151  }
152 
153  inline Polynom& Polynom::operator+=(const double& d){
154  (*this)[0]+=d;
155  return *this;
156  }
157 
159  if ( a.order_>order_ )
160  SetOrder(a.order_);
161  for(unsigned int i=0;i<=a.order_;i++){
162  (*this)[i]-=a[i];
163  }
164  return *this;
165  }
166 
167  inline Polynom& Polynom::operator-=(const double& d){
168  (*this)[0]-=d;
169  return *this;
170  }
171 
173  std::vector<double> c(order_ + a.order_ + 1, 0.0);
174  for(unsigned int i=0; i<=order_; i++) {
175  for(unsigned int j=0; j<=a.order_; j++) {
176  c[i+j] += (*this)[i] * a[j];
177  }
178  }
179  SetOrder((unsigned int)c.size()-1);
180  for(unsigned int i=0;i<c.size(); i++){
181  (*this)[i]=c[i];
182  }
183 
184  return *this;
185  }
186 
187  inline Polynom& Polynom::operator*=(const double& d){
188  for(unsigned int i=0;i<=order_;i++){
189  (*this)[i]*=d;
190  }
191  return *this;
192  }
193 
195  BIASERR("Polynom division not yet implemented!");
196  BIASABORT;
197  return *this;
198  }
199 
200  inline Polynom& Polynom::operator/=(const double& d){
201  for(unsigned int i=0;i<=order_;i++){
202  (*this)[i]/=d;
203  }
204  return *this;
205  }
206 
207  inline bool Polynom::operator==(const Polynom& a) const{
208  bool res = (this == &a);
209  if(!res){
210  res = order_ == a.order_;
211  if(res){
212  for(unsigned int i=0; i<=order_; i++){
213  res = (*this)[i]==a[i];
214  if(!res)
215  return res;
216  }
217  }
218  }
219  return res;
220  }
221 
222  inline bool Polynom::operator!=(const Polynom& a) const{
223  return !operator==(a);
224  }
225 
226  inline Polynom operator+(const Polynom& a, const Polynom& b){
227  Polynom c=a;
228  c+=b;
229  return c;
230  }
231 
232  inline Polynom operator-(const Polynom& a, const Polynom& b){
233  Polynom c=a;
234  c-=b;
235  return c;
236  }
237 
238  inline Polynom operator*(const Polynom& a, const Polynom& b){
239  Polynom c=a;
240  c*=b;
241  return c;
242  }
243 
244  inline Polynom operator*(const double& d, const Polynom& a){
245  Polynom c=a;
246  c*=d;
247  return c;
248  }
249 
250  inline Polynom operator*(const Polynom& a, const double& d){
251  Polynom c=a;
252  c*=d;
253  return c;
254  }
255 
256  inline Polynom operator/(const Polynom& a, const double& d){
257  Polynom c=a;
258  c/=d;
259  return c;
260  }
261 
262  inline Polynom operator/(const Polynom& a, const Polynom& b){
263  Polynom c=a;
264  c/=b;
265  return c;
266  }
267 
268  inline std::istream& operator>>(std::istream& s, BIAS::Polynom &p){
269  BIASERR(">> operator not yet implemented for Polynom");
270  BIASABORT;
271  return s;
272  }
273 
274  inline std::ostream& operator<<(std::ostream& s, const BIAS::Polynom &p){
275  int order = (int)p.GetOrder();
276  for (int i=order; i>=0; i--) {
277  if (i==order)
278  s << p[i];
279  else {
280  if (p[i]<0)
281  s << " - " << -p[i];
282  else
283  s << " + " << p[i];
284  }
285  if (i>1)
286  s << "x^"<< i;
287  else if (i==1)
288  s << "x";
289  }
290  return s;
291  }
292 
293  inline bool operator<(const BIAS::Polynom& p1, const BIAS::Polynom &p2){
294  unsigned int o1=p1.GetOrder();
295  unsigned int o2=p2.GetOrder();
296  if (o1==o2)
297  return p1[o1]<p2[o1];
298  else
299  return o1<o2;
300  }
301 
302  inline bool operator>(const BIAS::Polynom& p1, const BIAS::Polynom &p2){
303  unsigned int o1=p1.GetOrder();
304  unsigned int o2=p2.GetOrder();
305  if (o1==o2)
306  return p1[o1]>p2[o1];
307  else
308  return o1>o2;
309  }
310 
311 }//namespace
312 
313 #include <Base/Common/BIASpragmaEnd.hh>
314 
315 #endif //__POLYNOM_HH__
Polynom(double d=0.0)
Definition: Polynom.hh:49
void Prune()
removes zero leading coefficients
Definition: Polynom.hh:79
Polynom & operator-=(const Polynom &a)
Definition: Polynom.hh:158
Polynom & operator+=(const Polynom &a)
Definition: Polynom.hh:144
class for Polynoms of arbitary order
Definition: Polynom.hh:46
Polynom(const Polynom &toCopy)
Definition: Polynom.hh:54
double Evaluate(double const x) const
Definition: Polynom.hh:98
Polynom & operator=(const Polynom &a)
Definition: Polynom.hh:128
Polynom & operator*=(const Polynom &a)
Definition: Polynom.hh:172
unsigned int order_
Definition: Polynom.hh:125
DualQuaternion< T > operator/(const DualQuaternion< T > &l, const T &scalar)
DualQuaternion< T > operator-(const DualQuaternion< T > &l, const DualQuaternion< T > &r)
Polynom & operator/=(const Polynom &a)
Definition: Polynom.hh:194
DualQuaternion< T > operator+(const DualQuaternion< T > &l, const DualQuaternion< T > &r)
bool operator>(const BIAS::Polynom &p1, const BIAS::Polynom &p2)
Definition: Polynom.hh:302
bool operator<(const BIAS::Polynom &p1, const BIAS::Polynom &p2)
Definition: Polynom.hh:293
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
DualQuaternion< T > operator*(const DualQuaternion< T > &l, const T &scalar)
unsigned int GetOrder() const
Definition: Polynom.hh:108
void SetOrder(unsigned int i)
sets the order of polynom to i new generated coefficients are 0
Definition: Polynom.hh:70
bool operator==(const Polynom &a) const
Definition: Polynom.hh:207
bool operator!=(const Polynom &a) const
Definition: Polynom.hh:222
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157