5506
|
1 /* |
|
2 |
|
3 Copyright (C) 2005 David Bateman |
|
4 Copyright (C) 1998-2005 Andy Adler |
|
5 |
|
6 Octave is free software; you can redistribute it and/or modify it |
|
7 under the terms of the GNU General Public License as published by the |
|
8 Free Software Foundation; either version 2, or (at your option) any |
|
9 later version. |
|
10 |
|
11 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 for more details. |
|
15 |
|
16 You should have received a copy of the GNU General Public License |
|
17 along with this program; see the file COPYING. If not, write to the |
|
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
19 Boston, MA 02110-1301, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "defun-dld.h" |
|
28 #include "error.h" |
|
29 #include "gripes.h" |
|
30 #include "oct-obj.h" |
|
31 #include "utils.h" |
|
32 |
|
33 #include "SparseCmplxCHOL.h" |
|
34 #include "SparsedbleCHOL.h" |
|
35 #include "ov-re-sparse.h" |
|
36 #include "ov-cx-sparse.h" |
|
37 #include "oct-spparms.h" |
|
38 #include "sparse-util.h" |
|
39 |
|
40 static octave_value_list |
|
41 sparse_chol (const octave_value_list& args, const int nargout, |
|
42 const std::string& name, const bool LLt) |
|
43 { |
|
44 octave_value_list retval; |
|
45 int nargin = args.length (); |
|
46 |
|
47 if (nargin != 1 || nargout > 3) |
|
48 { |
|
49 print_usage (name); |
|
50 return retval; |
|
51 } |
|
52 |
|
53 octave_value arg = args(0); |
|
54 |
|
55 octave_idx_type nr = arg.rows (); |
|
56 octave_idx_type nc = arg.columns (); |
|
57 bool natural = (nargout != 3); |
|
58 |
|
59 int arg_is_empty = empty_arg (name.c_str(), nr, nc); |
|
60 |
|
61 if (arg_is_empty < 0) |
|
62 return retval; |
|
63 if (arg_is_empty > 0) |
|
64 return octave_value (Matrix ()); |
|
65 |
|
66 if (arg.is_real_type ()) |
|
67 { |
|
68 SparseMatrix m = arg.sparse_matrix_value (); |
|
69 |
|
70 if (! error_state) |
|
71 { |
|
72 octave_idx_type info; |
|
73 SparseCHOL fact (m, info, natural); |
|
74 if (nargout == 3) |
|
75 retval(2) = fact.Q(); |
|
76 |
|
77 if (nargout > 1 || info == 0) |
|
78 { |
|
79 retval(1) = fact.P(); |
|
80 if (LLt) |
|
81 retval(0) = fact.L(); |
|
82 else |
|
83 retval(0) = fact.R(); |
|
84 } |
|
85 else |
|
86 error ("%s: matrix not positive definite", name.c_str()); |
|
87 } |
|
88 } |
|
89 else if (arg.is_complex_type ()) |
|
90 { |
|
91 SparseComplexMatrix m = arg.sparse_complex_matrix_value (); |
|
92 |
|
93 if (! error_state) |
|
94 { |
|
95 octave_idx_type info; |
|
96 SparseComplexCHOL fact (m, info, natural); |
|
97 |
|
98 if (nargout == 3) |
|
99 retval(2) = fact.Q(); |
|
100 |
|
101 if (nargout > 1 || info == 0) |
|
102 { |
|
103 retval(1) = fact.P(); |
|
104 if (LLt) |
|
105 retval(0) = fact.L(); |
|
106 else |
|
107 retval(0) = fact.R(); |
|
108 } |
|
109 else |
|
110 error ("%s: matrix not positive definite", name.c_str()); |
|
111 } |
|
112 } |
|
113 else |
|
114 gripe_wrong_type_arg (name.c_str(), arg); |
|
115 |
|
116 return retval; |
|
117 } |
|
118 |
5534
|
119 // PKG_ADD: dispatch ("chol", "spchol", "sparse matrix"); |
|
120 // PKG_ADD: dispatch ("chol", "spchol", "sparse complex matrix"); |
|
121 // PKG_ADD: dispatch ("chol", "spchol", "sparse bool matrix"); |
5506
|
122 DEFUN_DLD (spchol, args, nargout, |
|
123 "-*- texinfo -*-\n\ |
|
124 @deftypefn {Loadable Function} {@var{r} =} spchol (@var{a})\n\ |
|
125 @deftypefnx {Loadable Function} {[@var{r}, @var{p}] =} spchol (@var{a})\n\ |
|
126 @deftypefnx {Loadable Function} {[@var{r}, @var{p}, @var{q}] =} spchol (@var{a})\n\ |
|
127 @cindex Cholesky factorization\n\ |
|
128 Compute the Cholesky factor, @var{r}, of the symmetric positive definite\n\ |
|
129 sparse matrix @var{a}, where\n\ |
|
130 @iftex\n\ |
|
131 @tex\n\ |
|
132 $ R^T R = A $.\n\ |
|
133 @end tex\n\ |
|
134 @end iftex\n\ |
|
135 @ifinfo\n\ |
|
136 \n\ |
|
137 @example\n\ |
|
138 r' * r = a.\n\ |
|
139 @end example\n\ |
|
140 @end ifinfo\n\ |
|
141 \n\ |
|
142 If called with 2 or more outputs @var{p} is the 0 when @var{r} is positive\n\ |
|
143 definite and @var{p} is a positive integer otherwise.\n\ |
|
144 \n\ |
|
145 If called with 3 outputs then a sparsity preserving row/column permutation\n\ |
|
146 is applied to @var{a} prior to the factorization. That is @var{r}\n\ |
|
147 is the factorization of @code{@var{a}(@var{q},@var{q})} such that\n\ |
|
148 @iftex\n\ |
|
149 @tex\n\ |
|
150 $ R^T R = Q A Q^T$.\n\ |
|
151 @end tex\n\ |
|
152 @end iftex\n\ |
|
153 @ifinfo\n\ |
|
154 \n\ |
|
155 @example\n\ |
|
156 r' * r = q * a * q'.\n\ |
|
157 @end example\n\ |
|
158 @end ifinfo\n\ |
|
159 \n\ |
|
160 Note that @code{splchol} factorizations is faster and use less memory.\n\ |
|
161 @end deftypefn\n\ |
|
162 @seealso{spcholinv, spchol2inv, splchol}") |
|
163 { |
|
164 return sparse_chol (args, nargout, "spchol", false); |
|
165 } |
|
166 |
5534
|
167 // PKG_ADD: dispatch ("lchol", "splchol", "sparse matrix"); |
|
168 // PKG_ADD: dispatch ("lchol", "splchol", "sparse complex matrix"); |
|
169 // PKG_ADD: dispatch ("lchol", "splchol", "sparse bool matrix"); |
5506
|
170 DEFUN_DLD (splchol, args, nargout, |
|
171 "-*- texinfo -*-\n\ |
|
172 @deftypefn {Loadable Function} {@var{l} =} splchol (@var{a})\n\ |
|
173 @deftypefnx {Loadable Function} {[@var{l}, @var{p}] =} splchol (@var{a})\n\ |
|
174 @deftypefnx {Loadable Function} {[@var{l}, @var{p}, @var{q}] =} splchol (@var{a})\n\ |
|
175 @cindex Cholesky factorization\n\ |
|
176 Compute the Cholesky factor, @var{l}, of the symmetric positive definite\n\ |
|
177 sparse matrix @var{a}, where\n\ |
|
178 @iftex\n\ |
|
179 @tex\n\ |
|
180 $ L L^T = A $.\n\ |
|
181 @end tex\n\ |
|
182 @end iftex\n\ |
|
183 @ifinfo\n\ |
|
184 \n\ |
|
185 @example\n\ |
|
186 l * l' = a.\n\ |
|
187 @end example\n\ |
|
188 @end ifinfo\n\ |
|
189 \n\ |
|
190 If called with 2 or more outputs @var{p} is the 0 when @var{l} is positive\n\ |
|
191 definite and @var{l} is a positive integer otherwise.\n\ |
|
192 \n\ |
|
193 If called with 3 outputs that a sparsity preserving row/column permutation\n\ |
|
194 is applied to @var{a} prior to the factorization. That is @var{l}\n\ |
|
195 is the factorization of @code{@var{a}(@var{q},@var{q})} such that\n\ |
|
196 @iftex\n\ |
|
197 @tex\n\ |
|
198 $ L R^T = A (Q, Q)$.\n\ |
|
199 @end tex\n\ |
|
200 @end iftex\n\ |
|
201 @ifinfo\n\ |
|
202 \n\ |
|
203 @example\n\ |
|
204 r * r' = a (q, q).\n\ |
|
205 @end example\n\ |
|
206 @end ifinfo\n\ |
|
207 \n\ |
|
208 Note that @code{splchol} factorizations is faster and use less memory\n\ |
|
209 than @code{spchol}. @code{splchol(@var{a})} is equivalent to\n\ |
|
210 @code{spchol(@var{a})'}.\n\ |
|
211 @end deftypefn\n\ |
|
212 @seealso{spcholinv, spchol2inv, splchol}") |
|
213 { |
|
214 return sparse_chol (args, nargout, "splchol", true); |
|
215 } |
|
216 |
5534
|
217 // PKG_ADD: dispatch ("cholinv", "spcholinv", "sparse matrix"); |
|
218 // PKG_ADD: dispatch ("cholinv", "spcholinv", "sparse complex matrix"); |
|
219 // PKG_ADD: dispatch ("cholinv", "spcholinv", "sparse bool matrix"); |
5506
|
220 DEFUN_DLD (spcholinv, args, , |
|
221 "-*- texinfo -*-\n\ |
|
222 @deftypefn {Loadable Function} {} spcholinv (@var{a})\n\ |
|
223 Use the Cholesky factorization to compute the inverse of the\n\ |
|
224 sparse symmetric positive definite matrix @var{a}.\n\ |
|
225 @seealso{spchol, spchol2inv}\n\ |
|
226 @end deftypefn") |
|
227 { |
|
228 octave_value retval; |
|
229 |
|
230 int nargin = args.length (); |
|
231 |
|
232 if (nargin == 1) |
|
233 { |
|
234 octave_value arg = args(0); |
|
235 |
|
236 octave_idx_type nr = arg.rows (); |
|
237 octave_idx_type nc = arg.columns (); |
|
238 |
|
239 if (nr == 0 || nc == 0) |
|
240 retval = Matrix (); |
|
241 else |
|
242 { |
|
243 if (arg.is_real_type ()) |
|
244 { |
|
245 SparseMatrix m = arg.sparse_matrix_value (); |
|
246 |
|
247 if (! error_state) |
|
248 { |
|
249 octave_idx_type info; |
|
250 SparseCHOL chol (m, info); |
|
251 if (info == 0) |
|
252 retval = chol.inverse (); |
|
253 else |
|
254 error ("spcholinv: matrix not positive definite"); |
|
255 } |
|
256 } |
|
257 else if (arg.is_complex_type ()) |
|
258 { |
|
259 SparseComplexMatrix m = arg.sparse_complex_matrix_value (); |
|
260 |
|
261 if (! error_state) |
|
262 { |
|
263 octave_idx_type info; |
|
264 SparseComplexCHOL chol (m, info); |
|
265 if (info == 0) |
|
266 retval = chol.inverse (); |
|
267 else |
|
268 error ("spcholinv: matrix not positive definite"); |
|
269 } |
|
270 } |
|
271 else |
|
272 gripe_wrong_type_arg ("spcholinv", arg); |
|
273 } |
|
274 } |
|
275 else |
|
276 print_usage ("spcholinv"); |
|
277 |
|
278 return retval; |
|
279 } |
|
280 |
5534
|
281 // PKG_ADD: dispatch ("chol2inv", "spchol2inv", "sparse matrix"); |
|
282 // PKG_ADD: dispatch ("chol2inv", "spchol2inv", "sparse complex matrix"); |
|
283 // PKG_ADD: dispatch ("chol2inv", "spchol2inv", "sparse bool matrix"); |
5506
|
284 DEFUN_DLD (spchol2inv, args, , |
|
285 "-*- texinfo -*-\n\ |
|
286 @deftypefn {Loadable Function} {} spchol2inv (@var{u})\n\ |
|
287 Invert a sparse symmetric, positive definite square matrix from its\n\ |
|
288 Cholesky decomposition, @var{u}. Note that @var{u} should be an\n\ |
|
289 upper-triangular matrix with positive diagonal elements.\n\ |
|
290 @code{chol2inv (@var{u})} provides @code{inv (@var{u}'*@var{u})} but\n\ |
|
291 it is much faster than using @code{inv}.\n\ |
|
292 @seealso{spchol, spcholinv}\n\ |
|
293 @end deftypefn") |
|
294 { |
|
295 octave_value retval; |
|
296 |
|
297 int nargin = args.length (); |
|
298 |
|
299 if (nargin == 1) |
|
300 { |
|
301 octave_value arg = args(0); |
|
302 |
|
303 octave_idx_type nr = arg.rows (); |
|
304 octave_idx_type nc = arg.columns (); |
|
305 |
|
306 if (nr == 0 || nc == 0) |
|
307 retval = Matrix (); |
|
308 else |
|
309 { |
|
310 if (arg.is_real_type ()) |
|
311 { |
|
312 SparseMatrix r = arg.sparse_matrix_value (); |
|
313 |
|
314 if (! error_state) |
|
315 retval = chol2inv (r); |
|
316 } |
|
317 else if (arg.is_complex_type ()) |
|
318 { |
|
319 SparseComplexMatrix r = arg.sparse_complex_matrix_value (); |
|
320 |
|
321 if (! error_state) |
|
322 retval = chol2inv (r); |
|
323 } |
|
324 else |
|
325 gripe_wrong_type_arg ("spchol2inv", arg); |
|
326 } |
|
327 } |
|
328 else |
|
329 print_usage ("spchol2inv"); |
|
330 |
|
331 return retval; |
|
332 } |
|
333 |
|
334 DEFUN_DLD (symbfact, args, nargout, |
|
335 "-*- texinfo -*-\n\ |
|
336 @deftypefn {Loadable Function} {[@var{count}, @var{h}, @var{parent}, @var{post}, @var{r}]} = symbfact (@var{s}, @var{typ}, @var{mode})\n\ |
|
337 \n\ |
|
338 Performs a symbolic factorization analysis on the sparse matrix @var{s}.\n\ |
|
339 Where\n\ |
|
340 \n\ |
|
341 @table @asis\n\ |
|
342 @item @var{s}\n\ |
|
343 @var{s} is a complex or real sparse matrix.\n\ |
|
344 \n\ |
|
345 @item @var{typ}\n\ |
|
346 Is the type of the factorization and can be one of\n\ |
|
347 \n\ |
|
348 @table @code\n\ |
|
349 @item sym\n\ |
|
350 Factorize @var{s}. This is the default.\n\ |
|
351 \n\ |
|
352 @item col\n\ |
|
353 Factorize @code{@var{s}' * @var{s}}.\n\ |
|
354 @item row\n\ |
|
355 Factorize @code{@var{s} * @var{s}'}.\n\ |
|
356 @item lo\n\ |
|
357 Factorize @code{@var{s}'}\n\ |
|
358 @end table\n\ |
|
359 \n\ |
|
360 @item @var{mode}\n\ |
|
361 The default is to return the Cholesky factorization for @var{r}, and if\n\ |
|
362 @var{mode} is 'L', the conjugate transpose of the Choleksy factorization\n\ |
|
363 is returned. The conjugate transpose version is faster and uses less\n\ |
|
364 memory, but returns the same values for @var{count}, @var{h}, @var{parent}\n\ |
|
365 and @var{post} outputs.\n\ |
|
366 @end table\n\ |
|
367 \n\ |
|
368 The output variables are\n\ |
|
369 \n\ |
|
370 @table @asis\n\ |
|
371 @item @var{count}\n\ |
|
372 The row counts of the Cholesky factorization as determined by @var{typ}.\n\ |
|
373 \n\ |
|
374 @item @var{h}\n\ |
|
375 The height of the elimination tree.\n\ |
|
376 \n\ |
|
377 @item @var{parent}\n\ |
|
378 The elimination tree itself.\n\ |
|
379 \n\ |
|
380 @item @var{post}\n\ |
|
381 A sparse boolean matrix whose structure is that of the Cholesky\n\ |
|
382 factorization as determined by @var{typ}.\n\ |
|
383 @end table\n\ |
|
384 @end deftypefn") |
|
385 { |
|
386 octave_value_list retval; |
|
387 int nargin = args.length (); |
|
388 |
|
389 if (nargin < 1 || nargin > 3 || nargout > 5) |
|
390 { |
|
391 print_usage ("symbfact"); |
|
392 return retval; |
|
393 } |
|
394 |
5512
|
395 #ifdef HAVE_CHOLMOD |
|
396 |
5506
|
397 cholmod_common Common; |
|
398 cholmod_common *cm = &Common; |
|
399 CHOLMOD_NAME(start) (cm); |
|
400 |
|
401 double spu = Voctave_sparse_controls.get_key ("spumoni"); |
|
402 if (spu == 0.) |
|
403 { |
|
404 cm->print = -1; |
|
405 cm->print_function = NULL; |
|
406 } |
|
407 else |
|
408 { |
|
409 cm->print = (int)spu + 2; |
|
410 cm->print_function =&SparseCholPrint; |
|
411 } |
|
412 |
|
413 cm->error_handler = &SparseCholError; |
|
414 cm->complex_divide = CHOLMOD_NAME(divcomplex); |
|
415 cm->hypotenuse = CHOLMOD_NAME(hypot); |
|
416 |
|
417 #ifdef HAVE_METIS |
|
418 // METIS 4.0.1 uses malloc and free, and will terminate MATLAB if it runs |
|
419 // out of memory. Use CHOLMOD's memory guard for METIS, which mxMalloc's |
|
420 // a huge block of memory (and then immediately mxFree's it) before calling |
|
421 // METIS |
|
422 cm->metis_memory = 2.0; |
|
423 |
|
424 #if defined(METIS_VERSION) |
|
425 #if (METIS_VERSION >= METIS_VER(4,0,2)) |
|
426 // METIS 4.0.2 uses function pointers for malloc and free |
|
427 METIS_malloc = cm->malloc_memory; |
|
428 METIS_free = cm->free_memory; |
|
429 // Turn off METIS memory guard. It is not needed, because mxMalloc will |
|
430 // safely terminate the mexFunction and free any workspace without killing |
|
431 // all of MATLAB. |
|
432 cm->metis_memory = 0.0; |
|
433 #endif |
|
434 #endif |
|
435 #endif |
|
436 |
|
437 double dummy; |
|
438 cholmod_sparse Astore; |
|
439 cholmod_sparse *A = &Astore; |
5527
|
440 A->packed = true; |
|
441 A->sorted = true; |
5506
|
442 A->nz = NULL; |
|
443 #ifdef IDX_TYPE_LONG |
|
444 A->itype = CHOLMOD_LONG; |
|
445 #else |
|
446 A->itype = CHOLMOD_INT; |
|
447 #endif |
|
448 A->dtype = CHOLMOD_DOUBLE; |
|
449 A->stype = 1; |
|
450 A->x = &dummy; |
|
451 |
|
452 if (args(0).is_real_type ()) |
|
453 { |
|
454 const SparseMatrix a = args(0).sparse_matrix_value(); |
|
455 A->nrow = a.rows(); |
|
456 A->ncol = a.cols(); |
|
457 A->p = a.cidx(); |
|
458 A->i = a.ridx(); |
|
459 A->nzmax = a.nonzero(); |
|
460 A->xtype = CHOLMOD_REAL; |
|
461 |
|
462 if (a.rows() > 0 && a.cols() > 0) |
|
463 A->x = a.data(); |
|
464 } |
|
465 else if (args(0).is_complex_type ()) |
|
466 { |
|
467 const SparseComplexMatrix a = args(0).sparse_complex_matrix_value(); |
|
468 A->nrow = a.rows(); |
|
469 A->ncol = a.cols(); |
|
470 A->p = a.cidx(); |
|
471 A->i = a.ridx(); |
|
472 A->nzmax = a.nonzero(); |
|
473 A->xtype = CHOLMOD_COMPLEX; |
|
474 |
|
475 if (a.rows() > 0 && a.cols() > 0) |
|
476 A->x = a.data(); |
|
477 } |
|
478 else |
|
479 gripe_wrong_type_arg ("symbfact", arg(0)); |
|
480 |
5527
|
481 octave_idx_type coletree = false; |
5506
|
482 octave_idx_type n = A->nrow; |
|
483 |
|
484 if (nargin > 1) |
|
485 { |
|
486 char ch; |
|
487 std::string str = args(1).string_value(); |
|
488 ch = tolower (str.c_str()[0]); |
|
489 if (ch == 'r') |
|
490 A->stype = 0; |
|
491 else if (ch == 'c') |
|
492 { |
|
493 n = A->ncol; |
5527
|
494 coletree = true; |
5506
|
495 A->stype = 0; |
|
496 } |
|
497 else if (ch == 's') |
|
498 A->stype = 1; |
|
499 else if (ch == 's') |
|
500 A->stype = -1; |
|
501 else |
|
502 error ("Unrecognized typ in symbolic factorization"); |
|
503 } |
|
504 |
|
505 if (A->stype && A->nrow != A->ncol) |
|
506 error ("Matrix must be square"); |
|
507 |
|
508 if (!error_state) |
|
509 { |
|
510 OCTAVE_LOCAL_BUFFER (octave_idx_type, Parent, n); |
|
511 OCTAVE_LOCAL_BUFFER (octave_idx_type, Post, n); |
|
512 OCTAVE_LOCAL_BUFFER (octave_idx_type, ColCount, n); |
|
513 OCTAVE_LOCAL_BUFFER (octave_idx_type, First, n); |
|
514 OCTAVE_LOCAL_BUFFER (octave_idx_type, Level, n); |
|
515 |
|
516 cholmod_sparse *F = CHOLMOD_NAME(transpose) (A, 0, cm); |
|
517 cholmod_sparse *Aup, *Alo; |
|
518 |
|
519 if (A->stype == 1 || coletree) |
|
520 { |
|
521 Aup = A ; |
|
522 Alo = F ; |
|
523 } |
|
524 else |
|
525 { |
|
526 Aup = F ; |
|
527 Alo = A ; |
|
528 } |
|
529 |
|
530 CHOLMOD_NAME(etree) (Aup, Parent, cm); |
|
531 |
|
532 if (cm->status < CHOLMOD_OK) |
|
533 { |
|
534 error("matrix corrupted"); |
|
535 goto symbfact_error; |
|
536 } |
|
537 |
|
538 if (CHOLMOD_NAME(postorder) (Parent, n, NULL, Post, cm) != n) |
|
539 { |
|
540 error("postorder failed"); |
|
541 goto symbfact_error; |
|
542 } |
|
543 |
|
544 CHOLMOD_NAME(rowcolcounts) (Alo, NULL, 0, Parent, Post, NULL, |
|
545 ColCount, First, Level, cm); |
|
546 |
|
547 if (cm->status < CHOLMOD_OK) |
|
548 { |
|
549 error("matrix corrupted"); |
|
550 goto symbfact_error; |
|
551 } |
|
552 |
|
553 if (nargout > 4) |
|
554 { |
|
555 cholmod_sparse *A1, *A2; |
|
556 |
|
557 if (A->stype == 1) |
|
558 { |
|
559 A1 = A; |
|
560 A2 = NULL; |
|
561 } |
|
562 else if (A->stype == -1) |
|
563 { |
|
564 A1 = F; |
|
565 A2 = NULL; |
|
566 } |
|
567 else if (coletree) |
|
568 { |
|
569 A1 = F; |
|
570 A2 = A; |
|
571 } |
|
572 else |
|
573 { |
|
574 A1 = A; |
|
575 A2 = F; |
|
576 } |
|
577 |
|
578 // count the total number of entries in L |
|
579 octave_idx_type lnz = 0 ; |
|
580 for (octave_idx_type j = 0 ; j < n ; j++) |
|
581 lnz += ColCount [j] ; |
|
582 |
|
583 |
|
584 // allocate the output matrix L (pattern-only) |
|
585 SparseBoolMatrix L (n, n, lnz); |
|
586 |
|
587 // initialize column pointers |
|
588 lnz = 0; |
|
589 for (octave_idx_type j = 0 ; j < n ; j++) |
|
590 { |
|
591 L.xcidx(j) = lnz; |
|
592 lnz += ColCount [j]; |
|
593 } |
|
594 L.xcidx(n) = lnz; |
|
595 |
|
596 |
|
597 /* create a copy of the column pointers */ |
|
598 octave_idx_type *W = First; |
|
599 for (octave_idx_type j = 0 ; j < n ; j++) |
|
600 W [j] = L.xcidx(j); |
|
601 |
|
602 // get workspace for computing one row of L |
5527
|
603 cholmod_sparse *R = cholmod_allocate_sparse (n, 1, n, false, true, |
5506
|
604 0, CHOLMOD_PATTERN, cm); |
|
605 octave_idx_type *Rp = static_cast<octave_idx_type *>(R->p); |
|
606 octave_idx_type *Ri = static_cast<octave_idx_type *>(R->i); |
|
607 |
|
608 // compute L one row at a time |
|
609 for (octave_idx_type k = 0 ; k < n ; k++) |
|
610 { |
|
611 // get the kth row of L and store in the columns of L |
|
612 cholmod_row_subtree (A1, A2, k, Parent, R, cm) ; |
|
613 for (octave_idx_type p = 0 ; p < Rp [1] ; p++) |
|
614 L.xridx (W [Ri [p]]++) = k ; |
|
615 |
|
616 // add the diagonal entry |
|
617 L.xridx (W [k]++) = k ; |
|
618 } |
|
619 |
|
620 // free workspace |
|
621 cholmod_free_sparse (&R, cm) ; |
|
622 |
|
623 |
|
624 // transpose L to get R, or leave as is |
|
625 if (nargin < 3) |
|
626 L = L.transpose (); |
|
627 |
|
628 // fill numerical values of L with one's |
|
629 for (octave_idx_type p = 0 ; p < lnz ; p++) |
|
630 L.xdata(p) = true; |
|
631 |
|
632 retval(4) = L; |
|
633 } |
|
634 |
|
635 ColumnVector tmp (n); |
|
636 if (nargout > 3) |
|
637 { |
|
638 for (octave_idx_type i = 0; i < n; i++) |
|
639 tmp(i) = Post[i] + 1; |
|
640 retval(3) = tmp; |
|
641 } |
|
642 |
|
643 if (nargout > 2) |
|
644 { |
|
645 for (octave_idx_type i = 0; i < n; i++) |
|
646 tmp(i) = Parent[i] + 1; |
|
647 retval(2) = tmp; |
|
648 } |
|
649 |
|
650 if (nargout > 1) |
|
651 { |
|
652 /* compute the elimination tree height */ |
|
653 octave_idx_type height = 0 ; |
|
654 for (int i = 0 ; i < n ; i++) |
|
655 height = (height > Level[i] ? height : Level[i]); |
|
656 height++ ; |
|
657 retval(1) = (double)height; |
|
658 } |
|
659 |
|
660 for (octave_idx_type i = 0; i < n; i++) |
|
661 tmp(i) = ColCount[i]; |
|
662 retval(0) = tmp; |
|
663 } |
|
664 |
5512
|
665 symbfact_error: |
|
666 #else |
|
667 error ("symbfact: not available in this version of Octave"); |
|
668 #endif |
|
669 |
5506
|
670 return retval; |
|
671 } |
|
672 |
|
673 /* |
|
674 ;;; Local Variables: *** |
|
675 ;;; mode: C++ *** |
|
676 ;;; End: *** |
|
677 */ |
|
678 |