STK++ 1.0
STK_Const_Math.cpp
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  Analysis
00027  * Purpose:  implementation of the class Const
00028  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00029  **/
00030 
00036 #include "../../STKernel/include/STK_Integer.h"
00037 #include "../../STKernel/include/STK_Real.h"
00038 #include "../include/STK_Const_Math.h"
00039 #include "../include/STK_ISerie.h"
00040 #include "../include/STK_Algo.h"
00041 
00042 namespace STK
00043 {
00044 
00045 namespace Const
00046 {
00047 
00059 class SeriePi : public ISerie<SeriePi>
00060 {
00061   private:
00062     mutable Real dig16_;
00063     mutable Integer k4_;
00064 
00065   public:
00068     inline SeriePi() : dig16_(1.), k4_(0)
00069     { ;}
00070 
00073     inline Real first() const
00074     { return 47./15.;}
00075  
00078     inline Real next() const
00079     { 
00080       dig16_ /= 16.;
00081       k4_    += 4;
00082      
00083       return  dig16_
00084             * ( 2.0/(k4_+0.5)
00085               - 1.0/(k4_+2.0)
00086               - 0.5/(k4_+2.5)
00087               - 0.5/(k4_+3.0)
00088               );
00089     }
00090  
00094     inline Real operator[](Integer k) const
00095     { Integer aux = 4*k;
00096       return  pow(2, -aux)
00097             * ( 2.0/(aux+0.5)
00098               - 1.0/(aux+2.0)
00099               - 0.5/(aux+2.5)
00100               - 0.5/(aux+3.0)
00101               );
00102   }
00103 };
00104 
00117 Real pi()
00118 {
00119   SeriePi f;
00120   return sumSerie<SeriePi>(f);
00121 }
00122 
00126 class SerieEuler : public ISerie<SerieEuler>
00127 {
00128   private:
00129     mutable Real k_;
00130 
00131   public:
00134     inline SerieEuler() : k_(2.)
00135     { ;}
00136 
00139     inline Real first() const
00140     { return (Const::_LN2_/2.);}
00141  
00144     inline Real next() const
00145     { 
00146       k_    += 1.;
00147       return  log(k_)/k_;
00148     }
00149  
00153     inline Real operator[](Integer k) const
00154     { return log(k + 2.)/(Real)(k + 2);}
00155 };
00156 
00166 Real euler()
00167 {
00168   SerieEuler f;
00169   return Const::_LN2_/2.0 + sumAlternateSerie<SerieEuler>(f)/Const::_LN2_;
00170 }
00171 
00172 } // namespace CONST
00173 
00174 } // namespace STK