Mercurial > hg > octave-lyh
changeset 5602:dfa2da0563e5
[project @ 2006-01-31 03:43:40 by jwe]
author | jwe |
---|---|
date | Tue, 31 Jan 2006 03:43:41 +0000 |
parents | c8ed37d87923 |
children | 2c66c36d2698 |
files | liboctave/ChangeLog liboctave/MArray.h liboctave/MArrayN.h liboctave/boolNDArray.h liboctave/so-array.h src/Cell.cc src/Cell.h src/ChangeLog src/DLD-FUNCTIONS/sparse.cc src/data.cc src/gripes.cc src/gripes.h src/ov-base-mat.h src/ov-base-scalar.h src/ov-base.cc src/ov-base.h src/ov.h |
diffstat | 17 files changed, 151 insertions(+), 75 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,10 @@ +2006-01-30 John W. Eaton <jwe@octave.org> + + * so-array.h (streamoff_array::nnz): New funtion. + * boolNDArray.h (boolNDArray::nnz): New function. + * MArrayN.h (MArrayN<T>::nnz): New function. + * MArray.h (MArray<T>::nnz): New function. + 2006-01-04 David Bateman <dbateman@free.fr> * Spars-op-defs.h (SPARSE_SPARSE_MUL): Previous change resulted in
--- a/liboctave/MArray.h +++ b/liboctave/MArray.h @@ -63,6 +63,24 @@ 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; + } + + // Currently, the OPS functions don't need to be friends, but that // may change.
--- a/liboctave/MArrayN.h +++ b/liboctave/MArrayN.h @@ -70,6 +70,23 @@ 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 ArrayN<T>::reshape (new_dims); }
--- a/liboctave/boolNDArray.h +++ b/liboctave/boolNDArray.h @@ -93,6 +93,23 @@ // 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; + } + private: boolNDArray (bool *d, dim_vector& dv) : ArrayN<bool> (d, dv) { }
--- a/liboctave/so-array.h +++ b/liboctave/so-array.h @@ -62,6 +62,23 @@ streamoff_array squeeze (void) const { return ArrayN<std::streamoff>::squeeze (); } + octave_idx_type nnz (void) const + { + octave_idx_type retval = 0; + + const std::streamoff *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 all (int dim = -1) const; boolNDArray any (int dim = -1) const;
--- a/src/Cell.cc +++ b/src/Cell.cc @@ -29,6 +29,7 @@ #include "Cell.h" #include "error.h" +#include "gripes.h" Cell::Cell (const string_vector& sv) : ArrayN<octave_value> () @@ -114,6 +115,13 @@ return *this; } +octave_idx_type +Cell::nnz (void) const +{ + gripe_wrong_type_arg ("nnz", "cell array"); + return -1; +} + Cell Cell::column (octave_idx_type i) const {
--- a/src/Cell.h +++ b/src/Cell.h @@ -90,6 +90,8 @@ Cell reshape (const dim_vector& new_dims) const { return ArrayN<octave_value>::reshape (new_dims); } + octave_idx_type nnz (void) const; + Cell column (octave_idx_type i) const; // XXX FIXME XXX
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,22 @@ +2006-01-30 John W. Eaton <jwe@octave.org> + + * gripes.cc (gripe_wrong_type_arg (const char*, const char*, bool)): + New function. + (gripe_wrong_type_arg (const char*, const std::string&, bool)): + Define using const char*, const char*, bool version. + (gripe_wrong_type_arg (const char*, const octave_value&, bool)): + Define using const char*, const std::string&, bool version. + + * ov.h (octave_value::nnz): New function. + * ov-base.cc (octave_base_value::nnz): New function. + * ov-base.h: Provide decl. + * ov-base-mat.h (octave_base_matrix<MT>::nnz): New function. + * ov-base-scalar.h (octave_base_scalar<MT>::nnz): New function. + * Cell.cc (Cell::nnz): New function. + * Cell.h: Provide decl. + * data.cc (Fnnz): New function. + * DLD-FUNCTIONS/sparse.cc (Fnnz): Delete. + 2006-01-13 John W. Eaton <jwe@octave.org> * ov-struct.cc (octave_struct::print_raw): Correctly print scalar
--- a/src/DLD-FUNCTIONS/sparse.cc +++ b/src/DLD-FUNCTIONS/sparse.cc @@ -404,74 +404,6 @@ return retval; } -DEFUN_DLD (nnz, args, , - "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {@var{scalar} =} nnz (@var{SM})\n\ -returns number of non zero elements in SM\n\ -@seealso{sparse}\n\ -@end deftypefn") -{ - octave_value retval; - - if (args.length() < 1) - { - print_usage ("nnz"); - return retval; - } - - if (args(0).class_name () == "sparse") - { - // XXX FIXME XXX should nonzero be a method of octave_base_value so that the - // below can be replaced with "retval = (double) (args(0).nonzero ());" - const octave_value& rep = args(0).get_rep (); - - if (args(0).type_name () == "sparse matrix") - retval = (double) ((const octave_sparse_matrix&) rep) .nonzero (); - else if (args(0).type_name () == "sparse complex matrix") - retval = (double) ((const octave_sparse_complex_matrix&) rep) .nonzero (); - else if (args(0).type_name () == "sparse bool matrix") - retval = (double) ((const octave_sparse_bool_matrix&) rep) .nonzero (); - } - else if (args(0).type_name () == "complex matrix") - { - const ComplexMatrix M = args(0).complex_matrix_value(); - octave_idx_type nnz = 0; - for( octave_idx_type j = 0; j < M.cols(); j++) - for( octave_idx_type i = 0; i < M.rows(); i++) - if (M (i, j) != 0.) - nnz++; - retval = (double) nnz; - } - else if (args(0).type_name () == "matrix") - { - const Matrix M = args(0).matrix_value(); - octave_idx_type nnz = 0; - for( octave_idx_type j = 0; j < M.cols(); j++) - for( octave_idx_type i = 0; i < M.rows(); i++) - if (M (i, j) != 0.) - nnz++; - retval = (double) nnz; - } - else if (args(0).type_name () == "string") - { - const charMatrix M = args(0).char_matrix_value(); - octave_idx_type nnz = 0; - for( octave_idx_type j = 0; j < M.cols(); j++) - for( octave_idx_type i = 0; i < M.rows(); i++) - if (M (i, j) != 0) - nnz++; - retval = (double) nnz; - } - else if (args(0).type_name () == "scalar") - retval = args(0).scalar_value() != 0.0 ? 1.0 : 0.0; - else if (args(0).type_name () == "complex scalar") - retval = args(0).complex_value() != 0.0 ? 1.0 : 0.0; - else - gripe_wrong_type_arg ("nnz", args(0)); - - return retval; -} - DEFUN_DLD (nzmax, args, , "-*- texinfo -*-\n\ @deftypefn {Loadable Function} {@var{scalar} =} nzmax (@var{SM})\n\
--- a/src/data.cc +++ b/src/data.cc @@ -1077,6 +1077,23 @@ return retval; } +DEFUN (nnz, args, , + "-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {@var{scalar} =} nnz (@var{a})\n\ +returns number of non zero elements in @var{a}.\n\ +@seealso{sparse}\n\ +@end deftypefn") +{ + octave_value retval; + + if (args.length () == 1) + retval = args(0).nnz (); + else + print_usage ("nnz"); + + return retval; +} + DEFUN (sum, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\
--- a/src/gripes.cc +++ b/src/gripes.cc @@ -124,12 +124,18 @@ } void -gripe_wrong_type_arg (const char *name, const std::string& s, bool is_error) +gripe_wrong_type_arg (const char *name, const char *s, bool is_error) { if (is_error) - error ("%s: wrong type argument `%s'", name, s.c_str ()); + error ("%s: wrong type argument `%s'", name, s); else - warning ("%s: wrong type argument `%s'", name, s.c_str ()); + warning ("%s: wrong type argument `%s'", name, s); +} + +void +gripe_wrong_type_arg (const char *name, const std::string& s, bool is_error) +{ + gripe_wrong_type_arg (name, s.c_str (), is_error); } void @@ -138,10 +144,7 @@ { std::string type = tc.type_name (); - if (is_error) - error ("%s: wrong type argument `%s'", name, type.c_str ()); - else - warning ("%s: wrong type argument `%s'", name, type.c_str ()); + gripe_wrong_type_arg (name, type, is_error); } void
--- a/src/gripes.h +++ b/src/gripes.h @@ -76,6 +76,10 @@ gripe_data_conversion (const char *from, const char *to); extern void +gripe_wrong_type_arg (const char *name, const char *s, + bool is_error = true); + +extern void gripe_wrong_type_arg (const char *name, const std::string& s, bool is_error = true);
--- a/src/ov-base-mat.h +++ b/src/ov-base-mat.h @@ -94,6 +94,8 @@ dim_vector dims (void) const { return matrix.dims (); } + octave_idx_type nnz (void) const { return matrix.nnz (); } + octave_value reshape (const dim_vector& new_dims) const { return MT (matrix.reshape (new_dims)); }
--- a/src/ov-base-scalar.h +++ b/src/ov-base-scalar.h @@ -81,6 +81,8 @@ dim_vector dims (void) const { static dim_vector dv (1, 1); return dv; } + octave_idx_type nnz (void) const { return (scalar != ST ()) ? 1 : 0; } + octave_value permute (const Array<int>&, bool = false) const { return scalar; }
--- a/src/ov-base.cc +++ b/src/ov-base.cc @@ -173,6 +173,13 @@ return retval; } +octave_idx_type +octave_base_value::nnz (void) const +{ + gripe_wrong_type_arg ("octave_base_value::nnz ()", type_name ()); + return -1; +} + octave_value octave_base_value::reshape (const dim_vector&) const {