Skip to content

Commit

Permalink
throw error classes from linalg.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Voigt authored and Alexander Voigt committed Aug 16, 2016
1 parent 9f04fc9 commit 1feb77f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
13 changes: 0 additions & 13 deletions legacy/diagonalization.cpp
Expand Up @@ -24,19 +24,6 @@

namespace flexiblesusy {

/**
* @class DiagonalizationError
* @brief Diagonalization failed
*/
class DiagonalizationError : public Error {
public:
explicit DiagonalizationError(const std::string& message_) : message(message_) {}
virtual ~DiagonalizationError() {}
virtual std::string what() const { return message; }
private:
std::string message;
};

namespace {
double AbsSqrt_d(double x)
{
Expand Down
13 changes: 13 additions & 0 deletions src/error.hpp
Expand Up @@ -171,6 +171,19 @@ class OutOfBoundsError : public Error {
std::string msg;
};

/**
* @class DiagonalizationError
* @brief Diagonalization failed
*/
class DiagonalizationError : public Error {
public:
explicit DiagonalizationError(const std::string& message_) : message(message_) {}
virtual ~DiagonalizationError() {}
virtual std::string what() const { return message; }
private:
std::string message;
};

}

#endif
61 changes: 31 additions & 30 deletions src/linalg.cpp
Expand Up @@ -9,6 +9,7 @@
#include "linalg.h"
#include "utils.h"
#include "def.h"
#include "error.hpp"
#include <cmath>
using std::abs;
using std::cos;
Expand Down Expand Up @@ -51,7 +52,7 @@ void DoubleVector::setEnd(int e) {
ostringstream ii;
ii << "DoubleVector.setEnd called with new end " << e << " before start " << start;
ii << *this;
throw(ii.str());
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
if (e != end) {
Expand Down Expand Up @@ -167,7 +168,7 @@ const DoubleMatrix & DoubleMatrix::operator=(double v) {
ostringstream ii;
ii << "DoubleMatrix = double overload; not square\n";
ii << *this << "=\n" << v;
throw(ii.str());
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
std::valarray<double> diagvec(v,rows);
Expand Down Expand Up @@ -251,7 +252,7 @@ double DoubleMatrix::trace() const {
if (rows != cols) {
ostringstream ii;
ii << "trace of non-square DoubleMatrix\n" << *this;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
double sum = 0.0;
Expand Down Expand Up @@ -296,7 +297,7 @@ void DoubleMatrix::associateOrderAbs(DoubleVector &v) {
(v.displayStart() != 1)) {
ostringstream ii;
ii << "Associated ordering incompatibility\n";
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
for (int i=v.displayStart(); i<=v.displayEnd(); ++i)
Expand All @@ -319,7 +320,7 @@ void DoubleMatrix::associateOrderAbs(DoubleMatrix & u,
(w.displayStart() != 1)) {
ostringstream ii;
ii << "Associated ordering incompatibility\n";
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
for (int i=w.displayStart(); i<=w.displayEnd(); ++i)
Expand All @@ -337,7 +338,7 @@ void DoubleMatrix::symmetrise() {
if (rows != cols) {
ostringstream ii;
ii << "Error: symmetrising rectangular matrix " << *this;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
for (int i=1; i<=rows; ++i)
Expand All @@ -353,7 +354,7 @@ double DoubleMatrix::compare(const DoubleMatrix & a) const {
ostringstream ii;
ii << "Error: comparing matrices of different sizes" << *this <<
" and " << a;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
return abs(x - a.x).sum();
Expand All @@ -366,7 +367,7 @@ double DoubleMatrix::compare(const ComplexMatrix & a) const {
ostringstream ii;
ii << "Error: comparing matrices of different sizes" << *this <<
" and " << a;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
ComplexMatrix temp(*this);
Expand All @@ -390,7 +391,7 @@ DoubleMatrix DoubleMatrix::inverse() const {
ostringstream ii;
ii << "Error in DoubleMatrix::invert, trying to invert non-square";
ii << " matrix " << *this << '\n';
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
DoubleMatrix u(ans.displayRows(), ans.displayRows());
Expand All @@ -405,7 +406,7 @@ DoubleMatrix DoubleMatrix::inverse() const {
ostringstream ii;
ii << "Problem in DoubleMatrix::inverse, " << *this
<< "non-invertible\n";
throw ii.str();
throw flexiblesusy::DiagonalizationError(ii.str());
}
}
DoubleMatrix temp(w);
Expand All @@ -426,7 +427,7 @@ double DoubleMatrix::diagonalise(DoubleMatrix & u, DoubleMatrix & v,
ostringstream ii;
ii << "Error: Trying to diagonalise matrix \n" << *this
<< "with u" << u << "v " << v << "w " << w;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
// Numerical routine replaces argument, so make a copy of elements
Expand All @@ -453,7 +454,7 @@ double DoubleMatrix::diagonaliseSym(DoubleMatrix & v, DoubleVector & w) const {
|| v.displayRows() !=c || c > 10) {
ostringstream ii;
ii << "Error: Trying to diagonalise matrix \n" << *this;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
// Numerical recipes routine replaces argument, so make a copy of elements
Expand Down Expand Up @@ -482,7 +483,7 @@ double DoubleMatrix::diagonaliseSym(ComplexMatrix & v, DoubleVector & w) const {
|| v.displayRows() !=c || c > 10) {
ostringstream ii;
ii << "Error: Trying to diagonalise matrix \n" << *this;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
// Numerical recipes routine replaces argument, so make a copy of elements
Expand Down Expand Up @@ -528,12 +529,12 @@ DoubleVector DoubleMatrix::sym2by2(double & theta) const {
ostringstream ii;
ii << "Called sym2by2 with matrix of dimension ("
<< displayRows() << ", " << displayCols() << ")\n";
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
if (display(1, 2) != display(2, 1)) {
ostringstream ii;
ii << "Called non symmetric sym2by2:" << *this;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif

Expand Down Expand Up @@ -563,7 +564,7 @@ DoubleVector DoubleMatrix::sym2by2(double & theta) const {
ii << "Inaccurate diagonalisation in LINALG::sym2by2\n";
ii << "Diagonalising " << * this;
ii << "Found diagonalised matrix to be " << mm;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif

Expand Down Expand Up @@ -604,7 +605,7 @@ DoubleVector DoubleMatrix::asy2by2(double & thetaL, double & thetaR) const {
ii << "diagonalising " << e << " where thetaR=" << thetaR
<< " thetaL=" << thetaL << '\n';
ii << "m=" << mm;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif

Expand Down Expand Up @@ -699,7 +700,7 @@ void positivise(double thetaL, double thetaR, const DoubleVector & diag,
ostringstream ii;
ii << "DoubleMatrix::positivise currentl only available for 2 by 2"
<< "matrices, not " << diag << u << v;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif

Expand All @@ -721,7 +722,7 @@ void diagonaliseSvd(DoubleMatrix & a, DoubleVector & w, DoubleMatrix & v) {
ostringstream ii;
ii << "Nans present in linalg.cpp:diagonaliseSvd: diagonalising\n"
<< a << endl;
throw ii.str();
throw flexiblesusy::DiagonalizationError(ii.str());
}

int n = a.displayCols(), m = a.displayRows();
Expand Down Expand Up @@ -849,7 +850,7 @@ void diagonaliseSvd(DoubleMatrix & a, DoubleVector & w, DoubleMatrix & v) {
break;
}
if (its == 30 || l < 1 || k < 1)
throw "no convergence in 30 diagonaliseSvd iterations";
throw flexiblesusy::DiagonalizationError("no convergence in 30 diagonaliseSvd iterations");

x = w(l);
nm = k - 1;
Expand Down Expand Up @@ -997,7 +998,7 @@ void diagonaliseJac(DoubleMatrix & a, int n, DoubleVector & d,
ii << "Too many iterations in routine diagonaliseJac: diagonalising\n"
<< a << "to" << d << " with " << v;
}
throw ii.str();
throw flexiblesusy::DiagonalizationError(ii.str());
}


Expand All @@ -1023,7 +1024,7 @@ void ComplexVector::setEnd(int e) {
if (e < start) {
ostringstream ii;
ii << "ComplexVector.setEnd called with incompatible length " << e << '\n';
throw(ii.str());
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
if (e != end) {
Expand Down Expand Up @@ -1130,7 +1131,7 @@ const ComplexMatrix & ComplexMatrix::operator=(const Complex &v) {
ostringstream ii;
ii << "ComplexMatrix = Complex overload; must be square\n";
ii << *this << "=\n" << v;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
std::valarray<Complex> diagvec(v,rows);
Expand Down Expand Up @@ -1208,7 +1209,7 @@ Complex ComplexMatrix::trace() const {
if (rows != cols) {
ostringstream ii;
ii << "Complexrace of non-square matrix\n" << *this;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
Complex sum(0.0);
Expand Down Expand Up @@ -1268,7 +1269,7 @@ void ComplexMatrix::symmetrise() {
if (rows != cols) {
ostringstream ii;
ii << "Error: symmetrising rectangular matrix " << *this;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
for (int i=1; i<=rows; ++i)
Expand All @@ -1284,7 +1285,7 @@ double ComplexMatrix::compare(const ComplexMatrix & a) const {
ostringstream ii;
ii << "Error: comparing matrices of different sizes" << *this <<
" and " << a;
throw ii.str();
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
return abs(x - a.x).sum().real();
Expand Down Expand Up @@ -1432,7 +1433,7 @@ DoubleMatrix DoubleMatrix::ludcmp(double & d) const {
big = 0.0;
for (j=1;j<=n;j++)
if ((temp = fabs(a.elmt(i, j))) > big) big = temp;
if (big == 0.0) throw("Singular matrix in routine ludcmp\n");
if (big == 0.0) throw flexiblesusy::DiagonalizationError("Singular matrix in routine ludcmp");
vv(i) = 1.0/big;
}

Expand Down Expand Up @@ -1506,7 +1507,7 @@ DoubleVector DoubleVector::divide(DoubleVector const &v) const {
ostringstream ii;
ii << "DoubleVector * overload; incompatible lengths\n";
ii << *this << "*\n" << v;
throw(ii.str());
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
DoubleVector temp(v);
Expand Down Expand Up @@ -1544,7 +1545,7 @@ const ComplexMatrix& ComplexMatrix::operator+=(const DoubleMatrix& other)
ostringstream ii;
ii << "ComplexMatrix::operator+(const DoubleMatrix&) incompatible lengths\n";
ii << *this << "*\n" << other;
throw(ii.str());
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
for (std::size_t i = 0; i < x.size(); i++)
Expand All @@ -1560,7 +1561,7 @@ ComplexMatrix ComplexMatrix::operator+(const DoubleMatrix& other)
ostringstream ii;
ii << "ComplexMatrix::operator+=(const DoubleMatrix&) incompatible lengths\n";
ii << *this << "*\n" << other;
throw(ii.str());
throw flexiblesusy::OutOfBoundsError(ii.str());
}
#endif
ComplexMatrix temp(*this);
Expand Down

0 comments on commit 1feb77f

Please sign in to comment.