[ VIGRA Homepage | Class Index | Function Index | File Index | Main Page ]

details vigra/mathutil.hxx VIGRA

00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 1998-2002 by Ullrich Koethe                  */
00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
00005 /*                                                                      */
00006 /*    This file is part of the VIGRA computer vision library.           */
00007 /*    ( Version 1.3.2, Jan 27 2005 )                                    */
00008 /*    You may use, modify, and distribute this software according       */
00009 /*    to the terms stated in the LICENSE file included in               */
00010 /*    the VIGRA distribution.                                           */
00011 /*                                                                      */
00012 /*    The VIGRA Website is                                              */
00013 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
00014 /*    Please direct questions, bug reports, and contributions to        */
00015 /*        koethe@informatik.uni-hamburg.de                              */
00016 /*                                                                      */
00017 /*  THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR          */
00018 /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED      */
00019 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
00020 /*                                                                      */
00021 /************************************************************************/
00022 
00023 #ifndef VIGRA_MATHUTIL_HXX
00024 #define VIGRA_MATHUTIL_HXX
00025 
00026 #include <cmath>
00027 #include <cstdlib>
00028 #include "vigra/config.hxx"
00029 #include "vigra/numerictraits.hxx"
00030 
00031 /*! \page MathConstants Mathematical Constants
00032 
00033     <TT>M_PI, M_SQRT2</TT>
00034 
00035     <b>\#include</b> "<a href="mathutil_8hxx-source.html">vigra/mathutil.hxx</a>"
00036 
00037     Since <TT>M_PI</TT> and <TT>M_SQRT2</TT> are not officially standardized,
00038     we provide definitions here for those compilers that don't support them.
00039 
00040     \code
00041     #ifndef M_PI
00042     #    define M_PI     3.14159265358979323846
00043     #endif
00044 
00045     #ifndef M_SQRT2
00046     #    define M_SQRT2  1.41421356237309504880
00047     #endif
00048     \endcode
00049 */
00050 #ifndef M_PI
00051 #    define M_PI     3.14159265358979323846
00052 #endif
00053 
00054 #ifndef M_SQRT2
00055 #    define M_SQRT2  1.41421356237309504880
00056 #endif
00057 
00058 namespace vigra {
00059 
00060 #ifndef __sun 
00061 
00062 /** \addtogroup MathFunctions Mathematical Functions
00063 
00064     Useful mathematical functions and functors.
00065 */
00066 //@{
00067 /*! The error function.
00068 
00069     With the exception of Solaris (where <tt>erf()</tt> is provided as an extension of the 
00070     C math library), VIGRA implements <tt>erf()</tt> as an approximation of the error 
00071     function
00072     
00073     \f[
00074         \mbox{erf}(x) = \int_0^x e^{-x^2} dx
00075     \f]
00076     
00077     according to the formula given in Press et al. "Numerical Recipes".
00078 
00079     <b>\#include</b> "<a href="mathutil_8hxx-source.html">vigra/mathutil.hxx</a>"<br>
00080     Namespace: vigra
00081 */
00082 template <class T>
00083 double erf(T x)
00084 {
00085     double t = 1.0/(1.0+0.5*VIGRA_CSTD::fabs(x));
00086     double ans = t*VIGRA_CSTD::exp(-x*x-1.26551223+t*(1.00002368+t*(0.37409196+
00087                                     t*(0.09678418+t*(-0.18628806+t*(0.27886807+
00088                                     t*(-1.13520398+t*(1.48851587+t*(-0.82215223+
00089                                     t*0.17087277)))))))));
00090     if (x >= 0.0)
00091         return 1.0 - ans;
00092     else
00093         return ans - 1.0;
00094 }
00095 
00096 #else
00097 
00098 using VIGRA_CSTD::erf;
00099 
00100 #endif
00101 
00102 // import functions into namespace vigra which VIGRA is going to overload
00103 using std::abs;  
00104 using VIGRA_CSTD::pow;  
00105 using VIGRA_CSTD::floor;  
00106 using VIGRA_CSTD::ceil;  
00107 
00108 /*! The square function.
00109 
00110     sq(x) is needed so often that it makes sense to define it as a function.
00111 
00112     <b>\#include</b> "<a href="mathutil_8hxx-source.html">vigra/mathutil.hxx</a>"<br>
00113     Namespace: vigra
00114 */
00115 template <class T>
00116 inline 
00117 typename NumericTraits<T>::Promote sq(T t)
00118 {
00119     return t*t;
00120 }
00121 
00122 #ifdef VIGRA_NO_HYPOT
00123 /*! Compute the Euclidean distance.
00124 
00125     The  hypot()  function  returns  the  sqrt(a*a  +  b*b).
00126     It is implemented in a way that minimizes round-off error.
00127 
00128     <b>\#include</b> "<a href="mathutil_8hxx-source.html">vigra/mathutil.hxx</a>"<br>
00129     Namespace: vigra
00130 */
00131 template <class T>
00132 T hypot(T a, T b) 
00133 { 
00134     T absa = abs(a), absb = abs(b);
00135     if (absa > absb) 
00136         return absa * VIGRA_CSTD::sqrt(1.0 + sq(absb/absa)); 
00137     else 
00138         return absb == NumericTraits<T>::zero()
00139                    ? NumericTraits<T>::zero()
00140                    : absb * VIGRA_CSTD::sqrt(1.0 + sq(absa/absb)); 
00141 }
00142 
00143 #else
00144 
00145 using ::hypot;
00146 
00147 #endif
00148 
00149 /*! The sign function.
00150 
00151     Returns 1, 0, or -1 depending on the signm of \a t.
00152 
00153     <b>\#include</b> "<a href="mathutil_8hxx-source.html">vigra/mathutil.hxx</a>"<br>
00154     Namespace: vigra
00155 */
00156 template <class T>
00157 T sign(T t) 
00158 { 
00159     return t > NumericTraits<T>::zero()
00160                ? NumericTraits<T>::one()
00161                : t < NumericTraits<T>::zero()
00162                     ? -NumericTraits<T>::one()
00163                     : NumericTraits<T>::zero();
00164 }
00165 
00166 /*! The binary sign function.
00167 
00168     Transfers the sign of \a t2 to \a t1.
00169 
00170     <b>\#include</b> "<a href="mathutil_8hxx-source.html">vigra/mathutil.hxx</a>"<br>
00171     Namespace: vigra
00172 */
00173 template <class T1, class T2>
00174 T1 sign(T1 t1, T2 t2) 
00175 { 
00176     return t2 >= NumericTraits<T2>::zero()
00177                ? abs(t1)
00178                : -abs(t1);
00179 }
00180 
00181 //@}
00182 
00183 } // namespace vigra
00184 
00185 
00186 #endif /* VIGRA_MATHUTIL_HXX */

© Ullrich Köthe (koethe@informatik.uni-hamburg.de)
Cognitive Systems Group, University of Hamburg, Germany

html generated using doxygen and Python
VIGRA 1.3.2 (27 Jan 2005)