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

matrix.cpp

Go to the documentation of this file.
00001 #include "matrix.hpp"
00002 
00003 /*=============Matrix===============*/
00004 
00005 Matrix::Row::Row(const Matrix* _M, unsigned _i)
00006     : M(_M), i(_i)
00007 { }
00008 
00009 double Matrix::Row::elem(unsigned j) const
00010 {
00011     return M->elem(i, j);
00012 }
00013 
00014 unsigned Matrix::Row::GetSize() const
00015 {
00016     return M->GetNumCols();
00017 }
00018 
00019 Matrix::Row Matrix::GetRow(unsigned i) const
00020 {
00021     return Row(this, i);
00022 }
00023 
00024 
00025 Matrix::Column::Column(const Matrix* _M, unsigned _j)
00026     : M(_M), j(_j)
00027 { }
00028 
00029 double Matrix::Column::elem(unsigned i) const
00030 {
00031     return M->elem(i, j);
00032 }
00033 
00034 unsigned Matrix::Column::GetSize() const
00035 {
00036     return M->GetNumRows();
00037 }
00038 
00039 Matrix::Column Matrix::GetCol(unsigned j) const
00040 {
00041     return Column(this, j);
00042 }
00043 
00044 
00045 /*=============MatrixBinExpr===============*/
00046 
00047 MatrixBinExpr::MatrixBinExpr(const Matrix& _M1, const Matrix& _M2)
00048     : M1(_M1), M2(_M2)
00049 { 
00050 #if HANDLEXCEPTION
00051     if((M1.GetNumRows() != M2.GetNumRows()) || 
00052         (M1.GetNumCols() != M2.GetNumCols()))
00053     {
00054         throw "assume M1 and M2 have the same size";
00055     }
00056 #endif
00057 }
00058 
00059 unsigned MatrixBinExpr::GetNumRows() const
00060 {
00061     return M1.GetNumRows();
00062 }
00063 
00064 unsigned MatrixBinExpr::GetNumCols() const
00065 {
00066     return M1.GetNumCols();
00067 }
00068 
00069 const Matrix& MatrixBinExpr::GetFirst() const
00070 {
00071     return M1;
00072 }
00073 
00074 const Matrix& MatrixBinExpr::GetSecond() const
00075 {
00076     return M2;
00077 }
00078 
00079 MatrixSum::MatrixSum(const Matrix& _M1, const Matrix& _M2)
00080     : MatrixBinExpr(_M1, _M2)
00081 { }
00082 
00083 double MatrixSum::elem(unsigned i, unsigned j) const
00084 {
00085 #if HANDLEXCEPTION
00086     if((i >= GetNumRows()) || (j >= GetNumCols()))
00087         throw "assume i < num_rows and j < num_cols";
00088     }
00089 #endif
00090     return M1.elem(i, j) + M2.elem(i, j);
00091 }
00092 
00093 MatrixSub::MatrixSub(const Matrix& _M1, const Matrix& _M2)
00094     : MatrixBinExpr(_M1, _M2)
00095 { }
00096 
00097 double MatrixSub::elem(unsigned i, unsigned j) const
00098 {
00099 #if HANDLEXCEPTION
00100     if((i >= GetNumRows()) || (j >= GetNumCols()))
00101         throw "assume i < num_rows and j < num_cols";
00102     }
00103 #endif
00104     return M1.elem(i, j) - M2.elem(i, j);
00105 }
00106 
00107 
00108 
00109 /*=============ReallocatableMatrix===============*/
00110 
00111 ReallocatableMatrix::ReallocatableMatrix(unsigned rows, unsigned start_cols)
00112     : num_rows(rows), num_cols(start_cols), allocsize(start_cols), mat(0)
00113 {
00114     Resize(allocsize);
00115 }
00116 
00117 void ReallocatableMatrix::Resize(unsigned needsize)
00118 {
00119     unsigned i;
00120     double **tmp;
00121     if(needsize > allocsize) {
00122         allocsize = needsize + ReallocatableMatrix_add_allocsize;
00123         tmp = new double*[allocsize];
00124         for(i = 0; i < num_cols; ++i){
00125             tmp[i] = mat[i];
00126         }
00127         delete [] mat;
00128         mat = tmp;
00129     }
00130     for(i = num_cols; i < needsize; ++i) {
00131         mat[i] = new double[num_rows];
00132     }
00133     num_cols = needsize;
00134 }
00135 
00136 unsigned ReallocatableMatrix::GetNumRows() const
00137 {
00138     return num_rows;
00139 }
00140 
00141 unsigned ReallocatableMatrix::GetNumCols() const
00142 {
00143     return num_cols;
00144 }
00145 
00146 double& ReallocatableMatrix::elem(unsigned i, unsigned j)
00147 {
00148 #if HANDLEXCEPTION
00149     if((i < num_rows) && (j < num_cols))
00150         throw "assume i < num_rows and j < num_cols";
00151     }
00152 #endif
00153     return mat[j][i];
00154 }
00155 
00156 double ReallocatableMatrix::elem(unsigned i, unsigned j) const
00157 {
00158 #if HANDLEXCEPTION
00159     if((i < num_rows) && (j < num_cols))
00160         throw "assume i < num_rows and j < num_cols";
00161     }
00162 #endif
00163     return mat[j][i];
00164 }
00165 
00166 ReallocatableMatrix::~ReallocatableMatrix()
00167 {
00168     unsigned i;
00169     for(i = 0; i < num_cols; ++i) {
00170         delete [] mat[i];
00171     }
00172     delete [] mat;
00173 }
00174 
00175 
00176 /*=============FunctionMatrix===============*/
00177 
00178 
00179 FunctionMatrix::FunctionMatrix(unsigned rows, unsigned cols, double _h,
00180         double (*func)(unsigned, unsigned, double))
00181     : num_rows(rows), num_cols(cols), h(_h), f(func)
00182 { }
00183 
00184 
00185 unsigned FunctionMatrix::GetNumRows() const
00186 {
00187     return num_rows;
00188 }
00189 
00190 unsigned FunctionMatrix::GetNumCols() const
00191 {
00192     return num_cols;
00193 }
00194 
00195 double FunctionMatrix::elem(unsigned i, unsigned j) const
00196 {
00197 #if HANDLEXCEPTION
00198     if((i < num_rows) && (j < num_cols))
00199         throw "assume i < num_rows and j < num_cols";
00200     }
00201 #endif
00202     return f(i, j, h);
00203 }

Generated on Sun May 25 01:58:04 2025 for SmoluchowskiSolver by Doxygen