library of assembled shared sources

http://lass.cocamware.com

polynomial.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_POLYNOMIAL_INL
00044 #define LASS_GUARDIAN_OF_INCLUSION_NUM_POLYNOMIAL_INL
00045 
00046 #include "num_common.h"
00047 #include "polynomial.h"
00048 
00049 namespace lass
00050 {
00051 namespace num
00052 {
00053 
00054 // --- public --------------------------------------------------------------------------------------
00055 
00056 template <typename T>
00057 Polynomial<T>::Polynomial()
00058 {
00059 }
00060 
00061 
00062 template <typename T>
00063 Polynomial<T>::Polynomial(TParam iScalar):
00064     a_(1, iScalar)
00065 {
00066 }
00067 
00068 
00069 
00070 template <typename T>
00071 Polynomial<T>::Polynomial(const TCoefficients& iCoefficients):
00072     a_(iCoefficients)
00073 {
00074 }
00075 
00076 
00077 template <typename T>
00078 template <typename InputIterator> 
00079 Polynomial<T>::Polynomial(InputIterator iBegin, InputIterator iEnd):
00080     a_(iBegin, iEnd)
00081 {
00082 }
00083 
00084 
00085 
00086 template <typename T> inline
00087 const typename Polynomial<T>::TCoefficients& 
00088 Polynomial<T>::coefficients() const
00089 {
00090     return a_;
00091 }
00092 
00093 
00094 
00095 template <typename T> inline
00096 const typename Polynomial<T>::TValue
00097 Polynomial<T>::operator[](size_t iIndex) const
00098 {
00099     LASS_ASSERT(iIndex < a_.size());
00100     return a_[iIndex];
00101 }
00102 
00103 
00104 
00105 template <typename T> inline
00106 const typename Polynomial<T>::TValue 
00107 Polynomial<T>::at(size_t iIndex) const
00108 {
00109     return iIndex < a_.size() ? a_[iIndex] : TNumTraits::zero;
00110 }
00111 
00112 
00113 
00114 template <typename T>
00115 const typename Polynomial<T>::TValue 
00116 Polynomial<T>::operator()(TParam iX) const
00117 {
00118     TValue result = TNumTraits::zero;
00119     TValue x = TNumTraits::one;
00120     const size_t n = a_.size();
00121     for (size_t i = 0; i < n; ++i)
00122     {
00123         result += a_[i] * x;
00124         x *= iX;
00125     }
00126     return result;
00127 }
00128 
00129 
00130 
00131 template <typename T>
00132 const Polynomial<T>& Polynomial<T>::operator+() const
00133 {
00134     return *this;
00135 }
00136 
00137 
00138 
00139 template <typename T>
00140 const Polynomial<T> Polynomial<T>::operator-() const
00141 {
00142     const size_t n = a_.size();
00143     TCoefficients result(n);
00144     for (size_t i = 0; i < n; ++i)
00145     {
00146         result[i] = -a_[i];
00147     }
00148     return Polynomial<T>(result);
00149 }
00150 
00151 
00152 
00153 template <typename T>
00154 Polynomial<T>& Polynomial<T>::operator+=(const Polynomial<T>& iOther)
00155 {
00156     const size_t n = iOther.a_.size();
00157     if (a_.size() < n)
00158     {
00159         a_.resize(n, TNumTraits::zero);
00160     }
00161     for (size_t i = 0; i < n; ++i)
00162     {
00163         a_[i] += iOther.a_[i];
00164     }
00165     return *this;
00166 }
00167 
00168 
00169 
00170 template <typename T>
00171 Polynomial<T>& Polynomial<T>::operator-=(const Polynomial<T>& iOther)
00172 {
00173     const size_t n = iOther.a_.size();
00174     if (a_.size() < n)
00175     {
00176         a_.resize(n, TNumTraits::zero);
00177     }
00178     for (size_t i = 0; i < n; ++i)
00179     {
00180         a_[i] -= iOther.a_[i];
00181     }
00182     return *this;
00183 }
00184 
00185 
00186 
00187 template <typename T>
00188 Polynomial<T>& Polynomial<T>::operator*=(const Polynomial<T>& iOther)
00189 {
00190     const size_t m = a_.size();
00191     const size_t n = iOther.a_.size();
00192     TCoefficients result(m + n - 1, TNumTraits::zero);
00193     for (size_t i = 0; i < m; ++i)
00194     {
00195         const TValue a = a_[i];
00196         for (size_t k = 0; k < n; ++k)
00197         {
00198             result[i + k] += a * iOther.a_[k];
00199         }
00200     }
00201     a_.swap(result);
00202     return *this;
00203 }
00204 
00205 
00206 
00207 template <typename T>
00208 Polynomial<T>& Polynomial<T>::operator+=(TParam iScalar)
00209 {
00210     if (a_.empty())
00211     {
00212         a_.resize(1, iScalar);
00213     }
00214     else
00215     {
00216         a_[0] += iScalar;
00217     }
00218     return *this;
00219 }
00220 
00221 
00222 
00223 template <typename T>
00224 Polynomial<T>& Polynomial<T>::operator-=(TParam iScalar)
00225 {
00226     if (a_.empty())
00227     {
00228         a_.resize(1, -iScalar);
00229     }
00230     else
00231     {
00232         a_[0] -= iScalar;
00233     }
00234     return *this;
00235 }
00236 
00237 
00238 
00239 template <typename T>
00240 Polynomial<T>& Polynomial<T>::operator*=(TParam iScalar)
00241 {
00242     const size_t n = a_.size();
00243     for (size_t i = 0; i < n; ++i)
00244     {
00245         a_[i] *= iScalar;
00246     }
00247     return *this;
00248 }
00249 
00250 
00251 
00252 template <typename T>
00253 Polynomial<T>& Polynomial<T>::operator/=(TParam iScalar)
00254 {
00255     const size_t n = a_.size();
00256     for (size_t i = 0; i < n; ++i)
00257     {
00258         a_[i] /= iScalar;
00259     }
00260     return *this;
00261 }
00262 
00263 
00264 
00265 template <typename T>
00266 Polynomial<T> Polynomial<T>::derivative() const
00267 {
00268     const size_t n = a_.size();
00269     if (n < 2)
00270     {
00271         return Polynomial<T>();
00272     }
00273     TCoefficients result(n - 1);
00274     for (size_t i = 1; i < n; ++i)
00275     {
00276         result[i - 1] = static_cast<T>(i) * a_[i];
00277     }
00278     return Polynomial<T>(result);
00279 }
00280 
00281 
00282 
00283 template <typename T>
00284 Polynomial<T> Polynomial<T>::integral() const
00285 {
00286     const size_t n = a_.size();
00287     if (n == 0)
00288     {
00289         return Polynomial<T>();
00290     }
00291     TCoefficients result(n + 1);
00292     result[0] = TNumTraits::zero;
00293     for (size_t i = 0; i < n; ++i)
00294     {
00295         result[i + 1] = a_[i] / static_cast<T>(i);
00296     }
00297     return Polynomial<T>(result);
00298 }
00299 
00300 
00301 
00302 template <typename T>
00303 Polynomial<T> Polynomial<T>::pow(unsigned iPower) const
00304 {
00305     Polynomial<T> result(1);
00306     for (unsigned i = 0; i < iPower; ++i)
00307     {
00308         result *= *this;
00309     }
00310     return result;
00311 }
00312 
00313 
00314 
00315 /** return size of coefficients.
00316  */
00317 template <typename T>
00318 const typename Polynomial<T>::size_type Polynomial<T>::size() const
00319 {
00320     return a_.size();
00321 }
00322 
00323 
00324 
00325 /** return iterator to first (lowest) coefficient
00326  */
00327 template <typename T>
00328 const typename Polynomial<T>::const_iterator Polynomial<T>::begin() const
00329 {
00330     return a_.begin();
00331 }
00332 
00333 
00334 
00335 /** return iterator to last (highest) coefficient
00336  */
00337 template <typename T>
00338 const typename Polynomial<T>::const_iterator Polynomial<T>::end() const
00339 {
00340     return a_.end();
00341 }
00342 
00343 
00344 
00345 /** return constant polynomial 1
00346  */
00347 template <typename T>
00348 Polynomial<T> Polynomial<T>::one()
00349 {
00350     static Polynomial<T> result(1);
00351     return result;
00352 }
00353 
00354 
00355 
00356 /** return linear polynomial x
00357  */
00358 template <typename T>
00359 Polynomial<T> Polynomial<T>::x()
00360 {
00361     static TValue coefficients[2] = { 0, 1 };
00362     static Polynomial<T> result(coefficients, coefficients + 2);
00363     return result;
00364 }
00365 
00366 
00367 
00368 // --- free ----------------------------------------------------------------------------------------
00369 
00370 template <typename T> 
00371 bool operator==(const Polynomial<T>& iA, const Polynomial<T>& iB)
00372 {
00373     typedef typename Polynomial<T>::TCoefficients TCoefficients;
00374     const TCoefficients& a = iA.coefficients();
00375     const TCoefficients& b = iA.coefficients();
00376     const size_t m = a.size();
00377     const size_t n = b.size();
00378     if (m != n)
00379     {
00380         return false;
00381     }
00382     for (size_t i = 0; i < n; ++i)
00383     {
00384         if (a[i] != b[i])
00385         {
00386             return false;
00387         }
00388     }
00389     return true;
00390 }
00391 
00392 
00393 
00394 template <typename T> inline 
00395 bool operator!=(const Polynomial<T>& iA, const Polynomial<T>& iB)
00396 {
00397     return !(iA == iB);
00398 }
00399 
00400 
00401 
00402 template <typename T> inline
00403 Polynomial<T> operator+(const Polynomial<T>& iA, const Polynomial<T>& iB)
00404 {
00405     Polynomial<T> result(iA);
00406     result += iB;
00407     return result;
00408 }
00409 
00410 
00411 
00412 template <typename T> inline
00413 Polynomial<T> operator-(const Polynomial<T>& iA, const Polynomial<T>& iB)
00414 {
00415     Polynomial<T> result(iA);
00416     result -= iB;
00417     return result;
00418 }
00419 
00420 
00421 
00422 template <typename T> inline
00423 Polynomial<T> operator*(const Polynomial<T>& iA, const Polynomial<T>& iB)
00424 {
00425     Polynomial<T> result(iA);
00426     result *= iB;
00427     return result;
00428 }
00429 
00430 
00431 
00432 template <typename T> inline
00433 Polynomial<T> operator+(const T& iA, const Polynomial<T>& iB)
00434 {
00435     Polynomial<T> result(iB);
00436     result += iA;
00437     return result;
00438 }
00439 
00440 
00441 
00442 template <typename T> inline
00443 Polynomial<T> operator-(const T& iA, const Polynomial<T>& iB)
00444 {
00445     Polynomial<T> result(-iB);
00446     result += iA;
00447     return result;
00448 }
00449 
00450 
00451 
00452 template <typename T> inline
00453 Polynomial<T> operator*(const T& iA, const Polynomial<T>& iB)
00454 {
00455     Polynomial<T> result(iB);
00456     result *= iA;
00457     return result;
00458 }
00459 
00460 
00461 
00462 template <typename T> inline
00463 Polynomial<T> operator+(const Polynomial<T>& iA, const T& iB)
00464 {
00465     Polynomial<T> result(iA);
00466     result += iB;
00467     return result;
00468 }
00469 
00470 
00471 
00472 template <typename T> inline
00473 Polynomial<T> operator-(const Polynomial<T>& iA, const T& iB)
00474 {
00475     Polynomial<T> result(iA);
00476     result -= iB;
00477     return result;
00478 }
00479 
00480 
00481 
00482 template <typename T> inline
00483 Polynomial<T> operator*(const Polynomial<T>& iA, const T& iB)
00484 {
00485     Polynomial<T> result(iA);
00486     result *= iB;
00487     return result;
00488 }
00489 
00490 
00491 
00492 template <typename T> inline
00493 Polynomial<T> operator/(const Polynomial<T>& iA, const T& iB)
00494 {
00495     Polynomial<T> result(iA);
00496     result /= iB;
00497     return result;
00498 }
00499 
00500 
00501 
00502 template <typename T, typename Char, typename Traits>
00503 std::basic_ostream<Char, Traits>&
00504 operator<<(std::basic_ostream<Char, Traits>& iS, const Polynomial<T>& iA)
00505 {
00506     typedef typename Polynomial<T>::TCoefficients TCoefficients;
00507     const TCoefficients& a = iA.coefficients();
00508     const size_t n = a.size();
00509     if (n == 0)
00510     {
00511         iS << T();
00512         return iS;
00513     }
00514     iS << a[0];
00515     for (size_t i = 1; i < n; ++i)
00516     {
00517         iS << " + " << a[i] << "x^" << static_cast<unsigned long>(i);
00518     }
00519     return iS;
00520 }
00521 
00522 
00523 
00524 }
00525 
00526 }
00527 
00528 #endif
00529 
00530 // EOF

Generated on Mon Nov 10 14:21:04 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo