Utility.h

Go to the documentation of this file.
00001 /*
00002  * Expression Template Matrix Library
00003  *
00004  * Copyright (C) 2004 - 2006 Ricky Lung <mtlung@users.sourceforge.net>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 
00030 #ifndef EXMAT_MATH_UTILITY_H
00031 #define EXMAT_MATH_UTILITY_H
00032 
00033 #ifdef _MSC_VER
00034 #   pragma warning( push )
00035 #   pragma warning( disable : 4786 )
00036 #endif  // _MSC_VER
00037 
00038 #include "../Mat.h"
00039 #include "FunctionTable.h"
00040 #include <cmath>
00041 
00042 #if (EXMAT_VC6 && EXMAT_VC6_DEFAULT_STL || EXMAT_ICL)
00043 #   define VC6_STD_MATH_WKAR
00044 #else
00045 #   define VC6_STD_MATH_WKAR std
00046 #endif
00047 
00048 namespace exmat {
00049 
00051 namespace Math {
00052 
00056 
00057 EXMAT_INLINE2 static double CPi() {
00058     return static_cast<double>(3.1415926535897932384626433832795);
00059 }
00061 EXMAT_INLINE2 static double C2Pi() {
00062     return static_cast<double>(3.1415926535897932384626433832795*2);
00063 }
00065 EXMAT_INLINE2 static double CPi_2() {
00066     return static_cast<double>(1.5707963267948966192313216916395);
00067 }
00069 EXMAT_INLINE2 static double CPi_4() {
00070     return static_cast<double>(0.785398163397448309616);
00071 }
00073 EXMAT_INLINE2 static double C1_Pi() {
00074     return static_cast<double>(0.318309886183790671538);
00075 }
00077 EXMAT_INLINE2 static double C2_Pi() {
00078     return static_cast<double>(0.636619772367581343076);
00079 }
00081 EXMAT_INLINE2 static double CE() {
00082     return static_cast<double>(2.7182818284590452353602874713527);
00083 }
00085 EXMAT_INLINE2 static double CSqrt2() {
00086     return static_cast<double>(1.41421356237309504880);
00087 }
00089 EXMAT_INLINE2 static double C1_Sqrt2() {
00090     return static_cast<double>(0.707106781186547524401);
00091 }
00093 
00094 
00098 
00100 EXMAT_INLINE2 static int abs(int x) {
00101     return int(VC6_STD_MATH_WKAR::abs(x));
00102 }
00104 EXMAT_INLINE2 static float abs(float x) {
00105     return float(VC6_STD_MATH_WKAR::fabs(x));
00106 }
00108 EXMAT_INLINE2 static double abs(double x) {
00109     return double(VC6_STD_MATH_WKAR::fabs(x));
00110 }
00111 
00113 EXMAT_INLINE2 static bool isEqual(double s1, double s2, double eps=1e-6) {
00114     return (Math::abs(s1 - s2) <= eps);
00115 }
00117 
00119 template<typename T> EXMAT_INLINE2 static
00120 T min(const T& x1, const T& x2) {
00121     return x1 < x2 ? x1 : x2;
00122 }
00124 template<typename T> EXMAT_INLINE2 static
00125 T min(const T& x1, const T& x2, const T& x3) {
00126     T tmp = min(x2, x3);
00127     return x1 < tmp ? x1 : tmp;
00128 }
00130 template<typename T> EXMAT_INLINE2 static
00131 T min(const T& x1, const T& x2, const T& x3, const T& x4) {
00132     T tmp = min(x2, x3, x4);
00133     return x1 < tmp ? x1 : tmp;
00134 }
00136 template<typename T> EXMAT_INLINE2 static
00137 T min(const T& x1, const T& x2, const T& x3, const T& x4, const T& x5) {
00138     T tmp = min(x2, x3, x4, x5);
00139     return x1 < tmp ? x1 : tmp;
00140 }
00142 EXMAT_INLINE2 static float min(const float* in, size_t N) {
00143     return PNS::FUNffpiTable[PNS::FUNffpi_ID::min](in, N);
00144 }
00145 
00147 template<typename T> EXMAT_INLINE2 static
00148 T max(const T& x1, const T& x2) {
00149     return x1 > x2 ? x1 : x2;
00150 }
00152 template<typename T> EXMAT_INLINE2 static
00153 T max(const T& x1, const T& x2, const T& x3) {
00154     T tmp = max(x2, x3);
00155     return x1 > tmp ? x1 : tmp;
00156 }
00158 template<typename T> EXMAT_INLINE2 static
00159 T max(const T& x1, const T& x2, const T& x3, const T& x4) {
00160     T tmp = max(x2, x3, x4);
00161     return x1 > tmp ? x1 : tmp;
00162 }
00164 template<typename T> EXMAT_INLINE2 static
00165 T max(const T& x1, const T& x2, const T& x3, const T& x4, const T& x5) {
00166     T tmp = max(x2, x3, x4, x5);
00167     return x1 > tmp ? x1 : tmp;
00168 }
00170 EXMAT_INLINE2 static float max(const float* in, size_t N) {
00171     return PNS::FUNffpiTable[PNS::FUNffpi_ID::max](in, N);
00172 }
00173 
00175 EXMAT_INLINE2 static float sum(const float* in, size_t N) {
00176     return PNS::FUNffpiTable[PNS::FUNffpi_ID::sum](in, N);
00177 }
00178 
00180 EXMAT_INLINE2 static float rcp(float x) {
00181     return PNS::FUNffTable[PNS::FUNff_ID::rcp](x);
00182 }
00183 EXMAT_INLINE2 static void rcp(const float* in, float* out, size_t N) {
00184     PNS::FUNvfpfpiTable[PNS::FUNff_ID::rcp](in, out, N);
00185 }
00186 
00188 
00189 EXMAT_INLINE2 static float sqrt(float x) {
00190     return PNS::FUNffTable[PNS::FUNff_ID::sqrt](x);
00191 }
00192 EXMAT_INLINE2 static double sqrt(double x) {
00193     using namespace std;
00194     return double(VC6_STD_MATH_WKAR::sqrt(x));
00195 }
00196 EXMAT_INLINE2 static void sqrt(const float* in, float* out, size_t N) {
00197     PNS::FUNvfpfpiTable[PNS::FUNff_ID::sqrt](in, out, N);
00198 }
00200 
00202 
00203 EXMAT_INLINE2 static float rsqrt(float x) {
00204     return PNS::FUNffTable[PNS::FUNff_ID::rsqrt](x);
00205 }
00206 EXMAT_INLINE2 static double rsqrt(double x) {
00207     using namespace std;
00208     return 1.0 / double(VC6_STD_MATH_WKAR::sqrt(x));
00209 }
00210 EXMAT_INLINE2 static void rsqrt(const float* in, float* out, size_t N) {
00211     PNS::FUNvfpfpiTable[PNS::FUNff_ID::rsqrt](in, out, N);
00212 }
00214 
00216 
00217 EXMAT_INLINE2 static float exp(float x) {
00218     return PNS::FUNffTable[PNS::FUNff_ID::exp](x);
00219 }
00220 EXMAT_INLINE2 static double exp(double x) {
00221     return double(VC6_STD_MATH_WKAR::exp(x));
00222 }
00223 EXMAT_INLINE2 static void exp(const float* in, float* out, size_t N) {
00224     PNS::FUNvfpfpiTable[PNS::FUNff_ID::exp](in, out, N);
00225 }
00227 
00229 
00230 EXMAT_INLINE2 static float log(float x) {
00231     return PNS::FUNffTable[PNS::FUNff_ID::log](x);
00232 }
00233 EXMAT_INLINE2 static double log(double x) {
00234     return double(VC6_STD_MATH_WKAR::log(x));
00235 }
00236 EXMAT_INLINE2 static void log(const float* in, float* out, size_t N) {
00237     PNS::FUNvfpfpiTable[PNS::FUNff_ID::log](in, out, N);
00238 }
00240 
00242 
00243 EXMAT_INLINE2 static float pow(float x, float y) {
00244     return PNS::FUNfffTable[PNS::FUNfff_ID::pow](x, y);
00245 }
00246 EXMAT_INLINE2 static double pow(double x, double y) {
00247     return double(VC6_STD_MATH_WKAR::pow(x, y));
00248 }
00250 
00251 
00255 
00256 EXMAT_INLINE2 static float sin(float x) {
00257     return PNS::FUNffTable[PNS::FUNff_ID::sin](x);
00258 }
00259 EXMAT_INLINE2 static double sin(double x) {
00260     return double(VC6_STD_MATH_WKAR::sin(x));
00261 }
00262 EXMAT_INLINE2 static void sin(const float* in, float* out, size_t N) {
00263     PNS::FUNvfpfpiTable[PNS::FUNff_ID::sin](in, out, N);
00264 }
00265 
00267 EXMAT_INLINE2 static float cos(float x) {
00268     return PNS::FUNffTable[PNS::FUNff_ID::cos](x);
00269 }
00270 EXMAT_INLINE2 static double cos(double x) {
00271     return double(VC6_STD_MATH_WKAR::cos(x));
00272 }
00273 EXMAT_INLINE2 static void cos(const float* in, float* out, size_t N) {
00274     PNS::FUNvfpfpiTable[PNS::FUNff_ID::cos](in, out, N);
00275 }
00276 
00278 EXMAT_INLINE2 static void sincos(float x, float& s, float& c) {
00279     PNS::FUNvffafaTable[PNS::FUNvffafa_ID::sincos](x, s, c);
00280 }
00282 EXMAT_INLINE2 static void sincos(double x, double& s, double& c) {
00283     s = VC6_STD_MATH_WKAR::sin(x);
00284     c = VC6_STD_MATH_WKAR::sin(c);
00285 }
00286 
00288 EXMAT_INLINE2 static float tan(float x) {
00289     return PNS::FUNffTable[PNS::FUNff_ID::tan](x);
00290 }
00291 EXMAT_INLINE2 static double tan(double x) {
00292     return double(VC6_STD_MATH_WKAR::tan(x));
00293 }
00294 EXMAT_INLINE2 static void tan(const float* in, float* out, size_t N) {
00295     PNS::FUNvfpfpiTable[PNS::FUNff_ID::tan](in, out, N);
00296 }
00297 
00299 EXMAT_INLINE2 static float asin(float x) {
00300     return PNS::FUNffTable[PNS::FUNff_ID::asin](x);
00301 }
00302 EXMAT_INLINE2 static double asin(double x) {
00303     return double(VC6_STD_MATH_WKAR::asin(x));
00304 }
00305 EXMAT_INLINE2 static void asin(const float* in, float* out, size_t N) {
00306     PNS::FUNvfpfpiTable[PNS::FUNff_ID::asin](in, out, N);
00307 }
00308 
00310 EXMAT_INLINE2 static float acos(float x) {
00311     return PNS::FUNffTable[PNS::FUNff_ID::acos](x);
00312 }
00313 EXMAT_INLINE2 static double acos(double x) {
00314     return double(VC6_STD_MATH_WKAR::acos(x));
00315 }
00316 EXMAT_INLINE2 static void acos(const float* in, float* out, size_t N) {
00317     PNS::FUNvfpfpiTable[PNS::FUNff_ID::acos](in, out, N);
00318 }
00319 
00321 EXMAT_INLINE2 static float atan(float x) {
00322     return PNS::FUNffTable[PNS::FUNff_ID::atan](x);
00323 }
00324 EXMAT_INLINE2 static double atan(double x) {
00325     return double(VC6_STD_MATH_WKAR::atan(x));
00326 }
00327 EXMAT_INLINE2 static void atan(const float* in, float* out, size_t N) {
00328     PNS::FUNvfpfpiTable[PNS::FUNff_ID::atan](in, out, N);
00329 }
00331 
00332 
00333 };  // namespace Math
00334 
00335 
00336 template<class Rep1, class Rep2, class ErrChk1, class ErrChk2> EXMAT_INLINE0 bool
00337 isEqual(const Mat<Rep1,ErrChk1>& mat1, const Mat<Rep2,ErrChk2>& mat2, double eps=1e-6) {
00338     typedef typename TypeTraits<TTYPENAME Rep1::value_type, TTYPENAME Rep2::value_type>::HigherValue value_type;
00339     typedef typename Rep1::index_type index_type;
00340     // Check for same dimension
00341     if(mat1.rows() != mat2.rows() || mat1.cols() != mat2.cols())
00342         return false;
00343     for(index_type i=0; i<mat1.rows(); ++i) for(index_type j=0; j<mat1.cols(); ++j) {
00344         if(Math::abs(mat1.getAt(i,j) - mat2.getAt(i,j)) > eps)
00345             return false;
00346     }
00347     return true;
00348 }
00349 
00350 
00351 }   // namespace exmat
00352 
00353 #ifdef _MSC_VER
00354 #   pragma warning( pop )
00355 #endif  // _MSC_VER
00356 
00357 #ifdef VC6_STD_MATH_WKAR
00358 #   undef VC6_STD_MATH_WKAR
00359 #endif
00360 
00361 #endif  // EXMAT_MATH_UTILITY_H

Generated on Sat May 6 23:12:03 2006 for Exmat by  doxygen 1.4.6-NO