# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1314245548 18000 # Node ID 4061106b1c4b26bac6473fbc265f6d9046e4ef83 # Parent d9d65c3017c363b1f2c9d29b400e35fa84687bd5 Enable automatic bsxfun for power operators * bsxfun.h: Put #include guards * int8NDArray.cc: Define bsxfun power operator for integral types. * int16NDArray.cc: Ditto. * int32NDArray.cc: Ditto. * int64NDArray.cc: fDitto. * uint8ADArray.cc: Ditto. * uint16NDArray.cc: Ditto. * uint32NDArray.cc: Ditto. * uint64NDArray.cc: Ditto. * mx-inlines.cc: Let the compiler decide to use Octave's own integral pow. * op-int.h: Call bsxfun for integral operators. * xpow.cc: Call bsxfun for float operators. diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/bsxfun.h --- a/liboctave/bsxfun.h Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/bsxfun.h Wed Aug 24 23:12:28 2011 -0500 @@ -19,6 +19,8 @@ . */ +#if !defined (bsxfun_h) +#define bsxfun_h 1 #include @@ -38,3 +40,5 @@ } return true; } + +#endif diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/dNDArray.cc --- a/liboctave/dNDArray.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/dNDArray.cc Wed Aug 24 23:12:28 2011 -0500 @@ -925,3 +925,5 @@ BSXFUN_OP_DEF_MXLOOP (pow, NDArray, mx_inline_pow) BSXFUN_OP2_DEF_MXLOOP (pow, ComplexNDArray, ComplexNDArray, NDArray, mx_inline_pow) +BSXFUN_OP2_DEF_MXLOOP (pow, ComplexNDArray, NDArray, + ComplexNDArray, mx_inline_pow) diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/dNDArray.h --- a/liboctave/dNDArray.h Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/dNDArray.h Wed Aug 24 23:12:28 2011 -0500 @@ -185,5 +185,7 @@ BSXFUN_OP_DECL (pow, NDArray, OCTAVE_API) BSXFUN_OP2_DECL (pow, ComplexNDArray, ComplexNDArray, NDArray, OCTAVE_API) +BSXFUN_OP2_DECL (pow, ComplexNDArray, NDArray, + ComplexNDArray, OCTAVE_API) #endif diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/int16NDArray.cc --- a/liboctave/int16NDArray.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/int16NDArray.cc Wed Aug 24 23:12:28 2011 -0500 @@ -54,3 +54,5 @@ BSXFUN_STDOP_DEFS_MXLOOP (int16NDArray) BSXFUN_STDREL_DEFS_MXLOOP (int16NDArray) + +BSXFUN_OP_DEF_MXLOOP (pow, int16NDArray, mx_inline_pow) diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/int32NDArray.cc --- a/liboctave/int32NDArray.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/int32NDArray.cc Wed Aug 24 23:12:28 2011 -0500 @@ -54,3 +54,5 @@ BSXFUN_STDOP_DEFS_MXLOOP (int32NDArray) BSXFUN_STDREL_DEFS_MXLOOP (int32NDArray) + +BSXFUN_OP_DEF_MXLOOP (pow, int32NDArray, mx_inline_pow) diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/int64NDArray.cc --- a/liboctave/int64NDArray.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/int64NDArray.cc Wed Aug 24 23:12:28 2011 -0500 @@ -54,3 +54,5 @@ BSXFUN_STDOP_DEFS_MXLOOP (int64NDArray) BSXFUN_STDREL_DEFS_MXLOOP (int64NDArray) + +BSXFUN_OP_DEF_MXLOOP (pow, int64NDArray, mx_inline_pow) diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/int8NDArray.cc --- a/liboctave/int8NDArray.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/int8NDArray.cc Wed Aug 24 23:12:28 2011 -0500 @@ -54,3 +54,5 @@ BSXFUN_STDOP_DEFS_MXLOOP (int8NDArray) BSXFUN_STDREL_DEFS_MXLOOP (int8NDArray) + +BSXFUN_OP_DEF_MXLOOP (pow, int8NDArray, mx_inline_pow) diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/mx-inlines.cc --- a/liboctave/mx-inlines.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/mx-inlines.cc Wed Aug 24 23:12:28 2011 -0500 @@ -288,7 +288,10 @@ inline void F (size_t n, R *r, X x, const Y *y) throw () \ { for (size_t i = 0; i < n; i++) r[i] = FUN (x, y[i]); } -DEFMXMAPPER2X (mx_inline_pow, std::pow) +// Let the compiler decide which pow to use, whichever best matches the +// arguments provided. +using std::pow; +DEFMXMAPPER2X (mx_inline_pow, pow) // Arbitrary function appliers. The function is a template parameter to enable // inlining. diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/uint16NDArray.cc --- a/liboctave/uint16NDArray.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/uint16NDArray.cc Wed Aug 24 23:12:28 2011 -0500 @@ -54,3 +54,5 @@ BSXFUN_STDOP_DEFS_MXLOOP (uint16NDArray) BSXFUN_STDREL_DEFS_MXLOOP (uint16NDArray) + +BSXFUN_OP_DEF_MXLOOP (pow, uint16NDArray, mx_inline_pow) diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/uint32NDArray.cc --- a/liboctave/uint32NDArray.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/uint32NDArray.cc Wed Aug 24 23:12:28 2011 -0500 @@ -54,3 +54,5 @@ BSXFUN_STDOP_DEFS_MXLOOP (uint32NDArray) BSXFUN_STDREL_DEFS_MXLOOP (uint32NDArray) + +BSXFUN_OP_DEF_MXLOOP (pow, uint32NDArray, mx_inline_pow) diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/uint64NDArray.cc --- a/liboctave/uint64NDArray.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/uint64NDArray.cc Wed Aug 24 23:12:28 2011 -0500 @@ -54,3 +54,5 @@ BSXFUN_STDOP_DEFS_MXLOOP (uint64NDArray) BSXFUN_STDREL_DEFS_MXLOOP (uint64NDArray) + +BSXFUN_OP_DEF_MXLOOP (pow, uint64NDArray, mx_inline_pow) diff -r d9d65c3017c3 -r 4061106b1c4b liboctave/uint8NDArray.cc --- a/liboctave/uint8NDArray.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/liboctave/uint8NDArray.cc Wed Aug 24 23:12:28 2011 -0500 @@ -54,3 +54,5 @@ BSXFUN_STDOP_DEFS_MXLOOP (uint8NDArray) BSXFUN_STDREL_DEFS_MXLOOP (uint8NDArray) + +BSXFUN_OP_DEF_MXLOOP (pow, uint8NDArray, mx_inline_pow) diff -r d9d65c3017c3 -r 4061106b1c4b src/OPERATORS/op-int.h --- a/src/OPERATORS/op-int.h Wed Aug 24 23:06:59 2011 -0500 +++ b/src/OPERATORS/op-int.h Wed Aug 24 23:12:28 2011 -0500 @@ -21,6 +21,7 @@ */ #include "quit.h" +#include "bsxfun.h" #define DEFINTBINOP_OP(name, t1, t2, op, t3) \ BINOPDECL (name, a1, a2) \ @@ -703,8 +704,15 @@ dim_vector b_dims = b.dims (); \ if (a_dims != b_dims) \ { \ - gripe_nonconformant ("operator .^", a_dims, b_dims); \ - return octave_value (); \ + if (is_valid_bsxfun (a_dims, b_dims)) \ + { \ + return bsxfun_pow (a, b); \ + } \ + else \ + { \ + gripe_nonconformant ("operator .^", a_dims, b_dims); \ + return octave_value (); \ + } \ } \ T1 ## NDArray result (a_dims); \ for (int i = 0; i < a.length (); i++) \ @@ -722,8 +730,15 @@ dim_vector b_dims = b.dims (); \ if (a_dims != b_dims) \ { \ - gripe_nonconformant ("operator .^", a_dims, b_dims); \ - return octave_value (); \ + if (is_valid_bsxfun (a_dims, b_dims)) \ + { \ + return bsxfun_pow (a, static_cast (b)); \ + } \ + else \ + { \ + gripe_nonconformant ("operator .^", a_dims, b_dims); \ + return octave_value (); \ + } \ } \ T1 ## NDArray result (a_dims); \ for (int i = 0; i < a.length (); i++) \ @@ -741,8 +756,15 @@ dim_vector b_dims = b.dims (); \ if (a_dims != b_dims) \ { \ - gripe_nonconformant ("operator .^", a_dims, b_dims); \ - return octave_value (); \ + if (is_valid_bsxfun (a_dims, b_dims)) \ + { \ + return bsxfun_pow (static_cast (a), b); \ + } \ + else \ + { \ + gripe_nonconformant ("operator .^", a_dims, b_dims); \ + return octave_value (); \ + } \ } \ T2 ## NDArray result (a_dims); \ for (int i = 0; i < a.length (); i++) \ @@ -760,8 +782,15 @@ dim_vector b_dims = b.dims (); \ if (a_dims != b_dims) \ { \ - gripe_nonconformant ("operator .^", a_dims, b_dims); \ - return octave_value (); \ + if (is_valid_bsxfun (a_dims, b_dims)) \ + { \ + return bsxfun_pow (a, static_cast (b)); \ + } \ + else \ + { \ + gripe_nonconformant ("operator .^", a_dims, b_dims); \ + return octave_value (); \ + } \ } \ T1 ## NDArray result (a_dims); \ for (int i = 0; i < a.length (); i++) \ @@ -779,8 +808,15 @@ dim_vector b_dims = b.dims (); \ if (a_dims != b_dims) \ { \ - gripe_nonconformant ("operator .^", a_dims, b_dims); \ - return octave_value (); \ + if (is_valid_bsxfun (a_dims, b_dims)) \ + { \ + return bsxfun_pow (static_cast (a), b); \ + } \ + else \ + { \ + gripe_nonconformant ("operator .^", a_dims, b_dims); \ + return octave_value (); \ + } \ } \ T2 ## NDArray result (a_dims); \ for (int i = 0; i < a.length (); i++) \ diff -r d9d65c3017c3 -r 4061106b1c4b src/xpow.cc --- a/src/xpow.cc Wed Aug 24 23:06:59 2011 -0500 +++ b/src/xpow.cc Wed Aug 24 23:12:28 2011 -0500 @@ -49,6 +49,8 @@ #include "utils.h" #include "xpow.h" +#include "bsxfun.h" + #ifdef _OPENMP #include #endif @@ -1243,8 +1245,21 @@ if (a_dims != b_dims) { - gripe_nonconformant ("operator .^", a_dims, b_dims); - return octave_value (); + if (is_valid_bsxfun (a_dims, b_dims)) + { + //Potentially complex results + NDArray xa = octave_value_extract (a); + NDArray xb = octave_value_extract (b); + if (! xb.all_integers () && xa.any_element_is_negative ()) + return octave_value (bsxfun_pow (ComplexNDArray (xa), xb)); + else + return octave_value (bsxfun_pow (xa, xb)); + } + else + { + gripe_nonconformant ("operator .^", a_dims, b_dims); + return octave_value (); + } } int len = a.length (); @@ -1318,8 +1333,15 @@ if (a_dims != b_dims) { - gripe_nonconformant ("operator .^", a_dims, b_dims); - return octave_value (); + if (is_valid_bsxfun (a_dims, b_dims)) + { + return bsxfun_pow (a, b); + } + else + { + gripe_nonconformant ("operator .^", a_dims, b_dims); + return octave_value (); + } } ComplexNDArray result (a_dims); @@ -1410,8 +1432,15 @@ if (a_dims != b_dims) { - gripe_nonconformant ("operator .^", a_dims, b_dims); - return octave_value (); + if (is_valid_bsxfun (a_dims, b_dims)) + { + return bsxfun_pow (a, b); + } + else + { + gripe_nonconformant ("operator .^", a_dims, b_dims); + return octave_value (); + } } ComplexNDArray result (a_dims); @@ -1453,8 +1482,15 @@ if (a_dims != b_dims) { - gripe_nonconformant ("operator .^", a_dims, b_dims); - return octave_value (); + if (is_valid_bsxfun (a_dims, b_dims)) + { + return bsxfun_pow (a, b); + } + else + { + gripe_nonconformant ("operator .^", a_dims, b_dims); + return octave_value (); + } } ComplexNDArray result (a_dims);