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})\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{m}, @var{n})\n\
66 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{s}, @var{m}, @var{n}, \"unique\")\n\
67 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{sv}, @var{m}, @var{n}, @var{nzmax})\n\
68 Create a sparse matrix from a full matrix, or row, column, value triplets.\n\
70 If @var{a} is a full matrix, convert it to a sparse matrix representation,\n\
71 removing all zero values in the process.\n\
73 Given the integer index vectors @var{i} and @var{j}, and a 1-by-@code{nnz}\n\
74 vector of real or complex values @var{sv}, construct the sparse matrix\n\
75 @code{S(@var{i}(@var{k}),@var{j}(@var{k})) = @var{sv}(@var{k})} with overall\n\
76 dimensions @var{m} and @var{n}. If any of @var{sv}, @var{i} or @var{j} are\n\
77 scalars, they are expanded to have a common size.\n\
79 If @var{m} or @var{n} are not specified their values are derived from the\n\
80 maximum index in the vectors @var{i} and @var{j} as given by\n\
81 @code{@var{m} = max (@var{i})}, @code{@var{n} = max (@var{j})}.\n\
83 @strong{Note}: if multiple values are specified with the same @var{i},\n\
84 @var{j} indices, the corresponding value in @var{s} will be the sum of the\n\
85 values at the repeated location. See @code{accumarray} for an example of how\n\
86 to produce different behavior, such as taking the minimum instead.\n\
88 If the option @qcode{\"unique\"} is given, and more than one value is\n\
89 specified at the same @var{i}, @var{j} indices, then the last specified\n\
90 value will be used.\n\
92 @code{sparse (@var{m}, @var{n})} will create an empty @var{m}x@var{n} sparse\n\
93 matrix and is equivalent to @code{sparse ([], [], [], @var{m}, @var{n})}\n\
95 The argument @code{nzmax} is ignored but accepted for compatibility with\n\
98 Example 1 (sum at repeated indices):\n\
102 @var{i} = [1 1 2]; @var{j} = [1 1 2]; @var{sv} = [3 4 5];\n\
103 sparse (@var{i}, @var{j}, @var{sv}, 3, 4)\n\
105 Compressed Column Sparse (rows = 3, cols = 4, nnz = 2 [17%])\n\
112 Example 2 (\"unique\" option):\n\
116 @var{i} = [1 1 2]; @var{j} = [1 1 2]; @var{sv} = [3 4 5];\n\
117 sparse (@var{i}, @var{j}, @var{sv}, 3, 4, \"unique\")\n\
119 Compressed Column Sparse (rows = 3, cols = 4, nnz = 2 [17%])\n\
125 @seealso{full, accumarray, spalloc, spdiags, speye, spones, sprand, sprandn, sprandsym, spconvert, spfun}\n\
129 int nargin = args.
length ();
148 else if (nargin == 2)
157 if (m >= 0 && n >= 0)
160 error (
"sparse: dimensions must be non-negative");
163 else if (nargin >= 3)
165 bool summation =
true;
166 if (nargin > 3 && args(nargin-1).
is_string ())
168 std::string opt = args(nargin-1).string_value ();
171 else if (opt ==
"sum" || opt ==
"summation")
174 error (
"sparse: invalid option: %s", opt.c_str ());
185 nzmax = args(5).idx_type_value ();
194 error (
"sparse: dimensions must be non-negative");
196 else if (nargin != 3)
206 m, n, summation, nzmax);
209 i, j, m, n, summation, nzmax);
212 m, n, summation, nzmax);
223 DEFUN (spalloc, args, ,
225 @deftypefn {Built-in Function} {@var{s} =} spalloc (@var{m}, @var{n}, @var{nz})\n\
226 Create an @var{m}-by-@var{n} sparse matrix with pre-allocated space for at\n\
227 most @var{nz} nonzero elements.\n\
229 This is useful for building a matrix incrementally by a sequence of indexed\n\
230 assignments. Subsequent indexed assignments after @code{spalloc} will reuse\n\
231 the pre-allocated memory, provided they are of one of the simple forms\n\
234 @item @code{@var{s}(I:J) = @var{x}}\n\
236 @item @code{@var{s}(:,I:J) = @var{x}}\n\
238 @item @code{@var{s}(K:L,I:J) = @var{x}}\n\
241 @b{and} that the following conditions are met:\n\
244 @item the assignment does not decrease nnz (@var{S}).\n\
246 @item after the assignment, nnz (@var{S}) does not exceed @var{nz}.\n\
248 @item no index is out of bounds.\n\
251 Partial movement of data may still occur, but in general the assignment will\n\
252 be more memory and time efficient under these circumstances. In particular,\n\
253 it is possible to efficiently build a pre-allocated sparse matrix from a\n\
254 contiguous block of columns.\n\
256 The amount of pre-allocated memory for a given matrix may be queried using\n\
257 the function @code{nzmax}.\n\
258 @seealso{nzmax, sparse}\n\
262 int nargin = args.
length ();
264 if (nargin == 2 || nargin == 3)
270 nz = args(2).idx_type_value ();
273 else if (m >= 0 && n >= 0 && nz >= 0)
276 error (
"spalloc: M,N,NZ must be non-negative");
void get_dimensions(const octave_value &a, const char *warn_for, dim_vector &dim)
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
void gripe_wrong_type_arg(const char *name, const char *s, bool is_error)
OCTINTERP_API void print_usage(void)
bool is_numeric_type(void) const
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
octave_idx_type nzmax(void) const
boolNDArray bool_array_value(bool warn=false) const
bool is_sparse_type(void) const
bool is_bool_type(void) const
bool is_string(void) const
SparseBoolMatrix sparse_bool_matrix_value(bool warn=false) const
bool is_complex_type(void) const
octave_idx_type length(void) const
SparseComplexMatrix sparse_complex_matrix_value(bool frc_str_conv=false) const
NDArray array_value(bool frc_str_conv=false) const
SparseMatrix sparse_matrix_value(bool frc_str_conv=false) const
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))