CoreOther.cpp

Go to the documentation of this file.
00001 //Copyright (c) 2004-2005, Baris Sumengen
00002 //All rights reserved.
00003 //
00004 // CIMPL Matrix Performance Library
00005 //
00006 //Redistribution and use in source and binary
00007 //forms, with or without modification, are
00008 //permitted provided that the following
00009 //conditions are met:
00010 //
00011 //    * No commercial use is allowed. 
00012 //    This software can only be used
00013 //    for non-commercial purposes. This 
00014 //    distribution is mainly intended for
00015 //    academic research and teaching.
00016 //    * Redistributions of source code must
00017 //    retain the above copyright notice, this
00018 //    list of conditions and the following
00019 //    disclaimer.
00020 //    * Redistributions of binary form must
00021 //    mention the above copyright notice, this
00022 //    list of conditions and the following
00023 //    disclaimer in a clearly visible part 
00024 //    in associated product manual, 
00025 //    readme, and web site of the redistributed 
00026 //    software.
00027 //    * Redistributions in binary form must
00028 //    reproduce the above copyright notice,
00029 //    this list of conditions and the
00030 //    following disclaimer in the
00031 //    documentation and/or other materials
00032 //    provided with the distribution.
00033 //    * The name of Baris Sumengen may not be
00034 //    used to endorse or promote products
00035 //    derived from this software without
00036 //    specific prior written permission.
00037 //
00038 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
00039 //HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00040 //EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
00041 //NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00042 //MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00043 //PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00044 //CONTRIBUTORS BE LIABLE FOR ANY
00045 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00046 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00047 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00048 //OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00049 //DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00050 //HOWEVER CAUSED AND ON ANY THEORY OF
00051 //LIABILITY, WHETHER IN CONTRACT, STRICT
00052 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00053 //OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00054 //OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00055 //POSSIBILITY OF SUCH DAMAGE.
00056 
00057 
00058 
00059 #include "./Core.h"
00060 
00061 
00062 namespace MathCore
00063 {
00064         void Ind2Sub(int rows, int cols, Vector<int>& ind, Vector<int>& I, Vector<int>& J)
00065         {
00066                 I = Vector<int>(ind.Length());
00067                 J = Vector<int>(ind.Length());
00068                 
00069                 int maxElem = rows*cols;
00070 
00071                 for(int i=0; i<ind.Length(); i++)
00072                 {
00073                         int elem = ind.ElemNC(i);
00074                         if(elem>=maxElem)
00075                         {
00076                                 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00077                                 Utility::RunTimeError("Index is larger than the size of the matrix!");
00078                         }
00079                         I.ElemNC(i) = elem % rows;
00080                         J.ElemNC(i) = elem / rows;
00081                 }
00082         }
00083         
00084         Vector<int> Sub2Ind(int rows, int cols, Vector<int>& I, Vector<int>& J)
00085         {
00086                 if(I.Length() != J.Length())
00087                 {
00088                         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00089                         Utility::RunTimeError("Index lengths are not the same!");
00090                 }
00091                 
00092                 Vector<int> temp(I.Length());
00093                 
00094                 for(int i=0; i<I.Length(); i++)
00095                 {
00096                         if(I.ElemNC(i)>=rows || J.ElemNC(i)>=cols)
00097                         {
00098                                 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00099                                 Utility::RunTimeError("Index is larger than the dimensions of the matrix!");
00100                         }
00101                         temp.ElemNC(i) = I.ElemNC(i) + rows*J.ElemNC(i);
00102                 }
00103                 
00104                 return temp;
00105         }
00106 
00107 
00108         
00109         Vector<double> LinSpace(double x, double y, int N)
00110         {
00111                 double step = (y-x)/(N-1);
00112                 Vector<double> temp(N);
00113                 temp.ElemNC(0) = x;
00114                 for(int i=1; i<N-1; i++)
00115                 {
00116                         temp.ElemNC(i) = temp.ElemNC(i-1) + step;
00117                 }
00118                 temp.ElemNC(N-1) = y;
00119                 return temp;
00120         }
00121         
00122         Vector<double> LinSpace(double x, double y)
00123         {
00124                 return LinSpace(x,y,100);
00125         }
00126 
00127 
00128         Vector<double> LogSpace(double x, double y, int N)
00129         {
00130                 double step = (y-x)/(N-1);
00131                 Vector<double> temp(N);
00132                 temp.ElemNC(0) = pow(10.0,x);
00133                 for(int i=1; i<N-1; i++)
00134                 {
00135                         temp.ElemNC(i) = pow(10.0, x+i*step);
00136                 }
00137                 temp.ElemNC(N-1) = pow(10.0,y);
00138                 return temp;
00139         }
00140         Vector<double> LogSpace(double x, double y)
00141         {
00142                 return LogSpace(x,y,50);
00143         }
00144 
00145         
00146         Matrix<float>& RandN(Matrix<float>& m)
00147         {
00148                 if(!RandomGen::Initialized())
00149                 {
00150                         RandomGen::Initialize();
00151                 }
00152                 
00153                 int le = m.Length()/2;
00154                 int rem = m.Length() % 2;
00155                 float rsq;
00156                 float v1;
00157                 float v2;
00158                 for(int i=0;i<le;i++)
00159                 {
00160                         do 
00161                         {
00162                                 float r1 = rand()/(float)RAND_MAX;
00163                                 v1 = 2.0f*r1 - 1.0f;
00164                                 float r2 = rand()/(float)RAND_MAX;
00165                                 v2 = 2.0f*r2 - 1.0f;
00166                                 rsq=v1*v1+v2*v2;
00167                         } 
00168                         while (rsq >= 1.0 || rsq == 0.0);
00169                         float fac = (float)sqrt(-2.0*log(rsq)/rsq);
00170                         m.ElemNC(2*i) = v1*fac;
00171                         m.ElemNC(2*i+1) = v2*fac;
00172                 }
00173                 if(rem == 1)
00174                 {
00175                         do 
00176                         {
00177                                 float r1 = rand()/(float)RAND_MAX;
00178                                 v1 = 2.0f*r1 - 1.0f;
00179                                 float r2 = rand()/(float)RAND_MAX;
00180                                 v2 = 2.0f*r2 - 1.0f;
00181                                 rsq=v1*v1+v2*v2;
00182                         } 
00183                         while (rsq >= 1.0 || rsq == 0.0);
00184                         float fac = (float)sqrt(-2.0*log(rsq)/rsq);
00185                         m.ElemNC(m.Length()-1) = v1*fac;
00186                 }
00187                 
00188                 return m;
00189         }
00190         Vector<float>& RandN(Vector<float>& m)
00191         {
00192                 
00193                 if(!RandomGen::Initialized())
00194                 {
00195                         RandomGen::Initialize();
00196                 }
00197                 
00198                 int le = m.Length()/2;
00199                 int rem = m.Length() % 2;
00200                 float rsq;
00201                 float v1;
00202                 float v2;
00203                 for(int i=0;i<le;i++)
00204                 {
00205                         do 
00206                         {
00207                                 float r1 = rand()/(float)RAND_MAX;
00208                                 v1 = 2.0f*r1 - 1.0f;
00209                                 float r2 = rand()/(float)RAND_MAX;
00210                                 v2 = 2.0f*r2 - 1.0f;
00211                                 rsq=v1*v1+v2*v2;
00212                         } 
00213                         while (rsq >= 1.0 || rsq == 0.0);
00214                         float fac = (float)sqrt(-2.0*log(rsq)/rsq);
00215                         m.ElemNC(2*i) = v1*fac;
00216                         m.ElemNC(2*i+1) = v2*fac;
00217                 }
00218                 if(rem == 1)
00219                 {
00220                         do 
00221                         {
00222                                 float r1 = rand()/(float)RAND_MAX;
00223                                 v1 = 2.0f*r1 - 1.0f;
00224                                 float r2 = rand()/(float)RAND_MAX;
00225                                 v2 = 2.0f*r2 - 1.0f;
00226                                 rsq=v1*v1+v2*v2;
00227                         } 
00228                         while (rsq >= 1.0 || rsq == 0.0);
00229                         float fac = (float)sqrt(-2.0*log(rsq)/rsq);
00230                         m.ElemNC(m.Length()-1) = v1*fac;
00231                 }
00232                 return m;
00233         }
00234 
00235 
00236         Matrix<double>& RandN(Matrix<double>& m)
00237         {
00238                 if(!RandomGen::Initialized())
00239                 {
00240                         RandomGen::Initialize();
00241                 }
00242                 
00243                 int le = m.Length()/2;
00244                 int rem = m.Length() % 2;
00245                 double rsq;
00246                 double v1;
00247                 double v2;
00248                 for(int i=0;i<le;i++)
00249                 {
00250                         do 
00251                         {
00252                                 double r1 = rand()/(double)RAND_MAX;
00253                                 v1 = 2.0*r1 - 1.0;
00254                                 double r2 = rand()/(double)RAND_MAX;
00255                                 v2 = 2.0*r2 - 1.0;
00256                                 rsq=v1*v1+v2*v2;
00257                         } 
00258                         while (rsq >= 1.0 || rsq == 0.0);
00259                         double fac=sqrt(-2.0*log(rsq)/rsq);
00260                         m.ElemNC(2*i) = v1*fac;
00261                         m.ElemNC(2*i+1) = v2*fac;
00262                 }
00263                 if(rem == 1)
00264                 {
00265                         do 
00266                         {
00267                                 double r1 = rand()/(double)RAND_MAX;
00268                                 v1 = 2.0*r1 - 1.0;
00269                                 double r2 = rand()/(double)RAND_MAX;
00270                                 v2 = 2.0*r2 - 1.0;
00271                                 rsq=v1*v1+v2*v2;
00272                         } 
00273                         while (rsq >= 1.0 || rsq == 0.0);
00274                         double fac=sqrt(-2.0*log(rsq)/rsq);
00275                         m.ElemNC(m.Length()-1) = v1*fac;
00276                 }
00277                 
00278                 return m;
00279         }
00280         
00281         
00282         Vector<double>& RandN(Vector<double>& m)
00283         {
00284                 
00285                 if(!RandomGen::Initialized())
00286                 {
00287                         RandomGen::Initialize();
00288                 }
00289                 
00290                 int le = m.Length()/2;
00291                 int rem = m.Length() % 2;
00292                 double rsq;
00293                 double v1;
00294                 double v2;
00295                 for(int i=0;i<le;i++)
00296                 {
00297                         do 
00298                         {
00299                                 double r1 = rand()/(double)RAND_MAX;
00300                                 v1 = 2.0*r1 - 1.0;
00301                                 double r2 = rand()/(double)RAND_MAX;
00302                                 v2 = 2.0*r2 - 1.0;
00303                                 rsq=v1*v1+v2*v2;
00304                         } 
00305                         while (rsq >= 1.0 || rsq == 0.0);
00306                         double fac=sqrt(-2.0*log(rsq)/rsq);
00307                         m.ElemNC(2*i) = v1*fac;
00308                         m.ElemNC(2*i+1) = v2*fac;
00309                 }
00310                 if(rem == 1)
00311                 {
00312                         do 
00313                         {
00314                                 double r1 = rand()/(double)RAND_MAX;
00315                                 v1 = 2.0*r1 - 1.0;
00316                                 double r2 = rand()/(double)RAND_MAX;
00317                                 v2 = 2.0*r2 - 1.0;
00318                                 rsq=v1*v1+v2*v2;
00319                         } 
00320                         while (rsq >= 1.0 || rsq == 0.0);
00321                         double fac=sqrt(-2.0*log(rsq)/rsq);
00322                         m.ElemNC(m.Length()-1) = v1*fac;
00323                 }
00324                 return m;
00325         }
00326 
00327 
00328 
00329         
00330         int NextPow2(int n)
00331         {
00332                 double l = log((double)n)/log(2.0);
00333                 return (int)pow(2,(int)ceil(l));
00334         }
00335 
00336 
00337 
00338 
00339         Matrix<ComplexFloat>& Rand(Matrix<ComplexFloat>& m, float max)
00340         {
00341                 if(!RandomGen::Initialized())
00342                 {
00343                         RandomGen::Initialize();
00344                 }
00345                 for(int i=0;i<m.Length();i++)
00346                 {
00347                         m.ElemNC(i) = ComplexFloat( (float)(rand()/(double)RAND_MAX*max), (float)(rand()/(double)RAND_MAX*max) );
00348                 }
00349                 return m;
00350         }
00351         
00352         Vector<ComplexFloat>& Rand(Vector<ComplexFloat>& m, float max)
00353         {
00354                 if(!RandomGen::Initialized())
00355                 {
00356                         RandomGen::Initialize();
00357                 }
00358                 for(int i=0;i<m.Length();i++)
00359                 {
00360                         m.ElemNC(i) = ComplexFloat( (float)(rand()/(double)RAND_MAX*max), (float)(rand()/(double)RAND_MAX*max) );
00361                 }
00362                 return m;
00363         }
00364 
00365 
00366         Matrix<ComplexDouble>& Rand(Matrix<ComplexDouble>& m, double max)
00367         {
00368                 if(!RandomGen::Initialized())
00369                 {
00370                         RandomGen::Initialize();
00371                 }
00372                 for(int i=0;i<m.Length();i++)
00373                 {
00374                         m.ElemNC(i) = ComplexDouble( (double)(rand()/(double)RAND_MAX*max), (double)(rand()/(double)RAND_MAX*max) );
00375                 }
00376                 return m;
00377         }
00378 
00379         Vector<ComplexDouble>& Rand(Vector<ComplexDouble>& m, double max)
00380         {
00381                 if(!RandomGen::Initialized())
00382                 {
00383                         RandomGen::Initialize();
00384                 }
00385                 for(int i=0;i<m.Length();i++)
00386                 {
00387                         m.ElemNC(i) = ComplexDouble( (double)(rand()/(double)RAND_MAX*max), (double)(rand()/(double)RAND_MAX*max) );
00388                 }
00389                 return m;
00390         }
00391 
00392 
00393 
00394 
00395 
00396 
00397 
00398 
00399 
00400 
00401 
00402 
00403 };
00404 
00405 
00406 
00407 
00408 
00409 
00410 
00411 
00412 
00413 
00414 
00415 
00416 
00417 
00418 
00419 
00420 
00421 
00422 
00423 
00424 
00425 
00426 

Generated on Thu Jan 20 08:43:41 2005 for CIMPL by  doxygen 1.3.9.1