Mercurial > hg > octave-lyh
changeset 9878:ead4f9c82a9a
implement Array<T>::nnz
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 27 Nov 2009 09:10:21 +0100 |
parents | cac3b4e5035b |
children | 034677ab6865 |
files | liboctave/Array.cc liboctave/Array.h liboctave/ChangeLog liboctave/MArray.h liboctave/MArrayN.h liboctave/boolNDArray.h |
diffstat | 6 files changed, 29 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Array.cc +++ b/liboctave/Array.cc @@ -2425,6 +2425,20 @@ } template <class T> +octave_idx_type +Array<T>::nnz (void) const +{ + const T *src = data (); + octave_idx_type nel = nelem (), retval = 0; + const T zero = T (); + for (octave_idx_type i = 0; i < nel; i++) + if (src[i] != zero) + retval++; + + return retval; +} + +template <class T> Array<octave_idx_type> Array<T>::find (octave_idx_type n, bool backward) const { @@ -2693,6 +2707,10 @@ template <> Array<bool> \ Array<T>::lookupb (const Array<T>&, sortmode) const \ { return Array<bool> (); } \ + \ +template <> octave_idx_type \ +Array<T>::nnz (void) const\ +{ return 0; } \ template <> Array<octave_idx_type> \ Array<T>::find (octave_idx_type, bool) const\ { return Array<octave_idx_type> (); } \
--- a/liboctave/Array.h +++ b/liboctave/Array.h @@ -641,6 +641,9 @@ // This looks up only exact matches, returning true/false if match. Array<bool> lookupb (const Array<T>& values, sortmode mode = UNSORTED) const; + // Count nonzero elements. + octave_idx_type nnz (void) const; + // Find indices of (at most n) nonzero elements. If n is specified, backward // specifies search from backward. Array<octave_idx_type> find (octave_idx_type n = -1, bool backward = false) const;
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,11 @@ +2009-11-27 Jaroslav Hajek <highegg@gmail.com> + + * Array.cc (Array<T>::nnz): New method. + * Array.h: Declare it. + * MArray.h (MArray<T>::nnz): Remove. + * MArrayN.h (MArrayN<T>::nnz): Remove. + * boolNDArray.h (boolNDArray::nnz): Remove. + 2009-11-25 Jaroslav Hajek <highegg@gmail.com> * dbleCHOL.cc (CHOL::init): Output LAPACK's info. Resize matrix if
--- a/liboctave/MArray.h +++ b/liboctave/MArray.h @@ -73,23 +73,6 @@ MArray<T> transpose (void) const { return Array<T>::transpose (); } MArray<T> hermitian (T (*fcn) (const T&) = 0) const { return Array<T>::hermitian (fcn); } - octave_idx_type nnz (void) const - { - octave_idx_type retval = 0; - - const T *d = this->data (); - - octave_idx_type nel = this->numel (); - - for (octave_idx_type i = 0; i < nel; i++) - { - if (d[i] != T ()) - retval++; - } - - return retval; - } - double norm (double p) const; float norm (float p) const;
--- a/liboctave/MArrayN.h +++ b/liboctave/MArrayN.h @@ -70,23 +70,6 @@ return *this; } - octave_idx_type nnz (void) const - { - octave_idx_type retval = 0; - - const T *d = this->data (); - - octave_idx_type nel = this->numel (); - - for (octave_idx_type i = 0; i < nel; i++) - { - if (d[i] != T ()) - retval++; - } - - return retval; - } - MArrayN<T> reshape (const dim_vector& new_dims) const { return Array<T>::reshape (new_dims); }
--- a/liboctave/boolNDArray.h +++ b/liboctave/boolNDArray.h @@ -101,23 +101,6 @@ // bool all_elements_are_real (void) const; // bool all_integers (double& max_val, double& min_val) const; - octave_idx_type nnz (void) const - { - octave_idx_type retval = 0; - - const bool *d = this->data (); - - octave_idx_type nel = this->numel (); - - for (octave_idx_type i = 0; i < nel; i++) - { - if (d[i]) - retval++; - } - - return retval; - } - boolNDArray diag (octave_idx_type k = 0) const; private: