Mercurial > hg > octave-lyh
diff src/DLD-FUNCTIONS/sparse.cc @ 7515:f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
author | David Bateman <dbateman@free.fr> |
---|---|
date | Fri, 22 Feb 2008 15:50:51 +0100 |
parents | f5005d9510f4 |
children | 8a42498edb30 |
line wrap: on
line diff
--- a/src/DLD-FUNCTIONS/sparse.cc +++ b/src/DLD-FUNCTIONS/sparse.cc @@ -39,12 +39,6 @@ #include "ov-cx-sparse.h" #include "ov-bool-sparse.h" -static bool -is_sparse (const octave_value& arg) -{ - return (arg.is_sparse_type ()); -} - DEFUN_DLD (issparse, args, , "-*- texinfo -*-\n\ @deftypefn {Loadable Function} {} issparse (@var{expr})\n\ @@ -57,7 +51,7 @@ return octave_value (); } else - return octave_value (is_sparse (args(0))); + return octave_value (args(0).is_sparse_type ()); } DEFUN_DLD (sparse, args, , @@ -132,7 +126,7 @@ { octave_value arg = args (0); - if (is_sparse (arg)) + if (arg.is_sparse_type ()) { if (use_complex) { @@ -387,342 +381,6 @@ return retval; } -#define SPARSE_DIM_ARG_BODY(NAME, FUNC) \ - int nargin = args.length(); \ - octave_value retval; \ - if ((nargin != 1 ) && (nargin != 2)) \ - print_usage (); \ - else { \ - int dim = (nargin == 1 ? -1 : args(1).int_value(true) - 1); \ - if (error_state) return retval; \ - if (dim < -1 || dim > 1) { \ - error (#NAME ": invalid dimension argument = %d", dim + 1); \ - return retval; \ - } \ - if (args(0).type_id () == \ - octave_sparse_matrix::static_type_id () || args(0).type_id () == \ - octave_sparse_bool_matrix::static_type_id ()) { \ - retval = args(0).sparse_matrix_value () .FUNC (dim); \ - } else if (args(0).type_id () == \ - octave_sparse_complex_matrix::static_type_id ()) { \ - retval = args(0).sparse_complex_matrix_value () .FUNC (dim); \ - } else \ - print_usage (); \ - } \ - return retval - -// PKG_ADD: dispatch ("prod", "spprod", "sparse matrix"); -// PKG_ADD: dispatch ("prod", "spprod", "sparse complex matrix"); -// PKG_ADD: dispatch ("prod", "spprod", "sparse bool matrix"); -DEFUN_DLD (spprod, args, , - "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {@var{y} =} spprod (@var{x},@var{dim})\n\ -Product of elements along dimension @var{dim}. If @var{dim} is omitted,\n\ -it defaults to 1 (column-wise products).\n\ -@seealso{spsum, spsumsq}\n\ -@end deftypefn") -{ - SPARSE_DIM_ARG_BODY (spprod, prod); -} - -// PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse matrix"); -// PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse complex matrix"); -// PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse bool matrix"); -DEFUN_DLD (spcumprod, args, , - "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {@var{y} =} spcumprod (@var{x},@var{dim})\n\ -Cumulative product of elements along dimension @var{dim}. If @var{dim}\n\ -is omitted, it defaults to 1 (column-wise cumulative products).\n\ -@seealso{spcumsum}\n\ -@end deftypefn") -{ - SPARSE_DIM_ARG_BODY (spcumprod, cumprod); -} - -// PKG_ADD: dispatch ("sum", "spsum", "sparse matrix"); -// PKG_ADD: dispatch ("sum", "spsum", "sparse complex matrix"); -// PKG_ADD: dispatch ("sum", "spsum", "sparse bool matrix"); -DEFUN_DLD (spsum, args, , - "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {@var{y} =} spsum (@var{x},@var{dim})\n\ -Sum of elements along dimension @var{dim}. If @var{dim} is omitted, it\n\ -defaults to 1 (column-wise sum).\n\ -@seealso{spprod, spsumsq}\n\ -@end deftypefn") -{ - SPARSE_DIM_ARG_BODY (spsum, sum); -} - -// PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse matrix"); -// PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse complex matrix"); -// PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse bool matrix"); -DEFUN_DLD (spcumsum, args, , - "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {@var{y} =} spcumsum (@var{x},@var{dim})\n\ -Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ -is omitted, it defaults to 1 (column-wise cumulative sums).\n\ -@seealso{spcumprod}\n\ -@end deftypefn") -{ - SPARSE_DIM_ARG_BODY (spcumsum, cumsum); -} - -// PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse matrix"); -// PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse complex matrix"); -// PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse bool matrix"); -DEFUN_DLD (spsumsq, args, , - "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {@var{y} =} spsumsq (@var{x},@var{dim})\n\ -Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ -is omitted, it defaults to 1 (column-wise sum of squares).\n\ -This function is equivalent to computing\n\ -@example\n\ -spsum (x .* spconj (x), dim)\n\ -@end example\n\ -but it uses less memory and avoids calling @code{spconj} if @var{x} is\n\ -real.\n\ -@seealso{spprod, spsum}\n\ -@end deftypefn") -{ - SPARSE_DIM_ARG_BODY (spsumsq, sumsq); -} - - -static octave_value -make_spdiag (const octave_value& a, const octave_value& b) -{ - octave_value retval; - - if (a.is_complex_type ()) - { - SparseComplexMatrix m = a.sparse_complex_matrix_value (); - octave_idx_type k = b.nint_value(true); - - if (error_state) - return retval; - - octave_idx_type nr = m.rows (); - octave_idx_type nc = m.columns (); - - if (nr == 0 || nc == 0) - retval = m; - else if (nr == 1 || nc == 1) - { - octave_idx_type roff = 0; - octave_idx_type coff = 0; - if (k > 0) - { - roff = 0; - coff = k; - } - else if (k < 0) - { - k = -k; - roff = k; - coff = 0; - } - - if (nr == 1) - { - octave_idx_type n = nc + k; - octave_idx_type nz = m.nzmax (); - SparseComplexMatrix r (n, n, nz); - for (octave_idx_type i = 0; i < coff+1; i++) - r.xcidx (i) = 0; - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) - { - r.xdata (i) = m.data (i); - r.xridx (i) = j + roff; - } - r.xcidx (j+coff+1) = m.cidx(j+1); - } - for (octave_idx_type i = nc+coff+1; i < n+1; i++) - r.xcidx (i) = nz; - retval = r; - } - else - { - octave_idx_type n = nr + k; - octave_idx_type nz = m.nzmax (); - octave_idx_type ii = 0; - octave_idx_type ir = m.ridx(0); - SparseComplexMatrix r (n, n, nz); - for (octave_idx_type i = 0; i < coff+1; i++) - r.xcidx (i) = 0; - for (octave_idx_type i = 0; i < nr; i++) - { - if (ir == i) - { - r.xdata (ii) = m.data (ii); - r.xridx (ii++) = ir + roff; - if (ii != nz) - ir = m.ridx (ii); - } - r.xcidx (i+coff+1) = ii; - } - for (octave_idx_type i = nr+coff+1; i < n+1; i++) - r.xcidx (i) = nz; - retval = r; - } - } - else - { - SparseComplexMatrix r = m.diag (k); - // Don't use numel, since it can overflow for very large matrices - if (r.rows () > 0 && r.cols () > 0) - retval = r; - } - } - else if (a.is_real_type ()) - { - SparseMatrix m = a.sparse_matrix_value (); - - octave_idx_type k = b.nint_value(true); - - if (error_state) - return retval; - - octave_idx_type nr = m.rows (); - octave_idx_type nc = m.columns (); - - if (nr == 0 || nc == 0) - retval = m; - else if (nr == 1 || nc == 1) - { - octave_idx_type roff = 0; - octave_idx_type coff = 0; - if (k > 0) - { - roff = 0; - coff = k; - } - else if (k < 0) - { - k = -k; - roff = k; - coff = 0; - } - - if (nr == 1) - { - octave_idx_type n = nc + k; - octave_idx_type nz = m.nzmax (); - SparseMatrix r (n, n, nz); - - for (octave_idx_type i = 0; i < coff+1; i++) - r.xcidx (i) = 0; - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) - { - r.xdata (i) = m.data (i); - r.xridx (i) = j + roff; - } - r.xcidx (j+coff+1) = m.cidx(j+1); - } - for (octave_idx_type i = nc+coff+1; i < n+1; i++) - r.xcidx (i) = nz; - retval = r; - } - else - { - octave_idx_type n = nr + k; - octave_idx_type nz = m.nzmax (); - octave_idx_type ii = 0; - octave_idx_type ir = m.ridx(0); - SparseMatrix r (n, n, nz); - for (octave_idx_type i = 0; i < coff+1; i++) - r.xcidx (i) = 0; - for (octave_idx_type i = 0; i < nr; i++) - { - if (ir == i) - { - r.xdata (ii) = m.data (ii); - r.xridx (ii++) = ir + roff; - if (ii != nz) - ir = m.ridx (ii); - } - r.xcidx (i+coff+1) = ii; - } - for (octave_idx_type i = nr+coff+1; i < n+1; i++) - r.xcidx (i) = nz; - retval = r; - } - } - else - { - SparseMatrix r = m.diag (k); - if (r.rows () > 0 && r.cols () > 0) - retval = r; - } - } - else - gripe_wrong_type_arg ("spdiag", a); - - return retval; -} - -static octave_value -make_spdiag (const octave_value& a) -{ - octave_value retval; - octave_idx_type nr = a.rows (); - octave_idx_type nc = a.columns (); - - if (nr == 0 || nc == 0) - retval = SparseMatrix (); - else - retval = make_spdiag (a, octave_value (0.)); - - return retval; -} - -// PKG_ADD: dispatch ("diag", "spdiag", "sparse matrix"); -// PKG_ADD: dispatch ("diag", "spdiag", "sparse complex matrix"); -// PKG_ADD: dispatch ("diag", "spdiag", "sparse bool matrix"); -DEFUN_DLD (spdiag, args, , - "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {} spdiag (@var{v}, @var{k})\n\ -Return a diagonal matrix with the sparse vector @var{v} on diagonal\n\ -@var{k}. The second argument is optional. If it is positive, the vector is\n\ -placed on the @var{k}-th super-diagonal. If it is negative, it is placed\n\ -on the @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and\n\ -the vector is placed on the main diagonal. For example,\n\ -\n\ -@example\n\ -@group\n\ -spdiag ([1, 2, 3], 1)\n\ -ans =\n\ -\n\ -Compressed Column Sparse (rows=4, cols=4, nnz=3)\n\ - (1 , 2) -> 1\n\ - (2 , 3) -> 2\n\ - (3 , 4) -> 3\n\ -@end group\n\ -@end example\n\ -\n\ -@noindent\n\ -Given a matrix argument, instead of a vector, @code{spdiag} extracts the\n\ -@var{k}-th diagonal of the sparse matrix.\n\ -@seealso{diag}\n\ -@end deftypefn") -{ - octave_value retval; - - int nargin = args.length (); - - if (nargin == 1 && args(0).is_defined ()) - retval = make_spdiag (args(0)); - else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) - retval = make_spdiag (args(0), args(1)); - else - print_usage (); - - return retval; -} - /* ;;; Local Variables: *** ;;; mode: C++ ***