annotate libinterp/corefcn/sparse.cc @ 20574:dd6345fd8a97

use exceptions for better invalid index error reporting (bug #45957) * lo-array-gripes.h, lo-array-gripes.cc (index_exception): New base class for indexing errors. (invalid_index, out_of_range): New classes. (gripe_index_out_of_range): New overloaded function. (gripe_invalid_index): New overloaded functions. Delete version with no arguments. (gripe_invalid_assignment_size, gripe_assignment_dimension_mismatch): Delete. Change uses of gripe functions as needed. * Cell.cc (Cell::index, Cell::assign, Cell::delete_elements): Use exceptions to collect error info about and handle indexing errors. * data.cc (Fnth_element, do_accumarray_sum, F__accumarray_sum__, do_accumarray_minmax, do_accumarray_minmax_fun, F__accumdim_sum__): Likewise. * oct-map.cc (octave_map::index, octave_map::assign, octave_map::delete_elements): Likewise. * sparse.cc (Fsparse): Likewise. * sub2ind.cc (Fsub2ind, Find2sub): Likewise. New tests. * utils.cc (dims_to_numel): Likewise. * ov-base-diag.cc (octave_base_diag<DMT, MT>::do_index_op, octave_base_diag<DMT, MT>::subsasgn): Likewise. * ov-base-mat.cc (octave_base_matrix<MT>::subsref, octave_base_matrix<MT>::assign): Likewise. * ov-base-sparse.cc (octave_base_sparse<T>::do_index_op, octave_base_sparse<T>::assign, octave_base_sparse<MT>::delete_elements): Likewise. * ov-classdef.cc (cdef_object_array::subsref, cdef_object_array::subsasgn): Likewise. * ov-java.cc (make_java_index): Likewise. * ov-perm.cc (octave_perm_matrix::do_index_op): Likewise. * ov-range.cc (octave_range::do_index_op): Likewise. * ov-re-diag.cc (octave_diag_matrix::do_index_op): Likewise. * ov-str-mat.cc (octave_char_matrix_str::do_index_op_internal): Likewise. * pt-assign.cc (tree_simple_assignment::rvalue1): Likewise. * pt-idx.cc (tree_index_expression::rvalue, tree_index_expression::lvalue): Likewise. * Array-util.cc (sub2ind): Likewise. * toplev.cc (main_loop): Also catch unhandled index_exception exceptions. * ov-base.cc (octave_base_value::index_vector): Improve error message. * ov-re-sparse.cc (octave_sparse_matrix::index_vector): Likewise. * ov-complex.cc (complex_index): New class. (gripe_complex_index): New function. (octave_complex::index_vector): Use it. * pt-id.h, pt-id.cc (tree_identifier::is_variable, tree_black_hole::is_variable): Now const. * pt-idx.cc (final_index_error): New static function. (tree_index_expression::rvalue, tree_index_expression::lvalue): Use it. * index.tst: New tests.
author Lachlan Andrew <lachlanbis@gmail.com>
date Fri, 02 Oct 2015 15:07:37 -0400
parents 4f45eaf83908
children f90c8372b7ba
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
1 /*
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
2
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 18850
diff changeset
3 Copyright (C) 2004-2015 David Bateman
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
4 Copyright (C) 1998-2004 Andy Adler
10521
4d1fc073fbb7 add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents: 10513
diff changeset
5 Copyright (C) 2010 VZLU Prague
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
6
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
7 This file is part of Octave.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
8
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
9 Octave is free software; you can redistribute it and/or modify it
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
10 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
11 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
12 option) any later version.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
13
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
14 Octave is distributed in the hope that it will be useful, but WITHOUT
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
17 for more details.
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
18
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
20 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
21 <http://www.gnu.org/licenses/>.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
22
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
23 */
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
24
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
26 #include <config.h>
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
27 #endif
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
28
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
29 #include <cstdlib>
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
30 #include <string>
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
31
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
32 #include "variables.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
33 #include "utils.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
34 #include "pager.h"
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10846
diff changeset
35 #include "defun.h"
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
36 #include "gripes.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
37 #include "quit.h"
10479
ded9beac7582 optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
38 #include "unwind-prot.h"
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
39
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
40 #include "ov-re-sparse.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
41 #include "ov-cx-sparse.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
42 #include "ov-bool-sparse.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
43
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10846
diff changeset
44 DEFUN (issparse, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
45 "-*- texinfo -*-\n\
15038
ab18578c2ade doc: Update docstrings for built-in functions to not use "Loadable Function" as type.
Rik <rik@octave.org>
parents: 14861
diff changeset
46 @deftypefn {Built-in Function} {} issparse (@var{x})\n\
11431
0d9640d755b1 Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents: 11097
diff changeset
47 Return true if @var{x} is a sparse matrix.\n\
0d9640d755b1 Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents: 11097
diff changeset
48 @seealso{ismatrix}\n\
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
49 @end deftypefn")
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
50 {
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
51 if (args.length () != 1)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
52 {
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
53 print_usage ();
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
54 return octave_value ();
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
55 }
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
56 else
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
57 return octave_value (args(0).is_sparse_type ());
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
58 }
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
59
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10846
diff changeset
60 DEFUN (sparse, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
61 "-*- texinfo -*-\n\
15038
ab18578c2ade doc: Update docstrings for built-in functions to not use "Loadable Function" as type.
Rik <rik@octave.org>
parents: 14861
diff changeset
62 @deftypefn {Built-in Function} {@var{s} =} sparse (@var{a})\n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
63 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{sv}, @var{m}, @var{n})\n\
15038
ab18578c2ade doc: Update docstrings for built-in functions to not use "Loadable Function" as type.
Rik <rik@octave.org>
parents: 14861
diff changeset
64 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{sv})\n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
65 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{m}, @var{n})\n\
15038
ab18578c2ade doc: Update docstrings for built-in functions to not use "Loadable Function" as type.
Rik <rik@octave.org>
parents: 14861
diff changeset
66 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{s}, @var{m}, @var{n}, \"unique\")\n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
67 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{sv}, @var{m}, @var{n}, @var{nzmax})\n\
20207
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
68 Create a sparse matrix from a full matrix, or row, column, value triplets.\n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
69 \n\
8106
8a42498edb30 Clarify doc for sparse function
David Bateman <dbateman@free.fr>
parents: 7515
diff changeset
70 If @var{a} is a full matrix, convert it to a sparse matrix representation,\n\
8a42498edb30 Clarify doc for sparse function
David Bateman <dbateman@free.fr>
parents: 7515
diff changeset
71 removing all zero values in the process.\n\
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
72 \n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
73 Given the integer index vectors @var{i} and @var{j}, and a 1-by-@code{nnz}\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
74 vector of real or complex values @var{sv}, construct the sparse matrix\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
75 @code{S(@var{i}(@var{k}),@var{j}(@var{k})) = @var{sv}(@var{k})} with overall\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
76 dimensions @var{m} and @var{n}. If any of @var{sv}, @var{i} or @var{j} are\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
77 scalars, they are expanded to have a common size.\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
78 \n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
79 If @var{m} or @var{n} are not specified their values are derived from the\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
80 maximum index in the vectors @var{i} and @var{j} as given by\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
81 @code{@var{m} = max (@var{i})}, @code{@var{n} = max (@var{j})}.\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
82 \n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
83 @strong{Note}: if multiple values are specified with the same @var{i},\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
84 @var{j} indices, the corresponding value in @var{s} will be the sum of the\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
85 values at the repeated location. See @code{accumarray} for an example of how\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
86 to produce different behavior, such as taking the minimum instead.\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
87 \n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
88 If the option @qcode{\"unique\"} is given, and more than one value is\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
89 specified at the same @var{i}, @var{j} indices, then the last specified\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
90 value will be used.\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
91 \n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
92 @code{sparse (@var{m}, @var{n})} will create an empty @var{m}x@var{n} sparse\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
93 matrix and is equivalent to @code{sparse ([], [], [], @var{m}, @var{n})}\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
94 \n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
95 The argument @code{nzmax} is ignored but accepted for compatibility with\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
96 @sc{matlab}.\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
97 \n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
98 Example 1 (sum at repeated indices):\n\
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
99 \n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
100 @example\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
101 @group\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
102 @var{i} = [1 1 2]; @var{j} = [1 1 2]; @var{sv} = [3 4 5];\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
103 sparse (@var{i}, @var{j}, @var{sv}, 3, 4)\n\
20207
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
104 @result{}\n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
105 Compressed Column Sparse (rows = 3, cols = 4, nnz = 2 [17%])\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
106 \n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
107 (1, 1) -> 7\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
108 (2, 2) -> 5\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
109 @end group\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
110 @end example\n\
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6547
diff changeset
111 \n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
112 Example 2 (\"unique\" option):\n\
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
113 \n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
114 @example\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
115 @group\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
116 @var{i} = [1 1 2]; @var{j} = [1 1 2]; @var{sv} = [3 4 5];\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
117 sparse (@var{i}, @var{j}, @var{sv}, 3, 4, \"unique\")\n\
20207
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
118 @result{}\n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
119 Compressed Column Sparse (rows = 3, cols = 4, nnz = 2 [17%])\n\
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6547
diff changeset
120 \n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
121 (1, 1) -> 4\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
122 (2, 2) -> 5\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
123 @end group\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
124 @end example\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
125 @seealso{full, accumarray, spalloc, spdiags, speye, spones, sprand, sprandn, sprandsym, spconvert, spfun}\n\
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
126 @end deftypefn")
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
127 {
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
128 octave_value retval;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
129 int nargin = args.length ();
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
130
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
131 // Temporarily disable sparse_auto_mutate if set (it's obsolete anyway).
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
132 unwind_protect frame;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
133 frame.protect_var (Vsparse_auto_mutate);
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
134 Vsparse_auto_mutate = false;
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
135
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
136 if (nargin == 1)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
137 {
18111
b560bac0fca2 maint: Don't use space between 'args' and '(' when doing indexing.
Rik <rik@octave.org>
parents: 18099
diff changeset
138 octave_value arg = args(0);
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
139 if (arg.is_bool_type ())
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
140 retval = arg.sparse_bool_matrix_value ();
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
141 else if (arg.is_complex_type ())
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
142 retval = arg.sparse_complex_matrix_value ();
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
143 else if (arg.is_numeric_type ())
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
144 retval = arg.sparse_matrix_value ();
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
145 else
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
146 gripe_wrong_type_arg ("sparse", arg);
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
147 }
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
148 else if (nargin == 2)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
149 {
18454
0821a51a9e1b allow sparse arguments for dimensions in sparse function (bug #41535)
John W. Eaton <jwe@octave.org>
parents: 17787
diff changeset
150 octave_idx_type m = 0;
0821a51a9e1b allow sparse arguments for dimensions in sparse function (bug #41535)
John W. Eaton <jwe@octave.org>
parents: 17787
diff changeset
151 octave_idx_type n = 0;
0821a51a9e1b allow sparse arguments for dimensions in sparse function (bug #41535)
John W. Eaton <jwe@octave.org>
parents: 17787
diff changeset
152
0821a51a9e1b allow sparse arguments for dimensions in sparse function (bug #41535)
John W. Eaton <jwe@octave.org>
parents: 17787
diff changeset
153 get_dimensions (args(0), args(1), "sparse", m, n);
10479
ded9beac7582 optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
154
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
155 if (! error_state)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
156 {
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
157 if (m >= 0 && n >= 0)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
158 retval = SparseMatrix (m, n);
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
159 else
18454
0821a51a9e1b allow sparse arguments for dimensions in sparse function (bug #41535)
John W. Eaton <jwe@octave.org>
parents: 17787
diff changeset
160 error ("sparse: dimensions must be non-negative");
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
161 }
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
162 }
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
163 else if (nargin >= 3)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
164 {
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
165 bool summation = true;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
166 if (nargin > 3 && args(nargin-1).is_string ())
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
167 {
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
168 std::string opt = args(nargin-1).string_value ();
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
169 if (opt == "unique")
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
170 summation = false;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
171 else if (opt == "sum" || opt == "summation")
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
172 summation = true;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
173 else
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
174 error ("sparse: invalid option: %s", opt.c_str ());
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
175
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
176 nargin -= 1;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
177 }
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
178
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
179 if (! error_state)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
180 {
18099
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
181 octave_idx_type m, n, nzmax;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
182 m = n = nzmax = -1;
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
183 if (nargin == 6)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
184 {
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
185 nzmax = args(5).idx_type_value ();
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
186 nargin --;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
187 }
10527
b4d2080b6df7 Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents: 10521
diff changeset
188
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
189 if (nargin == 5)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
190 {
18454
0821a51a9e1b allow sparse arguments for dimensions in sparse function (bug #41535)
John W. Eaton <jwe@octave.org>
parents: 17787
diff changeset
191 get_dimensions (args(3), args(4), "sparse", m, n);
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
192
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
193 if (! error_state && (m < 0 || n < 0))
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
194 error ("sparse: dimensions must be non-negative");
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
195 }
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
196 else if (nargin != 3)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
197 print_usage ();
10479
ded9beac7582 optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
198
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
199 if (! error_state)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
200 {
20574
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
201 int k = 0; // index we're checking when index_vector throws
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
202 try
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
203 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
204 idx_vector i = args(0).index_vector ();
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
205 k = 1;
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
206 idx_vector j = args(1).index_vector ();
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
207
20574
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
208 if (args(2).is_bool_type ())
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
209 retval = SparseBoolMatrix (args(2).bool_array_value (), i,j,
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
210 m, n, summation, nzmax);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
211 else if (args(2).is_complex_type ())
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
212 retval = SparseComplexMatrix (args(2).complex_array_value(),
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
213 i, j, m, n, summation, nzmax);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
214 else if (args(2).is_numeric_type ())
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
215 retval = SparseMatrix (args(2).array_value (), i, j,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
216 m, n, summation, nzmax);
20574
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
217 else
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
218 gripe_wrong_type_arg ("sparse", args(2));
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
219 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
220 catch (index_exception& e)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
221 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
222 // Rethrow to allow more info to be reported later.
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
223 e.set_pos_if_unset (2, k+1);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
224 throw;
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20207
diff changeset
225 }
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
226 }
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
227
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
228 }
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
229 }
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
230
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
231 return retval;
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
232 }
10513
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
233
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10846
diff changeset
234 DEFUN (spalloc, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
235 "-*- texinfo -*-\n\
15038
ab18578c2ade doc: Update docstrings for built-in functions to not use "Loadable Function" as type.
Rik <rik@octave.org>
parents: 14861
diff changeset
236 @deftypefn {Built-in Function} {@var{s} =} spalloc (@var{m}, @var{n}, @var{nz})\n\
11572
7d6d8c1e471f Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
237 Create an @var{m}-by-@var{n} sparse matrix with pre-allocated space for at\n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
238 most @var{nz} nonzero elements.\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
239 \n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
240 This is useful for building a matrix incrementally by a sequence of indexed\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
241 assignments. Subsequent indexed assignments after @code{spalloc} will reuse\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
242 the pre-allocated memory, provided they are of one of the simple forms\n\
10513
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
243 \n\
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
244 @itemize\n\
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
245 @item @code{@var{s}(I:J) = @var{x}}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
246 \n\
10513
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
247 @item @code{@var{s}(:,I:J) = @var{x}}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
248 \n\
10513
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
249 @item @code{@var{s}(K:L,I:J) = @var{x}}\n\
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
250 @end itemize\n\
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
251 \n\
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
252 @b{and} that the following conditions are met:\n\
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
253 \n\
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
254 @itemize\n\
14861
f7afecdd87ef maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
255 @item the assignment does not decrease nnz (@var{S}).\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
256 \n\
14861
f7afecdd87ef maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
257 @item after the assignment, nnz (@var{S}) does not exceed @var{nz}.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
258 \n\
10513
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
259 @item no index is out of bounds.\n\
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
260 @end itemize\n\
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
261 \n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
262 Partial movement of data may still occur, but in general the assignment will\n\
18848
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
263 be more memory and time efficient under these circumstances. In particular,\n\
53af80da6781 doc: Update documentation of sparse functions including seealso links.
Rik <rik@octave.org>
parents: 18843
diff changeset
264 it is possible to efficiently build a pre-allocated sparse matrix from a\n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
265 contiguous block of columns.\n\
10513
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
266 \n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
267 The amount of pre-allocated memory for a given matrix may be queried using\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
268 the function @code{nzmax}.\n\
10513
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
269 @seealso{nzmax, sparse}\n\
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
270 @end deftypefn")
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
271 {
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
272 octave_value retval;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
273 int nargin = args.length ();
10513
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
274
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
275 if (nargin == 2 || nargin == 3)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
276 {
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
277 octave_idx_type m = args(0).idx_type_value ();
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
278 octave_idx_type n = args(1).idx_type_value ();
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
279 octave_idx_type nz = 0;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
280 if (nargin == 3)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
281 nz = args(2).idx_type_value ();
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
282 if (error_state)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
283 ;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
284 else if (m >= 0 && n >= 0 && nz >= 0)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
285 retval = SparseMatrix (dim_vector (m, n), nz);
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
286 else
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
287 error ("spalloc: M,N,NZ must be non-negative");
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
288 }
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
289 else
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
290 print_usage ();
10513
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
291
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
292 return retval;
10513
c5005bc2b7a9 implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents: 10479
diff changeset
293 }