Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Constant.h

Go to the documentation of this file.
00001 
00005 /* Copyright (C) 2002 - 2005, Bernd Speiser */
00006 /* This file is part of Quantity.
00007 
00008 Quantity is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 Quantity is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017   
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00021 02111-1307, USA.
00022 */
00023 
00024 #ifndef _Constant_h
00025 #define _Constant_h
00026 
00027 #include "Quantity/Quantity.h"
00028 #include "Quantity/QuantityError.h"
00029 #include "Quantity/Variable.h"
00030 
00031 namespace quantity {
00032 
00034 //
00035 //  the Constant classes
00036 //
00038 
00040 
00045 template<class PQ, class SU = typename PQ::DefaultUnit::Unit>
00046   class Constant;
00047 
00049 
00052 template<long RL_N, long RL_D, long RM_N, long RM_D,
00053   long RT_N, long RT_D, long RE_N, long RE_D, long RTE_N, long RTE_D,
00054     long RA_N, long RA_D, long RLU_N, long RLU_D,
00055                        class BT, class UL, class DU, class SU, class ST>
00056   class Constant<Quantity<dimension::Dimension<
00057     BSUtilities::Rational<RL_N, RL_D>,
00058       BSUtilities::Rational<RM_N, RM_D>,
00059         BSUtilities::Rational<RT_N, RT_D>,
00060           BSUtilities::Rational<RE_N, RE_D>,
00061             BSUtilities::Rational<RTE_N, RTE_D>,
00062               BSUtilities::Rational<RA_N, RA_D>,
00063                 BSUtilities::Rational<RLU_N, RLU_D> >, BT, UL, DU, ST>,
00064                   SU> : public
00065       Quantity<dimension::Dimension<BSUtilities::Rational<RL_N, RL_D>,
00066         BSUtilities::Rational<RM_N, RM_D>,
00067           BSUtilities::Rational<RT_N, RT_D>,
00068             BSUtilities::Rational<RE_N, RE_D>,
00069               BSUtilities::Rational<RTE_N, RTE_D>,
00070                 BSUtilities::Rational<RA_N, RA_D>,
00071                   BSUtilities::Rational<RLU_N, RLU_D> >, BT, UL, DU, ST>
00072 
00073     {
00074       private:
00076 
00078         typedef dimension::Dimension<BSUtilities::Rational<RL_N, RL_D>,
00079             BSUtilities::Rational<RM_N, RM_D>,
00080               BSUtilities::Rational<RT_N, RT_D>,
00081                 BSUtilities::Rational<RE_N, RE_D>,
00082                   BSUtilities::Rational<RTE_N, RTE_D>,
00083                     BSUtilities::Rational<RA_N, RA_D>,
00084                               BSUtilities::Rational<RLU_N, RLU_D> > DIM;
00085 
00086       public:
00088 
00092         typedef typename 
00093                         unit::CheckUnit<unit::Unit<BT>, SU>::Check Unit;
00094 
00095 
00096       private:
00098 
00100         typedef Quantity<DIM, BT, UL, DU, ST> PQ;
00101 
00103 
00107         ST constant_value;
00108 
00109       public:
00111 
00113         Constant (void) : constant_value (ST(0)) {}
00114 
00116 
00118         Constant (const ST value) : constant_value (value) {}
00119 
00121 
00124         Constant (const ST value, const SU &)
00125                                              : constant_value (value) {}
00126 
00128 
00134         template<class NU>
00135           Constant (const ST value, const NU &) 
00136             : PQ (), constant_value (Reverse<SU, ST>::VAL
00137               (Standardize<typename CheckAgainstAllUnits<NU, UL>::RET, 
00138                                                    ST>::VAL (value))) {}
00139 
00140 
00142 
00149         Constant (const ST value, const std::string &symbol) 
00150           : PQ ()
00151             {::unit::Unit<BT> *unitp = PQ::findBySymbol (symbol);
00152 
00153              constant_value = Reverse<SU, ST>::VAL 
00154                          (dynamic_standardize<UL>::VAL (value, *unitp));
00155 
00156              delete (unitp);
00157             }
00158 
00160 
00167         Constant (const ST new_value, const char *symbol) 
00168           : PQ ()
00169             {::unit::Unit<BT> *unitp
00170                               = PQ::findBySymbol (std::string (symbol));
00171 
00172              constant_value = Reverse<SU, ST>::VAL 
00173                      (dynamic_standardize<UL>::VAL (new_value, *unitp));
00174 
00175              delete (unitp);
00176             }
00177 
00179 
00182         template<template<class, class> class Q1>
00183           Constant (const Q1<PQ, SU> &quantity)
00184             : PQ (), constant_value (quantity.value ())
00185               {namestring = quantity.name ();
00186                symbolstring = quantity.symbol ();
00187               }
00188 
00190 
00201         template<template<class, class> class Q1, class SU1>
00202           Constant (const Q1<PQ, SU1> &quantity)
00203             : PQ (), constant_value (Reverse<SU, ST>::VAL
00204                         (Standardize<SU1, ST>::VAL (quantity.value ())))
00205               {namestring = quantity.name ();
00206                symbolstring = quantity.symbol ();
00207               }
00208 
00210 
00217         template<template<class, class> class Q1, class BT1, 
00218                                        class DIM1, class DU1, class SU1>
00219           Constant (const Q1<Quantity<DIM1, BT1,
00220             Loki::Typelist<unit::NonPrefixable<BT1, unit::GenericUnit>,
00221                               Loki::NullType>, DU1, ST>, SU1> &quantity)
00222             : constant_value (Reverse<Unit, ST>::VAL
00223               (Standardize<typename
00224                  CheckSecondDimension<Variable<PQ, SU>,
00225                    Q1<Quantity<DIM1, BT1,
00226                      Loki::Typelist<unit::NonPrefixable<BT1,
00227                        unit::GenericUnit>, Loki::NullType>,
00228                          DU1, ST>, SU1> >::RET::Unit, ST>::VAL
00229                                                    (quantity.value ())))
00230               {namestring = quantity.name ();
00231                symbolstring = quantity.symbol ();
00232               }
00233 
00235 
00240         Constant (const Dynamic<ST> &dynamic)
00241           {if (dynamic._rl_n == RL_N/BSUtilities::Gcd<RL_N, RL_D>::RET
00242              && dynamic._rl_d == RL_D/BSUtilities::Gcd<RL_N, RL_D>::RET
00243              && dynamic._rm_n == RM_N/BSUtilities::Gcd<RM_N, RM_D>::RET
00244              && dynamic._rm_d == RM_D/BSUtilities::Gcd<RM_N, RM_D>::RET
00245              && dynamic._rt_n == RT_N/BSUtilities::Gcd<RT_N, RT_D>::RET
00246              && dynamic._rt_d == RT_D/BSUtilities::Gcd<RT_N, RT_D>::RET
00247              && dynamic._re_n == RE_N/BSUtilities::Gcd<RE_N, RE_D>::RET
00248              && dynamic._re_d == RE_D/BSUtilities::Gcd<RE_N, RE_D>::RET
00249              && dynamic._rte_n
00250                             == RTE_N/BSUtilities::Gcd<RTE_N, RTE_D>::RET
00251              && dynamic._rte_d
00252                             == RTE_D/BSUtilities::Gcd<RTE_N, RTE_D>::RET
00253              && dynamic._ra_n == RA_N/BSUtilities::Gcd<RA_N, RA_D>::RET
00254              && dynamic._ra_d == RA_D/BSUtilities::Gcd<RA_N, RA_D>::RET
00255              && dynamic._rlu_n
00256                             == RLU_N/BSUtilities::Gcd<RLU_N, RLU_D>::RET
00257              && dynamic._rlu_d
00258                            == RLU_D/BSUtilities::Gcd<RLU_N, RLU_D>::RET)
00259 
00260                constant_value = Reverse<SU, ST>::VAL (dynamic.value ());
00261 
00262             else
00263                {throw DimensionMismatch ();}
00264 
00265           }
00266 
00267       private:
00269 
00272         template<template<class, class> class Q1, class DIM1, class BT1,
00273                           class UL1, class DU1, class ST1, class SU1>
00274           Constant & operator= 
00275                   (const Q1<Quantity<DIM1, BT1, UL1, DU1, ST1>, SU1> &);
00276 
00278 
00281         template<class TQ> Constant & operator= (const TQ&);
00282 
00283       public:
00285 
00287         ST value (void) const {return constant_value;}
00288 
00290 
00293         ST standard_value (void) const 
00294                      {return Standardize<SU, ST>::VAL (constant_value);}
00295 
00297 
00302         template<class NU>
00303           ST value (const unit::NonPrefixable<BT, NU> &) const
00304             {return Reverse<typename CheckAgainstAllUnits<
00305               unit::NonPrefixable<BT, NU>, UL>::RET, ST>::VAL
00306                            (Standardize<SU, ST>::VAL (constant_value));}
00307 
00309 
00314         template<class NU, class P>
00315           ST value
00316             (const unit::Prefixed<unit::Prefixable<BT, NU>, P> &) const
00317             {return Reverse<typename CheckAgainstAllUnits<
00318               unit::Prefixed<unit::Prefixable<BT, NU>, P>, UL>::RET, 
00319                   ST>::VAL (Standardize<SU, ST>::VAL (constant_value));}
00320 
00322 
00327         template<class CUL, class EL>
00328           ST value
00329             (const unit::Composed<unit::ComposeBase<BT, CUL>, EL> &)
00330                                                                    const
00331             {return Reverse<typename CheckAgainstAllUnits<
00332               unit::Composed<unit::ComposeBase<BT, CUL>, EL>, UL>::RET,
00333                   ST>::VAL (Standardize<SU, ST>::VAL (constant_value));}
00334 
00336 
00343         ST value (const std::string &symbol)
00344           {
00345             ST value;
00346             ::unit::Unit<BT> *unitp = PQ::findBySymbol (symbol);
00347 
00348             value = dynamic_reverse<UL>::VAL
00349                     (Standardize<SU, ST>::VAL (constant_value), *unitp);
00350 
00351             delete (unitp);
00352 
00353             return value;
00354           }
00355 
00357 
00362         template<class NU>
00363           Constant<PQ, NU> operator () (const NU &) const
00364             {return Constant<PQ, NU> (constant_value, SU());}
00365 
00367 
00369         const std::string unitsymbol (void) const 
00370                                                  {return SU::Symbol ();}
00371 
00373 
00375         static std::string Unitsymbol (void) {return SU::Symbol ();}
00376 
00378 
00380         const std::string unitname (void) const {return SU::Name ();}
00381 
00383 
00385         static std::string Unitname (void) {return SU::Name ();}
00386 
00388 
00391         Variable<PQ, SU> operator+ (void) const
00392            {Variable<PQ, SU> new_constant (*this);
00393              return new_constant;
00394            }
00395 
00397 
00400         Variable<PQ, SU> operator- (void) const
00401            {Variable<PQ, SU> new_constant (*this);
00402              return new_constant *= -ST(1.0);
00403            }
00404 
00406 
00409         template <class Q>
00410           Variable<PQ, SU> operator+ (const Q &new_variable) const
00411             {Variable<PQ, SU> new_object = *this;
00412               return (new_object + new_variable);
00413             }
00414 
00416 
00419         template <class Q>
00420           Variable<PQ, SU> operator- (const Q &new_variable) const
00421             {Variable<PQ, SU> new_object = *this;
00422               return new_object -= new_variable;
00423             }
00424 
00426 
00428         Variable<PQ, SU> operator* (const ST factor) const
00429           {Variable<PQ, SU> new_object = *this;
00430             return new_object *= factor;
00431           }
00432 
00434 
00436         Variable<PQ, SU> operator/ (const ST factor) const
00437           {Variable<PQ, SU> new_object = *this;
00438             return new_object /= factor;
00439           }
00440 
00442 
00446         bool operator== (const Constant &rhs_variable) const
00447                        {return constant_value == rhs_variable.value ();}
00448 
00450 
00455         template <template<class, class> class Q1, class SU1>
00456           bool operator== (const Q1<PQ, SU1> &rhs_variable) const
00457             {return standard_value () == rhs_variable.standard_value ();
00458             }
00459 
00461 
00466         template <template<class, class> class Q1,
00467                  class DIM1, class BT1, class UL1, class DU1, class SU1>
00468           bool operator==
00469             (const Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00470                                                     &rhs_variable) const
00471             {
00472              return standard_value () == Standardize<typename
00473                CheckSecondDimension<Variable<PQ, SU>,
00474                  Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00475                          >::RET::Unit, ST>::VAL (rhs_variable.value ());
00476             }
00477 
00479 
00483         bool operator!= (const Constant &rhs_variable) const
00484                        {return constant_value != rhs_variable.value ();}
00485 
00487 
00492         template <template<class, class> class Q1, class SU1>
00493           bool operator!= (const Q1<PQ, SU1> &rhs_variable) const
00494             {return standard_value () != rhs_variable.standard_value ();
00495             }
00496 
00498 
00503         template <template<class, class> class Q1,
00504                  class DIM1, class BT1, class UL1, class DU1, class SU1>
00505           bool operator!=
00506             (const Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00507                                                     &rhs_variable) const
00508             {
00509              return standard_value () != Standardize<typename
00510                CheckSecondDimension<Variable<PQ, SU>,
00511                  Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00512                          >::RET::Unit, ST>::VAL (rhs_variable.value ());
00513             }
00514 
00516 
00520         bool operator> (const Constant &rhs_variable) const
00521                        {return constant_value >  rhs_variable.value ();}
00522 
00524 
00529         template <template<class, class> class Q1, class SU1>
00530           bool operator> (const Q1<PQ, SU1> &rhs_variable) const
00531             {return standard_value () >  rhs_variable.standard_value ();
00532             }
00533 
00535 
00540         template <template<class, class> class Q1,
00541                  class DIM1, class BT1, class UL1, class DU1, class SU1>
00542           bool operator>
00543             (const Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00544                                                     &rhs_variable) const
00545             {
00546              return standard_value () >  Standardize<typename
00547                CheckSecondDimension<Variable<PQ, SU>,
00548                  Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00549                          >::RET::Unit, ST>::VAL (rhs_variable.value ());
00550             }
00551 
00553 
00557         bool operator< (const Constant &rhs_variable) const
00558                        {return constant_value <  rhs_variable.value ();}
00559 
00561 
00566         template <template<class, class> class Q1, class SU1>
00567           bool operator< (const Q1<PQ, SU1> &rhs_variable) const
00568             {return standard_value () <  rhs_variable.standard_value ();
00569             }
00570 
00572 
00577         template <template<class, class> class Q1,
00578                  class DIM1, class BT1, class UL1, class DU1, class SU1>
00579           bool operator<
00580             (const Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00581                                                     &rhs_variable) const
00582             {
00583              return standard_value () <  Standardize<typename
00584                CheckSecondDimension<Variable<PQ, SU>,
00585                  Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00586                          >::RET::Unit, ST>::VAL (rhs_variable.value ());
00587             }
00588 
00590 
00594         bool operator>= (const Constant &rhs_variable) const
00595                        {return constant_value >= rhs_variable.value ();}
00596 
00598 
00603         template <template<class, class> class Q1, class SU1>
00604           bool operator>= (const Q1<PQ, SU1> &rhs_variable) const
00605             {return standard_value () 
00606                                     >=  rhs_variable.standard_value ();}
00607 
00609 
00614         template <template<class, class> class Q1,
00615                  class DIM1, class BT1, class UL1, class DU1, class SU1>
00616           bool operator>=
00617             (const Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00618                                                     &rhs_variable) const
00619             {
00620              return standard_value () >=  Standardize<typename
00621                CheckSecondDimension<Variable<PQ, SU>,
00622                  Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00623                          >::RET::Unit, ST>::VAL (rhs_variable.value ());
00624             }
00625 
00627 
00631         bool operator<= (const Constant &rhs_variable) const
00632                       {return constant_value <=  rhs_variable.value ();}
00633 
00635 
00640         template <template<class, class> class Q1, class SU1>
00641           bool operator<= (const Q1<PQ, SU1> &rhs_variable) const
00642             {return standard_value () 
00643                                     <=  rhs_variable.standard_value ();}
00644 
00646 
00651         template <template<class, class> class Q1,
00652                  class DIM1, class BT1, class UL1, class DU1, class SU1>
00653           bool operator<=
00654             (const Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00655                                                     &rhs_variable) const
00656             {
00657              return standard_value () <=  Standardize<typename
00658                CheckSecondDimension<Variable<PQ, SU>,
00659                  Q1<Quantity<DIM1, BT1, UL1, DU1, ST>, SU1>
00660                          >::RET::Unit, ST>::VAL (rhs_variable.value ());
00661             }
00662 
00664 
00669         template<class Q>
00670           typename GenerateVariable<Constant, Q>::Add operator*
00671                                                        (const Q &factor)
00672             {
00673               return typename GenerateVariable<Constant, Q>::Add
00674                          (standard_value () * factor.standard_value ());
00675             }
00676 
00678 
00683         template<class Q>
00684           typename GenerateVariable<Constant, Q>::Sub operator/
00685                                                        (const Q &factor)
00686             {
00687               return typename GenerateVariable<Constant, Q>::Sub
00688                          (standard_value () / factor.standard_value ());
00689             }
00690 
00692 
00695         friend
00696           typename GenerateVariable<Constant, Loki::NullType>::Inv 
00697                   operator/ (const ST numerator, const Constant &factor)
00698           {
00699             return typename 
00700               GenerateVariable<Constant, Loki::NullType>::Inv
00701                                  (numerator / factor.standard_value ());
00702           }
00703 
00705 
00711         template<long N, long D>
00712           typename GenerateVariable<Constant,
00713                                      BSUtilities::Rational<N, D> >::Mult
00714                                pow (const BSUtilities::Rational<N, D> &)
00715           {return 
00716              ::quantity::pow (*this, BSUtilities::Rational<N, D> ());}
00717 
00719 
00725         template<int I>
00726           typename GenerateVariable<Constant,
00727               BSUtilities::Rational<I, long(1)> >::Mult
00728                                         pow (typename Loki::Int2Type<I>)
00729           {return ::quantity::pow (*this, Loki::Int2Type<I> ());}
00730 
00732 
00738     template<class T>
00739       Dynamic<ST> pow (const T &exp) const
00740                                 {return ::quantity::pow (*this, exp);}
00741 
00742 
00744 
00746         friend
00747           typename
00748             GenerateVariable<Constant, BSUtilities::Rational<1, 2> 
00749               >::Mult sqrt (const Constant &constant)
00750           {
00751             return typename
00752               GenerateVariable<Constant, BSUtilities::Rational<1, 2> 
00753                         >::Mult (std::sqrt(constant.standard_value ()));
00754           }
00755 
00757 
00759         typename
00760           GenerateVariable<Constant, BSUtilities::Rational<1, 2> 
00761                                                      >::Mult sqrt (void)
00762           {
00763             return typename
00764               GenerateVariable<Constant, BSUtilities::Rational<1, 2> 
00765                                  >::Mult (std::sqrt(standard_value ()));
00766           }
00767 
00769 
00772       std::ostream & print_value (std::ostream &os) const
00773         {return os << constant_value << " " << SU::Symbol ();}
00774 
00776 
00778       void operator>> (std::string &str) const
00779           {str = BSUtilities::Conversion<Constant>::to_string (*this);}
00780 
00782 
00786       std::ostream & operator>> (std::ostream &os) const
00787                    {return this->print (os);}
00788 
00789     };
00790 
00791 }
00792 
00793 #endif /* _Constant_h */

Generated on Sun Jan 15 14:05:20 2006 for Quantity by doxygen 1.3.6