home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / n / newmat06.zip / NEWMATEX.CXX < prev    next >
C/C++ Source or Header  |  1992-11-30  |  10KB  |  355 lines

  1. //$$ newmatex.cxx                    Exception handler
  2.  
  3. // Copyright (C) 1992: R B Davies
  4.  
  5. #define WANT_STREAM                  // include.h will get stream fns
  6.  
  7. #include "include.h"                 // include standard files
  8. #include "newmat.h"
  9.  
  10.  
  11. // action = -1    print message and exit(1)
  12. //           0    no message if handler available
  13. //           1    print message and use handler
  14.  
  15.  
  16.  
  17. int SpaceException::action = 1;
  18. int DataException::action = 1;
  19. int ConvergenceException::action = 1;
  20. int ProgramException::action = 1;
  21. int InternalException::action = 1;
  22.  
  23.  
  24. static inline iabs(int i) { return i >= 0 ? i : -i; }
  25.  
  26.  
  27. MatrixDetails::MatrixDetails(const GeneralMatrix& A)
  28.    : type(A.Type()), nrows(A.Nrows()), ncols(A.Ncols())
  29. { MatrixBandWidth bw = A.BandWidth(); ubw = bw.upper; lbw = bw.lower; }
  30.  
  31. void MatrixDetails::PrintOut()
  32. {
  33.    cout << "MatrixType = " << (char*)type;
  34.    cout << "  # Rows = " << nrows;
  35.    cout << "; # Cols = " << ncols;
  36.    if (lbw >=0) cout << "; lower BW = " << lbw;
  37.    if (ubw >=0) cout << "; upper BW = " << ubw;
  38.    cout << "\n";
  39. }
  40.  
  41.  
  42.  
  43. SpaceException::SpaceException() : Exception(abs(action))
  44. {
  45.    if (action) cout << "Out of space on heap\n";
  46.    if (action < 0) exit(1);
  47. }
  48.  
  49. MatrixException::MatrixException(int action) : Exception(iabs(action))
  50. { if (action) cout << "The exception is from newmat.\n"; }
  51.  
  52. MatrixException::MatrixException(int action, const GeneralMatrix& A)
  53.    : Exception(iabs(action))
  54. {
  55.    if (action)
  56.    {
  57.       cout << "The exception is from newmat: details of matrix follow:\n";
  58.       MatrixDetails(A).PrintOut();
  59.    }
  60. }
  61.  
  62. MatrixException::MatrixException(int action, const GeneralMatrix& A,
  63.    const GeneralMatrix& B) : Exception(iabs(action))
  64. {
  65.    if (action)
  66.    {
  67.       cout << "The exception is from newmat: details of matrices follow:\n";
  68.       MatrixDetails(A).PrintOut();
  69.       MatrixDetails(B).PrintOut();
  70.    }
  71. }
  72.  
  73. DataException::DataException(const GeneralMatrix& A)
  74.    : MatrixException(action, A) {}
  75.  
  76. NPDException::NPDException(const GeneralMatrix& A)
  77.    : DataException(A)
  78. {
  79.    if (action) cout << "The matrix is not positive definite\n\n";
  80.    if (action < 0) exit(1);
  81. }
  82.  
  83. SingularException::SingularException(const GeneralMatrix& A)
  84.    : DataException(A)
  85. {
  86.    if (action) cout << "The matrix is singular\n\n";
  87.    if (action < 0) exit(1);
  88. }
  89.  
  90. ConvergenceException::ConvergenceException(const GeneralMatrix& A)
  91.    : MatrixException(action,A)
  92. {
  93.    if (action) cout << "Process fails to converge\n\n";
  94.    if (action < 0) exit(1);
  95. }
  96.  
  97. ProgramException::ProgramException(char* c) : MatrixException(action)
  98. {
  99.    if (action) cout << c << "\n\n";
  100.    if (action < 0) exit(1);
  101. }
  102.  
  103. ProgramException::ProgramException(char* c, const GeneralMatrix& A)
  104.    : MatrixException(action,A)
  105. {
  106.    if (action) cout << c << "\n\n";
  107.    if (action < 0) exit(1);
  108. }
  109.  
  110. ProgramException::ProgramException(char* c, const GeneralMatrix& A,
  111.    const GeneralMatrix& B) : MatrixException(action,A,B)
  112. {
  113.    if (action) cout << c << "\n\n";
  114.    if (action < 0) exit(1);
  115. }
  116.  
  117. ProgramException::ProgramException(const GeneralMatrix& A)
  118.    : MatrixException(action, A) {}
  119.  
  120. ProgramException::ProgramException() : MatrixException(action) {}
  121.  
  122. VectorException::VectorException() : ProgramException()
  123. {
  124.    if (action) cout << "Cannot convert matrix to vector\n\n";
  125.    if (action < 0) exit(1);
  126. }
  127.  
  128. VectorException::VectorException(const GeneralMatrix& A)
  129.    : ProgramException(A)
  130. {
  131.    if (action) cout << "Cannot convert matrix to vector\n\n";
  132.    if (action < 0) exit(1);
  133. }
  134.  
  135. NotSquareException::NotSquareException(const GeneralMatrix& A)
  136.    : ProgramException(A)
  137. {
  138.    if (action) cout << "Matrix is not square\n\n";
  139.    if (action < 0) exit(1);
  140. }
  141.  
  142. SubMatrixDimensionException::SubMatrixDimensionException()
  143.    : ProgramException()
  144. {
  145.    if (action) cout << "Incompatible submatrix dimension\n\n";
  146.    if (action < 0) exit(1);
  147. }
  148.  
  149. IncompatibleDimensionsException::IncompatibleDimensionsException()
  150.    : ProgramException()
  151. {
  152.    if (action) cout << "Incompatible dimensions\n\n";
  153.    if (action < 0) exit(1);
  154. }
  155.  
  156. NotDefinedException::NotDefinedException(char* op, char* matrix)
  157.    : ProgramException()
  158. {
  159.    if (action)
  160.       cout << "Operation " << op << " not defined for " << matrix << "\n\n";
  161.    if (action < 0) exit(1);
  162. }
  163.  
  164. CannotBuildException::CannotBuildException(char* matrix)
  165.    : ProgramException()
  166. {
  167.    if (action)
  168.       cout << "Cannot build matrix type " << matrix << "\n\n";
  169.    if (action < 0) exit(1);
  170. }
  171.  
  172. IndexException::IndexException(int i, const GeneralMatrix& A)
  173.    : ProgramException(A)
  174. {
  175.    if (action)
  176.       { cout << "Index error: requested index = " << i << "\n\n"; }
  177.    if (action < 0) exit(1);
  178. }
  179.  
  180. IndexException::IndexException(int i, int j, const GeneralMatrix& A)
  181.    : ProgramException(A)
  182. {
  183.    if (action)
  184.    {
  185.       cout << "Index error: requested indices = " << i << ", " << j << "\n\n";
  186.    }
  187.    if (action < 0) exit(1);
  188. }
  189.  
  190.  
  191. IndexException::IndexException(int i, const GeneralMatrix& A, Boolean)
  192.    : ProgramException(A)
  193. {
  194.    if (action)
  195.       { cout << "Element error: requested index (wrt 0) = " << i << "\n\n"; }
  196.    if (action < 0) exit(1);
  197. }
  198.  
  199. IndexException::IndexException(int i, int j, const GeneralMatrix& A, Boolean)
  200.    : ProgramException(A)
  201. {
  202.    if (action)
  203.    {
  204.       cout << "Element error: requested indices (wrt 0) = " 
  205.          << i << ", " << j << "\n\n";
  206.    }
  207.    if (action < 0) exit(1);
  208. }
  209.  
  210. InternalException::InternalException(char* c) : MatrixException(action)
  211. {
  212.    if (action) cout << c << "\n\n";
  213.    if (action < 0) exit(1);
  214. }
  215.  
  216.  
  217.  
  218.  
  219. /************************* ExeCounter functions *****************************/
  220.  
  221.  
  222.  
  223. int ExeCounter::nreports = 0;
  224.  
  225. ExeCounter::ExeCounter(int xl, int xf) : line(xl), fileid(xf), nexe(0) {}
  226.  
  227. ExeCounter::~ExeCounter()
  228. {
  229.    nreports++;
  230.    cout << nreports << "  " << fileid << "  " << line << "  " << nexe << "\n";
  231. }
  232.  
  233.  
  234.  
  235. /**************************** error handler *******************************/
  236.  
  237. void MatrixErrorNoSpace(void* v) { if (!v) Throw(SpaceException()); }
  238. // throw exception if v is null
  239.  
  240.  
  241.  
  242. /************************* test type manipulation ***************************/
  243.  
  244.  
  245.  
  246.  
  247. // These functions may cause problems for Glockenspiel 2.0c; they are used
  248. // only for testing so you can delete them
  249.  
  250.  
  251. void TestTypeAdd()
  252. {
  253.    MatrixType list[] = { MatrixType::UT,
  254.                          MatrixType::LT,
  255.                          MatrixType::Rt,
  256.                          MatrixType::Sm,
  257.              MatrixType::Dg,
  258.              MatrixType::RV,
  259.              MatrixType::CV,
  260.                          MatrixType::BM,
  261.                          MatrixType::UB,
  262.              MatrixType::LB,
  263.              MatrixType::SB };
  264.  
  265.    cout << "+     ";
  266.    for (int i=0; i<MatrixType::nTypes(); i++) cout << (char*)list[i] << " ";
  267.    cout << "\n";
  268.    for (i=0; i<MatrixType::nTypes(); i++)
  269.    {
  270.       cout << (char*)(list[i]) << " ";
  271.       for (int j=0; j<MatrixType::nTypes(); j++)
  272.      cout << (char*)(list[j]+list[i]) << " ";
  273.       cout << "\n";
  274.    }
  275.    cout << "\n";
  276. }
  277.  
  278. void TestTypeMult()
  279. {
  280.    MatrixType list[] = { MatrixType::UT,
  281.                          MatrixType::LT,
  282.                          MatrixType::Rt,
  283.                          MatrixType::Sm,
  284.                          MatrixType::Dg,
  285.                          MatrixType::RV,
  286.                          MatrixType::CV,
  287.                          MatrixType::BM,
  288.                          MatrixType::UB,
  289.                          MatrixType::LB,
  290.                          MatrixType::SB };
  291.    cout << "*     ";
  292.    for (int i=0; i<MatrixType::nTypes(); i++)
  293.       cout << (char*)list[i] << " ";
  294.    cout << "\n";
  295.    for (i=0; i<MatrixType::nTypes(); i++)
  296.    {
  297.       cout << (char*)list[i] << " ";
  298.       for (int j=0; j<MatrixType::nTypes(); j++)
  299.      cout << (char*)(list[j]*list[i]) << " ";
  300.       cout << "\n";
  301.    }
  302.    cout << "\n";
  303. }
  304.  
  305. void TestTypeOrder()
  306. {
  307.    MatrixType list[] = { MatrixType::UT,
  308.                          MatrixType::LT,
  309.                          MatrixType::Rt,
  310.                          MatrixType::Sm,
  311.                          MatrixType::Dg,
  312.                          MatrixType::RV,
  313.                          MatrixType::CV,
  314.                          MatrixType::BM,
  315.                          MatrixType::UB,
  316.                          MatrixType::LB,
  317.                          MatrixType::SB };
  318.    cout << ">=    ";
  319.    for (int i = 0; i<MatrixType::nTypes(); i++)
  320.       cout << (char*)list[i] << " ";
  321.    cout << "\n";
  322.    for (i=0; i<MatrixType::nTypes(); i++)
  323.    {
  324.       cout << (char*)list[i] << " ";
  325.       for (int j=0; j<MatrixType::nTypes(); j++)
  326.      cout << ((list[j]>=list[i]) ? "Yes   " : "No    ");
  327.       cout << "\n";
  328.    }
  329.    cout << "\n";
  330. }
  331.  
  332.  
  333. /************************* miscellanous errors ***************************/
  334.  
  335.  
  336. void CroutMatrix::GetRow(MatrixRowCol&)
  337.    { Throw(NotDefinedException("GetRow","Crout")); }
  338. void CroutMatrix::GetCol(MatrixRowCol&)
  339.    { Throw(NotDefinedException("GetCol","Crout")); }
  340. void CroutMatrix::operator=(const BaseMatrix&)
  341.    { Throw(NotDefinedException("=","Crout")); }
  342. void BandLUMatrix::GetRow(MatrixRowCol&)
  343.    { Throw(NotDefinedException("GetRow","BandLUMatrix")); }
  344. void BandLUMatrix::GetCol(MatrixRowCol&)
  345.    { Throw(NotDefinedException("GetCol","BandLUMatrix")); }
  346. void BandLUMatrix::operator=(const BaseMatrix&)
  347.    { Throw(NotDefinedException("=","BandLUMatrix")); }
  348. #ifdef TEMPS_DESTROYED_QUICKLY
  349.    ReturnMatrixX::ReturnMatrixX(const ReturnMatrixX& tm)
  350.      : gm(tm.gm) { Throw(ProgramException("ReturnMatrixX error")); }
  351. #endif
  352. void GetSubMatrix::operator=(const BaseMatrix&)
  353.    { Throw(ProgramException("= not defined for submatrix; use <<")); }
  354.  
  355.