Mercurial > octave-nkf
annotate libinterp/corefcn/sparse.cc @ 17281:bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Macro handles options ("on") or properties ("position") more elegantly
than @code{"text"}.
* doc/interpreter/macros.texi: Add new @qcode macro.
* doc/interpreter/tips.txi: Add documentation about @qcode macro.
* doc/interpreter/basics.txi, doc/interpreter/container.txi,
doc/interpreter/emacs.txi, doc/interpreter/errors.txi,
doc/interpreter/eval.txi, doc/interpreter/expr.txi,
doc/interpreter/external.txi, doc/interpreter/func.txi,
doc/interpreter/grammar.txi, doc/interpreter/image.txi,
doc/interpreter/install.txi, doc/interpreter/interp.txi,
doc/interpreter/io.txi, doc/interpreter/matrix.txi,
doc/interpreter/numbers.txi, doc/interpreter/oop.txi,
doc/interpreter/package.txi, doc/interpreter/plot.txi,
doc/interpreter/quad.txi, doc/interpreter/sparse.txi,
doc/interpreter/strings.txi, doc/interpreter/system.txi,
doc/interpreter/vectorize.txi, libinterp/corefcn/balance.cc,
libinterp/corefcn/bitfcns.cc, libinterp/corefcn/cellfun.cc,
libinterp/corefcn/conv2.cc, libinterp/corefcn/data.cc,
libinterp/corefcn/debug.cc, libinterp/corefcn/defaults.cc,
libinterp/corefcn/dirfns.cc, libinterp/corefcn/dlmread.cc,
libinterp/corefcn/error.cc, libinterp/corefcn/file-io.cc,
libinterp/corefcn/find.cc, libinterp/corefcn/gammainc.cc,
libinterp/corefcn/graphics.cc, libinterp/corefcn/help.cc,
libinterp/corefcn/hex2num.cc, libinterp/corefcn/input.cc,
libinterp/corefcn/load-path.cc, libinterp/corefcn/load-save.cc,
libinterp/corefcn/ls-oct-ascii.cc, libinterp/corefcn/lu.cc,
libinterp/corefcn/luinc.cc, libinterp/corefcn/matrix_type.cc,
libinterp/corefcn/oct-hist.cc, libinterp/corefcn/pager.cc,
libinterp/corefcn/pr-output.cc, libinterp/corefcn/pt-jit.cc,
libinterp/corefcn/qz.cc, libinterp/corefcn/rand.cc,
libinterp/corefcn/regexp.cc, libinterp/corefcn/schur.cc,
libinterp/corefcn/sighandlers.cc, libinterp/corefcn/sparse.cc,
libinterp/corefcn/spparms.cc, libinterp/corefcn/str2double.cc,
libinterp/corefcn/svd.cc, libinterp/corefcn/symtab.cc,
libinterp/corefcn/syscalls.cc, libinterp/corefcn/toplev.cc,
libinterp/corefcn/tril.cc, libinterp/corefcn/typecast.cc,
libinterp/corefcn/utils.cc, libinterp/corefcn/variables.cc,
libinterp/dldfcn/__init_fltk__.cc, libinterp/dldfcn/chol.cc,
libinterp/dldfcn/colamd.cc, libinterp/dldfcn/fftw.cc, libinterp/dldfcn/qr.cc,
libinterp/dldfcn/symbfact.cc, libinterp/octave-value/ov-base.cc,
libinterp/octave-value/ov-fcn-handle.cc,
libinterp/octave-value/ov-fcn-inline.cc, libinterp/octave-value/ov-java.cc,
libinterp/octave-value/ov-range.cc, libinterp/octave-value/ov-struct.cc,
libinterp/octave-value/ov-usr-fcn.cc, libinterp/parse-tree/oct-parse.in.yy,
libinterp/parse-tree/pt-binop.cc, libinterp/parse-tree/pt-eval.cc,
libinterp/parse-tree/pt-mat.cc, scripts/@ftp/ftp.m,
scripts/deprecated/java_convert_matrix.m, scripts/deprecated/java_debug.m,
scripts/deprecated/java_unsigned_conversion.m, scripts/deprecated/shell_cmd.m,
scripts/general/dblquad.m, scripts/general/display.m,
scripts/general/genvarname.m, scripts/general/idivide.m,
scripts/general/interp1.m, scripts/general/interp2.m,
scripts/general/interp3.m, scripts/general/interpn.m, scripts/general/isa.m,
scripts/general/profexplore.m, scripts/general/profile.m,
scripts/general/quadgk.m, scripts/general/randi.m, scripts/general/structfun.m,
scripts/general/subsindex.m, scripts/general/triplequad.m,
scripts/geometry/griddata.m, scripts/geometry/griddata3.m,
scripts/geometry/griddatan.m, scripts/geometry/voronoi.m, scripts/help/help.m,
scripts/help/lookfor.m, scripts/image/cmpermute.m, scripts/image/colormap.m,
scripts/image/image.m, scripts/image/imagesc.m, scripts/image/imfinfo.m,
scripts/image/imformats.m, scripts/image/imread.m, scripts/image/imshow.m,
scripts/image/imwrite.m, scripts/image/ind2gray.m, scripts/image/lines.m,
scripts/image/rgb2ind.m, scripts/image/spinmap.m, scripts/io/dlmwrite.m,
scripts/io/strread.m, scripts/io/textread.m, scripts/io/textscan.m,
scripts/java/javaclasspath.m, scripts/java/usejava.m,
scripts/miscellaneous/bzip2.m, scripts/miscellaneous/computer.m,
scripts/miscellaneous/copyfile.m, scripts/miscellaneous/debug.m,
scripts/miscellaneous/dos.m, scripts/miscellaneous/edit.m,
scripts/miscellaneous/gzip.m, scripts/miscellaneous/license.m,
scripts/miscellaneous/mkoctfile.m, scripts/miscellaneous/movefile.m,
scripts/miscellaneous/parseparams.m, scripts/miscellaneous/unix.m,
scripts/optimization/fminbnd.m, scripts/optimization/fminsearch.m,
scripts/optimization/fminunc.m, scripts/optimization/fsolve.m,
scripts/optimization/fzero.m, scripts/optimization/glpk.m,
scripts/optimization/lsqnonneg.m, scripts/optimization/optimset.m,
scripts/optimization/pqpnonneg.m, scripts/pkg/pkg.m, scripts/plot/allchild.m,
scripts/plot/ancestor.m, scripts/plot/area.m, scripts/plot/axis.m,
scripts/plot/bar.m, scripts/plot/barh.m, scripts/plot/box.m,
scripts/plot/caxis.m, scripts/plot/cla.m, scripts/plot/clabel.m,
scripts/plot/clf.m, scripts/plot/close.m, scripts/plot/colorbar.m,
scripts/plot/daspect.m, scripts/plot/ezmesh.m, scripts/plot/ezmeshc.m,
scripts/plot/ezsurf.m, scripts/plot/ezsurfc.m, scripts/plot/findall.m,
scripts/plot/findobj.m, scripts/plot/gcbo.m, scripts/plot/gcf.m,
scripts/plot/gco.m, scripts/plot/grid.m, scripts/plot/guihandles.m,
scripts/plot/hdl2struct.m, scripts/plot/hidden.m, scripts/plot/hold.m,
scripts/plot/isonormals.m, scripts/plot/isosurface.m, scripts/plot/legend.m,
scripts/plot/mesh.m, scripts/plot/meshc.m, scripts/plot/meshz.m,
scripts/plot/newplot.m, scripts/plot/orient.m, scripts/plot/pareto.m,
scripts/plot/patch.m, scripts/plot/pbaspect.m, scripts/plot/pcolor.m,
scripts/plot/plot.m, scripts/plot/print.m,
scripts/plot/private/__add_default_menu__.m, scripts/plot/quiver.m,
scripts/plot/quiver3.m, scripts/plot/refreshdata.m, scripts/plot/saveas.m,
scripts/plot/scatter.m, scripts/plot/scatter3.m, scripts/plot/shading.m,
scripts/plot/shrinkfaces.m, scripts/plot/slice.m, scripts/plot/stem.m,
scripts/plot/stem3.m, scripts/plot/struct2hdl.m, scripts/plot/subplot.m,
scripts/plot/surf.m, scripts/plot/surfc.m, scripts/plot/surfl.m,
scripts/plot/tetramesh.m, scripts/plot/uigetfile.m, scripts/plot/uimenu.m,
scripts/plot/uiputfile.m, scripts/plot/waterfall.m, scripts/plot/whitebg.m,
scripts/plot/xlim.m, scripts/plot/ylim.m, scripts/plot/zlim.m,
scripts/polynomial/conv.m, scripts/polynomial/polyout.m,
scripts/polynomial/splinefit.m, scripts/set/ismember.m, scripts/set/powerset.m,
scripts/set/setdiff.m, scripts/set/union.m, scripts/set/unique.m,
scripts/signal/detrend.m, scripts/signal/filter2.m, scripts/signal/freqz.m,
scripts/signal/periodogram.m, scripts/signal/spectral_adf.m,
scripts/signal/spectral_xdf.m, scripts/sparse/eigs.m, scripts/sparse/svds.m,
scripts/specfun/legendre.m, scripts/special-matrix/gallery.m,
scripts/statistics/base/mean.m, scripts/statistics/base/moment.m,
scripts/statistics/tests/cor_test.m,
scripts/statistics/tests/kolmogorov_smirnov_test.m,
scripts/statistics/tests/kolmogorov_smirnov_test_2.m,
scripts/statistics/tests/kruskal_wallis_test.m,
scripts/statistics/tests/prop_test_2.m, scripts/statistics/tests/sign_test.m,
scripts/statistics/tests/t_test.m, scripts/statistics/tests/t_test_2.m,
scripts/statistics/tests/t_test_regression.m,
scripts/statistics/tests/u_test.m, scripts/statistics/tests/var_test.m,
scripts/statistics/tests/welch_test.m,
scripts/statistics/tests/wilcoxon_test.m, scripts/statistics/tests/z_test.m,
scripts/statistics/tests/z_test_2.m, scripts/strings/base2dec.m,
scripts/strings/index.m, scripts/strings/isstrprop.m,
scripts/strings/mat2str.m, scripts/strings/regexptranslate.m,
scripts/strings/rindex.m, scripts/strings/str2num.m, scripts/strings/strcat.m,
scripts/strings/strjust.m, scripts/strings/strmatch.m,
scripts/strings/validatestring.m, scripts/testfun/demo.m,
scripts/testfun/example.m, scripts/testfun/test.m, scripts/time/addtodate.m,
scripts/time/asctime.m, scripts/time/datestr.m, scripts/time/datetick.m,
scripts/time/weekday.m, scripts/ui/errordlg.m, scripts/ui/helpdlg.m,
scripts/ui/inputdlg.m, scripts/ui/listdlg.m, scripts/ui/msgbox.m,
scripts/ui/questdlg.m, scripts/ui/warndlg.m: Use new @qcode macro.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 19 Aug 2013 20:46:38 -0700 |
parents | 2fc554ffbc28 |
children | d63878346099 |
rev | line source |
---|---|
5164 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14062
diff
changeset
|
3 Copyright (C) 2004-2012 David Bateman |
11523 | 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 | 6 |
7 This file is part of Octave. | |
5164 | 8 |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
5164 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
5164 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
29 #include <cstdlib> | |
30 #include <string> | |
31 | |
32 #include "variables.h" | |
33 #include "utils.h" | |
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 | 36 #include "gripes.h" |
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 | 39 |
40 #include "ov-re-sparse.h" | |
41 #include "ov-cx-sparse.h" | |
42 #include "ov-bool-sparse.h" | |
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, , |
5164 | 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 | 50 { |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14620
diff
changeset
|
51 if (args.length () != 1) |
5164 | 52 { |
5823 | 53 print_usage (); |
5164 | 54 return octave_value (); |
55 } | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
56 else |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
57 return octave_value (args(0).is_sparse_type ()); |
5164 | 58 } |
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, , |
5164 | 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\ |
ab18578c2ade
doc: Update docstrings for built-in functions to not use "Loadable Function" as type.
Rik <rik@octave.org>
parents:
14861
diff
changeset
|
63 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{sv}, @var{m}, @var{n}, @var{nzmax})\n\ |
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\ |
ab18578c2ade
doc: Update docstrings for built-in functions to not use "Loadable Function" as type.
Rik <rik@octave.org>
parents:
14861
diff
changeset
|
65 @deftypefnx {Built-in Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{s}, @var{m}, @var{n}, \"unique\")\n\ |
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{m}, @var{n})\n\ |
8106
8a42498edb30
Clarify doc for sparse function
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
67 Create a sparse matrix from the full matrix or row, column, value triplets.\n\ |
8a42498edb30
Clarify doc for sparse function
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
68 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
|
69 removing all zero values in the process.\n\ |
5164 | 70 \n\ |
8106
8a42498edb30
Clarify doc for sparse function
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
71 Given the integer index vectors @var{i} and @var{j}, a 1-by-@code{nnz} vector\n\ |
8a42498edb30
Clarify doc for sparse function
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
72 of real of complex values @var{sv}, overall dimensions @var{m} and @var{n}\n\ |
8a42498edb30
Clarify doc for sparse function
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
73 of the sparse matrix. The argument @code{nzmax} is ignored but accepted for\n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
74 compatibility with @sc{matlab}. If @var{m} or @var{n} are not specified\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
75 their values are derived from the maximum index in the vectors @var{i} and\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
76 @var{j} as given by @code{@var{m} = max (@var{i})},\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
77 @code{@var{n} = max (@var{j})}.\n\ |
5164 | 78 \n\ |
6556 | 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\ | |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14458
diff
changeset
|
81 be added. See @code{accumarray} for an example of how to produce different\n\ |
14458
c288881669b9
doc: Mention accumarray trick in sparse docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
82 behavior, such as taking the minimum instead.\n\ |
6556 | 83 \n\ |
6557 | 84 The following are all equivalent:\n\ |
5164 | 85 \n\ |
6556 | 86 @example\n\ |
87 @group\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\ | |
91 @end group\n\ | |
92 @end example\n\ | |
5164 | 93 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15195
diff
changeset
|
94 Given the option @qcode{\"unique\"}, if more than two values are specified\n\ |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15195
diff
changeset
|
95 for the same @var{i}, @var{j} indices, the last specified value will be\n\ |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15195
diff
changeset
|
96 used.\n\ |
5164 | 97 \n\ |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
98 @code{sparse (@var{m}, @var{n})} is equivalent to\n\ |
8106
8a42498edb30
Clarify doc for sparse function
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
99 @code{sparse ([], [], [], @var{m}, @var{n}, 0)}\n\ |
6556 | 100 \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\ | |
14458
c288881669b9
doc: Mention accumarray trick in sparse docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
103 @seealso{full, accumarray}\n\ |
5164 | 104 @end deftypefn") |
105 { | |
106 octave_value retval; | |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
107 int nargin = args.length (); |
5164 | 108 |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
109 // Temporarily disable sparse_auto_mutate if set (it's obsolete anyway). |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
110 unwind_protect frame; |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
111 frame.protect_var (Vsparse_auto_mutate); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
112 Vsparse_auto_mutate = false; |
5164 | 113 |
7287 | 114 if (nargin == 1) |
5164 | 115 { |
116 octave_value arg = args (0); | |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
117 if (arg.is_bool_type ()) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
118 retval = arg.sparse_bool_matrix_value (); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
119 else if (arg.is_complex_type ()) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
120 retval = arg.sparse_complex_matrix_value (); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
121 else if (arg.is_numeric_type ()) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
122 retval = arg.sparse_matrix_value (); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
123 else |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
124 gripe_wrong_type_arg ("sparse", arg); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
125 } |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
126 else if (nargin == 2) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
127 { |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
128 octave_idx_type m = 0, n = 0; |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
129 if (args(0).is_scalar_type () && args(1).is_scalar_type ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
130 { |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
131 m = args(0).idx_type_value (); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
132 n = args(1).idx_type_value (); |
8910
6e9f26506804
optimize diag -> sparse and perm -> sparse conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8458
diff
changeset
|
133 } |
5164 | 134 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
135 error ("sparse: dimensions M,N must be scalar"); |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
136 |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
137 if (! error_state) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
138 { |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
139 if (m >= 0 && n >= 0) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
140 retval = SparseMatrix (m, n); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
141 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
142 error ("sparse: dimensions M,N must be positive or zero"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
143 } |
5164 | 144 } |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
145 else if (nargin >= 3) |
5164 | 146 { |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
147 bool summation = true; |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
148 if (nargin > 3 && args(nargin-1).is_string ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
149 { |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
150 std::string opt = args(nargin-1).string_value (); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
151 if (opt == "unique") |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
152 summation = false; |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
153 else if (opt == "sum" || opt == "summation") |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
154 summation = true; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
155 else |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
156 error ("sparse: invalid option: %s", opt.c_str ()); |
5164 | 157 |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
158 nargin -= 1; |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
159 } |
5164 | 160 |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
161 if (! error_state) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
162 { |
10527
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
163 octave_idx_type m = -1, n = -1, nzmax = -1; |
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
164 if (nargin == 6) |
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
165 { |
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
166 nzmax = args(5).idx_type_value (); |
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
167 nargin --; |
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
168 } |
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
169 |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
170 if (nargin == 5) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
171 { |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
172 if (args(3).is_scalar_type () && args(4).is_scalar_type ()) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
173 { |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
174 m = args(3).idx_type_value (); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
175 n = args(4).idx_type_value (); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
176 } |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
177 else |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
178 error ("sparse: expecting scalar dimensions"); |
5164 | 179 |
180 | |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
181 if (! error_state && (m < 0 || n < 0)) |
14062
5b49cafe0599
Use non-negative, non-positive with hyphens in error messages.
Rik <octave@nomad.inbox5.com>
parents:
12483
diff
changeset
|
182 error ("sparse: dimensions must be non-negative"); |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
183 } |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
184 else if (nargin != 3) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
185 print_usage (); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
186 |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
187 if (! error_state) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
188 { |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
189 idx_vector i = args(0).index_vector (); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
190 idx_vector j = args(1).index_vector (); |
5164 | 191 |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
192 if (args(2).is_bool_type ()) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
193 retval = SparseBoolMatrix (args(2).bool_array_value (), i, j, |
10527
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
194 m, n, summation, nzmax); |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
195 else if (args(2).is_complex_type ()) |
10527
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
196 retval = SparseComplexMatrix (args(2).complex_array_value (), |
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
197 i, j, m, n, summation, nzmax); |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
198 else if (args(2).is_numeric_type ()) |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
199 retval = SparseMatrix (args(2).array_value (), i, j, |
10527
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
200 m, n, summation, nzmax); |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
201 else |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
202 gripe_wrong_type_arg ("sparse", args(2)); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
203 } |
5164 | 204 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
205 } |
5164 | 206 } |
207 | |
208 return retval; | |
209 } | |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
210 |
11097
ffb2f1ef2097
make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents:
10846
diff
changeset
|
211 DEFUN (spalloc, args, , |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
212 "-*- 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
|
213 @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
|
214 Create an @var{m}-by-@var{n} sparse matrix with pre-allocated space for at\n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
215 most @var{nz} nonzero elements. This is useful for building the matrix\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
216 incrementally by a sequence of indexed assignments. Subsequent indexed\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
217 assignments will reuse the pre-allocated memory, provided they are of one of\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
218 the simple forms\n\ |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
219 \n\ |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
220 @itemize\n\ |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
221 @item @code{@var{s}(I:J) = @var{x}}\n\ |
10840 | 222 \n\ |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
223 @item @code{@var{s}(:,I:J) = @var{x}}\n\ |
10840 | 224 \n\ |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
225 @item @code{@var{s}(K:L,I:J) = @var{x}}\n\ |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
226 @end itemize\n\ |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
227 \n\ |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
228 @b{and} that the following conditions are met:\n\ |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
229 \n\ |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
230 @itemize\n\ |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
231 @item the assignment does not decrease nnz (@var{S}).\n\ |
10840 | 232 \n\ |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
233 @item after the assignment, nnz (@var{S}) does not exceed @var{nz}.\n\ |
10840 | 234 \n\ |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
235 @item no index is out of bounds.\n\ |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
236 @end itemize\n\ |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
237 \n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
238 Partial movement of data may still occur, but in general the assignment will\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
239 be more memory and time-efficient under these circumstances. In particular,\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
240 it is possible to efficiently build a pre-allocated sparse matrix from\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
241 contiguous block of columns.\n\ |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
242 \n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
243 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
|
244 the function @code{nzmax}.\n\ |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
245 @seealso{nzmax, sparse}\n\ |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
246 @end deftypefn") |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
247 { |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
248 octave_value retval; |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
249 int nargin = args.length (); |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
250 |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
251 if (nargin == 2 || nargin == 3) |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
252 { |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
253 octave_idx_type m = args(0).idx_type_value (); |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
254 octave_idx_type n = args(1).idx_type_value (); |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
255 octave_idx_type nz = 0; |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
256 if (nargin == 3) |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
257 nz = args(2).idx_type_value (); |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
258 if (error_state) |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
259 ; |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
260 else if (m >= 0 && n >= 0 && nz >= 0) |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
261 retval = SparseMatrix (dim_vector (m, n), nz); |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
262 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
263 error ("spalloc: M,N,NZ must be non-negative"); |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
264 } |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
265 else |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
266 print_usage (); |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
267 |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
268 return retval; |
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10479
diff
changeset
|
269 } |