changeset 10513:c5005bc2b7a9

implement working spalloc
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 13 Apr 2010 12:36:24 +0200
parents aac9f4265048
children 40c58502a78b
files scripts/ChangeLog scripts/sparse/module.mk scripts/sparse/spalloc.m src/ChangeLog src/DLD-FUNCTIONS/sparse.cc src/ov-base-sparse.cc src/ov-base.cc
diffstat 7 files changed, 66 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Apr 13 12:36:21 2010 +0200
+++ b/scripts/ChangeLog	Tue Apr 13 12:36:24 2010 +0200
@@ -1,3 +1,8 @@
+2010-04-13  Jaroslav Hajek  <highegg@gmail.com>
+
+	* sparse/spalloc.m: Remove.
+	* sparse/module.mk: Update.
+
 2010-04-12  Ben Abbott <bpabbot@mac.com>
 
 	* plot/loglog.m: Minor ticks on by default for loglog plots.
--- a/scripts/sparse/module.mk	Tue Apr 13 12:36:21 2010 +0200
+++ b/scripts/sparse/module.mk	Tue Apr 13 12:36:24 2010 +0200
@@ -9,7 +9,6 @@
   sparse/nonzeros.m \
   sparse/pcg.m \
   sparse/pcr.m \
-  sparse/spalloc.m \
   sparse/spaugment.m \
   sparse/spconvert.m \
   sparse/spdiags.m \
--- a/scripts/sparse/spalloc.m	Tue Apr 13 12:36:21 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-## Copyright (C) 2004, 2005, 2006, 2007, 2009 David Bateman and Andy Adler
-##
-## This file is part of Octave.
-##
-## Octave is free software; you can redistribute it and/or modify it
-## under the terms of the GNU General Public License as published by
-## the Free Software Foundation; either version 3 of the License, or (at
-## your option) any later version.
-##
-## Octave is distributed in the hope that it will be useful, but
-## WITHOUT ANY WARRANTY; without even the implied warranty of
-## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-## General Public License for more details.
-##
-## You should have received a copy of the GNU General Public License
-## along with Octave; see the file COPYING.  If not, see
-## <http://www.gnu.org/licenses/>.
-
-## -*- texinfo -*-
-## @deftypefn {Function File} {@var{s} =} spalloc (@var{r}, @var{c}, @var{nz})
-## Returns an empty sparse matrix of size @var{r}-by-@var{c}.  As Octave
-## resizes sparse matrices at the first opportunity, so that no additional 
-## space is needed, the argument @var{nz} is ignored.  This function is 
-## provided only for compatibility reasons.
-##
-## It should be noted that this means that code like
-##
-## @example
-## @group
-## k = 5;
-## nz = r * k;
-## s = spalloc (r, c, nz)
-## for j = 1:c
-##   idx = randperm (r);
-##   s (:, j) = [zeros(r - k, 1); rand(k, 1)] (idx);
-## endfor
-## @end group
-## @end example
-##
-## will reallocate memory at each step.  It is therefore vitally important
-## that code like this is vectorized as much as possible.
-## @seealso{sparse, nzmax}
-## @end deftypefn
-
-function s = spalloc (r, c, nz)
-
-  if (nargin < 2)
-    print_usage ();
-  endif
-
-  s = sparse (r, c);
-endfunction
--- a/src/ChangeLog	Tue Apr 13 12:36:21 2010 +0200
+++ b/src/ChangeLog	Tue Apr 13 12:36:24 2010 +0200
@@ -1,3 +1,8 @@
+2010-04-13  Jaroslav Hajek  <highegg@gmail.com>
+
+	* DLD-FUNCTIONS/sparse.cc (Fspalloc): New DEFUN.
+	* ov-base.cc (octave_base_value::nzmax): Return numel by default.
+
 2010-04-13  Jaroslav Hajek  <highegg@gmail.com>
 
 	* ov-base-sparse.cc (octave_base_sparse::assign): Rewrite.
--- a/src/DLD-FUNCTIONS/sparse.cc	Tue Apr 13 12:36:21 2010 +0200
+++ b/src/DLD-FUNCTIONS/sparse.cc	Tue Apr 13 12:36:24 2010 +0200
@@ -196,3 +196,57 @@
 
    return retval;
 }
+
+DEFUN_DLD (spalloc, args, ,
+    "-*- texinfo -*-\n\
+@deftypefn {Loadable Function} {@var{s} =} spalloc (@var{m}, @var{n}, @var{nz})\n\
+Creates a @var{m}-by-@var{n} sparse matrix with preallocated space for at most\n\
+@var{nz} nonzero elements. This is useful for building the matrix incrementally\n\
+by a sequence of indexed assignments. Subsequent indexed assignments will reuse\n\
+the pre-allocated memory, provided they are of one of the simple forms\n\
+\n\
+@itemize\n\
+@item @code{@var{s}(I:J) = @var{x}}\n\
+@item @code{@var{s}(:,I:J) = @var{x}}\n\
+@item @code{@var{s}(K:L,I:J) = @var{x}}\n\
+@end itemize\n\
+\n\
+@b{and} that the following conditions are met:\n\
+\n\
+@itemize\n\
+@item the assignment does not decrease nnz(@var{S}).\n\
+@item after the assignment, nnz(@var{S}) does not exceed @var{nz}.\n\
+@item no index is out of bounds.\n\
+@end itemize\n\
+\n\
+Partial movement of data may still occur, but in general the assignment will be more\n\
+memory and time-efficient under these circumstances. In particular, it is possible\n\
+to efficiently build a pre-allocated sparse matrix from contiguous block of columns.\n\
+\n\
+The amount of preallocated memory for a given matrix may be queried using the function\n\
+@code{nzmax}.\n\
+@seealso{nzmax, sparse}\n\
+@end deftypefn")
+{
+   octave_value retval;
+   int nargin = args.length ();
+
+   if (nargin == 2 || nargin == 3)
+     {
+       octave_idx_type m = args(0).idx_type_value ();
+       octave_idx_type n = args(1).idx_type_value ();
+       octave_idx_type nz = 0;
+       if (nargin == 3)
+         nz = args(2).idx_type_value ();
+       if (error_state)
+         ;
+       else if (m >= 0 && n >= 0 && nz >= 0)
+         retval = SparseMatrix (dim_vector (m, n), nz);
+       else
+         error ("spalloc: m,n,nz must be non-negative");
+     }
+   else
+     print_usage ();
+
+   return retval;
+}
--- a/src/ov-base-sparse.cc	Tue Apr 13 12:36:21 2010 +0200
+++ b/src/ov-base-sparse.cc	Tue Apr 13 12:36:24 2010 +0200
@@ -261,7 +261,7 @@
   bool retval = false;
   dim_vector dv = matrix.dims ();
   octave_idx_type nel = dv.numel ();
-  octave_idx_type nz = nzmax ();
+  octave_idx_type nz = nnz ();
 
   if (nz == nel && nel > 0)
     {
--- a/src/ov-base.cc	Tue Apr 13 12:36:21 2010 +0200
+++ b/src/ov-base.cc	Tue Apr 13 12:36:24 2010 +0200
@@ -299,8 +299,7 @@
 octave_idx_type
 octave_base_value::nzmax (void) const
 {
-  gripe_wrong_type_arg ("octave_base_value::nzmax ()", type_name ());
-  return -1;
+  return numel ();
 }
 
 octave_idx_type