util.h

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #ifndef MECHSYS_SORT_H
00023 #define MECHSYS_SORT_H
00024 
00025 #include <cmath>
00026 
00027 #ifdef HAVE_CONFIG_H
00028   #include "config.h"
00029 #else
00030   #ifndef REAL
00031     #define REAL double
00032   #endif
00033 #endif
00034 
00035 #include "util/string.h"
00036 
00037 namespace Util
00038 {
00039 
00040 inline REAL Pi         ()                    { return 3.14159265358979;             }
00041 inline REAL ToRad      (REAL deg_angle)      { return 0.0174532925199433*deg_angle; }
00042 inline REAL ToDeg      (REAL rad_angle)      { return 57.2957795130823*rad_angle;   }
00043 inline REAL Sgn        (REAL Val)            { return (Val>=0.0 ? +1.0 : -1.0);     }
00044 inline REAL Signal     (REAL Val, REAL Zero) { return (fabs(Val)<=Zero ? 0.0 : Sgn(Val)); }
00045 inline REAL Acos       (REAL Val)            { return (Val>=1.0 ?  0.0 : (Val<=-1.0 ? Pi() : acos(Val)) ); }
00046 inline bool Str2Bool   (String const & Str)  { if (Str==String("true") || Str==String("TRUE") || Str==String("True")) return true; else return false; }
00047 inline bool IsNanOrInf (REAL Val)            { int r=std::fpclassify(Val); if (r==FP_NAN) return true; if (r==FP_INFINITE) return true; return false; }
00048 inline REAL Sq2        ()                    { return 1.41421356237310; }
00049 inline REAL Sq3        ()                    { return 1.73205080756888; }
00050 inline REAL Sq6        ()                    { return 2.44948974278318; }
00051 inline REAL Sign       (REAL a, REAL b)      { return (b>=0.0 ? fabs(a) : -fabs(a)); }
00052 template <typename Type>
00053 inline Type Min        (Type a, Type b)      { return (a<b ? a : b); }
00054 template <typename Type>
00055 inline Type Max        (Type a, Type b)      { return (a>b ? a : b); }
00056 
00057 inline void Swap(REAL & x, REAL & y)
00058 { // {{{
00059     REAL temp = x;
00060     x = y;
00061     y = temp;
00062 } // }}}
00063 
00064 inline void Sort(REAL A[], int Size) // ascending sort
00065 { // {{{
00066     for (int i=0; i<Size-1; ++i)
00067     {
00068         int min_index = i;
00069 
00070         // Find the index of the minimum element
00071         for (int j=i+1; j<Size; ++j)
00072             if (A[j] < A[min_index])
00073                 min_index = j;
00074 
00075         // Swap if i-th element not already smallest
00076         if (min_index > i)
00077             Util::Swap(A[i], A[min_index]);
00078     }
00079 } // }}}
00080 
00081 inline void FindBestSquare (int Size, int & nRow, int & nCol)
00082 { // {{{
00083     nRow = -1;  // not found
00084     nCol = -1;  // not found
00085     for (int x=1; x<=Size; ++x)
00086     {
00087         if ((x*x)>=Size)
00088         {
00089             if ((x*x)==Size)
00090             {
00091                 nRow = x;
00092                 nCol = x;
00093                 return;
00094             }
00095             else
00096             {
00097                 for (int y=x; y>=1; --y)
00098                 {
00099                     if ((x*y)==Size)
00100                     {
00101                         nRow = x;
00102                         nCol = y;
00103                         return;
00104                     }
00105                 }
00106             }
00107         }
00108     }
00109 } // }}}
00110 
00111 }; // namespace Util
00112 
00113 #endif // MECHSYS_SORT_H
00114 
00115 // vim:fdm=marker

Generated on Wed Jan 24 15:56:27 2007 for MechSys by  doxygen 1.4.7