Changeset 508 in Sophya for trunk/SophyaLib/NTools/matrix.cc


Ignore:
Timestamp:
Oct 25, 1999, 12:36:22 PM (26 years ago)
Author:
ansari
Message:

Vector/Matrix OVector/OMatrix cmv 25/10/99

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/NTools/matrix.cc

    r490 r508  
    1 // $Id: matrix.cc,v 1.5 1999-10-21 15:25:49 ansari Exp $
     1// $Id: matrix.cc,v 1.6 1999-10-25 10:36:09 ansari Exp $
    22
    33#include "machdefs.h"
     
    2626
    2727//++
    28 // Class        Matrix
     28// Class        OMatrix
    2929// Lib  Outils++
    3030// include      matrix.h
     
    3838
    3939//++
    40 Matrix::Matrix()
     40OMatrix::OMatrix()
    4141//
    4242//      Construit une matrice 1x1 (pour ppersist).
     
    5050
    5151//++
    52 Matrix::Matrix(int r, int c)
     52OMatrix::OMatrix(int r, int c)
    5353//
    5454//      Construit une matrice de r lignes et c colonnes.
     
    6262
    6363//++
    64 Matrix::Matrix(int r, int c, double* values)
     64OMatrix::OMatrix(int r, int c, double* values)
    6565//
    6666//      Construit une matrice de r lignes et c colonnes. On fournit
     
    7373
    7474//++
    75 Matrix::Matrix(const Matrix& a)
     75OMatrix::OMatrix(const OMatrix& a)
    7676//
    7777//      Constructeur par copie.
     
    8585
    8686//++
    87 Matrix::Matrix(const TMatrix<r_8>& a)
     87OMatrix::OMatrix(const TMatrix<r_8>& a)
    8888//
    8989//      Constructeur par copie a partir d'une TMatrix<r_8>.
     
    9999
    100100
    101 Matrix::~Matrix()
     101OMatrix::~OMatrix()
    102102{
    103103  DBASSERT(ndata == nr*nc);
     
    111111
    112112//++
    113 void Matrix::Zero()
     113void OMatrix::Zero()
    114114//
    115115//      Remise à zero de tous les éléments
     
    121121
    122122//++
    123 void Matrix::Realloc(int r, int c, bool force)
     123void OMatrix::Realloc(int r, int c, bool force)
    124124//
    125125//      Change la taille de la matrice. Réallocation physique seulement si
     
    145145
    146146//++
    147 Matrix& Matrix::operator = (const Matrix& a)
     147OMatrix& OMatrix::operator = (const OMatrix& a)
    148148//
    149149//      Opérateur d'affectation.
     
    158158
    159159//++
    160 Matrix& Matrix::operator = (double x)
     160OMatrix& OMatrix::operator = (double x)
    161161//
    162162//      Opérateur d'affectation depuis scalaire : identité * scalaire.
     
    176176
    177177//++
    178 ostream& operator << (ostream& s, const Matrix& a)
     178ostream& operator << (ostream& s, const OMatrix& a)
    179179//
    180180//      Impression
     
    194194
    195195//++
    196 Matrix& Matrix::operator *= (double b)
     196OMatrix& OMatrix::operator *= (double b)
    197197//
    198198//--
     
    208208
    209209//++
    210 Matrix& Matrix::operator += (double b)
     210OMatrix& OMatrix::operator += (double b)
    211211//
    212212//--
     
    222222
    223223//++
    224 Matrix& Matrix::operator -= (double b)
     224OMatrix& OMatrix::operator -= (double b)
    225225//
    226226//--
     
    236236
    237237//++
    238 Matrix& Matrix::operator /= (double b)
     238OMatrix& OMatrix::operator /= (double b)
    239239//
    240240//--
     
    251251
    252252//++
    253 Matrix operator * (const Matrix& a, double b)
    254 //
    255 //--
    256 {
    257   Matrix result(a);
     253OMatrix operator * (const OMatrix& a, double b)
     254//
     255//--
     256{
     257  OMatrix result(a);
    258258  return (result *= b);
    259259}
    260260
    261261//++
    262 Matrix operator * (double b, const Matrix& a)
    263 //
    264 //--
    265 {
    266   Matrix result(a);
     262OMatrix operator * (double b, const OMatrix& a)
     263//
     264//--
     265{
     266  OMatrix result(a);
    267267  return (result *= b);
    268268}
    269269
    270270//++
    271 Matrix operator + (const Matrix& a, double b)
    272 //
    273 //--
    274 {
    275   Matrix result(a);
     271OMatrix operator + (const OMatrix& a, double b)
     272//
     273//--
     274{
     275  OMatrix result(a);
    276276  return (result += b);
    277277}
    278278
    279279//++
    280 Matrix operator + (double b, const Matrix& a)
    281 //
    282 //--
    283 {
    284   Matrix result(a);
     280OMatrix operator + (double b, const OMatrix& a)
     281//
     282//--
     283{
     284  OMatrix result(a);
    285285  return (result += b);
    286286}
    287287
    288288//++
    289 Matrix operator - (const Matrix& a, double b)
    290 //
    291 //--
    292 {
    293   Matrix result(a);
     289OMatrix operator - (const OMatrix& a, double b)
     290//
     291//--
     292{
     293  OMatrix result(a);
    294294  return (result -= b);
    295295}
    296296
    297297//++
    298 Matrix operator - (double b, const Matrix& a)
    299 //
    300 //--
    301 {
    302   Matrix result(a);
     298OMatrix operator - (double b, const OMatrix& a)
     299//
     300//--
     301{
     302  OMatrix result(a);
    303303  result *= -1;
    304304  return (result += b);
     
    306306
    307307//++
    308 Matrix operator / (const Matrix& a, double b)
    309 //
    310 //--
    311 {
    312   Matrix result(a);
     308OMatrix operator / (const OMatrix& a, double b)
     309//
     310//--
     311{
     312  OMatrix result(a);
    313313  return (result /= b);
    314314}
    315315
    316316//++
    317 Matrix operator * (const Matrix& a, int b)
    318 //
    319 //--
    320 {
    321   Matrix result(a);
     317OMatrix operator * (const OMatrix& a, int b)
     318//
     319//--
     320{
     321  OMatrix result(a);
    322322  return (result *= b);
    323323}
    324324
    325325//++
    326 Matrix operator * (int b, const Matrix& a)
    327 //
    328 //--
    329 {
    330   Matrix result(a);
     326OMatrix operator * (int b, const OMatrix& a)
     327//
     328//--
     329{
     330  OMatrix result(a);
    331331  return (result *= b);
    332332}
    333333
    334334//++
    335 Matrix operator + (const Matrix& a, int b)
    336 //
    337 //--
    338 {
    339   Matrix result(a);
     335OMatrix operator + (const OMatrix& a, int b)
     336//
     337//--
     338{
     339  OMatrix result(a);
    340340  return (result += b);
    341341}
    342342
    343343//++
    344 Matrix operator + (int b, const Matrix& a)
    345 //
    346 //--
    347 {
    348   Matrix result(a);
     344OMatrix operator + (int b, const OMatrix& a)
     345//
     346//--
     347{
     348  OMatrix result(a);
    349349  return (result += b);
    350350}
    351351
    352352//++
    353 Matrix operator - (const Matrix& a, int b)
    354 //
    355 //--
    356 {
    357   Matrix result(a);
     353OMatrix operator - (const OMatrix& a, int b)
     354//
     355//--
     356{
     357  OMatrix result(a);
    358358  return (result -= b);
    359359}
    360360
    361361//++
    362 Matrix operator - (int b, const Matrix& a)
    363 //
    364 //--
    365 {
    366   Matrix result(a);
     362OMatrix operator - (int b, const OMatrix& a)
     363//
     364//--
     365{
     366  OMatrix result(a);
    367367  result *= -1;
    368368  return (result += b);
     
    370370
    371371//++
    372 Matrix operator / (const Matrix& a, int b)
    373 //
    374 //--
    375 {
    376   Matrix result(a);
     372OMatrix operator / (const OMatrix& a, int b)
     373//
     374//--
     375{
     376  OMatrix result(a);
    377377  return (result /= b);
    378378}
     
    382382
    383383//++
    384 Matrix& Matrix::operator += (const Matrix& a)
     384OMatrix& OMatrix::operator += (const OMatrix& a)
    385385//
    386386//--
     
    399399
    400400//++
    401 Matrix& Matrix::operator -= (const Matrix& a)
     401OMatrix& OMatrix::operator -= (const OMatrix& a)
    402402//
    403403//--
     
    417417
    418418//++
    419 Matrix& Matrix::operator *= (const Matrix& a)
     419OMatrix& OMatrix::operator *= (const OMatrix& a)
    420420//
    421421//--
     
    448448
    449449//++
    450 Matrix operator + (const Matrix& a, const Matrix& b)
     450OMatrix operator + (const OMatrix& a, const OMatrix& b)
    451451//
    452452//--
     
    454454  if (b.nc != a.nc || b.nr != a.nr) THROW(sizeMismatchErr);
    455455
    456   Matrix c(a);
     456  OMatrix c(a);
    457457  return (c += b);
    458458}
    459459
    460460//++
    461 Matrix operator - (const Matrix& a, const Matrix& b)
     461OMatrix operator - (const OMatrix& a, const OMatrix& b)
    462462//
    463463//--
     
    465465  if (b.nc != a.nc || b.nr != a.nr) THROW(sizeMismatchErr);
    466466
    467   Matrix c(a);
     467  OMatrix c(a);
    468468  return (c -= b);
    469469}
     
    471471#if 0
    472472
    473 Matrix operator * (const Matrix& a, const Matrix& b)
     473OMatrix operator * (const OMatrix& a, const OMatrix& b)
    474474{
    475475  if (a.nc != b.nr) THROW(sizeMismatchErr);
    476476
    477   Matrix c(a.nr, b.nc);
     477  OMatrix c(a.nr, b.nc);
    478478
    479479  double* pc = c.data;
     
    505505
    506506//++
    507 Matrix operator * (const Matrix& a, const Matrix& b)
     507OMatrix operator * (const OMatrix& a, const OMatrix& b)
    508508//
    509509//--
     
    511511  if (a.nc != b.nr) THROW(sizeMismatchErr);
    512512
    513   Matrix c(a.nr, b.nc);
     513  OMatrix c(a.nr, b.nc);
    514514
    515515  for (int rc = 0; rc < c.nr; rc++)
     
    527527
    528528//++
    529 Matrix Matrix::Transpose() const
     529OMatrix OMatrix::Transpose() const
    530530//
    531531//      Retourne la transposée.
     
    536536{
    537537#if !HAS_NAMED_RETURN
    538 Matrix a(nc,nr);
     538OMatrix a(nc,nr);
    539539#endif
    540540  for (int i=0; i<nr; i++)
     
    548548
    549549//++
    550 Matrix Matrix::Inverse() const
     550OMatrix OMatrix::Inverse() const
    551551//
    552552//      Retourne la matrice inverse.
     
    559559{
    560560#if !HAS_NAMED_RETURN
    561 Matrix b(nc,nr);
    562 #endif
    563   Matrix a(*this);
     561OMatrix b(nc,nr);
     562#endif
     563  OMatrix a(*this);
    564564  b = 1.0;
    565   if (fabs(Matrix::GausPiv(a,b)) < 1.e-50) THROW(singMatxErr);
     565  if (fabs(OMatrix::GausPiv(a,b)) < 1.e-50) THROW(singMatxErr);
    566566#if !HAS_NAMED_RETURN
    567567return b;
     
    570570
    571571
    572 void Matrix::WriteSelf(POutPersist& s) const
     572void OMatrix::WriteSelf(POutPersist& s) const
    573573{
    574574  ASSERT(ndata == nr*nc);
     
    577577}
    578578
    579 void Matrix::ReadSelf(PInPersist& s)
     579void OMatrix::ReadSelf(PInPersist& s)
    580580{
    581581  int_4 r,c;
     
    587587
    588588
    589 MatrixRC::MatrixRC()
     589OMatrixRC::OMatrixRC()
    590590: matrix(0), data(0), index(0), step(0)
    591591{}
    592592
    593 MatrixRC::MatrixRC(Matrix& m, RCKind rckind, int ind)
     593OMatrixRC::OMatrixRC(OMatrix& m, RCKind rckind, int ind)
    594594: matrix(&m), data(Org(m,rckind,ind)),
    595595  index(ind), step(Step(m,rckind)), kind(rckind)
     
    599599
    600600
    601 int MatrixRC::Next()
     601int OMatrixRC::Next()
    602602{
    603603  if (!matrix) return -1;             // Failure ?
     
    621621
    622622
    623 int MatrixRC::Prev()
     623int OMatrixRC::Prev()
    624624{
    625625  if (!matrix) return -1;             // Failure ?
     
    638638
    639639
    640 int MatrixRC::SetCol(int c)
     640int OMatrixRC::SetCol(int c)
    641641{
    642642  if (!matrix) return -1;             // Failure ?
     
    650650
    651651
    652 int MatrixRC::SetRow(int r)
     652int OMatrixRC::SetRow(int r)
    653653{
    654654  if (!matrix) return -1;             // Failure ?
     
    662662
    663663
    664 int MatrixRC::SetDiag()
     664int OMatrixRC::SetDiag()
    665665{
    666666  if (!matrix) return -1;             // Failure ?
     
    674674
    675675
    676 MatrixRC& MatrixRC::operator = (const MatrixRC& rc)
     676OMatrixRC& OMatrixRC::operator = (const OMatrixRC& rc)
    677677{
    678678  matrix = rc.matrix;
     
    684684}
    685685
    686 //MatrixRC::operator Vector() const
    687 Vector MatrixRC::GetVect() const
     686//OMatrixRC::operator OVector() const
     687OVector OMatrixRC::GetVect() const
    688688#if HAS_NAMED_RETURN
    689689return v(NElts())
     
    691691{
    692692#if !HAS_NAMED_RETURN
    693   Vector v(NElts());
     693  OVector v(NElts());
    694694#endif
    695695  int n = NElts();
     
    702702
    703703
    704 MatrixRC& MatrixRC::operator += (const MatrixRC& rc)
     704OMatrixRC& OMatrixRC::operator += (const OMatrixRC& rc)
    705705{
    706706  int n = NElts();
     
    716716
    717717
    718 MatrixRC& MatrixRC::operator -= (const MatrixRC& rc)
     718OMatrixRC& OMatrixRC::operator -= (const OMatrixRC& rc)
    719719{
    720720  int n = NElts();
     
    729729
    730730
    731 MatrixRC& MatrixRC::operator *= (double x)
     731OMatrixRC& OMatrixRC::operator *= (double x)
    732732{
    733733  int n = NElts();
     
    740740
    741741
    742 MatrixRC& MatrixRC::operator /= (double x)
     742OMatrixRC& OMatrixRC::operator /= (double x)
    743743{
    744744  int n = NElts();
     
    751751
    752752
    753 MatrixRC& MatrixRC::operator -= (double x)
     753OMatrixRC& OMatrixRC::operator -= (double x)
    754754{
    755755  int n = NElts();
     
    762762
    763763
    764 MatrixRC& MatrixRC::operator += (double x)
     764OMatrixRC& OMatrixRC::operator += (double x)
    765765{
    766766  int n = NElts();
     
    774774
    775775
    776 double operator * (const MatrixRC& a, const MatrixRC& b)
     776double operator * (const OMatrixRC& a, const OMatrixRC& b)
    777777{
    778778  int n = a.NElts();
     
    786786
    787787
    788 MatrixRC& MatrixRC::LinComb(double a, double b, const MatrixRC& rc, int first)
     788OMatrixRC& OMatrixRC::LinComb(double a, double b, const OMatrixRC& rc, int first)
    789789{
    790790  int n = NElts();
     
    799799
    800800
    801 MatrixRC& MatrixRC::LinComb(double b, const MatrixRC& rc, int first)
     801OMatrixRC& OMatrixRC::LinComb(double b, const OMatrixRC& rc, int first)
    802802{
    803803  int n = NElts();
     
    813813
    814814
    815 MatrixRC Matrix::Row(int r) const
     815OMatrixRC OMatrix::Row(int r) const
    816816#if HAS_NAMED_RETURN
    817 return rc((Matrix&)*this, matrixRow, r)
     817return rc((OMatrix&)*this, matrixRow, r)
    818818#endif
    819819{
    820820#if !HAS_NAMED_RETURN
    821         MatrixRC rc((Matrix&)*this, matrixRow, r);
     821        OMatrixRC rc((OMatrix&)*this, matrixRow, r);
    822822        return rc;
    823823#endif
     
    825825
    826826
    827 MatrixRC Matrix::Col(int c) const
     827OMatrixRC OMatrix::Col(int c) const
    828828#if HAS_NAMED_RETURN
    829 return rc((Matrix&)*this, matrixCol, c)
     829return rc((OMatrix&)*this, matrixCol, c)
    830830#endif
    831831{
    832832#if !HAS_NAMED_RETURN
    833         MatrixRC rc((Matrix&)*this, matrixCol, c);
     833        OMatrixRC rc((OMatrix&)*this, matrixCol, c);
    834834        return rc;
    835835#endif
     
    837837
    838838
    839 MatrixRC Matrix::Diag() const
     839OMatrixRC OMatrix::Diag() const
    840840#if HAS_NAMED_RETURN
    841 return rc((Matrix&)*this, matrixDiag)
     841return rc((OMatrix&)*this, matrixDiag)
    842842#endif
    843843{
    844844#if !HAS_NAMED_RETURN
    845         MatrixRC rc((Matrix&)*this, matrixDiag);
     845        OMatrixRC rc((OMatrix&)*this, matrixDiag);
    846846        return rc;
    847847#endif
     
    849849
    850850
    851 int MatrixRC::IMaxAbs(int first)
     851int OMatrixRC::IMaxAbs(int first)
    852852{
    853853  int n=NElts();
     
    865865
    866866
    867 void MatrixRC::Swap(MatrixRC& rc1, MatrixRC& rc2)
     867void OMatrixRC::Swap(OMatrixRC& rc1, OMatrixRC& rc2)
    868868{
    869869  int n=rc1.NElts();
     
    877877
    878878
    879 double Matrix::GausPiv(Matrix& a, Matrix& b)
     879double OMatrix::GausPiv(OMatrix& a, OMatrix& b)
    880880{
    881881  int n = a.NRows();
     
    902902    double ld = a.NRows() * log(nrm);
    903903    if (ld <= LN_MINDOUBLE || ld >= LN_MAXDOUBLE) {
    904      // cerr << "Matrix warning, overflow for det" << endl;
     904     // cerr << "OMatrix warning, overflow for det" << endl;
    905905    } else {
    906906      det = exp(ld);
     
    908908  }
    909909
    910   MatrixRC pivRowa(a,matrixRow);
    911   MatrixRC pivRowb(b,matrixRow);
     910  OMatrixRC pivRowa(a,matrixRow);
     911  OMatrixRC pivRowb(b,matrixRow);
    912912
    913913  for (int k=0; k<n-1; k++) {
    914914    int iPiv = a.Col(k).IMaxAbs(k);
    915915    if (iPiv != k) {
    916       MatrixRC aIPiv(a.Row(iPiv));
    917       MatrixRC aK(a.Row(k));
    918       MatrixRC::Swap(aIPiv,aK);
    919       MatrixRC bIPiv(b.Row(iPiv));
    920       MatrixRC bK(b.Row(k));
    921       MatrixRC::Swap(bIPiv,bK);
     916      OMatrixRC aIPiv(a.Row(iPiv));
     917      OMatrixRC aK(a.Row(k));
     918      OMatrixRC::Swap(aIPiv,aK);
     919      OMatrixRC bIPiv(b.Row(iPiv));
     920      OMatrixRC bK(b.Row(k));
     921      OMatrixRC::Swap(bIPiv,bK);
    922922    }
    923923    double pivot = a(k,k);
     
    958958
    959959//++
    960 double Matrix::Norm1()
     960double OMatrix::Norm1()
    961961//
    962962//      Norme 1 : somme des valeurs absolues.
     
    970970
    971971//++
    972 double Matrix::Norm2()
     972double OMatrix::Norm2()
    973973//
    974974//      Norme 2, euclidienne.
     
    983983//////////////////////////////////////////////////////////
    984984//++
    985 Matrix Matrix::FitResidus(GeneralFit& gfit
     985OMatrix OMatrix::FitResidus(GeneralFit& gfit
    986986               ,double xorg,double yorg,double dx,double dy)
    987987//
     
    992992{
    993993if(NCol()<=0||NRows()<=0)
    994   throw(SzMismatchError("Matrix::FitResidus: size mismatch\n"));
     994  throw(SzMismatchError("OMatrix::FitResidus: size mismatch\n"));
    995995GeneralFunction* f = gfit.GetFunction();
    996996if(f==NULL)
    997   throw(NullPtrError("Matrix::FitResidus: NULL pointer\n"));
    998 Vector par = gfit.GetParm();
    999 Matrix m(*this);
     997  throw(NullPtrError("OMatrix::FitResidus: NULL pointer\n"));
     998OVector par = gfit.GetParm();
     999OMatrix m(*this);
    10001000for(int i=0;i<NRows();i++) for(int j=0;j<NCol();j++) {
    10011001  double x[2] = {xorg+j*dx,yorg+i*dy};
     
    10061006
    10071007//++
    1008 Matrix Matrix::FitFunction(GeneralFit& gfit
     1008OMatrix OMatrix::FitFunction(GeneralFit& gfit
    10091009               ,double xorg,double yorg,double dx,double dy)
    10101010//
     
    10151015{
    10161016if(NCol()<=0||NRows()<=0)
    1017   throw(SzMismatchError("Matrix::FitFunction: size mismatch\n"));
     1017  throw(SzMismatchError("OMatrix::FitFunction: size mismatch\n"));
    10181018GeneralFunction* f = gfit.GetFunction();
    10191019if(f==NULL)
    1020   throw(NullPtrError("Matrix::FitFunction: NULL pointer\n"));
    1021 Vector par = gfit.GetParm();
    1022 Matrix m(*this);
     1020  throw(NullPtrError("OMatrix::FitFunction: NULL pointer\n"));
     1021OVector par = gfit.GetParm();
     1022OMatrix m(*this);
    10231023for(int i=0;i<NRows();i++) for(int j=0;j<NCol();j++) {
    10241024  double x[2] = {xorg+j*dx,yorg+i*dy};
Note: See TracChangeset for help on using the changeset viewer.