home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sources / misc / 4254 / bandmat.cxx next >
Encoding:
C/C++ Source or Header  |  1993-01-11  |  10.1 KB  |  380 lines

  1. //$$ bandmat.cxx                     Band matrix definitions
  2.  
  3. // Copyright (C) 1991,2,3: R B Davies
  4.  
  5. #define WANT_MATH                    // include.h will get math fns
  6.  
  7. #include "include.h"
  8.  
  9. #include "newmat.h"
  10. #include "newmatrc.h"
  11.  
  12. //#define REPORT { static ExeCounter ExeCount(__LINE__,4); ++ExeCount; }
  13.  
  14. #define REPORT {}
  15.  
  16. //#define REPORT1 { static ExeCounter ExeCount(__LINE__,4); ExeCount++; }
  17.  
  18. // REPORT1 constructors only - doesn't work in turbo and Borland C++
  19.  
  20. #define REPORT1 {}
  21.  
  22. //#define MONITOR(what,storage,store) \
  23. //   { cout << what << " " << storage << " at " << (long)store << "\n"; }
  24.  
  25. #define MONITOR(what,store,storage) {}
  26.  
  27. BandMatrix::BandMatrix(const BaseMatrix& M)
  28. {
  29.    REPORT1 // CheckConversion(M);
  30.    MatrixConversionCheck mcc;
  31.    GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::BM);
  32.    GetMatrix(gmx); CornerClear();
  33. }
  34.  
  35. void BandMatrix::SetParameters(const GeneralMatrix* gmx)
  36. {
  37.    MatrixBandWidth bw = gmx->BandWidth();
  38.    lower = bw.lower; upper = bw.upper;
  39. }
  40.  
  41. void BandMatrix::ReDimension(int n, int lb, int ub)
  42. {
  43.    REPORT
  44.    Tracer tr("BandMatrix::ReDimension");
  45.    if (lb<0 || ub<0) Throw(ProgramException("Undefined bandwidth"));
  46.    lower = (lb<=n) ? lb : n-1; upper = (ub<=n) ? ub : n-1;
  47.    GeneralMatrix::ReDimension(n,n,n*(lower+1+upper)); CornerClear();
  48. }
  49.  
  50. void BandMatrix::operator=(const BaseMatrix& X)
  51. {
  52.    REPORT // CheckConversion(X);
  53.    MatrixConversionCheck mcc;
  54.    Eq(X,MatrixType::BM); CornerClear();
  55. }
  56.  
  57. void BandMatrix::CornerClear() const
  58. {
  59.    // set unused parts of BandMatrix to zero
  60.    REPORT
  61.    int i = lower; Real* s = store; int bw = lower + 1 + upper;
  62.    while (i)
  63.       { int j = i--; Real* sj = s; s += bw; while (j--) *sj++ = 0.0; }
  64.    i = upper; s = store + storage;
  65.    while (i)
  66.       { int j = i--; Real* sj = s; s -= bw; while (j--) *(--sj) = 0.0; }
  67. }
  68.  
  69. MatrixBandWidth MatrixBandWidth::operator+(const MatrixBandWidth& bw) const
  70. {
  71.    int l = bw.lower; int u = bw.upper;
  72.    l = (lower < 0 || l < 0) ? -1 : (lower > l) ? lower : l;
  73.    u = (upper < 0 || u < 0) ? -1 : (upper > u) ? upper : u;
  74.    return MatrixBandWidth(l,u);
  75. }
  76.  
  77. MatrixBandWidth MatrixBandWidth::operator*(const MatrixBandWidth& bw) const
  78. {
  79.    int l = bw.lower; int u = bw.upper;
  80.    l = (lower < 0 || l < 0) ? -1 : lower+l;
  81.    u = (upper < 0 || u < 0) ? -1 : upper+u;
  82.    return MatrixBandWidth(l,u);
  83. }
  84.  
  85. UpperBandMatrix::UpperBandMatrix(const BaseMatrix& M)
  86. {
  87.    REPORT1 // CheckConversion(M);
  88.    MatrixConversionCheck mcc;
  89.    GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::UB);
  90.    GetMatrix(gmx); CornerClear();
  91. }
  92.  
  93. void UpperBandMatrix::operator=(const BaseMatrix& X)
  94. {
  95.    REPORT // CheckConversion(X);
  96.    MatrixConversionCheck mcc;
  97.    Eq(X,MatrixType::UB); CornerClear();
  98. }
  99.  
  100. LowerBandMatrix::LowerBandMatrix(const BaseMatrix& M)
  101. {
  102.    REPORT1 // CheckConversion(M);
  103.    MatrixConversionCheck mcc;
  104.    GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::LB);
  105.    GetMatrix(gmx); CornerClear();
  106. }
  107.  
  108. void LowerBandMatrix::operator=(const BaseMatrix& X)
  109. {
  110.    REPORT // CheckConversion(X);
  111.    MatrixConversionCheck mcc;
  112.    Eq(X,MatrixType::LB); CornerClear();
  113. }
  114.  
  115. BandLUMatrix::BandLUMatrix(const BaseMatrix& m)
  116. {
  117.    REPORT1
  118.    Tracer tr("BandLUMatrix");
  119.    GeneralMatrix* gm = ((BaseMatrix&)m).Evaluate(MatrixType::BM);
  120.    GetMatrix(gm);
  121.    m1 = ((BandMatrix*)gm)->lower; m2 = ((BandMatrix*)gm)->upper;
  122.    if (nrows!=ncols) Throw(NotSquareException(*this));
  123.    d = TRUE; sing = FALSE;
  124.    indx = new int [nrows]; MatrixErrorNoSpace(indx);
  125.    MONITOR_INT_NEW("Index (BndLUMat)",nrows,indx)
  126.    storage2 = nrows * m1;
  127.    store2 = new Real [storage2]; MatrixErrorNoSpace(store2);
  128.    MONITOR_REAL_NEW("Make (BandLUMat)",storage2,store2)
  129.    ludcmp();
  130. }
  131.  
  132. BandLUMatrix::~BandLUMatrix()
  133. {
  134.    MONITOR_INT_DELETE("Index (BndLUMat)",nrows,indx)
  135.    MONITOR_REAL_DELETE("Delete (BndLUMt)",storage2,store2)
  136. #ifdef Version21
  137.    delete [] indx; delete [] store2;
  138. #else
  139.    delete [nrows] indx; delete [storage2] store2;
  140. #endif
  141. }
  142.  
  143. MatrixType BandLUMatrix::Type() const { return MatrixType::BC; }
  144.  
  145.  
  146. LogAndSign BandLUMatrix::LogDeterminant() const
  147. {
  148.    if (sing) return 0.0;
  149.    Real* a = store; int w = m1+1+m2; LogAndSign sum; int i = nrows;
  150.    while (i--) { sum *= *a; a += w; }
  151.    if (!d) sum.ChangeSign(); return sum;
  152. }
  153.  
  154. GeneralMatrix* BandMatrix::MakeSolver()
  155. {
  156.    REPORT
  157.    GeneralMatrix* gm = new BandLUMatrix(*this);
  158.    MatrixErrorNoSpace(gm); gm->ReleaseAndDelete(); return gm;
  159. }
  160.  
  161.  
  162. void BandLUMatrix::ludcmp()
  163. {
  164.    REPORT
  165.    Real* a = store;
  166.    int i = m1; int j = m2; int k; int n = nrows; int w = m1 + 1 + m2;
  167.    while (i)
  168.    {
  169.       Real* ai = a + i;
  170.       k = ++j; while (k--) *a++ = *ai++;
  171.       k = i--; while (k--) *a++ = 0.0;
  172.    }
  173.  
  174.    a = store; int l = m1;
  175.    for (k=0; k<n; k++)
  176.    {
  177.       Real x = *a; i = k; Real* aj = a;
  178.       if (l < n) l++;
  179.       for (j=k+1; j<l; j++)
  180.          { aj += w; if (fabs(x) < fabs(*aj)) { x = *aj; i = j; } }
  181.       indx[k] = i;
  182.       if (x==0) { sing = TRUE; return; }
  183.       if (i!=k)
  184.       {
  185.          d = !d; Real* ak = a; Real* ai = store + i * w; j = w;
  186.          while (j--) { x = *ak; *ak++ = *ai; *ai++ = x; }
  187.       }
  188.       aj = a + w; Real* m = store2 + m1 * k;
  189.       for (j=k+1; j<l; j++)
  190.       {
  191.          *m++ = x = *aj / *a; i = w; Real* ak = a;
  192.      while (--i) { Real* aj1 = aj++; *aj1 = *aj - x * *(++ak); }
  193.          *aj++ = 0.0;
  194.       }
  195.       a += w;
  196.    }
  197. }
  198.  
  199. void BandLUMatrix::lubksb(Real* B, int mini)
  200. {
  201.    REPORT
  202.    Tracer tr("BandLUMatrix::lubksb");
  203.    if (sing) Throw(SingularException(*this));
  204.    int n = nrows; int l = m1; int w = m1 + 1 + m2;
  205.  
  206.    for (int k=0; k<n; k++)
  207.    {
  208.       int i = indx[k];
  209.       if (i!=k) { Real x=B[k]; B[k]=B[i]; B[i]=x; }
  210.       if (l<n) l++;
  211.       Real* m = store2 + k*m1; Real* b = B+k; Real* bi = b;
  212.       for (i=k+1; i<l; i++)  *(++bi) -= *m++ * *b;
  213.    }
  214.  
  215.    l = -m1;
  216.    for (int i = n-1; i>=mini; i--)
  217.    {
  218.       Real* b = B + i; Real* bk = b; Real x = *bk;
  219.       Real* a = store + w*i; Real y = *a;
  220.       int k = l+m1; while (k--) x -=  *(++a) * *(++bk);
  221.       *b = x / y;
  222.       if (l < m2) l++;
  223.    }
  224. }
  225.  
  226. void BandLUMatrix::Solver(MatrixRowCol& mcout, const MatrixRowCol& mcin)
  227. {
  228.    REPORT
  229.    Real* el = mcin.store; int i = mcin.skip;
  230.    while (i--) *el++ = 0.0;
  231.    el += mcin.storage; i = nrows - mcin.skip - mcin.storage;
  232.    while (i--) *el++ = 0.0;
  233.    lubksb(mcin.store, mcout.skip);
  234. }
  235.  
  236. // Do we need check for entirely zero output?
  237.  
  238.  
  239. void UpperBandMatrix::Solver(MatrixRowCol& mcout,
  240.    const MatrixRowCol& mcin)
  241. {
  242.    REPORT
  243.    Real* elx = mcin.store+mcout.skip; int i = mcin.skip-mcout.skip;
  244.    while (i-- > 0) *elx++ = 0.0;
  245.    int nr = mcin.skip+mcin.storage; elx = mcin.store+nr; Real* el = elx;
  246.    int j = mcout.skip+mcout.storage-nr; i = nr-mcout.skip;
  247.    while (j-- > 0) *elx++ = 0.0;
  248.  
  249.    Real* Ael = store + (upper+1)*(i-1)+1; j = 0;
  250.    while (i-- > 0)
  251.    {
  252.       elx = el; Real sum = 0.0; int jx = j;
  253.       while (jx--) sum += *(--Ael) * *(--elx);
  254.       elx--; *elx = (*elx - sum) / *(--Ael);
  255.       if (j<upper) Ael -= upper - (++j); else el--;
  256.    }
  257. }
  258.  
  259. void LowerBandMatrix::Solver(MatrixRowCol& mcout,
  260.    const MatrixRowCol& mcin)
  261. {
  262.    REPORT
  263.    Real* elx = mcin.store+mcout.skip; int i = mcin.skip-mcout.skip;
  264.    while (i-- > 0) *elx++ = 0.0;
  265.    int nc = mcin.skip; i = nc+mcin.storage; elx = mcin.store+i;
  266.    int nr = mcout.skip+mcout.storage; int j = nr-i; i = nr-nc;
  267.    while (j-- > 0) *elx++ = 0.0;
  268.  
  269.    Real* el = mcin.store+nc; Real* Ael = store + (lower+1)*nc + lower; j = 0;
  270.    while (i-- > 0)
  271.    {
  272.       elx = el; Real sum = 0.0; int jx = j;
  273.       while (jx--) sum += *Ael++ * *elx++;
  274.       *elx = (*elx - sum) / *Ael++;
  275.       if (j<lower) Ael += lower - (++j); else el++;
  276.    }
  277. }
  278.  
  279.  
  280. LogAndSign BandMatrix::LogDeterminant() const
  281. {
  282.    REPORT
  283.    BandLUMatrix C(*this); return C.LogDeterminant();
  284. }
  285.  
  286. LogAndSign LowerBandMatrix::LogDeterminant() const
  287. {
  288.    REPORT
  289.    int i = nrows; LogAndSign sum; Real* s = store + lower; int j = lower + 1;
  290.    while (i--) { sum *= *s; s += j; }
  291.    ((GeneralMatrix&)*this).tDelete(); return sum;
  292. }
  293.  
  294. LogAndSign UpperBandMatrix::LogDeterminant() const
  295. {
  296.    REPORT
  297.    int i = nrows; LogAndSign sum; Real* s = store; int j = upper + 1;
  298.    while (i--) { sum *= *s; s += j; }
  299.    ((GeneralMatrix&)*this).tDelete(); return sum;
  300. }
  301.  
  302. GeneralMatrix* SymmetricBandMatrix::MakeSolver()
  303. {
  304.    REPORT
  305.    GeneralMatrix* gm = new BandLUMatrix(*this);
  306.    MatrixErrorNoSpace(gm); gm->ReleaseAndDelete(); return gm;
  307. }
  308.  
  309. SymmetricBandMatrix::SymmetricBandMatrix(const BaseMatrix& M)
  310. {
  311.    REPORT1  // CheckConversion(M);
  312.    MatrixConversionCheck mcc;
  313.    GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::SB);
  314.    GetMatrix(gmx);
  315. }
  316.  
  317. GeneralMatrix* SymmetricBandMatrix::Transpose(TransposedMatrix*, MatrixType mt)
  318. { REPORT  return Evaluate(mt); }
  319.  
  320. LogAndSign SymmetricBandMatrix::LogDeterminant() const
  321. {
  322.    REPORT
  323.    BandLUMatrix C(*this); return C.LogDeterminant();
  324. }
  325.  
  326. void SymmetricBandMatrix::SetParameters(const GeneralMatrix* gmx)
  327. { lower = gmx->BandWidth().lower; }
  328.  
  329. void SymmetricBandMatrix::ReDimension(int n, int lb)
  330. {
  331.    REPORT
  332.    Tracer tr("SymmetricBandMatrix::ReDimension");
  333.    if (lb<0) Throw(ProgramException("Undefined bandwidth"));
  334.    lower = (lb<=n) ? lb : n-1;
  335.    GeneralMatrix::ReDimension(n,n,n*(lower+1));
  336. }
  337.  
  338. void SymmetricBandMatrix::operator=(const BaseMatrix& X)
  339. {
  340.    REPORT // CheckConversion(X);
  341.    MatrixConversionCheck mcc;
  342.    Eq(X,MatrixType::SB);
  343. }
  344.  
  345. void SymmetricBandMatrix::CornerClear() const
  346. {
  347.    // set unused parts of BandMatrix to zero
  348.    REPORT
  349.    int i = lower; Real* s = store; int bw = lower + 1;
  350.    while (i)
  351.       { int j = i--; Real* sj = s; s += bw; while (j--) *sj++ = 0.0; }
  352. }
  353.  
  354. MatrixBandWidth SymmetricBandMatrix::BandWidth() const
  355.    { return MatrixBandWidth(lower,lower); }
  356.  
  357. inline Real square(Real x) { return x*x; }
  358.  
  359.  
  360. Real SymmetricBandMatrix::SumSquare() const
  361. {
  362.    REPORT
  363.    CornerClear();
  364.    Real sum1=0.0; Real sum2=0.0; Real* s=store; int i=nrows; int l=lower;
  365.    while (i--)
  366.       { int j = l; while (j--) sum2 += square(*s++); sum1 += square(*s++); }
  367.    ((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2;
  368. }
  369.  
  370. Real SymmetricBandMatrix::SumAbsoluteValue() const
  371. {
  372.    REPORT
  373.    CornerClear();
  374.    Real sum1=0.0; Real sum2=0.0; Real* s=store; int i=nrows; int l=lower;
  375.    while (i--)
  376.       { int j = l; while (j--) sum2 += fabs(*s++); sum1 += fabs(*s++); }
  377.    ((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2;
  378. }
  379.  
  380.