44 DEFUN (issparse, args, ,
46 @deftypefn {Built-in Function} {} issparse (@var{x})\n\
47 Return true if @var{x} is a sparse matrix.\n\
51 if (args.length () != 1)
60 DEFUN (sparse, args, ,
62 @deftypefn {Built-in Function} {@var{s} =} sparse (@var{a})\n\
63 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{sv}, @var{m}, @var{n}, @var{nzmax})\n\
64 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{sv})\n\
65 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{s}, @var{m}, @var{n}, \"unique\")\n\
66 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{m}, @var{n})\n\
67 Create a sparse matrix from the full matrix or row, column, value triplets.\n\
68 If @var{a} is a full matrix, convert it to a sparse matrix representation,\n\
69 removing all zero values in the process.\n\
71 Given the integer index vectors @var{i} and @var{j}, a 1-by-@code{nnz} vector\n\
72 of real of complex values @var{sv}, overall dimensions @var{m} and @var{n}\n\
73 of the sparse matrix. The argument @code{nzmax} is ignored but accepted for\n\
74 compatibility with @sc{matlab}. If @var{m} or @var{n} are not specified\n\
75 their values are derived from the maximum index in the vectors @var{i} and\n\
76 @var{j} as given by @code{@var{m} = max (@var{i})},\n\
77 @code{@var{n} = max (@var{j})}.\n\
79 @strong{Note}: if multiple values are specified with the same\n\
80 @var{i}, @var{j} indices, the corresponding values in @var{s} will\n\
81 be added. See @code{accumarray} for an example of how to produce different\n\
82 behavior, such as taking the minimum instead.\n\
84 The following are all equivalent:\n\
88 s = sparse (i, j, s, m, n)\n\
89 s = sparse (i, j, s, m, n, \"summation\")\n\
90 s = sparse (i, j, s, m, n, \"sum\")\n\
94 Given the option @qcode{\"unique\"}, if more than two values are specified\n\
95 for the same @var{i}, @var{j} indices, the last specified value will be\n\
98 @code{sparse (@var{m}, @var{n})} is equivalent to\n\
99 @code{sparse ([], [], [], @var{m}, @var{n}, 0)}\n\
101 If any of @var{sv}, @var{i} or @var{j} are scalars, they are expanded\n\
102 to have a common size.\n\
103 @seealso{full, accumarray}\n\
107 int nargin = args.
length ();
126 else if (nargin == 2)
131 m = args(0).idx_type_value ();
132 n = args(1).idx_type_value ();
135 error (
"sparse: dimensions M,N must be scalar");
139 if (m >= 0 && n >= 0)
142 error (
"sparse: dimensions M,N must be positive or zero");
145 else if (nargin >= 3)
147 bool summation =
true;
148 if (nargin > 3 && args(nargin-1).
is_string ())
150 std::string opt = args(nargin-1).string_value ();
153 else if (opt ==
"sum" || opt ==
"summation")
156 error (
"sparse: invalid option: %s", opt.c_str ());
166 nzmax = args(5).idx_type_value ();
174 m = args(3).idx_type_value ();
175 n = args(4).idx_type_value ();
178 error (
"sparse: expecting scalar dimensions");
182 error (
"sparse: dimensions must be non-negative");
184 else if (nargin != 3)
194 m, n, summation,
nzmax);
197 i, j, m, n, summation,
nzmax);
200 m, n, summation,
nzmax);
211 DEFUN (spalloc, args, ,
213 @deftypefn {Built-in Function} {@var{s} =} spalloc (@var{m}, @var{n}, @var{nz})\n\
214 Create an @var{m}-by-@var{n} sparse matrix with pre-allocated space for at\n\
215 most @var{nz} nonzero elements. This is useful for building the matrix\n\
216 incrementally by a sequence of indexed assignments. Subsequent indexed\n\
217 assignments will reuse the pre-allocated memory, provided they are of one of\n\
221 @item @code{@var{s}(I:J) = @var{x}}\n\
223 @item @code{@var{s}(:,I:J) = @var{x}}\n\
225 @item @code{@var{s}(K:L,I:J) = @var{x}}\n\
228 @b{and} that the following conditions are met:\n\
231 @item the assignment does not decrease nnz (@var{S}).\n\
233 @item after the assignment, nnz (@var{S}) does not exceed @var{nz}.\n\
235 @item no index is out of bounds.\n\
238 Partial movement of data may still occur, but in general the assignment will\n\
239 be more memory and time-efficient under these circumstances. In particular,\n\
240 it is possible to efficiently build a pre-allocated sparse matrix from\n\
241 contiguous block of columns.\n\
243 The amount of pre-allocated memory for a given matrix may be queried using\n\
244 the function @code{nzmax}.\n\
245 @seealso{nzmax, sparse}\n\
249 int nargin = args.
length ();
251 if (nargin == 2 || nargin == 3)
257 nz = args(2).idx_type_value ();
260 else if (m >= 0 && n >= 0 && nz >= 0)
263 error (
"spalloc: M,N,NZ must be non-negative");