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