Mercurial > hg > octave-terminal
changeset 8882:8633ae24a892
gripe when dividing a diagonal matrix by zero
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 27 Feb 2009 07:51:20 +0100 |
parents | 8ed42c679af5 |
children | 7de0992eb123 |
files | src/ChangeLog src/OPERATORS/op-dms-template.cc |
diffstat | 2 files changed, 24 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2009-02-27 Jaroslav Hajek <highegg@gmail.com> + + * OPERATORS/op-dms-template.cc (gripe_if_zero): New template static + function. + (dmsdiv, sdmldiv): Call it. + 2009-02-26 John W. Eaton <jwe@octave.org> * symtab.h (symbol_table::symbol_record::symbol_record_rep::forced):
--- a/src/OPERATORS/op-dms-template.cc +++ b/src/OPERATORS/op-dms-template.cc @@ -25,6 +25,7 @@ #endif #include "ops.h" +#include "gripes.h" #include "xpow.h" #include SINCLUDE #include MINCLUDE @@ -45,18 +46,33 @@ DEFNDBINOP_OP (dmsadd, MATRIX, SCALAR, MATRIXV, SCALARV, +) DEFNDBINOP_OP (dmssub, MATRIX, SCALAR, MATRIXV, SCALARV, -) DEFNDBINOP_OP (dmsmul, MATRIX, SCALAR, MATRIXV, SCALARV, *) -DEFNDBINOP_OP (dmsdiv, MATRIX, SCALAR, MATRIXV, SCALARV, /) #define OCTAVE_MATRIX CONCAT2(octave_, MATRIX) #define OCTAVE_SCALAR CONCAT2(octave_, SCALAR) #define MATRIX_VALUE CONCAT2(MATRIXV, _value) #define SCALAR_VALUE CONCAT2(SCALARV, _value) +template <class T> +static T +gripe_if_zero (T x) +{ + if (x == T ()) + gripe_divide_by_zero (); + return x; +} + +DEFBINOP (dmsdiv, MATRIX, SCALAR) +{ + CAST_BINOP_ARGS (const OCTAVE_MATRIX&, const OCTAVE_SCALAR&); + + return v1.MATRIX_VALUE () / gripe_if_zero (v2.SCALAR_VALUE ()); +} + DEFBINOP (sdmldiv, SCALAR, MATRIX) { CAST_BINOP_ARGS (const OCTAVE_SCALAR&, const OCTAVE_MATRIX&); - return v2.MATRIX_VALUE () / v1.SCALAR_VALUE (); + return v2.MATRIX_VALUE () / gripe_if_zero (v1.SCALAR_VALUE ()); } DEFBINOP (dmspow, MATRIX, SCALAR)