BALL  1.4.1
function.h
Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_MATHS_FUNCTION_H
00006 #define BALL_MATHS_FUNCTION_H
00007 
00008 #ifndef BALL_COMMON_H
00009 # include <BALL/common.h>
00010 #endif
00011 
00012 namespace BALL
00013 {
00014 
00019   template <int constant_template>
00020   class ConstantFunction
00021   {
00022     public:
00023 
00024     BALL_CREATE(ConstantFunction)
00025 
00026     
00029 
00034     BALL_INLINE
00035     float operator () (float /* x */) const
00036     {
00037       return constant_template;
00038     }
00040 
00041   };
00042 
00043 
00048   template <typename DataType = float>
00049   class MutableConstant
00050   {
00051     public:
00052 
00053     BALL_CREATE(MutableConstant)
00054 
00055     
00058 
00061     MutableConstant();
00062 
00065     MutableConstant(const MutableConstant<DataType>& constant);
00066 
00069     MutableConstant(DataType constant);
00070 
00073     virtual ~MutableConstant();
00074 
00076 
00079 
00082     MutableConstant<DataType>& operator = (const MutableConstant<DataType>& constant);
00083 
00085 
00088 
00091     bool operator == (const MutableConstant<DataType>& constant) const;
00092 
00094 
00097 
00102     BALL_INLINE
00103     DataType operator () (const DataType& /* x */) const
00104     {
00105       return constant_;
00106     }
00107 
00109 
00112 
00115     void setConstant(DataType constant)
00116     {
00117       constant_ = constant;
00118     }
00119 
00123     const DataType& getConstant() const
00124     {
00125       return constant_;
00126     }
00127   
00129 
00130     protected:
00131 
00132     /*_ the constant
00133     */
00134     DataType constant_;
00135 
00136   };
00137 
00138 
00142   template <typename First, typename Second, typename DataType = float>
00143   class Addition
00144   {
00145     public:
00146 
00147     BALL_CREATE(Addition)
00148 
00149     
00152 
00155     Addition();
00156 
00159     Addition(const Addition<First, Second, DataType>& addition);
00160 
00163     virtual ~Addition();
00164 
00166 
00169 
00172     Addition<First, Second, DataType>& operator = (const Addition<First, Second, DataType>& addition);
00173 
00175 
00178 
00181     bool operator == (const Addition<First, Second, DataType>& addition) const;
00182 
00184 
00187 
00192     BALL_INLINE
00193     DataType operator () (const DataType& x) const 
00194     {
00195       return (first_(x) + second_(x));
00196     }
00197 
00199 
00202 
00205     void setFirst(const First& first)
00206     {
00207       first_ = first;
00208     }
00209 
00213     First& getFirst()
00214     {
00215       return first_;
00216     }
00217 
00220     const First& getFirst() const
00221     {
00222       return first_;
00223     }
00224 
00227     void setSecond(const Second& second)
00228     {
00229       second_ = second;
00230     }
00231 
00235     Second& getSecond()
00236     {
00237       return second_;
00238     }
00239   
00242     const Second& getSecond() const
00243     {
00244       return second_;
00245     }
00246   
00248 
00249     protected:
00250 
00251     /*_ the first argument of the addition
00252     */
00253     First first_;
00254 
00255     /*_ the second argument of the addition
00256     */
00257     Second second_;
00258 
00259   };
00260 
00261 
00264   template <typename First, typename Second, typename DataType = float>
00265   class Subtraction
00266   {
00267     public:
00268 
00269     BALL_CREATE(Subtraction)
00270 
00271     
00274 
00277     Subtraction();
00278 
00281     Subtraction(const Subtraction& subtraction);
00282 
00285     virtual ~Subtraction();
00286 
00288 
00291 
00294     Subtraction<First, Second, DataType>& operator = (const Subtraction<First, Second, DataType>& subtraction);
00295 
00297 
00300 
00303     bool operator == (const Subtraction<First, Second, DataType>& subtraction) const;
00304 
00306 
00309 
00314     BALL_INLINE
00315     DataType operator () (const DataType& x) const
00316     {
00317       return (first_(x) - second_(x));
00318     }
00319 
00321 
00324 
00327     void setFirst(const First& first)
00328     {
00329       first_ = first;
00330     }
00331 
00335     First& getFirst()
00336     {
00337       return first_;
00338     }
00339 
00342     void setSecond(const Second& second)
00343     {
00344       second_ = second;
00345     }
00346 
00350     Second& getSecond()
00351     {
00352       return second_;
00353     }
00354   
00356 
00357     protected:
00358 
00359     /*_ the first argument of the subtraction
00360     */
00361     First first_;
00362 
00363     /*_ the second argument of the subtraction
00364     */
00365     Second second_;
00366   };
00367 
00368 
00371   template <typename First, typename Second, typename DataType = float>
00372   class Product
00373   {
00374     public:
00375 
00376     BALL_CREATE(Product)
00377 
00378     
00381 
00384     Product();
00385 
00388     Product(const Product& product);
00389 
00392     virtual ~Product();
00393 
00395 
00398 
00401     Product<First, Second, DataType>& operator = (const Product<First, Second, DataType>& product);
00402 
00404 
00407 
00410     bool operator == (const Product<First, Second, DataType>& product) const;
00411 
00413 
00416 
00421     BALL_INLINE
00422     DataType operator () (const DataType& x) const 
00423     {
00424       return (first_(x) * second_(x));
00425     }
00426 
00428 
00431 
00434     void setFirst(const First& first)
00435     {
00436       first_ = first;
00437     }
00438 
00442     First& getFirst()
00443     {
00444       return first_;
00445     }
00446 
00450     const First& getFirst() const
00451     {
00452       return first_;
00453     }
00454 
00458     void setSecond(const Second& second)
00459     {
00460       second_ = second;
00461     }
00462 
00466     Second& getSecond()
00467     {
00468       return second_;
00469     }
00470   
00472 
00473     protected:
00474 
00475     /*_ the first argument of the product
00476     */
00477     First first_;
00478 
00479     /*_ the second argument of the product
00480     */
00481     Second second_;
00482 
00483   };
00484 
00485 
00488   template <typename First, typename Second, typename DataType = float>
00489   class Division
00490   {
00491     public:
00492 
00493     BALL_CREATE(Division)
00494 
00495     
00498 
00501     Division();
00502 
00505     Division(const Division& division);
00506 
00509     virtual ~Division();
00510 
00512 
00515 
00518     Division<First, Second, DataType>& operator = (const Division<First, Second, DataType>& division);
00519 
00521 
00524 
00527     bool operator == (const Division<First, Second, DataType>& division) const;
00528 
00530 
00533 
00539     BALL_INLINE
00540     DataType operator () (const DataType& x) const 
00541     {
00542       DataType val = second_(x);
00543       if (val != 0.0)
00544       {
00545         return (first_(x) / val);
00546       }
00547       else
00548       {
00549         throw Exception::DivisionByZero(__FILE__, __LINE__);
00550       }
00551     }
00552 
00554 
00557 
00560     void setFirst(const First& first)
00561     {
00562       first_ = first;
00563     }
00564 
00568     First& getFirst()
00569     {
00570       return first_;
00571     }
00572 
00575     void setSecond(const Second& second)
00576     {
00577       second_ = second;
00578     }
00579 
00583     Second& getSecond()
00584     {
00585       return second_;
00586     }
00587   
00589 
00590     protected:
00591 
00592     /*_ the first argument of the division
00593     */
00594     First first_;
00595 
00596     /*_ the second argument of the division
00597     */
00598     Second second_;
00599 
00600   };
00601 
00602 
00605   template <typename Function, typename DataType = float>
00606   class Reciprocal
00607   {
00608     public:
00609 
00610     BALL_CREATE(Reciprocal)
00611 
00612     
00615 
00618     Reciprocal();
00619 
00622     Reciprocal(const Reciprocal& reciprocal);
00623 
00626     virtual ~Reciprocal();
00627 
00629 
00632 
00635     Reciprocal<Function, DataType>& operator = (const Reciprocal<Function, DataType>& reciprocal);
00636 
00638 
00641 
00644     bool operator == (const Reciprocal<Function, DataType>& reciprocal) const;
00645 
00647 
00650 
00656     BALL_INLINE
00657     DataType operator () (const DataType& x) const 
00658     {
00659       DataType val = function_(x);
00660       if (val != 0)
00661       {
00662         return (1 / val);
00663       }
00664       else
00665       {
00666         throw Exception::DivisionByZero(__FILE__, __LINE__);
00667       }
00668     }
00669 
00671 
00674 
00677     void setFunction(const Function& function)
00678     {
00679       function_ = function;
00680     }
00681 
00685     const Function& getFunction() const
00686     {
00687       return function_;
00688     }
00689   
00691 
00692     protected:
00693 
00694     /*_ the argument of the reciprocal
00695     */
00696     Function function_;
00697 
00698   };
00699 
00700 
00703   template <typename Function, typename DataType = float>
00704   class SquareFunction
00705   {
00706     public:
00707 
00708     BALL_CREATE(SquareFunction)
00709 
00710     
00713 
00716     SquareFunction();
00717 
00720     SquareFunction(const SquareFunction& square);
00721 
00724     virtual ~SquareFunction();
00725 
00727 
00730 
00733     SquareFunction<Function, DataType>& operator = (const SquareFunction<Function, DataType>& square);
00734 
00736 
00739 
00742     bool operator == (const SquareFunction<Function, DataType>& square) const;
00743 
00745 
00748 
00753     BALL_INLINE
00754     DataType operator () (const DataType& x) const
00755     {
00756       DataType val = function_(x);
00757       return val * val;
00758     }
00759 
00761 
00764 
00767     void setFunction(const Function& function)
00768     {
00769       function_ = function;
00770     }
00771 
00775     const Function& getFunction() const
00776     {
00777       return function_;
00778     }
00779   
00781 
00782     protected:
00783 
00784     /*_ the argument of the square
00785     */
00786     Function function_;
00787 
00788   };
00789 
00790 
00793   template <typename Function, typename DataType = float>
00794   class CubicFunction
00795   {
00796     public:
00797 
00798     BALL_CREATE(CubicFunction)
00799 
00800     
00803 
00806     CubicFunction();
00807 
00810     CubicFunction(const CubicFunction& cubic);
00811 
00814     virtual ~CubicFunction();
00815 
00817 
00820 
00823     CubicFunction<Function, DataType>& operator = (const CubicFunction<Function, DataType>& cubic);
00824 
00826 
00829 
00832     bool operator == (const CubicFunction<Function, DataType>& cubic) const;
00833 
00835 
00838 
00843     BALL_INLINE
00844     DataType operator () (const DataType& x) const 
00845     {
00846       DataType val = function_(x);
00847       return val * val * val;
00848     }
00849 
00851 
00854 
00857     void setFunction(const Function& function)
00858     {
00859       function_ = function;
00860     }
00861 
00865     const Function& getFunction() const
00866     {
00867       return function_;
00868     }
00869   
00871 
00872     protected:
00873 
00874     /*_ the argument of the cubic
00875     */
00876     Function function_;
00877 
00878   };
00879 
00880 
00883   template <typename Function, typename DataType = float>
00884   class MutablePower
00885   {
00886     public:
00887 
00888     BALL_CREATE(MutablePower)
00889 
00890     
00893 
00896     MutablePower();
00897 
00900     MutablePower(const MutablePower& power);
00901 
00904     virtual ~MutablePower();
00905 
00907 
00910 
00913     MutablePower<Function, DataType>& operator = (const MutablePower<Function, DataType>& power);
00914 
00916 
00919 
00922     bool operator == (const MutablePower<Function, DataType>& power) const;
00923 
00925 
00928 
00933     BALL_INLINE
00934     DataType operator () (const DataType& x) const
00935     {
00936       return pow(function_(x), exponent_);
00937     }
00938 
00940 
00943 
00946     void setFunction(const Function& function)
00947     {
00948       function_ = function;
00949     }
00950 
00954     const Function& getFunction() const
00955     {
00956       return function_;
00957     }
00958   
00962     void setExponent(DataType exp);
00963 
00967     DataType getExponent() const;
00968 
00970 
00971     protected:
00972 
00973     /*_ the argument of the power
00974     */
00975     Function function_;
00976 
00977     /*_ the exponent
00978     */
00979     DataType exponent_;
00980   };
00981 
00982 
00983   template <typename DataType>
00984   BALL_INLINE
00985   MutableConstant<DataType>::MutableConstant()
00986     : constant_(0)
00987   {
00988   }
00989 
00990   template <typename DataType>
00991   BALL_INLINE
00992   MutableConstant<DataType>::MutableConstant
00993     (const MutableConstant<DataType>& constant)
00994     : constant_(constant.constant_)
00995   {
00996   }
00997 
00998   template <typename DataType>
00999   BALL_INLINE
01000   MutableConstant<DataType>::MutableConstant(DataType constant)
01001     : constant_(constant)
01002   {
01003   }
01004 
01005   template <typename DataType>
01006   BALL_INLINE
01007   MutableConstant<DataType>::~MutableConstant()
01008   {
01009   }
01010 
01011   template <typename DataType>
01012   BALL_INLINE
01013   MutableConstant<DataType>& MutableConstant<DataType>::operator = 
01014     (const MutableConstant<DataType>& constant)
01015   {
01016     constant_ = constant.constant_;
01017     return *this;
01018   }
01019 
01020   template <typename DataType>
01021   BALL_INLINE
01022   bool MutableConstant<DataType>::operator == 
01023     (const MutableConstant<DataType>& constant) const
01024   {
01025     return (constant_ == constant.constant_);
01026   }
01027 
01028 
01029   template <typename First, typename Second, typename DataType>
01030   BALL_INLINE
01031   Addition<First, Second, DataType>::Addition()
01032     : first_(),
01033       second_()
01034   {
01035   }
01036 
01037   template <typename First, typename Second, typename DataType>
01038   BALL_INLINE
01039   Addition<First, Second, DataType>::Addition(const Addition& addition)
01040     : first_(addition.first_),
01041       second_(addition.second_)
01042   {
01043   }
01044 
01045   template <typename First, typename Second, typename DataType>
01046   BALL_INLINE
01047   Addition<First, Second, DataType>::~Addition()
01048   {
01049   }
01050 
01051   template <typename First, typename Second, typename DataType>
01052   BALL_INLINE
01053   Addition<First, Second, DataType>& Addition<First, Second, DataType>::operator = (const Addition<First, Second, DataType>& addition)
01054   {
01055     first_ = addition.first_;
01056     second_ = addition.second_;
01057     return *this;
01058   }
01059 
01060   template <typename First, typename Second, typename DataType>
01061   BALL_INLINE
01062   bool Addition<First, Second, DataType>::operator == (const Addition<First, Second, DataType>& addition) const
01063   {
01064     return ((first_ == addition.first_) && (second_ == addition.second_));
01065   }
01066 
01067 
01068   template <typename First, typename Second, typename DataType>
01069   BALL_INLINE
01070   Subtraction<First, Second, DataType>::Subtraction()
01071     : first_(),
01072       second_()
01073   {
01074   }
01075 
01076   template <typename First, typename Second, typename DataType>
01077   BALL_INLINE
01078   Subtraction<First, Second, DataType>::Subtraction(const Subtraction& subtraction)
01079     : first_(subtraction.first_),
01080       second_(subtraction.second_)
01081   {
01082   }
01083 
01084   template <typename First, typename Second, typename DataType>
01085   BALL_INLINE
01086   Subtraction<First, Second, DataType>::~Subtraction()
01087   {
01088   }
01089 
01090   template <typename First, typename Second, typename DataType>
01091   BALL_INLINE
01092   Subtraction<First, Second, DataType>& Subtraction<First, Second, DataType>::operator = (const Subtraction<First, Second, DataType>& subtraction)
01093   {
01094     first_ = subtraction.first_;
01095     second_ = subtraction.second_;
01096     return *this;
01097   }
01098 
01099   template <typename First, typename Second, typename DataType>
01100   BALL_INLINE
01101   bool Subtraction<First, Second, DataType>::operator == (const Subtraction<First, Second, DataType>& subtraction) const
01102   {
01103     return ((first_ == subtraction.first_) && (second_ == subtraction.second_));
01104   }
01105 
01106 
01107   template <typename First, typename Second, typename DataType>
01108   BALL_INLINE
01109   Product<First, Second, DataType>::Product()
01110     : first_(),
01111       second_()
01112   {
01113   }
01114 
01115   template <typename First, typename Second, typename DataType>
01116   BALL_INLINE
01117   Product<First, Second, DataType>::Product(const Product& product)
01118     : first_(product.first_),
01119       second_(product.second_)
01120   {
01121   }
01122 
01123   template <typename First, typename Second, typename DataType>
01124   BALL_INLINE
01125   Product<First, Second, DataType>::~Product()
01126   {
01127   }
01128 
01129   template <typename First, typename Second, typename DataType>
01130   BALL_INLINE
01131   Product<First, Second, DataType>& Product<First, Second, DataType>::operator = (const Product<First, Second, DataType>& product)
01132   {
01133     first_ = product.first_;
01134     second_ = product.second_;
01135     return *this;
01136   }
01137 
01138   template <typename First, typename Second, typename DataType>
01139   BALL_INLINE
01140   bool Product<First, Second, DataType>::operator == (const Product<First, Second, DataType>& product) const
01141   {
01142     return ((first_ == product.first_) && (second_ == product.second_));
01143   }
01144 
01145 
01146   template <typename First, typename Second, typename DataType>
01147   BALL_INLINE
01148   Division<First, Second, DataType>::Division()
01149     : first_(),
01150       second_()
01151   {
01152   }
01153 
01154   template <typename First, typename Second, typename DataType>
01155   BALL_INLINE
01156   Division<First, Second, DataType>::Division(const Division& division)
01157     : first_(division.first_),
01158       second_(division.second_)
01159   {
01160   }
01161 
01162   template <typename First, typename Second, typename DataType>
01163   BALL_INLINE
01164   Division<First, Second, DataType>::~Division()
01165   {
01166   }
01167 
01168   template <typename First, typename Second, typename DataType>
01169   BALL_INLINE
01170   Division<First, Second, DataType>& Division<First, Second, DataType>::operator = (const Division<First, Second, DataType>& division)
01171   {
01172     first_ = division.first_;
01173     second_ = division.second_;
01174     return *this;
01175   }
01176 
01177   template <typename First, typename Second, typename DataType>
01178   BALL_INLINE
01179   bool Division<First, Second, DataType>::operator == (const Division<First, Second, DataType>& division) const
01180   {
01181     return ((first_ == division.first_) && (second_ == division.second_));
01182   }
01183 
01184 
01185   template <typename Function, typename DataType>
01186   BALL_INLINE
01187   SquareFunction<Function, DataType>::SquareFunction()
01188     : function_()
01189   {
01190   }
01191 
01192   template <typename Function, typename DataType>
01193   BALL_INLINE
01194   SquareFunction<Function, DataType>::SquareFunction(const SquareFunction& square)
01195     : function_(square.function_)
01196   {
01197   }
01198 
01199   template <typename Function, typename DataType>
01200   BALL_INLINE
01201   SquareFunction<Function, DataType>::~SquareFunction()
01202   {
01203   }
01204 
01205   template <typename Function, typename DataType>
01206   BALL_INLINE
01207   SquareFunction<Function, DataType>& SquareFunction<Function, DataType>::operator = (const SquareFunction<Function, DataType>& square)
01208   {
01209     function_ = square.function_;
01210     return *this;
01211   }
01212 
01213   template <typename Function, typename DataType>
01214   BALL_INLINE
01215   bool SquareFunction<Function, DataType>::operator == (const SquareFunction& square) const
01216   {
01217     return (function_ == square.function_);
01218   }
01219 
01220 
01221   template <typename Function, typename DataType>
01222   BALL_INLINE
01223   CubicFunction<Function, DataType>::CubicFunction()
01224     : function_(0)
01225   {
01226   }
01227 
01228   template <typename Function, typename DataType>
01229   BALL_INLINE
01230   CubicFunction<Function, DataType>::CubicFunction(const CubicFunction& cubic)
01231     : function_(cubic.function_)
01232   {
01233   }
01234 
01235   template <typename Function, typename DataType>
01236   BALL_INLINE
01237   CubicFunction<Function, DataType>::~CubicFunction()
01238   {
01239   }
01240 
01241   template <typename Function, typename DataType>
01242   BALL_INLINE
01243   CubicFunction<Function, DataType>& CubicFunction<Function, DataType>::operator = (const CubicFunction<Function, DataType>& cubic)
01244   {
01245     function_ = cubic.function_;
01246     return *this;
01247   }
01248 
01249   template <typename Function, typename DataType>
01250   BALL_INLINE
01251   bool CubicFunction<Function, DataType>::operator == (const CubicFunction& cubic) const
01252   {
01253     return (function_ == cubic.function_);
01254   }
01255 
01256 
01257   template <typename Function, typename DataType>
01258   BALL_INLINE
01259   Reciprocal<Function, DataType>::Reciprocal()
01260     : function_()
01261   {
01262   }
01263 
01264   template <typename Function, typename DataType>
01265   BALL_INLINE
01266   Reciprocal<Function, DataType>::Reciprocal(const Reciprocal& reciprocal)
01267     : function_(reciprocal.function_)
01268   {
01269   }
01270 
01271   template <typename Function, typename DataType>
01272   BALL_INLINE
01273   Reciprocal<Function, DataType>::~Reciprocal()
01274   {
01275   }
01276 
01277   template <typename Function, typename DataType>
01278   BALL_INLINE
01279   Reciprocal<Function, DataType>& Reciprocal<Function, DataType>::operator = (const Reciprocal<Function, DataType>& reciprocal)
01280   {
01281     function_ = reciprocal.function_;
01282     return *this;
01283   }
01284 
01285   template <typename Function, typename DataType>
01286   BALL_INLINE
01287   bool Reciprocal<Function, DataType>::operator == (const Reciprocal& reciprocal) const 
01288   {
01289     return (function_ == reciprocal.function_);
01290   }
01291 
01292 
01293   template <typename Function, typename DataType>
01294   BALL_INLINE
01295   MutablePower<Function, DataType>::MutablePower()
01296     : function_(0),
01297       exponent_(0)
01298   {
01299   }
01300 
01301   template <typename Function, typename DataType>
01302   BALL_INLINE
01303   MutablePower<Function, DataType>::MutablePower(const MutablePower& power)
01304     : function_(power.function_),
01305       exponent_(power.exponent_)
01306   {
01307   }
01308 
01309   template <typename Function, typename DataType>
01310   BALL_INLINE
01311   MutablePower<Function, DataType>::~MutablePower()
01312   {
01313   }
01314 
01315   template <typename Function, typename DataType>
01316   BALL_INLINE
01317   MutablePower<Function, DataType>& MutablePower<Function, DataType>::operator = (const MutablePower& power)
01318   {
01319     function_ = power.function_;
01320     exponent_ = power.exponent_;
01321     return *this;
01322   }
01323 
01324   template <typename Function, typename DataType>
01325   BALL_INLINE
01326   bool MutablePower<Function, DataType>::operator == (const MutablePower& power) const 
01327   {
01328     return ((exponent_ == power.exponent_)
01329       && (function_ == power.function_));
01330   }
01331 
01332   template <typename Function, typename DataType>
01333   BALL_INLINE
01334   DataType MutablePower<Function, DataType>::getExponent() const
01335   {
01336     return exponent_;
01337   }
01338 
01339   template <typename Function, typename DataType>
01340   BALL_INLINE
01341   void MutablePower<Function, DataType>::setExponent(DataType exp)
01342   {
01343     exponent_ = exp;
01344   }
01345 } // namespace BALL
01346 
01347 #endif // BALL_MATHS_FUNCTION_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines