File: sparse-chol.cc

package info (click to toggle)
octave 10.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 145,484 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 660; xml: 192
file content (575 lines) | stat: -rw-r--r-- 12,950 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1998-2025 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <cstddef>

#include "CSparse.h"
#include "MatrixType.h"
#include "dRowVector.h"
#include "dSparse.h"
#include "lo-error.h"
#include "oct-cmplx.h"
#include "oct-sparse.h"
#include "oct-spparms.h"
#include "quit.h"
#include "sparse-chol.h"
#include "sparse-util.h"

OCTAVE_BEGIN_NAMESPACE(octave)

OCTAVE_BEGIN_NAMESPACE(math)

template <typename chol_type>
class sparse_chol<chol_type>::sparse_chol_rep
{
public:

  sparse_chol_rep ()
    : m_is_pd (false), m_minor_p (0), m_perm (), m_rcond (0)
#if defined (HAVE_CHOLMOD)
    , m_L (nullptr), m_common ()
#endif
  { }

  sparse_chol_rep (const chol_type& a, bool natural, bool force)
    : m_is_pd (false), m_minor_p (0), m_perm (), m_rcond (0)
#if defined (HAVE_CHOLMOD)
    , m_L (nullptr), m_common ()
#endif
  {
    init (a, natural, force);
  }

  sparse_chol_rep (const chol_type& a, octave_idx_type& info,
                   bool natural, bool force)
    : m_is_pd (false), m_minor_p (0), m_perm (), m_rcond (0)
#if defined (HAVE_CHOLMOD)
    , m_L (nullptr), m_common ()
#endif
  {
    info = init (a, natural, force);
  }

  OCTAVE_DISABLE_COPY_MOVE (sparse_chol_rep)

  ~sparse_chol_rep ()
  {
#if defined (HAVE_CHOLMOD)
    if (m_L)
      CHOLMOD_NAME (free_sparse) (&m_L, &m_common);

    CHOLMOD_NAME(finish) (&m_common);
#endif
  }

#if defined (HAVE_CHOLMOD)
  cholmod_sparse * L () const
  {
    return m_L;
  }

  cholmod_common * cc () const
  {
    cholmod_common *ret = const_cast<cholmod_common *> (&m_common);
    return ret;
  }

#endif

  octave_idx_type P () const
  {
#if defined (HAVE_CHOLMOD)
    return (m_minor_p == from_size_t (m_L->ncol) ? 0 : m_minor_p + 1);
#else
    return 0;
#endif
  }

  RowVector perm () const { return m_perm + 1; }

  SparseMatrix Q () const;

  bool is_positive_definite () const { return m_is_pd; }

  double rcond () const { return m_rcond; }

private:

  bool m_is_pd;

  octave_idx_type m_minor_p;

  RowVector m_perm;

  double m_rcond;

#if defined (HAVE_CHOLMOD)
  cholmod_sparse *m_L;

  cholmod_common m_common;

  void drop_zeros (const cholmod_sparse *S);
#endif

  octave_idx_type init (const chol_type& a, bool natural, bool force);
};

#if defined (HAVE_CHOLMOD)

// Can't use CHOLMOD_NAME(drop)(0.0, S, cm) because it doesn't treat
// complex matrices.

template <typename chol_type>
void
sparse_chol<chol_type>::sparse_chol_rep::drop_zeros (const cholmod_sparse *S)
{
  if (! S)
    return;

  octave_idx_type *Sp = static_cast<octave_idx_type *> (S->p);
  octave_idx_type *Si = static_cast<octave_idx_type *> (S->i);
  chol_elt *Sx = static_cast<chol_elt *> (S->x);

  octave_idx_type pdest = 0;
  octave_idx_type ncol = S->ncol;

  for (octave_idx_type k = 0; k < ncol; k++)
    {
      octave_idx_type p = Sp[k];
      octave_idx_type pend = Sp[k+1];
      Sp[k] = pdest;

      for (; p < pend; p++)
        {
          chol_elt sik = Sx[p];

          if (CHOLMOD_IS_NONZERO (sik))
            {
              if (p != pdest)
                {
                  Si[pdest] = Si[p];
                  Sx[pdest] = sik;
                }

              pdest++;
            }
        }
    }

  Sp[ncol] = pdest;
}

// Must provide a specialization for this function.
template <typename T>
int
get_xtype ();

template <>
inline int
get_xtype<double> ()
{
  return CHOLMOD_REAL;
}

template <>
inline int
get_xtype<Complex> ()
{
  return CHOLMOD_COMPLEX;
}

#endif

template <typename chol_type>
octave_idx_type
sparse_chol<chol_type>::sparse_chol_rep::init (const chol_type& a,
    bool natural, bool force)
{
  octave_idx_type info = 0;

#if defined (HAVE_CHOLMOD)

  octave_idx_type a_nr = a.rows ();
  octave_idx_type a_nc = a.cols ();

  if (a_nr != a_nc)
    (*current_liboctave_error_handler)
      ("sparse_chol requires square matrix");

  cholmod_common *cm = &m_common;

  // Setup initial parameters

  CHOLMOD_NAME(start) (cm);
  cm->prefer_zomplex = false;

  double spu = sparse_params::get_key ("spumoni");

  if (spu == 0.)
    {
      cm->print = -1;
      SUITESPARSE_ASSIGN_FPTR (printf_func, cm->print_function, nullptr);
    }
  else
    {
      cm->print = static_cast<int> (spu) + 2;
      SUITESPARSE_ASSIGN_FPTR (printf_func, cm->print_function,
                               &SparseCholPrint);
    }

  cm->error_handler = &SparseCholError;

  SUITESPARSE_ASSIGN_FPTR2 (divcomplex_func, cm->complex_divide,
                            divcomplex);

  SUITESPARSE_ASSIGN_FPTR2 (hypot_func, cm->hypotenuse, hypot);

  cm->final_asis = false;
  cm->final_super = false;
  cm->final_ll = true;
  cm->final_pack = true;
  cm->final_monotonic = true;
  cm->final_resymbol = false;

  cholmod_sparse A;
  cholmod_sparse *ac = &A;
  double dummy;

  ac->nrow = a_nr;
  ac->ncol = a_nc;

  ac->p = a.cidx ();
  ac->i = a.ridx ();
  ac->nzmax = a.nnz ();
  ac->packed = true;
  ac->sorted = true;
  ac->nz = nullptr;
#if defined (OCTAVE_ENABLE_64)
  ac->itype = CHOLMOD_LONG;
#else
  ac->itype = CHOLMOD_INT;
#endif
  ac->dtype = CHOLMOD_DOUBLE;
  ac->stype = 1;
  ac->xtype = get_xtype<chol_elt> ();

  if (a_nr < 1)
    ac->x = &dummy;
  else
    ac->x = a.data ();

  // use natural ordering if no q output parameter
  if (natural)
    {
      cm->nmethods = 1;
      cm->method[0].ordering = CHOLMOD_NATURAL;
      cm->postorder = false;
    }

  cholmod_factor *Lfactor = CHOLMOD_NAME(analyze) (ac, cm);
  CHOLMOD_NAME(factorize) (ac, Lfactor, cm);

  m_is_pd = cm->status == CHOLMOD_OK;
  info = (m_is_pd ? 0 : cm->status);

  if (m_is_pd || force)
    {
      m_rcond = CHOLMOD_NAME(rcond) (Lfactor, cm);

      m_minor_p = Lfactor->minor;

      m_L = CHOLMOD_NAME(factor_to_sparse) (Lfactor, cm);

      if (m_minor_p > 0 && m_minor_p < a_nr)
        {
          std::size_t n1 = a_nr + 1;
          m_L->p = CHOLMOD_NAME(realloc) (m_minor_p+1,
                                          sizeof(octave_idx_type),
                                          m_L->p, &n1, cm);

          CHOLMOD_NAME(reallocate_sparse)
          (static_cast<octave_idx_type *> (m_L->p)[m_minor_p],
           m_L, cm);

          m_L->ncol = m_minor_p;
        }

      drop_zeros (m_L);

      if (! natural)
        {
          m_perm.resize (a_nr);
          for (octave_idx_type i = 0; i < a_nr; i++)
            m_perm(i) = static_cast<octave_idx_type *> (Lfactor->Perm)[i];
        }
    }

  // NAME used to prefix statistics report from print_common
  static char blank_name[] = " ";

  CHOLMOD_NAME(print_common) (blank_name, cm);
  CHOLMOD_NAME(free_factor) (&Lfactor, cm);

  return info;

#else

  octave_unused_parameter (a);
  octave_unused_parameter (natural);
  octave_unused_parameter (force);

  (*current_liboctave_error_handler)
    ("support for CHOLMOD was unavailable or disabled when liboctave was built");

  return info;

#endif
}

template <typename chol_type>
SparseMatrix
sparse_chol<chol_type>::sparse_chol_rep::Q () const
{
#if defined (HAVE_CHOLMOD)

  octave_idx_type n = m_L->nrow;
  SparseMatrix p (n, n, n);

  for (octave_idx_type i = 0; i < n; i++)
    {
      p.xcidx (i) = i;
      p.xridx (i) = static_cast<octave_idx_type> (m_perm (i));
      p.xdata (i) = 1;
    }

  p.xcidx (n) = n;

  return p;

#else

  return SparseMatrix ();

#endif
}

template <typename chol_type>
sparse_chol<chol_type>::sparse_chol ()
  : m_rep (new typename sparse_chol<chol_type>::sparse_chol_rep ())
{ }

template <typename chol_type>
sparse_chol<chol_type>::sparse_chol (const chol_type& a, bool natural,
                                     bool force)
  : m_rep (new typename
           sparse_chol<chol_type>::sparse_chol_rep (a, natural, force))
{ }

template <typename chol_type>
sparse_chol<chol_type>::sparse_chol (const chol_type& a,
                                     octave_idx_type& info,
                                     bool natural, bool force)
  : m_rep (new typename
           sparse_chol<chol_type>::sparse_chol_rep (a, info, natural, force))
{ }

template <typename chol_type>
sparse_chol<chol_type>::sparse_chol (const chol_type& a,
                                     octave_idx_type& info,
                                     bool natural)
  : m_rep (new typename
           sparse_chol<chol_type>::sparse_chol_rep (a, info, natural, false))
{ }

template <typename chol_type>
sparse_chol<chol_type>::sparse_chol (const chol_type& a,
                                     octave_idx_type& info)
  : m_rep (new typename
           sparse_chol<chol_type>::sparse_chol_rep (a, info, false, false))
{ }

template <typename chol_type>
chol_type
sparse_chol<chol_type>::L () const
{
#if defined (HAVE_CHOLMOD)

  cholmod_sparse *m = m_rep->L ();

  octave_idx_type nc = m->ncol;
  octave_idx_type nnz
    = from_suitesparse_long (CHOLMOD_NAME(nnz) (m, m_rep->cc ()));

  chol_type ret (m->nrow, nc, nnz);

  for (octave_idx_type j = 0; j < nc+1; j++)
    ret.xcidx (j) = static_cast<octave_idx_type *> (m->p)[j];

  for (octave_idx_type i = 0; i < nnz; i++)
    {
      ret.xridx (i) = static_cast<octave_idx_type *> (m->i)[i];
      ret.xdata (i) = static_cast<chol_elt *> (m->x)[i];
    }

  return ret;

#else

  return chol_type ();

#endif
}

template <typename chol_type>
octave_idx_type
sparse_chol<chol_type>::P () const
{
  return m_rep->P ();
}

template <typename chol_type>
RowVector
sparse_chol<chol_type>::perm () const
{
  return m_rep->perm ();
}

template <typename chol_type>
SparseMatrix
sparse_chol<chol_type>::Q () const
{
  return m_rep->Q ();
}

template <typename chol_type>
bool
sparse_chol<chol_type>::is_positive_definite () const
{
  return m_rep->is_positive_definite ();
}

template <typename chol_type>
double
sparse_chol<chol_type>::rcond () const
{
  return m_rep->rcond ();
}

template <typename chol_type>
chol_type
sparse_chol<chol_type>::inverse () const
{
  chol_type retval;

#if defined (HAVE_CHOLMOD)

  cholmod_sparse *m = m_rep->L ();
  octave_idx_type n = m->ncol;
  RowVector m_perm = m_rep->perm ();
  double rcond2;
  octave_idx_type info;
  MatrixType mattype (MatrixType::Upper);
  chol_type linv = L ().hermitian ().inverse (mattype, info, rcond2, 1, 0);

  if (m_perm.numel () == n)
    {
      SparseMatrix Qc = Q ();

      retval = Qc * linv * linv.hermitian () * Qc.transpose ();
    }
  else
    retval = linv * linv.hermitian ();

#endif

  return retval;
}

template <typename chol_type>
chol_type
chol2inv (const chol_type& r)
{
  octave_idx_type r_nr = r.rows ();
  octave_idx_type r_nc = r.cols ();
  chol_type retval;

  if (r_nr != r_nc)
    (*current_liboctave_error_handler) ("U must be a square matrix");

  MatrixType mattype (r);
  int typ = mattype.type (false);
  double rcond;
  octave_idx_type info;
  chol_type rtra, multip;

  if (typ == MatrixType::Upper)
    {
      rtra = r.transpose ();
      multip = (rtra*r);
    }
  else if (typ == MatrixType::Lower)
    {
      rtra = r.transpose ();
      multip = (r*rtra);
    }
  else
    (*current_liboctave_error_handler) ("U must be a triangular matrix");

  MatrixType mattypenew (multip);
  retval = multip.inverse (mattypenew, info, rcond, true, false);
  return retval;
}

// SparseComplexMatrix specialization (the value for the NATURAL
// parameter in the sparse_chol<T>::sparse_chol_rep constructor is
// different from the default).

template <>
OCTAVE_API
sparse_chol<SparseComplexMatrix>::sparse_chol (const SparseComplexMatrix& a,
    octave_idx_type& info)
  : m_rep (new sparse_chol<SparseComplexMatrix>::sparse_chol_rep (a, info,
           true,
           false))
{ }

// Instantiations we need.

template class OCTAVE_API sparse_chol<SparseMatrix>;

template class sparse_chol<SparseComplexMatrix>;

template OCTAVE_API SparseMatrix
chol2inv<SparseMatrix> (const SparseMatrix& r);

template OCTAVE_API SparseComplexMatrix
chol2inv<SparseComplexMatrix> (const SparseComplexMatrix& r);

OCTAVE_END_NAMESPACE(math)
OCTAVE_END_NAMESPACE(octave)