Mercurial > hg > octave-lojdl
changeset 7919:9d080df0c843
new NDArray constructor for ArrayN<octave_idx_type>
author | David Bateman <dbateman@free.fr> |
---|---|
date | Mon, 30 Jun 2008 15:51:31 +0200 |
parents | 78eef61f75d5 |
children | e56bb65186f6 |
files | liboctave/ChangeLog liboctave/dNDArray.cc liboctave/dNDArray.h src/ChangeLog src/DLD-FUNCTIONS/besselj.cc src/DLD-FUNCTIONS/lookup.cc src/DLD-FUNCTIONS/max.cc src/data.cc src/ov-bool.h |
diffstat | 9 files changed, 86 insertions(+), 182 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,9 @@ +2008-07-10 David Bateman <dbateman@free.fr> + + * dNDArray.cc (NDArray::NDArray (const Array<octave_idx_type>&, + bool, bool)): New constructor. + * dNDArray.h: Provide decl. + 2008-07-10 Michael Goffioul <michael.goffioul@gmail.com> * lo-specfun.h: Fix typo in erff/erfcf declaration.
--- a/liboctave/dNDArray.cc +++ b/liboctave/dNDArray.cc @@ -41,6 +41,48 @@ #if defined (HAVE_FFTW3) #include "oct-fftw.h" +NDArray::NDArray (const Array<octave_idx_type>& a, bool zero_based, + bool negative_to_nan) +{ + const octave_idx_type *pa = a.fortran_vec (); + resize (a.dims ()); + double *ptmp = fortran_vec (); + if (negative_to_nan) + { + double nan_val = lo_ieee_nan_value (); + + if (zero_based) + for (octave_idx_type i = 0; i < a.numel (); i++) + { + double val = static_cast<double> + (pa[i] + static_cast<octave_idx_type> (1)); + if (val <= 0) + ptmp[i] = nan_val; + else + ptmp[i] = val; + } + else + for (octave_idx_type i = 0; i < a.numel (); i++) + { + double val = static_cast<double> (pa[i]); + if (val <= 0) + ptmp[i] = nan_val; + else + ptmp[i] = val; + } + } + else + { + if (zero_based) + for (octave_idx_type i = 0; i < a.numel (); i++) + ptmp[i] = static_cast<double> + (pa[i] + static_cast<octave_idx_type> (1)); + else + for (octave_idx_type i = 0; i < a.numel (); i++) + ptmp[i] = static_cast<double> (pa[i]); + } +} + ComplexNDArray NDArray::fourier (int dim) const {
--- a/liboctave/dNDArray.h +++ b/liboctave/dNDArray.h @@ -47,6 +47,9 @@ NDArray (const Matrix& a) : MArrayN<double> (a) { } + NDArray (const Array<octave_idx_type>& a, bool zero_based = false, + bool negative_to_nan = false); + template <class U> NDArray (const MArrayN<U>& a) : MArrayN<double> (a) { }
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,17 @@ +2008-07-10 David Bateman <dbateman@free.fr> + + * DLD-FUNCTIONS/lookup.cc (assign): Delete. + (Flookup): Use new NDArray constructors rather than assign. + * data.cc (Fsort): Use new NDArray constructors rather than + directly assigning. + * besselj.cc (int_array2_to_matrix, int_arrayn_to_array, + int_array2_to_float_matrix, int_arrayn_to_float_array): Delete. + (do-bessel): Use new NDArray constructors. + * max.cc (MINMAX_SPARSE_BODY, MINMAX_INT_BODY, MINMAX_SINGLE_BODY, + MINMAX_DOUBLE_BODY): Use new NDArray constructors. + * ov-bool.h (array_value, float_array_value): Explictly cast + boolean scalar to double before the assignment to avoid ambiguity. + 2008-07-10 David Bateman <dbateman@free.fr> * ls-mat4.cc (read_mat_binary_data, save_mat_binary_data): Add
--- a/src/DLD-FUNCTIONS/besselj.cc +++ b/src/DLD-FUNCTIONS/besselj.cc @@ -79,80 +79,6 @@ } \ while (0) -static inline Matrix -int_array2_to_matrix (const Array2<octave_idx_type>& a) -{ - octave_idx_type nr = a.rows (); - octave_idx_type nc = a.cols (); - - Matrix retval (nr, nc); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = 0; i < nr; i++) - { - OCTAVE_QUIT; - - retval(i,j) = static_cast<double> (a(i,j)); - } - - return retval; -} - -static inline NDArray -int_arrayN_to_array (const ArrayN<octave_idx_type>& a) -{ - dim_vector dv = a.dims (); - int nel = dv.numel (); - - NDArray retval (dv); - - for (int i = 0; i < nel; i++) - { - OCTAVE_QUIT; - - retval(i) = static_cast<double> (a(i)); - } - - return retval; -} - -static inline FloatMatrix -int_array2_to_float_matrix (const Array2<octave_idx_type>& a) -{ - octave_idx_type nr = a.rows (); - octave_idx_type nc = a.cols (); - - FloatMatrix retval (nr, nc); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = 0; i < nr; i++) - { - OCTAVE_QUIT; - - retval(i,j) = static_cast<float> (a(i,j)); - } - - return retval; -} - -static inline FloatNDArray -int_arrayN_to_float_array (const ArrayN<octave_idx_type>& a) -{ - dim_vector dv = a.dims (); - int nel = dv.numel (); - - FloatNDArray retval (dv); - - for (int i = 0; i < nel; i++) - { - OCTAVE_QUIT; - - retval(i) = static_cast<float> (a(i)); - } - - return retval; -} - static void gripe_bessel_arg (const char *fn, const char *arg) { @@ -213,7 +139,7 @@ DO_BESSEL (type, alpha, x, scaled, ierr, result); if (nargout > 1) - retval(1) = int_arrayN_to_float_array (ierr); + retval(1) = NDArray (ierr); retval(0) = result; } @@ -249,7 +175,7 @@ DO_BESSEL (type, ralpha, cx, scaled, ierr, result); if (nargout > 1) - retval(1) = int_array2_to_float_matrix (ierr); + retval(1) = NDArray (ierr); retval(0) = result; } @@ -277,7 +203,7 @@ DO_BESSEL (type, alpha, x, scaled, ierr, result); if (nargout > 1) - retval(1) = int_arrayN_to_float_array (ierr); + retval(1) = NDArray (ierr); retval(0) = result; } @@ -296,7 +222,7 @@ DO_BESSEL (type, alpha, x, scaled, ierr, result); if (nargout > 1) - retval(1) = int_arrayN_to_float_array (ierr); + retval(1) = NDArray (ierr); retval(0) = result; } @@ -348,7 +274,7 @@ DO_BESSEL (type, alpha, x, scaled, ierr, result); if (nargout > 1) - retval(1) = int_arrayN_to_array (ierr); + retval(1) = NDArray (ierr); retval(0) = result; } @@ -384,7 +310,7 @@ DO_BESSEL (type, ralpha, cx, scaled, ierr, result); if (nargout > 1) - retval(1) = int_array2_to_matrix (ierr); + retval(1) = NDArray (ierr); retval(0) = result; } @@ -412,7 +338,7 @@ DO_BESSEL (type, alpha, x, scaled, ierr, result); if (nargout > 1) - retval(1) = int_arrayN_to_array (ierr); + retval(1) = NDArray (ierr); retval(0) = result; } @@ -431,7 +357,7 @@ DO_BESSEL (type, alpha, x, scaled, ierr, result); if (nargout > 1) - retval(1) = int_arrayN_to_array (ierr); + retval(1) = NDArray (ierr); retval(0) = result; } @@ -666,7 +592,7 @@ result = airy (z, kind == 1, scale, ierr); if (nargout > 1) - retval(1) = int_arrayN_to_float_array (ierr); + retval(1) = NDArray (ierr); retval(0) = result; } @@ -688,7 +614,7 @@ result = airy (z, kind == 1, scale, ierr); if (nargout > 1) - retval(1) = int_arrayN_to_array (ierr); + retval(1) = NDArray (ierr); retval(0) = result; }
--- a/src/DLD-FUNCTIONS/lookup.cc +++ b/src/DLD-FUNCTIONS/lookup.cc @@ -49,25 +49,6 @@ || str.find (std::toupper (c)) != std::string::npos); } -// FIXME -- remove these one once octave_value supports octave_idx_type. -static octave_value& -assign (octave_value& ov, octave_idx_type idx) -{ - double tmp = idx; - ov = tmp; - return ov; -} - -static octave_value& -assign (octave_value& ov, const ArrayN<octave_idx_type>& ida) -{ - NDArray tmp (ida.dims ()); - for (int i = 0; i < ida.numel (); i++) - tmp(i) = ida(i); - ov = tmp; - return ov; -} - // normal ascending comparator static bool ov_str_less (const octave_value& a, const octave_value& b) @@ -239,8 +220,7 @@ std::less<double> ()); } - //retval(0) = idx; - assign (retval(0), idx); + retval(0) = NDArray (idx); } else if (str_case) { @@ -288,8 +268,7 @@ idx(i) = bin_lookup (table.data (), table.length (), y(i), std::ptr_fun (ov_str_comp)); - //retval(0) = idx; - assign (retval(0), idx); + retval(0) = NDArray (idx); } else { @@ -298,8 +277,7 @@ idx = bin_lookup (table.data (), table.length (), argy, std::ptr_fun (ov_str_comp)); - //retval(0) = idx; - assign (retval(0), idx); + retval(0) = static_cast<double> (idx); } } else
--- a/src/DLD-FUNCTIONS/max.cc +++ b/src/DLD-FUNCTIONS/max.cc @@ -100,21 +100,7 @@ octave_idx_type len = index.numel (); \ \ if (len > 0) \ - { \ - double nan_val = lo_ieee_nan_value (); \ - \ - NDArray idx (index.dims ()); \ - \ - for (octave_idx_type i = 0; i < len; i++) \ - { \ - OCTAVE_QUIT; \ - octave_idx_type tmp = index.elem (i) + 1; \ - idx.elem (i) = (tmp <= 0) \ - ? nan_val : static_cast<double> (tmp); \ - } \ - \ - retval(1) = idx; \ - } \ + retval(1) = NDArray (index, true, true); \ else \ retval(1) = NDArray (); \ } \ @@ -276,23 +262,9 @@ octave_idx_type len = index.numel (); \ \ if (len > 0) \ - { \ - float nan_val = lo_ieee_nan_value (); \ - \ - FloatNDArray idx (index.dims ()); \ - \ - for (octave_idx_type i = 0; i < len; i++) \ - { \ - OCTAVE_QUIT; \ - octave_idx_type tmp = index.elem (i) + 1; \ - idx.elem (i) = (tmp <= 0) \ - ? nan_val : static_cast<float> (tmp); \ - } \ - \ - retval(1) = idx; \ - } \ + retval(1) = NDArray (index, true, true); \ else \ - retval(1) = FloatNDArray (); \ + retval(1) = NDArray (); \ } \ else \ { \ @@ -423,21 +395,7 @@ octave_idx_type len = index.numel (); \ \ if (len > 0) \ - { \ - double nan_val = lo_ieee_nan_value (); \ - \ - NDArray idx (index.dims ()); \ - \ - for (octave_idx_type i = 0; i < len; i++) \ - { \ - OCTAVE_QUIT; \ - octave_idx_type tmp = index.elem (i) + 1; \ - idx.elem (i) = (tmp <= 0) \ - ? nan_val : static_cast<double> (tmp); \ - } \ - \ - retval(1) = idx; \ - } \ + retval(1) = NDArray (index, true, true); \ else \ retval(1) = NDArray (); \ } \ @@ -518,21 +476,7 @@ octave_idx_type len = index.numel (); \ \ if (len > 0) \ - { \ - double nan_val = lo_ieee_nan_value (); \ - \ - NDArray idx (index.dims ()); \ - \ - for (octave_idx_type i = 0; i < len; i++) \ - { \ - OCTAVE_QUIT; \ - octave_idx_type tmp = index.elem (i) + 1; \ - idx.elem (i) = (tmp <= 0) \ - ? nan_val : static_cast<double> (tmp); \ - } \ - \ - retval(1) = idx; \ - } \ + retval(1) = NDArray (index, true, true); \ else \ retval(1) = NDArray (); \ } \
--- a/src/data.cc +++ b/src/data.cc @@ -5248,16 +5248,7 @@ Array<octave_idx_type> sidx; retval (0) = arg.sort (sidx, dim, smode); - - octave_idx_type *ps = sidx.fortran_vec (); - NDArray midx (sidx.dims ()); - double *pm = midx.fortran_vec (); - - for (octave_idx_type i = 0; i < sidx.numel (); i++) - pm [i] = static_cast<double> - (ps [i] + static_cast<octave_idx_type> (1)); - - retval (1) = midx; + retval (1) = NDArray (sidx, true); } else retval(0) = arg.sort (dim, smode);
--- a/src/ov-bool.h +++ b/src/ov-bool.h @@ -134,10 +134,10 @@ { return FloatMatrix (1, 1, scalar); } NDArray array_value (bool = false) const - { return NDArray (dim_vector (1, 1), scalar); } + { return NDArray (dim_vector (1, 1), static_cast<double> (scalar)); } FloatNDArray float_array_value (bool = false) const - { return FloatNDArray (dim_vector (1, 1), scalar); } + { return FloatNDArray (dim_vector (1, 1), static_cast<double> (scalar)); } Complex complex_value (bool = false) const { return scalar; }