marMatrix.cpp
Go to the documentation of this file.00001
00002
00004
00005 #include "marMatrix.h"
00006 #include <assert.h>
00007
00009
00011 marMatrix::marMatrix(size_t size1, size_t size2)
00012 :shallowCopy(false)
00013 {
00014 _size1=size1;
00015 _size2=size2;
00016 _data=new double[_size1*_size2];
00017 }
00018
00019 marMatrix::marMatrix(double *data, size_t size1, size_t size2)
00020 :shallowCopy(true)
00021 {
00022 if (size1==0 || size2==0)
00023 {
00024 assert(false);
00025 }
00026 else
00027 {
00028 _size1=size1;
00029 _size2=size2;
00030 _data=data;
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 }
00041 }
00042
00043
00044 marMatrix::marMatrix(const marMatrix &m)
00045 :shallowCopy(false)
00046 {
00047 if (m._size1==0 || m._size2==0)
00048 {
00049 assert(false);
00050 }
00051 else
00052 {
00053 _size1=m._size1;
00054 _size2=m._size2;
00055
00056 size_t iMax=_size1*_size2;
00057 _data=new double[iMax];
00058
00059 int i;
00060 for (i=0;i<iMax;i++)
00061 {
00062 _data[i]=m._data[i];
00063 }
00064 }
00065 }
00066
00068
00070 marMatrix::~marMatrix()
00071 {
00072 if (!shallowCopy)
00073 {
00074 delete _data;
00075 }
00076 else
00077 {
00078 _data=NULL;
00079 }
00080 }
00081
00083
00085
00086
00087 std::ostream& operator<<(std::ostream& os,const marMatrix& m)
00088 {
00089 int i,j;
00090 for (i =0;i<m._size1;i++)
00091 {
00092 for (j =0;j<m._size2;j++)
00093 {
00094 os << " " << m._data[i*m._size2+j];
00095 }
00096 os << "\n";
00097 }
00098 return(os);
00099 }
00100
00101
00102
00103 marMatrix::operator double*() const
00104 {
00105 return(_data);
00106 }
00107
00108
00109
00110 double& marMatrix::operator()(size_t i, size_t j)
00111 {
00112 return(_data[i*_size2+j]);
00113 }
00114
00115 const double& marMatrix::operator()(size_t i, size_t j) const
00116 {
00117 return(_data[i*_size2+j]);
00118 }
00119
00120
00121
00122 marMatrix& marMatrix::operator=(const marMatrix& o)
00123 {
00124 _size1=o._size1;
00125 _size2=o._size2;
00126 if (!(_data==NULL)&&(!shallowCopy))
00127 {
00128 delete _data;
00129 }
00130 shallowCopy=false;
00131 size_t iMax=_size1*_size2;
00132 _data=new double[iMax];
00133
00134 int i;
00135 for (i=0;i<iMax;i++)
00136 {
00137 _data[i]=o._data[i];
00138 }
00139 return (*this);
00140 }
00141
00142 marMatrix& marMatrix::operator=(double o)
00143 {
00144 if (_size1!=0 && _size2!=0)
00145 {
00146 if (!(_data==NULL)&&(!shallowCopy))
00147 {
00148 delete _data;
00149 }
00150 shallowCopy=false;
00151 size_t iMax=_size1*_size2;
00152 _data=new double[iMax];
00153 for (int i=0;i<iMax;i++)
00154 {
00155 _data[i]=o;
00156 }
00157 }
00158 else
00159 {
00160 assert(false);
00161 }
00162 return (*this);
00163 }
00164
00165 marMatrix& marMatrix::operator=(double*o)
00166 {
00167 if (_size1!=0 && _size2!=0)
00168 {
00169 if (!(_data==NULL)&&(!shallowCopy))
00170 {
00171 delete _data;
00172 }
00173 shallowCopy=false;
00174 size_t iMax=_size1*_size2;
00175 _data=new double[iMax];
00176 for (int i=0;i<iMax;i++)
00177 {
00178 _data[i]=o[i];
00179 }
00180 }
00181 else
00182 {
00183 assert(false);
00184 }
00185 return (*this);
00186 }
00187
00188
00189
00190 bool marMatrix::operator==(const marMatrix& o) const
00191 {
00192 bool equal=true;
00193
00194 if(_size1!=o._size1 || _size2!=o._size2)
00195 {
00196 return(false);
00197 }
00198 size_t iMax=_size1*_size2;
00199
00200 for (int i=0;i<iMax && equal;i++)
00201 {
00202 equal=equal && (_data[i]==o._data[i]);
00203 }
00204 return(equal);
00205 }
00206
00207 bool marMatrix::operator!=(const marMatrix& o) const
00208 {
00209 return(!((*this)==o));
00210 }
00211
00212
00213
00214 marMatrix marMatrix::operator+(const marMatrix& o)
00215 {
00216 marMatrix result(*this);
00217
00218 if ((o._size1!=result._size1)
00219 || (result._size1==0)
00220 || (o._size2!=result._size2)
00221 || (result._size2==0))
00222 {
00223 assert(false);
00224 }
00225 else
00226 {
00227 size_t iMax=result._size1*result._size2;
00228 int i;
00229 for (i=0 ; i < iMax ; i++)
00230 {
00231 result._data[i]+=o._data[i];
00232 }
00233 }
00234 return result;
00235 }
00236
00237 marMatrix marMatrix::operator+(double o)
00238 {
00239 marMatrix result(*this);
00240
00241 if ((result._size1==0) || (result._size2==0))
00242 {
00243 assert(false);
00244 }
00245 else
00246 {
00247 size_t iMax=result._size1*result._size2;
00248 int i;
00249 for (i=0;i < iMax;i++)
00250 {
00251 result._data[i]+=o;
00252 }
00253 }
00254 return result;
00255 }
00256
00257 marMatrix marMatrix::operator+(double*o)
00258 {
00259 marMatrix result(*this);
00260
00261 if ((result._size1==0) || (result._size2==0))
00262 {
00263 assert(false);
00264 }
00265 else
00266 {
00267 size_t iMax=result._size1*result._size2;
00268 int i;
00269 for (i=0;i < iMax;i++)
00270 {
00271 result._data[i]+=o[i];
00272 }
00273 }
00274 return result;
00275 }
00276
00277
00278
00279 marMatrix marMatrix::operator-(const marMatrix& o)
00280 {
00281 marMatrix result(*this);
00282
00283 if ((o._size1!=result._size1)
00284 || (result._size1==0)
00285 || (o._size2!=result._size2)
00286 || (result._size2==0))
00287 {
00288 assert(false);
00289 }
00290 else
00291 {
00292 size_t iMax=result._size1*result._size2;
00293 int i;
00294 for (i=0;i < iMax;i++)
00295 {
00296 result._data[i]-=o._data[i];
00297 }
00298 }
00299 return result;
00300 }
00301
00302 marMatrix marMatrix::operator-(double o)
00303 {
00304 marMatrix result(*this);
00305
00306 if ((result._size1==0) || (result._size2==0))
00307 {
00308 assert(false);
00309 }
00310 else
00311 {
00312 size_t iMax=result._size1*result._size2;
00313 int i;
00314 for (i=0;i < iMax;i++)
00315 {
00316 result._data[i]-=o;
00317 }
00318 }
00319 return result;
00320 }
00321
00322 marMatrix marMatrix::operator-(double*o)
00323 {
00324 marMatrix result(*this);
00325
00326 if ((result._size1==0) || (result._size2==0))
00327 {
00328 assert(false);
00329 }
00330 else
00331 {
00332 size_t iMax=result._size1*result._size2;
00333 int i;
00334 for (i=0;i < iMax;i++)
00335 {
00336 result._data[i]-=o[i];
00337 }
00338 }
00339 return result;
00340 }
00341
00342
00343
00344 marMatrix marMatrix::operator*(double o)
00345 {
00346 marMatrix result(*this);
00347
00348 if ((result._size1==0) || (result._size2==0))
00349 {
00350 assert(false);
00351 }
00352 else
00353 {
00354 size_t iMax=result._size1*result._size2;
00355 int i;
00356 for (i=0;i < iMax;i++)
00357 {
00358 result._data[i]*=o;
00359 }
00360 }
00361 return result;
00362 }
00363
00364 marMatrix marMatrix::operator*(const marMatrix &o)
00365 {
00366 size_t mA=_size1;
00367 size_t nA=_size2;
00368 size_t mB=o._size1;
00369 size_t nB=o._size2;
00370 size_t mR=mA;
00371 size_t nR=nB;
00372
00373 marMatrix result(mR,nR);
00374 result=0.0;
00375
00376 int k,i,j;
00377 if (nA==mB)
00378 {
00379 for (k=0;k<nA;k++)
00380 {
00381 for (i=0;i<mR;i++)
00382 {
00383 for (j=0;j<nR;j++)
00384 {
00385 result(i,j)+=(*this)(i,k)*o(k,j);
00386 }
00387 }
00388 }
00389 }
00390 else
00391 {
00392 assert(false);
00393 }
00394 return result;
00395 }
00396
00397 marVector marMatrix::operator*(const marVector &o)
00398 {
00399 marVector result(_size1);
00400 if (o.size()!=_size2)
00401 {
00402 assert(false);
00403 }
00404 else
00405 {
00406 marMatrix resultM(_size1,1);
00407 marMatrix B((double*)o,o.size(),1);
00408 resultM=(*this)*B;
00409 result=(double*)resultM;
00410 }
00411 return result;
00412 }
00413
00415
00417 size_t marMatrix::rows() const
00418 {
00419 return _size1;
00420 }
00421
00422 size_t marMatrix::columns() const
00423 {
00424 return _size2;
00425 }