| 1 | //$$ newmatex.cpp Exception handler |
|---|
| 2 | |
|---|
| 3 | // Copyright (C) 1992,3,4,7: 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 | #ifdef use_namespace |
|---|
| 11 | namespace NEWMAT { |
|---|
| 12 | #endif |
|---|
| 13 | |
|---|
| 14 | unsigned long OverflowException::Select; |
|---|
| 15 | unsigned long SingularException::Select; |
|---|
| 16 | unsigned long NPDException::Select; |
|---|
| 17 | unsigned long ConvergenceException::Select; |
|---|
| 18 | unsigned long ProgramException::Select; |
|---|
| 19 | unsigned long IndexException::Select; |
|---|
| 20 | unsigned long VectorException::Select; |
|---|
| 21 | unsigned long NotSquareException::Select; |
|---|
| 22 | unsigned long SubMatrixDimensionException::Select; |
|---|
| 23 | unsigned long IncompatibleDimensionsException::Select; |
|---|
| 24 | unsigned long NotDefinedException::Select; |
|---|
| 25 | unsigned long CannotBuildException::Select; |
|---|
| 26 | unsigned long InternalException::Select; |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | static void MatrixDetails(const GeneralMatrix& A) |
|---|
| 31 | // write matrix details to Exception buffer |
|---|
| 32 | { |
|---|
| 33 | MatrixBandWidth bw = A.BandWidth(); int ubw = bw.upper; int lbw = bw.lower; |
|---|
| 34 | Exception::AddMessage("MatrixType = "); |
|---|
| 35 | Exception::AddMessage(A.Type().Value()); |
|---|
| 36 | Exception::AddMessage(" # Rows = "); Exception::AddInt(A.Nrows()); |
|---|
| 37 | Exception::AddMessage("; # Cols = "); Exception::AddInt(A.Ncols()); |
|---|
| 38 | if (lbw >=0) |
|---|
| 39 | { Exception::AddMessage("; lower BW = "); Exception::AddInt(lbw); } |
|---|
| 40 | if (ubw >=0) |
|---|
| 41 | { Exception::AddMessage("; upper BW = "); Exception::AddInt(ubw); } |
|---|
| 42 | Exception::AddMessage("\n"); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | NPDException::NPDException(const GeneralMatrix& A) |
|---|
| 46 | : Runtime_error() |
|---|
| 47 | { |
|---|
| 48 | Select = Exception::Select; |
|---|
| 49 | AddMessage("detected by Newmat: matrix not positive definite\n\n"); |
|---|
| 50 | MatrixDetails(A); |
|---|
| 51 | Tracer::AddTrace(); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | SingularException::SingularException(const GeneralMatrix& A) |
|---|
| 55 | : Runtime_error() |
|---|
| 56 | { |
|---|
| 57 | Select = Exception::Select; |
|---|
| 58 | AddMessage("detected by Newmat: matrix is singular\n\n"); |
|---|
| 59 | MatrixDetails(A); |
|---|
| 60 | Tracer::AddTrace(); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | ConvergenceException::ConvergenceException(const GeneralMatrix& A) |
|---|
| 64 | : Runtime_error() |
|---|
| 65 | { |
|---|
| 66 | Select = Exception::Select; |
|---|
| 67 | AddMessage("detected by Newmat: process fails to converge\n\n"); |
|---|
| 68 | MatrixDetails(A); |
|---|
| 69 | Tracer::AddTrace(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | ConvergenceException::ConvergenceException(const char* c) : Runtime_error() |
|---|
| 73 | { |
|---|
| 74 | Select = Exception::Select; |
|---|
| 75 | AddMessage("detected by Newmat: "); |
|---|
| 76 | AddMessage(c); AddMessage("\n\n"); |
|---|
| 77 | if (c) Tracer::AddTrace(); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | OverflowException::OverflowException(const char* c) : Runtime_error() |
|---|
| 81 | { |
|---|
| 82 | Select = Exception::Select; |
|---|
| 83 | AddMessage("detected by Newmat: "); |
|---|
| 84 | AddMessage(c); AddMessage("\n\n"); |
|---|
| 85 | if (c) Tracer::AddTrace(); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | ProgramException::ProgramException(const char* c) : Logic_error() |
|---|
| 89 | { |
|---|
| 90 | Select = Exception::Select; |
|---|
| 91 | AddMessage("detected by Newmat: "); |
|---|
| 92 | AddMessage(c); AddMessage("\n\n"); |
|---|
| 93 | if (c) Tracer::AddTrace(); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | ProgramException::ProgramException(const char* c, const GeneralMatrix& A) |
|---|
| 97 | : Logic_error() |
|---|
| 98 | { |
|---|
| 99 | Select = Exception::Select; |
|---|
| 100 | AddMessage("detected by Newmat: "); |
|---|
| 101 | AddMessage(c); AddMessage("\n\n"); |
|---|
| 102 | MatrixDetails(A); |
|---|
| 103 | if (c) Tracer::AddTrace(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | ProgramException::ProgramException(const char* c, const GeneralMatrix& A, |
|---|
| 107 | const GeneralMatrix& B) : Logic_error() |
|---|
| 108 | { |
|---|
| 109 | Select = Exception::Select; |
|---|
| 110 | AddMessage("detected by Newmat: "); |
|---|
| 111 | AddMessage(c); AddMessage("\n\n"); |
|---|
| 112 | MatrixDetails(A); MatrixDetails(B); |
|---|
| 113 | if (c) Tracer::AddTrace(); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | ProgramException::ProgramException(const char* c, MatrixType a, MatrixType b) |
|---|
| 117 | : Logic_error() |
|---|
| 118 | { |
|---|
| 119 | Select = Exception::Select; |
|---|
| 120 | AddMessage("detected by Newmat: "); |
|---|
| 121 | AddMessage(c); AddMessage("\nMatrixTypes = "); |
|---|
| 122 | AddMessage(a.Value()); AddMessage("; "); |
|---|
| 123 | AddMessage(b.Value()); AddMessage("\n\n"); |
|---|
| 124 | if (c) Tracer::AddTrace(); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | VectorException::VectorException() : Logic_error() |
|---|
| 128 | { |
|---|
| 129 | Select = Exception::Select; |
|---|
| 130 | AddMessage("detected by Newmat: cannot convert matrix to vector\n\n"); |
|---|
| 131 | Tracer::AddTrace(); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | VectorException::VectorException(const GeneralMatrix& A) |
|---|
| 135 | : Logic_error() |
|---|
| 136 | { |
|---|
| 137 | Select = Exception::Select; |
|---|
| 138 | AddMessage("detected by Newmat: cannot convert matrix to vector\n\n"); |
|---|
| 139 | MatrixDetails(A); |
|---|
| 140 | Tracer::AddTrace(); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | NotSquareException::NotSquareException(const GeneralMatrix& A) |
|---|
| 144 | : Logic_error() |
|---|
| 145 | { |
|---|
| 146 | Select = Exception::Select; |
|---|
| 147 | AddMessage("detected by Newmat: matrix is not square\n\n"); |
|---|
| 148 | MatrixDetails(A); |
|---|
| 149 | Tracer::AddTrace(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | SubMatrixDimensionException::SubMatrixDimensionException() |
|---|
| 153 | : Logic_error() |
|---|
| 154 | { |
|---|
| 155 | Select = Exception::Select; |
|---|
| 156 | AddMessage("detected by Newmat: incompatible submatrix dimension\n\n"); |
|---|
| 157 | Tracer::AddTrace(); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | IncompatibleDimensionsException::IncompatibleDimensionsException() |
|---|
| 161 | : Logic_error() |
|---|
| 162 | { |
|---|
| 163 | Select = Exception::Select; |
|---|
| 164 | AddMessage("detected by Newmat: incompatible dimensions\n\n"); |
|---|
| 165 | Tracer::AddTrace(); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | IncompatibleDimensionsException::IncompatibleDimensionsException |
|---|
| 169 | (const GeneralMatrix& A, const GeneralMatrix& B) |
|---|
| 170 | : Logic_error() |
|---|
| 171 | { |
|---|
| 172 | Select = Exception::Select; |
|---|
| 173 | AddMessage("detected by Newmat: incompatible dimensions\n\n"); |
|---|
| 174 | MatrixDetails(A); MatrixDetails(B); |
|---|
| 175 | Tracer::AddTrace(); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | NotDefinedException::NotDefinedException(const char* op, const char* matrix) |
|---|
| 179 | : Logic_error() |
|---|
| 180 | { |
|---|
| 181 | Select = Exception::Select; |
|---|
| 182 | AddMessage("detected by Newmat: "); |
|---|
| 183 | AddMessage(op); |
|---|
| 184 | AddMessage(" not defined for "); |
|---|
| 185 | AddMessage(matrix); |
|---|
| 186 | AddMessage("\n\n"); |
|---|
| 187 | Tracer::AddTrace(); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | CannotBuildException::CannotBuildException(const char* matrix) |
|---|
| 191 | : Logic_error() |
|---|
| 192 | { |
|---|
| 193 | Select = Exception::Select; |
|---|
| 194 | AddMessage("detected by Newmat: cannot build matrix type "); |
|---|
| 195 | AddMessage(matrix); AddMessage("\n\n"); |
|---|
| 196 | Tracer::AddTrace(); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | IndexException::IndexException(int i, const GeneralMatrix& A) |
|---|
| 200 | : Logic_error() |
|---|
| 201 | { |
|---|
| 202 | Select = Exception::Select; |
|---|
| 203 | AddMessage("detected by Newmat: index error: requested index = "); |
|---|
| 204 | AddInt(i); AddMessage("\n\n"); |
|---|
| 205 | MatrixDetails(A); |
|---|
| 206 | Tracer::AddTrace(); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | IndexException::IndexException(int i, int j, const GeneralMatrix& A) |
|---|
| 210 | : Logic_error() |
|---|
| 211 | { |
|---|
| 212 | Select = Exception::Select; |
|---|
| 213 | AddMessage("detected by Newmat: index error: requested indices = "); |
|---|
| 214 | AddInt(i); AddMessage(", "); AddInt(j); |
|---|
| 215 | AddMessage("\n\n"); |
|---|
| 216 | MatrixDetails(A); |
|---|
| 217 | Tracer::AddTrace(); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | IndexException::IndexException(int i, const GeneralMatrix& A, bool) |
|---|
| 222 | : Logic_error() |
|---|
| 223 | { |
|---|
| 224 | Select = Exception::Select; |
|---|
| 225 | AddMessage("detected by Newmat: element error: requested index (wrt 0) = "); |
|---|
| 226 | AddInt(i); |
|---|
| 227 | AddMessage("\n\n"); |
|---|
| 228 | MatrixDetails(A); |
|---|
| 229 | Tracer::AddTrace(); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | IndexException::IndexException(int i, int j, const GeneralMatrix& A, bool) |
|---|
| 233 | : Logic_error() |
|---|
| 234 | { |
|---|
| 235 | Select = Exception::Select; |
|---|
| 236 | AddMessage( |
|---|
| 237 | "detected by Newmat: element error: requested indices (wrt 0) = "); |
|---|
| 238 | AddInt(i); AddMessage(", "); AddInt(j); |
|---|
| 239 | AddMessage("\n\n"); |
|---|
| 240 | MatrixDetails(A); |
|---|
| 241 | Tracer::AddTrace(); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | InternalException::InternalException(const char* c) : Logic_error() |
|---|
| 245 | { |
|---|
| 246 | Select = Exception::Select; |
|---|
| 247 | AddMessage("internal error detected by Newmat: please inform author\n"); |
|---|
| 248 | AddMessage(c); AddMessage("\n\n"); |
|---|
| 249 | Tracer::AddTrace(); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | /************************* ExeCounter functions *****************************/ |
|---|
| 256 | |
|---|
| 257 | #ifdef DO_REPORT |
|---|
| 258 | |
|---|
| 259 | int ExeCounter::nreports; // will be set to zero |
|---|
| 260 | |
|---|
| 261 | ExeCounter::ExeCounter(int xl, int xf) : line(xl), fileid(xf), nexe(0) {} |
|---|
| 262 | |
|---|
| 263 | ExeCounter::~ExeCounter() |
|---|
| 264 | { |
|---|
| 265 | nreports++; |
|---|
| 266 | cout << "REPORT " << setw(6) << nreports << " " |
|---|
| 267 | << setw(6) << fileid << " " << setw(6) << line |
|---|
| 268 | << " " << setw(6) << nexe << "\n"; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | #endif |
|---|
| 272 | |
|---|
| 273 | /**************************** error handler *******************************/ |
|---|
| 274 | |
|---|
| 275 | void MatrixErrorNoSpace(void* v) { if (!v) Throw(Bad_alloc()); } |
|---|
| 276 | // throw exception if v is null |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | /************************* miscellanous errors ***************************/ |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | void CroutMatrix::GetRow(MatrixRowCol&) |
|---|
| 285 | { Throw(NotDefinedException("GetRow","Crout")); } |
|---|
| 286 | void CroutMatrix::GetCol(MatrixRowCol&) |
|---|
| 287 | { Throw(NotDefinedException("GetCol","Crout")); } |
|---|
| 288 | void CroutMatrix::operator=(const BaseMatrix&) |
|---|
| 289 | { Throw(NotDefinedException("=","Crout")); } |
|---|
| 290 | void BandLUMatrix::GetRow(MatrixRowCol&) |
|---|
| 291 | { Throw(NotDefinedException("GetRow","BandLUMatrix")); } |
|---|
| 292 | void BandLUMatrix::GetCol(MatrixRowCol&) |
|---|
| 293 | { Throw(NotDefinedException("GetCol","BandLUMatrix")); } |
|---|
| 294 | void BandLUMatrix::operator=(const BaseMatrix&) |
|---|
| 295 | { Throw(NotDefinedException("=","BandLUMatrix")); } |
|---|
| 296 | void BaseMatrix::IEQND() const |
|---|
| 297 | { Throw(NotDefinedException("inequalities", "matrices")); } |
|---|
| 298 | #ifdef TEMPS_DESTROYED_QUICKLY_R |
|---|
| 299 | ReturnMatrixX::ReturnMatrixX(const ReturnMatrixX& tm) |
|---|
| 300 | : gm(tm.gm) { Throw(ProgramException("ReturnMatrixX error")); } |
|---|
| 301 | #endif |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | #ifdef use_namespace |
|---|
| 305 | } |
|---|
| 306 | #endif |
|---|
| 307 | |
|---|