Mercurial > hg > octave-lojdl
changeset 7602:7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 19 Mar 2008 10:38:33 -0400 |
parents | 8a939b217863 |
children | 689652eb95d1 |
files | liboctave/CSparse.cc liboctave/ChangeLog liboctave/MSparse.h liboctave/Sparse.h liboctave/dSparse.cc |
diffstat | 5 files changed, 83 insertions(+), 296 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/CSparse.cc +++ b/liboctave/CSparse.cc @@ -39,6 +39,7 @@ #include "CSparse.h" #include "boolSparse.h" #include "dSparse.h" +#include "functor.h" #include "oct-spparms.h" #include "SparseCmplxLU.h" #include "oct-sparse.h" @@ -7445,164 +7446,19 @@ SparseMatrix SparseComplexMatrix::map (dmapper fcn) const { - SparseMatrix result; - double f_zero = fcn (0.); - - if (f_zero != 0.) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (data(i)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseMatrix (nr, nc, nz); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - double val = fcn (data (i)); - if (val != 0.0) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse<Complex>::map<double> (func_ptr (fcn)); } SparseComplexMatrix SparseComplexMatrix::map (cmapper fcn) const { - SparseComplexMatrix result; - Complex f_zero = fcn (0.); - - if (f_zero != 0.) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseComplexMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (data(i)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseComplexMatrix (nr, nc, nz); - Complex zero = Complex (0.0, 0.0); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - Complex val = fcn (data (i)); - if (val != zero) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse<Complex>::map<Complex> (func_ptr (fcn)); } SparseBoolMatrix SparseComplexMatrix::map (bmapper fcn) const { - SparseBoolMatrix result; - bool f_zero = fcn (0.); - - if (f_zero) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseBoolMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (data(i)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseBoolMatrix (nr, nc, nz); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - bool val = fcn (data (i)); - if (val) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse<Complex>::map<bool> (func_ptr (fcn)); } std::ostream&
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,17 @@ +2008-03-19 David Bateman <dbateman@free.fr> + + * Sparse.h (template <class U, class F> Sparse<U> map (F fcn) + const): New template function. + * MSparse.h (template <class U, class F> MSparse<U> map (F fcn) + const): ditto. + * dSparse.cc (SparseMatrix SparseMatrix::map (dmapper) const, + SparseComplexMatrix SparseMatrix::map (cmapper) const, + SparseBoolMatrix SparseMatrix::map (bmapper) const): Rewrite in + terms of the new template functor. + * CSparse.cc (SparseMatrix SparseComplexMatrix::map (dmapper) const, + SparseComplexMatrix SparseComplexMatrix::map (cmapper) const, + SparseBoolMatrix SparseComplexMatrix::map (bmapper) const): ditto. + 2008-03-18 David Bateman <dbateman@free.fr> * lo-specfun.cc (Complex xlgamma (const Complex&)): New function.
--- a/liboctave/MSparse.h +++ b/liboctave/MSparse.h @@ -112,6 +112,12 @@ { return Sparse<T>::ipermute (vec); } + template <class U, class F> + MSparse<U> map (F fcn) const + { + return Sparse<T>::template map<U> (fcn); + } + // Currently, the OPS functions don't need to be friends, but that // may change.
--- a/liboctave/Sparse.h +++ b/liboctave/Sparse.h @@ -521,6 +521,61 @@ Sparse<T> sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; Sparse<T> sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0, sortmode mode = ASCENDING) const; + + template <class U, class F> + Sparse<U> + map (F fcn) const + { + Sparse<U> result; + U f_zero = fcn (0.); + + if (f_zero != 0.) + { + octave_idx_type nr = rows (); + octave_idx_type nc = cols (); + + result = Sparse<U> (nr, nc, f_zero); + + for (octave_idx_type j = 0; j < nc; j++) + for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) + { + OCTAVE_QUIT; + /* Use data instead of elem for better performance. */ + result.data (ridx (i) + j * nr) = fcn (data(i)); + } + + result.maybe_compress (true); + } + else + { + octave_idx_type nz = nnz (); + octave_idx_type nr = rows (); + octave_idx_type nc = cols (); + + result = Sparse<U> (nr, nc, nz); + octave_idx_type ii = 0; + result.cidx (ii) = 0; + + for (octave_idx_type j = 0; j < nc; j++) + { + for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) + { + U val = fcn (data (i)); + if (val != 0.0) + { + result.data (ii) = val; + result.ridx (ii++) = ridx (i); + } + OCTAVE_QUIT; + } + result.cidx (j+1) = ii; + } + + result.maybe_compress (false); + } + + return result; + } }; // NOTE: these functions should be friends of the Sparse<T> class and
--- a/liboctave/dSparse.cc +++ b/liboctave/dSparse.cc @@ -39,6 +39,7 @@ #include "CSparse.h" #include "boolSparse.h" #include "dSparse.h" +#include "functor.h" #include "oct-spparms.h" #include "SparsedbleLU.h" #include "MatrixType.h" @@ -7537,164 +7538,19 @@ SparseMatrix SparseMatrix::map (dmapper fcn) const { - SparseMatrix result; - double f_zero = fcn (0.); - - if (f_zero != 0.) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (data(i)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseMatrix (nr, nc, nz); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - double val = fcn (data (i)); - if (val != 0.0) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse<double>::map<double> (func_ptr (fcn)); } SparseComplexMatrix SparseMatrix::map (cmapper fcn) const { - SparseComplexMatrix result; - Complex f_zero = fcn (0.); - - if (f_zero != 0.) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseComplexMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (Complex (data(i), 0.0)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseComplexMatrix (nr, nc, nz); - Complex zero = Complex (0.0, 0.0); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - Complex val = fcn (Complex (data (i), 0.0)); - if (val != zero) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse<double>::map<Complex> (func_ptr (fcn)); } SparseBoolMatrix SparseMatrix::map (bmapper fcn) const { - SparseBoolMatrix result; - bool f_zero = fcn (0.); - - if (f_zero) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseBoolMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (data(i)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseBoolMatrix (nr, nc, nz); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - bool val = fcn (data (i)); - if (val) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse<double>::map<bool> (func_ptr (fcn)); } std::ostream&