Mercurial > octave
changeset 29831:ef138252af51
xpow.cc: Use static_cast<int> in a consistent manner for exponent.
* xpow.cc (xpow (DiagMatrix, double), xpow (FloatDiagMatrix, float)): Cast
exponent to integer just once and use that result (variable bint) in
innermost for loop calling std::pow.
* xpow.cc (elem_xpow (NDArray, double), elem_xpow (FloatNDArray, float)):
Re-write code to test for "xisint (b)" rather than "! xisint (b)" to match
stytle of all other functions.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 28 Jun 2021 11:50:03 -0700 |
parents | 0474c3a27d16 |
children | f0da0d901d8e |
files | libinterp/corefcn/xpow.cc |
diffstat | 1 files changed, 68 insertions(+), 66 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/xpow.cc Mon Jun 28 11:12:23 2021 -0700 +++ b/libinterp/corefcn/xpow.cc Mon Jun 28 11:50:03 2021 -0700 @@ -302,9 +302,10 @@ if (xisint (b)) { + int bint = static_cast<int> (b); DiagMatrix r (nr, nc); for (octave_idx_type i = 0; i < nc; i++) - r.dgxelem (i) = std::pow (a.dgxelem (i), b); + r.dgxelem (i) = std::pow (a.dgxelem (i), bint); retval = r; } else @@ -1179,7 +1180,38 @@ { octave_value retval; - if (! xisint (b)) + if (xisint (b)) + { + NDArray result (a.dims ()); + + int bint = static_cast<int> (b); + if (bint == 2) + { + for (octave_idx_type i = 0; i < a.numel (); i++) + result.xelem (i) = a(i) * a(i); + } + else if (bint == 3) + { + for (octave_idx_type i = 0; i < a.numel (); i++) + result.xelem (i) = a(i) * a(i) * a(i); + } + else if (bint == -1) + { + for (octave_idx_type i = 0; i < a.numel (); i++) + result.xelem (i) = 1.0 / a(i); + } + else + { + for (octave_idx_type i = 0; i < a.numel (); i++) + { + octave_quit (); + result.xelem (i) = std::pow (a(i), bint); + } + } + + retval = result; + } + else { if (a.any_element_is_negative ()) { @@ -1206,37 +1238,6 @@ retval = result; } } - else - { - NDArray result (a.dims ()); - - int ib = static_cast<int> (b); - if (ib == 2) - { - for (octave_idx_type i = 0; i < a.numel (); i++) - result.xelem (i) = a(i) * a(i); - } - else if (ib == 3) - { - for (octave_idx_type i = 0; i < a.numel (); i++) - result.xelem (i) = a(i) * a(i) * a(i); - } - else if (ib == -1) - { - for (octave_idx_type i = 0; i < a.numel (); i++) - result.xelem (i) = 1.0 / a(i); - } - else - { - for (octave_idx_type i = 0; i < a.numel (); i++) - { - octave_quit (); - result.xelem (i) = std::pow (a(i), ib); - } - } - - retval = result; - } return retval; } @@ -1716,9 +1717,10 @@ if (xisint (b)) { + int bint = static_cast<int> (b); FloatDiagMatrix r (nr, nc); for (octave_idx_type i = 0; i < nc; i++) - r.dgxelem (i) = std::pow (a.dgxelem (i), b); + r.dgxelem (i) = std::pow (a.dgxelem (i), bint); retval = r; } else @@ -2496,7 +2498,38 @@ { octave_value retval; - if (! xisint (b)) + if (xisint (b)) + { + FloatNDArray result (a.dims ()); + + int bint = static_cast<int> (b); + if (bint == 2) + { + for (octave_idx_type i = 0; i < a.numel (); i++) + result.xelem (i) = a(i) * a(i); + } + else if (bint == 3) + { + for (octave_idx_type i = 0; i < a.numel (); i++) + result.xelem (i) = a(i) * a(i) * a(i); + } + else if (bint == -1) + { + for (octave_idx_type i = 0; i < a.numel (); i++) + result.xelem (i) = 1.0f / a(i); + } + else + { + for (octave_idx_type i = 0; i < a.numel (); i++) + { + octave_quit (); + result.xelem (i) = std::pow (a(i), bint); + } + } + + retval = result; + } + else { if (a.any_element_is_negative ()) { @@ -2525,37 +2558,6 @@ retval = result; } } - else - { - FloatNDArray result (a.dims ()); - - int ib = static_cast<int> (b); - if (ib == 2) - { - for (octave_idx_type i = 0; i < a.numel (); i++) - result.xelem (i) = a(i) * a(i); - } - else if (ib == 3) - { - for (octave_idx_type i = 0; i < a.numel (); i++) - result.xelem (i) = a(i) * a(i) * a(i); - } - else if (ib == -1) - { - for (octave_idx_type i = 0; i < a.numel (); i++) - result.xelem (i) = 1.0f / a(i); - } - else - { - for (octave_idx_type i = 0; i < a.numel (); i++) - { - octave_quit (); - result.xelem (i) = std::pow (a(i), ib); - } - } - - retval = result; - } return retval; }