Main Page | Namespace List | Class List | File List | Class Members | File Members

DMatrix.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Claudio Attaccalite                             *
00003  *   claudio@freescience.info                                              *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 
00022 #ifndef DMatrix_H
00023 #define DMatrix_H
00024 #if HAVE_CONFIG
00025 #include <config.h>
00026 #endif
00027 
00028 #include <iostream>
00029 #include "DVector.h"
00030 #include "lapack_wrapper.h"
00031 #include "blas_wrapper.h"
00032 
00033 
00034 using namespace std;
00035 
00036 
00043 class DMatrix
00044 {
00045   private:
00050   int n_1;
00051 
00052 
00057   int n_2;
00058 
00063   double* pArray;
00064 
00069   double* tmpArray;
00070 
00074   double temp;
00075 
00079   int one;
00080 
00081 public:
00087   int dim1(){return n_1;}
00088 
00094   int dim2(){return n_2;}
00095 
00101   int size(){return n_1*n_2;}
00102 
00107   double* array(){return pArray;}
00108 
00113  void transpose()
00114  {
00115  if(n_1 !=n_2)
00116  {
00117          cerr << " Transpose only for square matrix ! " << endl;
00118          exit(1);
00119  }
00120  for(int i=0;i<n_1;i++)
00121         for(int j=0;j<i;j++)
00122         {
00123                 temp=pArray[j+i*n_1];
00124                 pArray[j+i*n_1]=pArray[i+j*n_1];
00125                 pArray[i+j*n_1]=temp;
00126         }
00127  }
00128 
00129 
00136   void allocate(int i, int j)
00137     {
00138       if( n_1 != i || n_2 != j )
00139         {
00140           deallocate();
00141           
00142           n_1 = i;
00143           n_2 = j;
00144           
00145           if(n_1 >= 1 && n_2 >= 1)
00146               pArray = new double[n_1*n_2];
00147           else 
00148               pArray = 0;
00149         }
00150     }
00151 
00152 
00157   void deallocate()
00158     {
00159       if(n_2>0 && n_1>0)
00160       {
00161        delete [] pArray;
00162       }
00163        pArray = 0;
00164 
00165       n_1 = 0;
00166       n_2 = 0;
00167     }
00168 
00169 
00174   void operator=(const DMatrix & rhs)
00175     {
00176      int tot=n_1*n_2;
00177 #ifdef DEBUG
00178      if(n_1 != rhs.n_1 || n_2 != rhs.n_2) allocate(rhs.n_1,rhs.n_2);
00179 #endif
00180      
00181          F77_FUNC(dcopy,DCOPY)(&tot,rhs.pArray,&one,pArray,&one);
00182     }
00183 
00184 
00189   void operator=(const double C)
00190     {
00191       for(int i=0; i<n_1*n_2;i++) pArray[i] = C;
00192     }
00193 
00198   void operator*=(double C)
00199     {
00200         int tot=n_1*n_2;
00201         F77_FUNC(dscal,DSCAL)(&tot,&C,pArray,&one);
00202     }
00203 
00208   void operator/=(const double C)
00209     {
00210         int tot=n_1*n_2;
00211         double inv=1.0/C;
00212         F77_FUNC(dscal,DSCAL)(&tot,&inv,pArray,&one);
00213     }
00214 
00215 
00220   DMatrix(){pArray = 0; n_1 = 0; n_2 = 0; one = 1;}
00221 
00222 
00230   DMatrix(int i, int j) {pArray = 0; n_1 = 0; n_2 = 0; one=1; allocate(i,j);}
00231 
00232 
00239   DMatrix( const DMatrix & rhs)
00240     {
00241       n_1 = 0;
00242       n_2 = 0;
00243       pArray = 0;
00244       allocate(rhs.n_1,rhs.n_2);
00245       int tot=n_1*n_2;
00246       one=1;
00247       F77_FUNC(dcopy,DCOPY)(&tot,rhs.pArray,&one,pArray,&one);
00248     }
00249 
00250 
00255   ~DMatrix(){deallocate();}
00256 
00260   double SumAll()
00261   {
00262    double sum=0;
00263    for(int i=0;i<n_1*n_2;i++) sum+=pArray[i];
00264    return sum;
00265   }
00266  
00270   double Trace()
00271   {
00272 #ifdef DEBUG
00273    if(n_1 !=n_2)
00274    {
00275          cerr << " Trace only for Squared DMatrix ! " << endl;
00276          exit(1);
00277    } 
00278 #endif
00279    double sum=0;
00280    for(int i=0;i<n_1;i++) sum+=pArray[i*n_1+i];
00281    return sum;
00282   }
00283   
00287   void setrow(int irow,DVector &vector)
00288   {
00289 #ifdef DEBUG
00290           if(vector.n_1!=n_2 || irow>=n_1)
00291           { 
00292                   cerr << " ERROR! dimension array copying row! " << endl;
00293                   exit(1);
00294           }
00295 #endif
00296    F77_FUNC(dcopy,DCOPY)(&n_2,vector.pArray,&one,pArray+irow,&n_1);
00297   }
00298 
00302   void setrow(int irow,double *vector)
00303   {
00304    F77_FUNC(dcopy,DCOPY)(&n_2,vector,&one,pArray+irow,&n_1);
00305   }
00306 
00310   void setrow(int irow,double *vector,int lda)
00311   {
00312    F77_FUNC(dcopy,DCOPY)(&n_2,vector,&lda,pArray+irow,&n_1);
00313   }
00314   
00318   void setcol(int icol, const DVector & vector)
00319   {
00320 #ifdef DEBUG
00321           if(vector.n_1!=n_1 || icol>=n_2)
00322           { 
00323                   cerr << " ERROR! dimension array copying coloumn! " << endl;
00324                   exit(1);
00325           }
00326 #endif
00327    F77_FUNC(dcopy,DCOPY)(&n_1,vector.pArray,&one,pArray+icol*n_1,&one);
00328   }
00329 
00333   void setcol(int icol,double *vector)
00334   {
00335 #ifdef DEBUG
00336           if(icol>=n_2)
00337           { 
00338                   cerr << " ERROR! dimension array copying coloumn! " << endl;
00339                   exit(1);
00340           }
00341 #endif
00342    F77_FUNC(dcopy,DCOPY)(&n_1,vector,&one,pArray+icol*n_1,&one);
00343   }
00344  
00348   void row_scale(int irow, double C)
00349   {
00350 #ifdef DEBUG
00351           if(irow>=n_1)
00352           { 
00353                   cerr << " ERROR! dimension array in row_scale! " << endl;
00354                   exit(1);
00355           }
00356 #endif
00357    F77_FUNC(dscal,DSCAL)(&n_2,&C,pArray+irow,&n_1);
00358   }
00359 
00360 
00361 
00362   
00366   double rowdot(int irow, const DVector & vector)
00367   {
00368 #ifdef DEBUG
00369           if(vector.n_1!=n_2 || irow>=n_1)
00370           { 
00371                   cerr << " ERROR! dimension array in row dot! " << endl;
00372                   exit(1);
00373           }
00374 #endif
00375    return F77_FUNC(ddot,DDOT)(&n_2,pArray+irow,&n_1,vector.pArray,&one);
00376   }
00377 
00381   double coldot(int icol, const DVector &vector)
00382   {
00383 #ifdef DEBUG
00384           if(vector.n_1!=n_1 || icol>=n_2)
00385           { 
00386                   cerr << " ERROR! dimension array in coloumn dot! " << endl;
00387                   exit(1);
00388           }
00389 #endif
00390    return F77_FUNC(ddot,DDOT)(&n_1,pArray+icol*n_1,&one,vector.pArray,&one);
00391   }
00392   
00396   void col_scale(int icol, double C)
00397   {
00398 #ifdef DEBUG
00399           if(icol>=n_2)
00400           { 
00401                   cerr << " ERROR! dimension array in col_rescale! " << endl;
00402                   exit(1);
00403           }
00404 #endif
00405    F77_FUNC(dscal,DSCAL)(&n_1,&C,pArray+icol*n_1,&one);
00406   }
00407 
00408 
00412   double rowdot(int irow, double *vector)
00413   {
00414    return F77_FUNC(ddot,DDOT)(&n_2,pArray+irow,&n_1,vector,&one);
00415   }
00416 
00420   double coldot(int icol, double *vector)
00421   {
00422    return F77_FUNC(ddot,DDOT)(&n_1,pArray+icol*n_1,&one,vector,&one);
00423   }
00424 
00425 
00426 
00430   double *row_pointer(int irow)
00431   {
00432 #ifdef DEBUG
00433           if(irow>=n_1)
00434           { 
00435                   cerr << " ERROR! dimension array getting a row! " << endl;
00436                   exit(1);
00437           }
00438 #endif
00439    return(pArray+irow);
00440   }
00441   
00445   DVector row(int irow)
00446   {
00447 #ifdef DEBUG
00448           if(irow>=n_1)
00449           { 
00450                   cerr << " ERROR! dimension array getting a row! " << endl;
00451                   exit(1);
00452           }
00453 #endif
00454    DVector TEMP(n_2);
00455    F77_FUNC(dcopy,DCOPY)(&n_2,pArray+irow,&n_1,TEMP.pArray,&one);
00456    return TEMP;
00457   }
00458 
00459 
00460 
00461 
00465   double *col_pointer(int icol)
00466   {
00467 #ifdef DEBUG
00468           if(icol>=n_2)
00469           { 
00470                   cerr << " ERROR! dimension array getting a coloumn! " << endl;
00471                   exit(1);
00472           }
00473 #endif
00474    return(pArray+icol*n_1);
00475   }
00476 
00477 
00478   
00482   DVector col(int icol)
00483   {
00484 #ifdef DEBUG
00485           if(icol>=n_2)
00486           { 
00487                   cerr << " ERROR! dimension array getting a coloumn! " << endl;
00488                   exit(1);
00489           }
00490 #endif
00491    DVector TEMP(n_1);
00492    F77_FUNC(dcopy,DCOPY)(&n_1,pArray+icol*n_1,&one,TEMP.pArray,&one);
00493    return TEMP;
00494   }
00495 
00499   double SumRow(const int irow)
00500   {
00501 #ifdef DEBUG
00502           if(irow>=n_1)
00503           { 
00504                   cerr << " ERROR! dimension array getting a row! " << endl;
00505                   exit(1);
00506           }
00507 #endif
00508    double sum=0;
00509    for(int i=0;i<n_2;i++) sum+=pArray[irow+i*n_1];
00510    return sum;
00511   }
00512 
00513 
00517   double SumCol(const int icol)
00518   {
00519 #ifdef DEBUG
00520           if(icol>=n_2)
00521           { 
00522                   cerr << " ERROR! dimension array getting a row! " << endl;
00523                   exit(1);
00524           }
00525 #endif
00526    double sum=0;
00527    for(int i=0;i<n_1;i++) sum+=pArray[i+icol*n_1];
00528    return sum;
00529   }
00530 
00531 
00532 
00533 
00534 
00539   DMatrix operator+( const DMatrix & rhs)
00540     {
00541 #ifdef DEBUG
00542       if( n_1 != rhs.n_1 || n_2 !=rhs.n_2 )
00543         {
00544           cerr << "ERROR: incorrect sizes for two DMatrix's in DMatrix+"
00545                << "DMatrix" << endl;
00546           exit(1);
00547         }
00548       if( n_1 <= 0 || n_2 <=0 )
00549         {
00550           cerr << "ERROR: DMatrix of size 0 used in DMatrix+DMatrix" << endl;
00551           exit(1);
00552         }
00553 #endif
00554       int tot=n_1*n_2;
00555       double d_one=1.0;
00556       DMatrix  A(n_1,n_2);
00557       F77_FUNC(dcopy,DCOPY)(&tot,pArray,&one,A.pArray,&one);
00558       F77_FUNC(daxpy,DAXPY)(&tot,&d_one,rhs.pArray,&one,A.pArray,&one); 
00559       return A;
00560     }
00561 
00562 
00567   DMatrix operator-( const DMatrix & rhs)
00568     {
00569 #ifdef DEBUG
00570       if( n_1 != rhs.n_1 || n_2 !=rhs.n_2 )
00571         {
00572           cerr << "ERROR: incorrect sizes for two DMatrix's in DMatrix-"
00573                << "DMatrix" << endl;
00574           exit(1);
00575         }
00576       if( n_1 <= 0 || n_2 <=0)
00577         {
00578           cerr << "ERROR: DMatrix of size 0 used in DMatrix-DMatrix" << endl;
00579           exit(1);
00580         }
00581 #endif
00582       DMatrix A(n_1,n_2);
00583       int tot=n_1*n_2;
00584       double d_m_one=-1.0;
00585       F77_FUNC(dcopy,DCOPY)(&tot,pArray,&one,A.pArray,&one);
00586       F77_FUNC(daxpy,DAXPY)(&tot,&d_m_one,rhs.pArray,&one,A.pArray,&one); 
00587       return A;
00588     }
00589 
00590   
00594   double & operator()(int i,int j)
00595   {
00596 #ifdef DEBUG
00597           if(i>=n_1 || j>=n_2)
00598           { 
00599                   cerr << " ERROR! index out of range! " 
00600                        << "("<<i<<","<<j<<")" <<  endl;
00601                   exit(1);
00602           }
00603 #endif
00604           return pArray[i+j*n_1];
00605   }
00606 
00607 
00612   friend ostream& operator<<(ostream & strm, const DMatrix & rhs)
00613     {
00614       for(int i=0; i<rhs.n_1;i++)
00615         {
00616           for(int j=0; j<rhs.n_2; j++)
00617             {
00618               strm << rhs.pArray[i+j*rhs.n_1] << "\t";
00619             }
00620           strm << endl;
00621         }
00622       return strm;
00623     }
00624 
00625 };
00626 
00627 #endif

Generated on Wed Aug 16 19:03:50 2006 for MyLapack by  doxygen 1.4.4