Mercurial > octave
annotate libinterp/dldfcn/symbfact.cc @ 21585:bad3ed83330d
symbfact.cc: Overhaul dldfcn.
* symbfact.cc: Redo docstring. Eliminate nargout input validation.
Improve input validation for TYP and MODE variables. Add label
cleanup: and use goto statements to guarantee that memory allocated
in cholmod library is cleaned up. Use std::max rather than hand-coded
max routine. Add BIST tests.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 04 Apr 2016 09:00:22 -0700 |
parents | ad0599a0acc6 |
children | d7a268e68e69 |
rev | line source |
---|---|
5506 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19139
diff
changeset
|
3 Copyright (C) 2005-2015 David Bateman |
11523 | 4 Copyright (C) 1998-2005 Andy Adler |
7016 | 5 |
6 This file is part of Octave. | |
5506 | 7 |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
5506 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
5506 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21229
diff
changeset
|
25 # include "config.h" |
5506 | 26 #endif |
27 | |
21186
7f35125714b4
don't install some internal headers and template sources
John W. Eaton <jwe@octave.org>
parents:
21145
diff
changeset
|
28 #include "oct-locbuf.h" |
7f35125714b4
don't install some internal headers and template sources
John W. Eaton <jwe@octave.org>
parents:
21145
diff
changeset
|
29 #include "oct-sparse.h" |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7036
diff
changeset
|
30 #include "oct-spparms.h" |
21186
7f35125714b4
don't install some internal headers and template sources
John W. Eaton <jwe@octave.org>
parents:
21145
diff
changeset
|
31 #include "sparse-chol.h" |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7036
diff
changeset
|
32 #include "sparse-util.h" |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7036
diff
changeset
|
33 |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7036
diff
changeset
|
34 #include "ov-re-sparse.h" |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7036
diff
changeset
|
35 #include "ov-cx-sparse.h" |
5506 | 36 #include "defun-dld.h" |
37 #include "error.h" | |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20946
diff
changeset
|
38 #include "errwarn.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20939
diff
changeset
|
39 #include "ovl.h" |
5506 | 40 #include "utils.h" |
41 | |
42 DEFUN_DLD (symbfact, args, nargout, | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 "-*- texinfo -*-\n\ |
21585 | 44 @deftypefn {} {[@var{count}, @var{h}, @var{parent}, @var{post}, @var{R}] =} symbfact (@var{S})\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20792
diff
changeset
|
45 @deftypefnx {} {[@dots{}] =} symbfact (@var{S}, @var{typ})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20792
diff
changeset
|
46 @deftypefnx {} {[@dots{}] =} symbfact (@var{S}, @var{typ}, @var{mode})\n\ |
5506 | 47 \n\ |
21585 | 48 Perform a symbolic factorization analysis of the sparse matrix @var{S}.\n\ |
20163
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
49 \n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
50 The input variables are\n\ |
5506 | 51 \n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
52 @table @var\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
53 @item S\n\ |
21585 | 54 @var{S} is a real or complex sparse matrix.\n\ |
5506 | 55 \n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
56 @item typ\n\ |
5506 | 57 Is the type of the factorization and can be one of\n\ |
58 \n\ | |
21585 | 59 @table @asis\n\ |
60 @item @qcode{\"sym\"} (default)\n\ | |
61 Factorize @var{S}. Assumes @var{S} is symmetric and uses the upper\n\ | |
62 triangular portion of the matrix.\n\ | |
5506 | 63 \n\ |
21585 | 64 @item @qcode{\"col\"}\n\ |
65 Factorize @tcode{@var{S}' * @var{S}}.\n\ | |
10840 | 66 \n\ |
21585 | 67 @item @qcode{\"row\"}\n\ |
17268
1c21f264d26f
doc: Rename @xcode macro to @tcode (transpose code) for clarity.
Rik <rik@octave.org>
parents:
16316
diff
changeset
|
68 Factorize @tcode{@var{S} * @var{S}'}.\n\ |
10840 | 69 \n\ |
21585 | 70 @item @qcode{\"lo\"}\n\ |
71 Factorize @tcode{@var{S}'}. Assumes @var{S} is symmetric and uses the lower\n\ | |
72 triangular portion of the matrix.\n\ | |
5506 | 73 @end table\n\ |
74 \n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
75 @item mode\n\ |
21585 | 76 When @var{mode} is unspecified return the Cholesky@tie{}factorization for\n\ |
77 @var{R}. If @var{mode} is @qcode{\"lower\"} or @qcode{\"L\"} then return\n\ | |
78 the conjugate transpose @tcode{@var{R}'} which is a lower triangular factor.\n\ | |
79 The conjugate transpose version is faster and uses less memory, but still\n\ | |
80 returns the same values for all other outputs: @var{count}, @var{h},\n\ | |
81 @var{parent}, and @var{post}.\n\ | |
5506 | 82 @end table\n\ |
83 \n\ | |
21585 | 84 The output variables are:\n\ |
5506 | 85 \n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
86 @table @var\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
87 @item count\n\ |
21547
ad0599a0acc6
doc: Wrap C++ docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
88 The row counts of the Cholesky@tie{}factorization as determined by\n\ |
21585 | 89 @var{typ}. The computational difficulty of performing the true\n\ |
90 factorization using @code{chol} is @code{sum (@var{count} .^ 2)}.\n\ | |
5506 | 91 \n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
92 @item h\n\ |
5506 | 93 The height of the elimination tree.\n\ |
94 \n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
95 @item parent\n\ |
5506 | 96 The elimination tree itself.\n\ |
97 \n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
98 @item post\n\ |
21585 | 99 A sparse boolean matrix whose structure is that of the\n\ |
100 Cholesky@tie{}factorization as determined by @var{typ}.\n\ | |
5506 | 101 @end table\n\ |
21585 | 102 @seealso{chol, etree, treelayout}\n\ |
5506 | 103 @end deftypefn") |
104 { | |
20898
8da80da1ac37
maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
105 #ifdef HAVE_CHOLMOD |
8da80da1ac37
maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
106 |
5506 | 107 int nargin = args.length (); |
108 | |
21585 | 109 if (nargin < 1 || nargin > 3) |
20790
c2d9556d51d0
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20726
diff
changeset
|
110 print_usage (); |
5506 | 111 |
20898
8da80da1ac37
maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
112 octave_value_list retval; |
21585 | 113 |
5506 | 114 double dummy; |
115 cholmod_sparse Astore; | |
116 cholmod_sparse *A = &Astore; | |
5527 | 117 A->packed = true; |
118 A->sorted = true; | |
7520 | 119 A->nz = 0; |
21229
a83e7a384ee0
create and install a subset of config.h in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
120 #if defined (OCTAVE_ENABLE_64) |
5506 | 121 A->itype = CHOLMOD_LONG; |
122 #else | |
123 A->itype = CHOLMOD_INT; | |
124 #endif | |
125 A->dtype = CHOLMOD_DOUBLE; | |
126 A->stype = 1; | |
127 A->x = &dummy; | |
128 | |
129 if (args(0).is_real_type ()) | |
130 { | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
131 const SparseMatrix a = args(0).sparse_matrix_value (); |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
132 A->nrow = a.rows (); |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
133 A->ncol = a.cols (); |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
134 A->p = a.cidx (); |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
135 A->i = a.ridx (); |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
136 A->nzmax = a.nnz (); |
5506 | 137 A->xtype = CHOLMOD_REAL; |
138 | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
139 if (a.rows () > 0 && a.cols () > 0) |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
140 A->x = a.data (); |
5506 | 141 } |
142 else if (args(0).is_complex_type ()) | |
143 { | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
144 const SparseComplexMatrix a = args(0).sparse_complex_matrix_value (); |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
145 A->nrow = a.rows (); |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
146 A->ncol = a.cols (); |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
147 A->p = a.cidx (); |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
148 A->i = a.ridx (); |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
149 A->nzmax = a.nnz (); |
5506 | 150 A->xtype = CHOLMOD_COMPLEX; |
151 | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
152 if (a.rows () > 0 && a.cols () > 0) |
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
153 A->x = a.data (); |
5506 | 154 } |
155 else | |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20946
diff
changeset
|
156 err_wrong_type_arg ("symbfact", args(0)); |
5506 | 157 |
5527 | 158 octave_idx_type coletree = false; |
5506 | 159 octave_idx_type n = A->nrow; |
160 | |
161 if (nargin > 1) | |
162 { | |
21585 | 163 std::string str = args(1).xstring_value ("TYP must be a string"); |
164 // FIXME: The input validation could be improved to use strncmp | |
5506 | 165 char ch; |
21585 | 166 ch = tolower (str[0]); |
167 if (ch == 'r') // 'row' | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
168 A->stype = 0; |
21585 | 169 else if (ch == 'c') // 'col' |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
170 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
171 n = A->ncol; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
172 coletree = true; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
173 A->stype = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
174 } |
21585 | 175 else if (ch == 's') // 'sym' (default) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
176 A->stype = 1; |
21585 | 177 else if (ch == 'l') // 'lo' |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
178 A->stype = -1; |
5506 | 179 else |
21585 | 180 error ("symbfact: unrecognized TYP \"%s\"", str.c_str ()); |
181 } | |
182 | |
183 if (nargin == 3) | |
184 { | |
185 std::string str = args(2).xstring_value ("MODE must be a string"); | |
186 // FIXME: The input validation could be improved to use strncmp | |
187 char ch; | |
188 ch = toupper (str[0]); | |
189 if (ch != 'L') | |
190 error ("symbfact: unrecognized MODE \"%s\"", str.c_str ()); | |
5506 | 191 } |
192 | |
193 if (A->stype && A->nrow != A->ncol) | |
21110
3d0d84305600
Use err_square_matrix_required more widely.
Rik <rik@octave.org>
parents:
21109
diff
changeset
|
194 err_square_matrix_required ("symbfact", "S"); |
5506 | 195 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
196 OCTAVE_LOCAL_BUFFER (octave_idx_type, Parent, n); |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
197 OCTAVE_LOCAL_BUFFER (octave_idx_type, Post, n); |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
198 OCTAVE_LOCAL_BUFFER (octave_idx_type, ColCount, n); |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
199 OCTAVE_LOCAL_BUFFER (octave_idx_type, First, n); |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
200 OCTAVE_LOCAL_BUFFER (octave_idx_type, Level, n); |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
201 |
21585 | 202 cholmod_common Common; |
203 cholmod_common *cm = &Common; | |
204 CHOLMOD_NAME(start) (cm); | |
205 | |
206 double spu = octave_sparse_params::get_key ("spumoni"); | |
207 if (spu == 0.) | |
208 { | |
209 cm->print = -1; | |
210 SUITESPARSE_ASSIGN_FPTR (printf_func, cm->print_function, 0); | |
211 } | |
212 else | |
213 { | |
214 cm->print = static_cast<int> (spu) + 2; | |
215 SUITESPARSE_ASSIGN_FPTR (printf_func, cm->print_function, &SparseCholPrint); | |
216 } | |
217 | |
218 cm->error_handler = &SparseCholError; | |
219 SUITESPARSE_ASSIGN_FPTR2 (divcomplex_func, cm->complex_divide, divcomplex); | |
220 SUITESPARSE_ASSIGN_FPTR2 (hypot_func, cm->hypotenuse, hypot); | |
221 | |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
222 cholmod_sparse *F = CHOLMOD_NAME(transpose) (A, 0, cm); |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
223 cholmod_sparse *Aup, *Alo; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
224 |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
225 if (A->stype == 1 || coletree) |
5506 | 226 { |
21585 | 227 Aup = A; |
228 Alo = F; | |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
229 } |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
230 else |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
231 { |
21585 | 232 Aup = F; |
233 Alo = A; | |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
234 } |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
235 |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
236 CHOLMOD_NAME(etree) (Aup, Parent, cm); |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
237 |
21585 | 238 ColumnVector tmp (n); // Declaration must precede any goto cleanup. |
239 std::string err_msg; | |
240 | |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
241 if (cm->status < CHOLMOD_OK) |
21585 | 242 { |
243 err_msg = "symbfact: matrix corrupted"; | |
244 goto cleanup; | |
245 } | |
5506 | 246 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
247 if (CHOLMOD_NAME(postorder) (Parent, n, 0, Post, cm) != n) |
21585 | 248 { |
249 err_msg = "symbfact: postorder failed"; | |
250 goto cleanup; | |
251 } | |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
252 |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
253 CHOLMOD_NAME(rowcolcounts) (Alo, 0, 0, Parent, Post, 0, |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
254 ColCount, First, Level, cm); |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
255 |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
256 if (cm->status < CHOLMOD_OK) |
21585 | 257 { |
258 err_msg = "symbfact: matrix corrupted"; | |
259 goto cleanup; | |
260 } | |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
261 |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
262 if (nargout > 4) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
263 { |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
264 cholmod_sparse *A1, *A2; |
5506 | 265 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
266 if (A->stype == 1) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
267 { |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
268 A1 = A; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
269 A2 = 0; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
270 } |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
271 else if (A->stype == -1) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
272 { |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
273 A1 = F; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
274 A2 = 0; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
275 } |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
276 else if (coletree) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
277 { |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
278 A1 = F; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
279 A2 = A; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
280 } |
5506 | 281 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
282 { |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
283 A1 = A; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
284 A2 = F; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
285 } |
5506 | 286 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
287 // count the total number of entries in L |
21585 | 288 octave_idx_type lnz = 0; |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
289 for (octave_idx_type j = 0 ; j < n ; j++) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
290 lnz += ColCount[j]; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
291 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
292 // allocate the output matrix L (pattern-only) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
293 SparseBoolMatrix L (n, n, lnz); |
5506 | 294 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
295 // initialize column pointers |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
296 lnz = 0; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
297 for (octave_idx_type j = 0 ; j < n ; j++) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
298 { |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
299 L.xcidx(j) = lnz; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
300 lnz += ColCount[j]; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
301 } |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
302 L.xcidx(n) = lnz; |
5506 | 303 |
20946 | 304 // create a copy of the column pointers |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
305 octave_idx_type *W = First; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
306 for (octave_idx_type j = 0 ; j < n ; j++) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
307 W[j] = L.xcidx (j); |
5506 | 308 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
309 // get workspace for computing one row of L |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
310 cholmod_sparse *R |
21585 | 311 = CHOLMOD_NAME(allocate_sparse) (n, 1, n, false, true, |
312 0, CHOLMOD_PATTERN, cm); | |
313 octave_idx_type *Rp = static_cast<octave_idx_type *> (R->p); | |
314 octave_idx_type *Ri = static_cast<octave_idx_type *> (R->i); | |
5506 | 315 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
316 // compute L one row at a time |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
317 for (octave_idx_type k = 0 ; k < n ; k++) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
318 { |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
319 // get the kth row of L and store in the columns of L |
21585 | 320 CHOLMOD_NAME(row_subtree) (A1, A2, k, Parent, R, cm); |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
321 for (octave_idx_type p = 0 ; p < Rp[1] ; p++) |
21585 | 322 L.xridx (W[Ri[p]]++) = k; |
5506 | 323 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
324 // add the diagonal entry |
21585 | 325 L.xridx (W[k]++) = k; |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
326 } |
5506 | 327 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
328 // free workspace |
21585 | 329 CHOLMOD_NAME(free_sparse) (&R, cm); |
5506 | 330 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
331 // transpose L to get R, or leave as is |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
332 if (nargin < 3) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
333 L = L.transpose (); |
5506 | 334 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
335 // fill numerical values of L with one's |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
336 for (octave_idx_type p = 0 ; p < lnz ; p++) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
337 L.xdata(p) = true; |
5506 | 338 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
339 retval(4) = L; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
340 } |
5506 | 341 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
342 if (nargout > 3) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
343 { |
5506 | 344 for (octave_idx_type i = 0; i < n; i++) |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
345 tmp(i) = Post[i] + 1; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
346 retval(3) = tmp; |
5506 | 347 } |
348 | |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
349 if (nargout > 2) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
350 { |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
351 for (octave_idx_type i = 0; i < n; i++) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
352 tmp(i) = Parent[i] + 1; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
353 retval(2) = tmp; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
354 } |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
355 |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
356 if (nargout > 1) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
357 { |
20946 | 358 // compute the elimination tree height |
21585 | 359 octave_idx_type height = 0; |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
360 for (int i = 0 ; i < n ; i++) |
21585 | 361 height = std::max (height, Level[i]); |
362 height++; | |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
363 retval(1) = static_cast<double> (height); |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
364 } |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
365 |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
366 for (octave_idx_type i = 0; i < n; i++) |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
367 tmp(i) = ColCount[i]; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
368 retval(0) = tmp; |
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20163
diff
changeset
|
369 |
21585 | 370 cleanup: |
371 CHOLMOD_NAME(free_sparse) (&F, cm); | |
372 CHOLMOD_NAME(finish) (cm); | |
373 | |
374 if (! err_msg.empty ()) | |
375 error (err_msg.c_str ()); | |
376 | |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20898
diff
changeset
|
377 return retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20898
diff
changeset
|
378 |
5512 | 379 #else |
21109
bd1752782e56
Use err_disabled_feature, warn_disabled_feature throughout code base.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
380 err_disabled_feature ("symbfact", "CHOLMOD"); |
5512 | 381 #endif |
5506 | 382 } |
21585 | 383 |
384 /* | |
385 %!testif HAVE_CHOLMOD | |
386 %! A = sparse (magic (3)); | |
387 %! [count, h, parent, post, r] = symbfact (A); | |
388 %! assert (count, [3; 2; 1]); | |
389 %! assert (h, 3); | |
390 %! assert (parent, [2; 3; 0]); | |
391 %! assert (r, sparse (triu (true (3)))); | |
392 | |
393 %!testif HAVE_CHOLMOD | |
394 %! ## Test MODE "lower" | |
395 %! A = sparse (magic (3)); | |
396 %! [~, ~, ~, ~, l] = symbfact (A, "sym", "lower"); | |
397 %! assert (l, sparse (tril (true (3)))); | |
398 | |
399 %!testif HAVE_CHOLMOD | |
400 %! ## Bug #42587, singular matrix | |
401 %! A = sparse ([1 0 8;0 1 8;8 8 1]); | |
402 %! [count, h, parent, post, r] = symbfact (A); | |
403 | |
404 ## Test input validation | |
405 %!testif HAVE_CHOLMOD | |
406 %! fail ("symbfact ()"); | |
407 %! fail ("symbfact (1,2,3,4)"); | |
408 %! fail ("symbfact ({1})", "wrong type argument 'cell'"); | |
409 %! fail ("symbfact (sparse (1), {1})", "TYP must be a string"); | |
410 %! fail ("symbfact (sparse (1), 'foobar')", 'unrecognized TYP "foobar"'); | |
411 %! fail ("symbfact (sparse (1), 'sym', {'L'})", "MODE must be a string"); | |
412 %! fail ('symbfact (sparse (1), "sym", "foobar")', 'unrecognized MODE "foobar"'); | |
413 %! fail ("symbfact (sparse ([1, 2; 3, 4; 5, 6]))", "S must be a square matrix"); | |
414 | |
415 */ |