From d015b317994eba2240b3fc556808c481d9e8bcec Mon Sep 17 00:00:00 2001 From: Kai Ni Date: Sat, 9 Jan 2010 07:06:29 +0000 Subject: [PATCH] more meaningful error messages for matrix operations --- cpp/Matrix.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cpp/Matrix.h b/cpp/Matrix.h index 202369885..e6cbd6340 100644 --- a/cpp/Matrix.h +++ b/cpp/Matrix.h @@ -11,6 +11,7 @@ #pragma once #include +#include #include #include #include "Vector.h" @@ -84,7 +85,8 @@ bool assert_equal(const Matrix& A, const Matrix& B, double tol = 1e-9); * overload * for matrix-vector multiplication (as BOOST does not) */ inline Vector operator*(const Matrix& A, const Vector & v) { - if (A.size2()!=v.size()) throw(std::invalid_argument("Matrix operator* : A.n!=v.size")); + if (A.size2()!=v.size()) throw std::invalid_argument( + boost::str(boost::format("Matrix operator* : A.n(%d)!=v.size(%d)") % A.size2() % v.size())); return prod(A,v); } @@ -93,7 +95,8 @@ inline Vector operator*(const Matrix& A, const Vector & v) { * We transpose the vectors for speed. */ inline Vector operator^(const Matrix& A, const Vector & v) { - if (A.size1()!=v.size()) throw(std::invalid_argument("Matrix operator^ : A.m!=v.size")); + if (A.size1()!=v.size()) throw std::invalid_argument( + boost::str(boost::format("Matrix operator^ : A.m(%d)!=v.size(%d)") % A.size1() % v.size())); Vector vt = trans(v); Vector vtA = prod(vt,A); return trans(vtA); @@ -103,7 +106,8 @@ inline Vector operator^(const Matrix& A, const Vector & v) { * overload * for vector*matrix multiplication (as BOOST does not) */ inline Vector operator*(const Vector & v, const Matrix& A) { - if (A.size1()!=v.size()) throw(std::invalid_argument("Matrix operator* : A.m!=v.size")); + if (A.size1()!=v.size()) throw std::invalid_argument( + boost::str(boost::format("Matrix operator* : A.m(%d)!=v.size(%d)") % A.size1() % v.size())); return prod(v,A); } @@ -111,7 +115,8 @@ inline Vector operator*(const Vector & v, const Matrix& A) { * overload * for matrix multiplication (as BOOST does not) */ inline Matrix operator*(const Matrix& A, const Matrix& B) { - if (A.size2()!=B.size1()) throw(std::invalid_argument("Matrix operator* : A.n!=B.m")); + if (A.size2()!=B.size1()) throw std::invalid_argument( + boost::str(boost::format("Matrix operator* : A.n(%d)!=B.m(%d)") % A.size2() % B.size1())); return prod(A,B); }