src/expde/math_lib/math_lib.h

Go to the documentation of this file.
00001 //    expde: expression templates for partial differential equations.
00002 //    Copyright (C) 2001  Christoph Pflaum
00003 //    This program is free software; you can redistribute it and/or modify
00004 //    it under the terms of the GNU General Public License as published by
00005 //    the Free Software Foundation; either version 2 of the License, or
00006 //    (at your option) any later version.
00007 //
00008 //    This program is distributed in the hope that it will be useful,
00009 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //    GNU General Public License for more details.
00012 //
00013 //                 SEE  Notice1.doc made by 
00014 //                 LAWRENCE LIVERMORE NATIONAL LABORATORY
00015 //
00016 
00017 // ------------------------------------------------------------
00018 // math_lib.h
00019 //
00020 // ------------------------------------------------------------
00021 
00022 #ifndef MATHLIB_H_
00023 #define MATHLIB_H_
00024        
00025 #ifndef M_PI
00026 #define M_PI 3.1415927
00027 #endif
00028 
00029 #ifdef COMP_GNUOLD
00030 
00031 #else
00032  #include <iostream>
00033  #include <fstream>           
00034  using std::ofstream;
00035  using std::cout;
00036  using std::endl;           
00037 #endif 
00038          
00039 
00041 // 1. next prim
00042 // 2. MAX, MIN
00043 // 3. 3D vector class
00044 // 4. geometric operators for 3D vectors
00045 // 5. others
00047 
00048 
00049 
00051 // 1. next prim 
00052 
00053 
00054 // Find next prim
00055 int Find_next_prime(int zahl);
00056 
00057 
00058 // trigonometric functions
00059 
00060 inline double arccos(const double y) {
00061    return atan2(sqrt(1-y*y),y);
00062    // if(y>=0) return atan(sqrt(1-y*y)/y);
00063    // else    return atan(sqrt(1-y*y)/y) + M_PI;
00064 }
00065 
00066 inline double my_tan(const double y) {
00067   return tan(y);
00068 }
00069 
00070 inline double my_atan(const double y) {
00071   return atan(y);
00072 }
00073 
00074 inline double my_sqrt(const double y) {
00075   return sqrt(y);
00076 }
00077 
00079 // MAX, MIN
00080 
00081 
00082 /* Maximum */
00083 inline double MAX(double a, double b) {
00084   if(a < b) return b;
00085   else return a;
00086 }
00087 
00088 inline double MAX(double a, double b, double c) {
00089   return MAX(MAX(a,b),c);
00090 }
00091 
00092 inline int MAX(int a, int b) {
00093   if(a < b) return b;
00094   else return a;
00095 }
00096 
00097 inline int MAX(int a, int b, int c) {
00098   static int m;
00099   if(a < b) m = b;
00100   else m = a;
00101   if(c < m) return m;
00102   else return c;
00103 }
00104 
00105 /* Minimum */
00106 inline double MIN(double a, double b) {
00107   if(a < b) return a;
00108   else return b;
00109 }
00110 
00111 /* Absolute value */
00112 inline double ABS(double a) {
00113   if(a < 0.0) return -a;
00114   else return a;
00115 }
00116 
00117 /* Absolute value */
00118 inline int ABS(int a) {
00119   if(a < 0) return -a;
00120   else return a;
00121 }
00122 
00123 
00125 // 3. a simple 3D vector class
00127 
00128 class D3vector {
00129  public:
00130   double x,y,z;
00131   D3vector(double cx, double cy, double cz) : x(cx), y(cy), z(cz) {}; 
00132   D3vector(double c) : x(c), y(c), z(c) {}; 
00133   D3vector() : x(0), y(0), z(0) {}; 
00134   void Print();
00135   void Print(ofstream *Datei);
00136   void operator=(const D3vector& v) { x=v.x; y=v.y; z=v.z; }
00137   double operator[](int i) const {
00138     if(i==0) return x;
00139     if(i==1) return y;
00140     return z;
00141   }
00142 };   
00143 
00144 inline D3vector MAX(D3vector V1, D3vector V2) {
00145   return D3vector(MAX(V1.x,V2.x),MAX(V1.y,V2.y),MAX(V1.z,V2.z));
00146 }
00147 
00148 inline D3vector MIN(D3vector V1, D3vector V2) {
00149   return D3vector(MIN(V1.x,V2.x),MIN(V1.y,V2.y),MIN(V1.z,V2.z));
00150 }
00151 
00152 inline D3vector operator+(const D3vector& v,const D3vector& w) {
00153   return D3vector(v.x+w.x,v.y+w.y,v.z+w.z);
00154 };
00155 
00156 inline D3vector operator*(const D3vector& v,const D3vector& w) {
00157   return D3vector(v.x*w.x,v.y*w.y,v.z*w.z);
00158 };
00159 
00160 inline D3vector operator/(const D3vector& v,const D3vector& w) {
00161   return D3vector(v.x/w.x,v.y/w.y,v.z/w.z);
00162 };
00163 
00164 inline D3vector operator-(const D3vector& v,const D3vector& w) {
00165   return D3vector(v.x-w.x,v.y-w.y,v.z-w.z);
00166 };
00167 
00168 inline D3vector operator/(const D3vector& v,const double f) {
00169   return D3vector(v.x/f,v.y/f,v.z/f);
00170 };
00171 
00172 inline D3vector operator*(const D3vector& v,const double f) {
00173   return D3vector(v.x*f,v.y*f,v.z*f);
00174 };
00175 
00176 inline D3vector operator*(const double f, const D3vector& v) {
00177   return D3vector(v.x*f,v.y*f,v.z*f);
00178 };
00179 
00180 inline bool operator<(const D3vector& v,const D3vector& w) {
00181   return (v.x<w.x && v.y<w.y && v.z<w.z);
00182 };
00183 inline bool operator==(const D3vector& v,const D3vector& w) {
00184   return (v.x==w.x && v.y==w.y && v.z==w.z);
00185 };
00186 
00187 
00189 // 4. geometric operators for 3D vectors
00190 
00191 inline double L_infty(const D3vector& v) {
00192   return MAX(ABS(v.x),ABS(v.y),ABS(v.z));
00193 }
00194 
00195 inline double norm(const D3vector& v) {
00196   return my_sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
00197 }
00198 
00199 // scalar product
00200 inline double product(const D3vector& v, const D3vector& w) {
00201   return v.x*w.x+v.y*w.y+v.z*w.z;
00202 }
00203 
00204 inline D3vector cross_product(const D3vector& v, const D3vector& w) {
00205   return D3vector(v.y*w.z-v.z*w.y,
00206                   v.z*w.x-v.x*w.z,
00207                   v.x*w.y-v.y*w.x);
00208 };
00209 
00210 inline D3vector normal_vector_of_triangle(const D3vector& va,
00211                                           const D3vector& vb,
00212                                           const D3vector& vc) {
00213   D3vector z;
00214   z = cross_product(va-vb,vc-vb);
00215   return z / norm(z);
00216 }
00217 
00218 
00219 inline double angle_between_vectors(const D3vector& va,
00220                                     const D3vector& vb) {
00221   return arccos(product(va,vb) / (norm(va)*norm(vb))) / M_PI * 180 ;
00222 }
00223 
00224 
00225 
00226 inline double max_interior_angel_of_triangle(const D3vector& va,
00227                                              const D3vector& vb,
00228                                              const D3vector& vc) {
00229   return MAX(angle_between_vectors(vc-va,vb-va),
00230              angle_between_vectors(va-vb,vc-vb),
00231              angle_between_vectors(vb-vc,va-vc));
00232 }
00233 
00234 // va, vb, vc is one face
00235 // vb, vd, vc is one face
00236 inline double angle_between_faces(const D3vector& va,
00237                                   const D3vector& vb,
00238                                   const D3vector& vc,
00239                                   const D3vector& vd) {
00240   return 180 - angle_between_vectors(normal_vector_of_triangle(va,vb,vc),
00241                                      normal_vector_of_triangle(vb,vd,vc));
00242 }
00243 
00244 double calc_maximal_edge_angle(const D3vector& va,
00245                                const D3vector& vb,
00246                                const D3vector& vc,
00247                                const D3vector& vd);
00248 
00249 double calc_maximal_face_angle(const D3vector& va,
00250                                const D3vector& vb,
00251                                const D3vector& vc,
00252                                const D3vector& vd);
00253 
00254 // volumne of tetrahedra
00255 
00256 #define x0 V0.x
00257 #define y0 V0.y
00258 #define z0 V0.z
00259 
00260 #define x1 V1.x
00261 #define y1 V1.y
00262 #define z1 V1.z
00263 
00264 #define x2 V2.x
00265 #define y2 V2.y
00266 #define z2 V2.z
00267 
00268 #define x3 V3.x
00269 #define y3 V3.y
00270 #define z3 V3.z
00271 
00272 inline double Calc_det(const D3vector& V0, const D3vector& V1,
00273                        const D3vector& V2, const D3vector& V3) {
00274   return (x1*y2*z0 - 
00275           x1*y3*z0 - x0*y2*z1 + x0*y3*z1 - x1*y0*z2 + x0*y1*z2 - x0*y3*z2 + 
00276           x1*y3*z2 + x3*(y1*z0 - y2*z0 - y0*z1 + y2*z1 + y0*z2 - y1*z2) + 
00277           x1*y0*z3 - x0*y1*z3 + x0*y2*z3 - x1*y2*z3 + 
00278           x2*(y3*z0 + y0*z1 - y3*z1 - y0*z3 + y1*(-z0 + z3)));
00279 }
00280 
00281 #undef x0
00282 #undef y0
00283 #undef z0
00284 
00285 #undef x1
00286 #undef y1
00287 #undef z1
00288 
00289 #undef x2
00290 #undef y2
00291 #undef z2
00292 
00293 #undef x3
00294 #undef y3
00295 #undef z3
00296 
00297 inline double Calc_bary_weights(const D3vector& V0, const D3vector& V1,
00298                                 const D3vector& V2, const D3vector& V3,
00299                                 const D3vector& V, int i) {
00300   if(i==0) return Calc_det(V,V1,V2,V3);
00301   if(i==1) return Calc_det(V0,V,V2,V3);
00302   if(i==2) return Calc_det(V0,V1,V,V3);
00303   if(i==3) return Calc_det(V0,V1,V2,V);
00304   cout << " error in Calc_bary_weights! " << endl;
00305   return (double) -1.0;
00306 }
00307 
00308 
00309 
00311 // 5. others
00312 
00313 
00314 /* Test ob Zahl gerade */
00315 inline int gerade(int N) {
00316   return (~N)&1;
00317 }
00318 
00319 // Berechnung der Zweierpotenz 
00320 inline int Zweipotenz (int k) {
00321   return 1<<k;
00322 }
00323 
00324 
00326 // Implementierung einiger Memberfunktionen
00328 
00329 
00330 // D3vector 
00331 // ----------
00332 
00333 inline void D3vector::Print() {
00334   cout << "Coordinate: " << x << ", " << y << ", " << z << ";";
00335 };
00336 
00337 inline void D3vector::Print(ofstream *Datei) {
00338   *Datei  << x << " " << y << " " << z;
00339 };
00340 
00341 
00342      
00343 
00344 #endif
00345    
00346   

Generated on Mon Jan 16 13:23:42 2006 for IPPL by  doxygen 1.4.6