# HG changeset patch # User jwe # Date 845144673 0 # Node ID 170053c0f75efbe671684687f96257026cf72e4f # Parent d9147efd1a9348370ead28b3f97db21903b05460 [project @ 1996-10-12 18:23:01 by jwe] diff --git a/liboctave/dDiagMatrix.cc b/liboctave/dDiagMatrix.cc --- a/liboctave/dDiagMatrix.cc +++ b/liboctave/dDiagMatrix.cc @@ -38,7 +38,7 @@ // Diagonal Matrix class. -int +bool DiagMatrix::operator == (const DiagMatrix& a) const { if (rows () != a.rows () || cols () != a.cols ()) @@ -47,7 +47,7 @@ return equal (data (), a.data (), length ()); } -int +bool DiagMatrix::operator != (const DiagMatrix& a) const { return !(*this == a); @@ -309,10 +309,13 @@ { int nr = rows (); int nc = cols (); - if (nr != a.rows () || nc != a.cols ()) + + int a_nr = a.rows (); + int a_nc = a.cols (); + + if (nr != a_nr || nc != a_nc) { - (*current_liboctave_error_handler) - ("nonconformant matrix += operation attempted"); + gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); return *this; } @@ -330,10 +333,13 @@ { int nr = rows (); int nc = cols (); - if (nr != a.rows () || nc != a.cols ()) + + int a_nr = a.rows (); + int a_nc = a.cols (); + + if (nr != a_nr || nc != a_nc) { - (*current_liboctave_error_handler) - ("nonconformant matrix -= operation attempted"); + gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); return *this; } @@ -353,12 +359,13 @@ { int nr_a = a.rows (); int nc_a = a.cols (); + int nr_b = b.rows (); int nc_b = b.cols (); + if (nc_a != nr_b) { - (*current_liboctave_error_handler) - ("nonconformant matrix multiplication attempted"); + gripe_nonconformant ("operaotr *", nr_a, nc_a, nr_b, nc_b); return DiagMatrix (); } diff --git a/liboctave/dDiagMatrix.h b/liboctave/dDiagMatrix.h --- a/liboctave/dDiagMatrix.h +++ b/liboctave/dDiagMatrix.h @@ -55,8 +55,8 @@ return *this; } - int operator == (const DiagMatrix& a) const; - int operator != (const DiagMatrix& a) const; + bool operator == (const DiagMatrix& a) const; + bool operator != (const DiagMatrix& a) const; DiagMatrix& fill (double val); DiagMatrix& fill (double val, int beg, int end); diff --git a/liboctave/dMatrix.cc b/liboctave/dMatrix.cc --- a/liboctave/dMatrix.cc +++ b/liboctave/dMatrix.cc @@ -143,16 +143,16 @@ elem (i, j) = a.elem (i, j); } -int +bool Matrix::operator == (const Matrix& a) const { if (rows () != a.rows () || cols () != a.cols ()) - return 0; + return false; return equal (data (), a.data (), length ()); } -int +bool Matrix::operator != (const Matrix& a) const { return !(*this == a); @@ -1418,10 +1418,13 @@ { int nr = rows (); int nc = cols (); - if (nr != a.rows () || nc != a.cols ()) + + int a_nr = a.rows (); + int a_nc = a.cols (); + + if (nr != a_nr || nc != a_nc) { - (*current_liboctave_error_handler) - ("nonconformant matrix += operation attempted"); + gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); return *this; } @@ -1440,10 +1443,13 @@ { int nr = rows (); int nc = cols (); - if (nr != a.rows () || nc != a.cols ()) + + int a_nr = a.rows (); + int a_nc = a.cols (); + + if (nr != a_nr || nc != a_nc) { - (*current_liboctave_error_handler) - ("nonconformant matrix -= operation attempted"); + gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); return *this; } @@ -1460,10 +1466,15 @@ Matrix& Matrix::operator += (const DiagMatrix& a) { - if (rows () != a.rows () || cols () != a.cols ()) + int nr = rows (); + int nc = cols (); + + int a_nr = a.rows (); + int a_nc = a.cols (); + + if (nr != a_nr || nc != a_nc) { - (*current_liboctave_error_handler) - ("nonconformant matrix += operation attempted"); + gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); return *this; } @@ -1476,10 +1487,15 @@ Matrix& Matrix::operator -= (const DiagMatrix& a) { - if (rows () != a.rows () || cols () != a.cols ()) + int nr = rows (); + int nc = cols (); + + int a_nr = a.rows (); + int a_nc = a.cols (); + + if (nr != a_nr || nc != a_nc) { - (*current_liboctave_error_handler) - ("nonconformant matrix += operation attempted"); + gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); return *this; } @@ -1517,8 +1533,7 @@ int a_len = a.length (); if (len != a_len) - (*current_liboctave_error_handler) - ("nonconformant vector multiplication attempted"); + gripe_nonconformant ("operator *", len, 1, 1, a_len); else { if (len != 0) @@ -1578,10 +1593,13 @@ { int nr = m.rows (); int nc = m.cols (); - if (nr != a.rows () || nc != a.cols ()) + + int a_nr = a.rows (); + int a_nc = a.cols (); + + if (nr != a_nr || nc != a_nc) { - (*current_liboctave_error_handler) - ("nonconformant matrix addition attempted"); + gripe_nonconformant ("operator +", nr, nc, a_nr, a_nc); return Matrix (); } @@ -1601,10 +1619,13 @@ { int nr = m.rows (); int nc = m.cols (); - if (nr != a.rows () || nc != a.cols ()) + + int a_nr = a.rows (); + int a_nc = a.cols (); + + if (nr != a_nr || nc != a_nc) { - (*current_liboctave_error_handler) - ("nonconformant matrix subtraction attempted"); + gripe_nonconformant ("operator -", nr, nc, a_nr, a_nc); return Matrix (); } @@ -1631,8 +1652,7 @@ int a_nc = a.cols (); if (nc != a_nr) - (*current_liboctave_error_handler) - ("nonconformant matrix multiplication attempted"); + gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); else { if (nr == 0 || nc == 0 || a_nc == 0) @@ -1686,10 +1706,13 @@ { int nr = m.rows (); int nc = m.cols (); - if (nr != a.rows () || nc != a.cols ()) + + int a_nr = a.rows (); + int a_nc = a.cols (); + + if (nr != a_nr || nc != a_nc) { - (*current_liboctave_error_handler) - ("nonconformant matrix addition attempted"); + gripe_nonconformant ("operator +", nr, nc, a_nr, a_nc); return Matrix (); } @@ -1708,10 +1731,13 @@ { int nr = m.rows (); int nc = m.cols (); - if (nr != a.rows () || nc != a.cols ()) + + int a_nr = a.rows (); + int a_nc = a.cols (); + + if (nr != a_nr || nc != a_nc) { - (*current_liboctave_error_handler) - ("nonconformant matrix subtraction attempted"); + gripe_nonconformant ("operator -", nr, nc, a_nr, a_nc); return Matrix (); } @@ -1734,8 +1760,7 @@ int a_nc = a.cols (); if (nc != a_nr) { - (*current_liboctave_error_handler) - ("nonconformant matrix multiplication attempted"); + gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); return Matrix (); } @@ -1787,8 +1812,7 @@ int a_nc = a.cols (); if (nc != a_nr) - (*current_liboctave_error_handler) - ("nonconformant matrix multiplication attempted"); + gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); else { if (nr == 0 || nc == 0 || a_nc == 0) @@ -1845,10 +1869,60 @@ d[i] = f (d[i]); } +bool +Matrix::any_element_is_negative (void) const +{ + int nr = rows (); + int nc = cols (); + + for (int j = 0; j < nc; j++) + for (int i = 0; i < nr; i++) + if (elem (i, j) < 0.0) + return true; + + return false; +} + + +bool +Matrix::any_element_is_inf_or_nan (void) const +{ + int nr = rows (); + int nc = cols (); + + for (int j = 0; j < nc; j++) + for (int i = 0; i < nr; i++) + { + double val = elem (i, j); + if (xisinf (val) || xisnan (val)) + return 1; + } + return 0; +} + +bool +Matrix::all_elements_are_int_or_inf_or_nan (void) const +{ + int nr = rows (); + int nc = cols (); + + for (int j = 0; j < nc; j++) + for (int i = 0; i < nr; i++) + { + double val = elem (i, j); + if (xisnan (val) || D_NINT (val) == val) + continue; + else + return false; + } + + return true; +} + // Return nonzero if any element of M is not an integer. Also extract // the largest and smallest values and return them in MAX_VAL and MIN_VAL. -int +bool Matrix::all_integers (double& max_val, double& min_val) const { int nr = rows (); @@ -1860,7 +1934,7 @@ min_val = elem (0, 0); } else - return 0; + return false; for (int j = 0; j < nc; j++) for (int i = 0; i < nr; i++) @@ -1874,16 +1948,17 @@ min_val = val; if (D_NINT (val) != val) - return 0; + return false; } - return 1; + + return true; } -int +bool Matrix::too_large_for_float (void) const { int nr = rows (); - int nc = columns (); + int nc = cols (); for (int j = 0; j < nc; j++) for (int i = 0; i < nr; i++) @@ -1891,10 +1966,10 @@ double val = elem (i, j); if (val > FLT_MAX || val < FLT_MIN) - return 1; + return true; } - return 0; + return false; } // XXX FIXME XXX Do these really belong here? They should maybe be @@ -2249,6 +2324,21 @@ return retval; } +Matrix +Matrix::abs (void) const +{ + int nr = rows (); + int nc = cols (); + + Matrix retval (nr, nc); + + for (int j = 0; j < nc; j++) + for (int i = 0; i < nr; i++) + retval (i, j) = fabs (elem (i, j)); + + return retval; +} + ColumnVector Matrix::diag (void) const { @@ -3196,7 +3286,7 @@ } } else - (*current_liboctave_error_handler) ("nonconformant matrices"); + gripe_nonconformant ("qzval", a_nr, a_nc, b_nr, b_nc); } else (*current_liboctave_error_handler) ("qzval: square matrices required"); diff --git a/liboctave/dMatrix.h b/liboctave/dMatrix.h --- a/liboctave/dMatrix.h +++ b/liboctave/dMatrix.h @@ -68,8 +68,8 @@ return *this; } - int operator == (const Matrix& a) const; - int operator != (const Matrix& a) const; + bool operator == (const Matrix& a) const; + bool operator != (const Matrix& a) const; // destructive insert/delete/reorder operations @@ -173,7 +173,7 @@ // column vector by row vector -> matrix operations - friend Matrix operator * (const ColumnVector& a, const RowVector& a); + friend Matrix operator * (const ColumnVector& a, const RowVector& b); // diagonal matrix by scalar -> matrix operations @@ -207,8 +207,11 @@ friend Matrix map (d_c_Mapper f, const ComplexMatrix& a); void map (d_d_Mapper f); - int all_integers (double& max_val, double& min_val) const; - int too_large_for_float (void) const; + bool any_element_is_negative (void) const; + bool any_element_is_inf_or_nan (void) const; + bool all_elements_are_int_or_inf_or_nan (void) const; + bool all_integers (double& max_val, double& min_val) const; + bool too_large_for_float (void) const; Matrix all (void) const; Matrix any (void) const; @@ -218,6 +221,7 @@ Matrix prod (void) const; Matrix sum (void) const; Matrix sumsq (void) const; + Matrix abs (void) const; ColumnVector diag (void) const; ColumnVector diag (int k) const;