Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

linalg.h

Go to the documentation of this file.
00001 /*!
00002   \file   linalg.h
00003   \date   Jan 2005
00004   
00005   \brief  Linear algebra class.
00006     
00007 */
00008 
00009 #ifndef _LINALG_H_
00010 #define _LINALG_H_
00011 
00012 #include "debug.h"
00013 
00014 class matrix {
00015  protected:
00016   int rows;
00017   int columns;
00018   double **A;
00019  public:
00020   matrix(int r = 1, int c = 1) 
00021     {
00022       int i,j;
00023       rows=r; columns=c;
00024       A = new double*[rows];
00025       for (i=0;i<=rows-1;i++)
00026         A[i] = new double[columns];
00027       for (i=0;i<=rows-1;i++)
00028         {
00029           for(j=0;j<=columns-1;j++)
00030             A[i][j]=0;
00031         }
00032     }
00033   matrix(const matrix &B)
00034     {
00035       int i,j;
00036       rows = B.rows;
00037       columns = B.columns;
00038       A = new double*[rows];
00039       for (i=0;i<=rows-1;i++)
00040         A[i] = new double[columns];
00041       for (i=0;i<=rows-1;i++)
00042         {
00043           for(j=0;j<=columns-1;j++)
00044             A[i][j]=B.A[i][j];
00045         }
00046     }
00047   ~matrix() 
00048     {
00049       long i;
00050       for (i=0; i<=rows-1; i++)
00051         delete [] A[i];
00052       delete [] A;
00053     }
00054   matrix &operator=(const matrix &B)
00055     {
00056       long i,j;
00057       for (i=0; i<=rows-1; i++)
00058         delete [] A[i];
00059       delete [] A;
00060       rows = B.rows;
00061       columns = B.columns;
00062       A = new double*[rows];
00063       for (i=0;i<=rows-1;i++)
00064         A[i] = new double[columns];
00065       for (i=0;i<=rows-1;i++)
00066         {
00067           for(j=0;j<=columns-1;j++)
00068             A[i][j] = B.A[i][j];
00069         }
00070       return *this;
00071     }
00072   long *size() {long *dim = new long[2]; dim[0] = rows; dim[1]=columns; return(dim);}
00073   long row() const {return(rows);} 
00074   long column() const {return(columns);} 
00075   void newsize(long r, long c = 1)                        //warning: This deletes the original matrix
00076     { 
00077       long i,j;
00078       for (i=0; i<=rows-1; i++)
00079         delete [] A[i];
00080       delete [] A;
00081       rows = r; columns = c;
00082       A = new double*[rows];
00083       for (i=0;i<=rows-1;i++)
00084         A[i] = new double[columns];
00085       for (i=0;i<=rows-1;i++)
00086         {
00087           for(j=0;j<=columns-1;j++)
00088             A[i][j]=0;
00089         }
00090     }
00091   double get(long i, long j = 0) const {return(A[i][j]);}
00092   void set(double x, long i, long j=0) {A[i][j]=x;}
00093   void add(double x, long i, long j=0) {A[i][j] += x;};
00094   void sub(double x, long i, long j=0) {A[i][j] -= x;};
00095   void mul(double x, long i, long j=0) {A[i][j] *= x;};
00096   //! Maps function f to every element in matrix
00097   /*! 
00098     
00099   \param f real-function
00100   
00101   \return matrix
00102   */
00103   matrix map(double(*f)(double));
00104   matrix getrow(long i) const;
00105   matrix getcolumn(long i) const;
00106   matrix addcolumns();
00107   void setrow(const matrix &B, long i);
00108   void setcolumn(const matrix &B, long i);
00109   long operator==(const matrix &B);
00110   matrix operator*(const double &x);
00111   matrix operator*(const matrix &B);
00112   matrix trans();
00113   matrix operator+(const matrix &B);
00114   matrix operator-(const matrix &B);
00115   matrix inv();
00116   double twonorm();
00117   double onenorm();
00118   //! calculates empirical mean of each column in matrix
00119   /*! 
00120     
00121   
00122   \return vector where entry i contains empirical mean of column i.
00123   */
00124   safevector<double> mean() const; 
00125   //!  calculates empirical variance of each column in matrix
00126   /*! 
00127     
00128   
00129   \return vector where entry i contains empirical variance of column i.
00130   */
00131   safevector<double> var() const;
00132   //!  calculates empirical covariance matrix of matrix
00133   /*! Here we interpret the matrix as follows:
00134     Let n be the number of columns in the matrix, then we interpret the columns
00135     of the matrix as realizations of the stochastic variables X1,...,Xn.
00136     This function calculates the covariance matrix of (X1,...,Xn).
00137   
00138   \return Covariance-matrix of X1,...,Xn (columns in the incoming matrix).
00139   */
00140   matrix covar() const;
00141 
00142   double maxvalue();
00143   const char* row2string(int r);
00144   void print() const;
00145   void stdprint() const;
00146   void swap(long i, long j)
00147     { double *tmp = A[i];
00148     A[i]=A[j];
00149     A[j] = tmp;
00150     }
00151   void swapc(long i, long j)
00152     { 
00153       double tmp;
00154       long k;
00155       for(k=0;k<=rows-1;k++)
00156         { tmp=A[k][i];
00157         A[k][i]= A[k][j];
00158         A[k][j]=tmp;
00159         }
00160     }     
00161 };
00162 
00163 
00164 
00165 //! calculates empirical covariance (columnwise) of two matrices.
00166 /*! 
00167   If matrix 2 is a (column)-vector. Then the covariance between matrix 2 and 
00168   each of the columns in matrix 1 is returned.
00169 
00170 \param X matrix 1
00171 \param Y matrix 2
00172 
00173 \return vector where entry i is the covariance between
00174 */
00175 matrix covar2(matrix const& X, matrix const& Y);
00176 
00177 
00178 
00179 #endif // _LINALG_H_

Generated on Tue Feb 14 16:05:52 2006 for estfunc by doxygen 1.3.6