GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
__ichol__.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2013-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "oct-locbuf.h"
31 #include "oct-norm.h"
32 
33 #include "defun.h"
34 #include "error.h"
35 
36 #include "builtin-defun-decls.h"
37 
38 // Secondary functions for complex and real case used in ichol algorithms.
40 {
41 #if defined (HAVE_CXX_COMPLEX_SETTERS)
42  b.imag (-b.imag ());
43 #elif defined (HAVE_CXX_COMPLEX_REFERENCE_ACCESSORS)
44  b.imag () = -b.imag ();
45 #else
46  b = b.conj ();
47 #endif
48  return a * b;
49 }
50 
51 double ichol_mult_real (double a, double b)
52 {
53  return a * b;
54 }
55 
57 {
58  if (pivot.imag () != 0)
59  error ("ichol: non-real pivot encountered. The matrix must be Hermitian.");
60  else if (pivot.real () < 0)
61  error ("ichol: negative pivot encountered");
62 
63  return true;
64 }
65 
66 bool ichol_checkpivot_real (double pivot)
67 {
68  if (pivot < 0)
69  error ("ichol: negative pivot encountered");
70 
71  return true;
72 }
73 
74 template <typename octave_matrix_t, typename T, T (*ichol_mult) (T, T),
75  bool (*ichol_checkpivot) (T)>
76 void ichol_0 (octave_matrix_t& sm, const std::string michol = "off")
77 {
78  const octave_idx_type n = sm.cols ();
79  octave_idx_type j1, jend, j2, jrow, jjrow, j, jw, i, k, jj, r;
80  T tl;
81 
82  char opt;
83  enum {OFF, ON};
84  if (michol == "on")
85  opt = ON;
86  else
87  opt = OFF;
88 
89  // Input matrix pointers
90  octave_idx_type *cidx = sm.cidx ();
91  octave_idx_type *ridx = sm.ridx ();
92  T* data = sm.data ();
93 
94  // Working arrays
98  OCTAVE_LOCAL_BUFFER (T, dropsums, n);
99 
100  // Initialize working arrays
101  for (i = 0; i < n; i++)
102  {
103  iw[i] = -1;
104  Llist[i] = -1;
105  Lfirst[i] = -1;
106  dropsums[i] = 0;
107  }
108 
109  // Loop over all columns
110  for (k = 0; k < n; k++)
111  {
112  j1 = cidx[k];
113  j2 = cidx[k+1];
114  for (j = j1; j < j2; j++)
115  iw[ridx[j]] = j;
116 
117  jrow = Llist[k];
118  // Iterate over each non-zero element in the actual row.
119  while (jrow != -1)
120  {
121  jjrow = Lfirst[jrow];
122  jend = cidx[jrow+1];
123  for (jj = jjrow; jj < jend; jj++)
124  {
125  r = ridx[jj];
126  jw = iw[r];
127  tl = ichol_mult (data[jj], data[jjrow]);
128  if (jw != -1)
129  data[jw] -= tl;
130  else
131  // Because of the symmetry of the matrix, we know
132  // the drops in the column r are also in the column k.
133  if (opt == ON)
134  {
135  dropsums[r] -= tl;
136  dropsums[k] -= tl;
137  }
138  }
139  // Update the linked list and the first entry of the actual column.
140  if ((jjrow + 1) < jend)
141  {
142  Lfirst[jrow]++;
143  j = jrow;
144  jrow = Llist[jrow];
145  Llist[j] = Llist[ridx[Lfirst[j]]];
146  Llist[ridx[Lfirst[j]]] = j;
147  }
148  else
149  jrow = Llist[jrow];
150  }
151 
152  if (opt == ON)
153  data[j1] += dropsums[k];
154 
155  // Test for j1 == j2 must be first to avoid invalid ridx[j1] access
156  if (j1 == j2 || ridx[j1] != k)
157  error ("ichol: encountered a pivot equal to 0");
158 
159  if (! ichol_checkpivot (data[j1]))
160  break;
161 
162  data[cidx[k]] = std::sqrt (data[j1]);
163 
164  // Update Llist and Lfirst with the k-column information. Also,
165  // scale the column elements by the pivot and reset the working array iw.
166  if (k < (n - 1))
167  {
168  iw[ridx[j1]] = -1;
169  for (i = j1 + 1; i < j2; i++)
170  {
171  iw[ridx[i]] = -1;
172  data[i] /= data[j1];
173  }
174  Lfirst[k] = j1;
175  if ((Lfirst[k] + 1) < j2)
176  {
177  Lfirst[k]++;
178  jjrow = ridx[Lfirst[k]];
179  Llist[k] = Llist[jjrow];
180  Llist[jjrow] = k;
181  }
182  }
183  }
184 }
185 
186 DEFUN (__ichol0__, args, ,
187  doc: /* -*- texinfo -*-
188 @deftypefn {} {@var{L} =} __ichol0__ (@var{A}, @var{michol})
189 Undocumented internal function.
190 @end deftypefn */)
191 {
192  if (args.length () != 2)
193  print_usage ();
194 
195  std::string michol = args(1).string_value ();
196 
197  // In ICHOL0 algorithm the zero-pattern of the input matrix is preserved
198  // so its structure does not change during the algorithm. The same input
199  // matrix is used to build the output matrix due to that fact.
200  if (! args(0).iscomplex ())
201  {
202  SparseMatrix sm = Ftril (args(0))(0).sparse_matrix_value ();
204  ichol_checkpivot_real> (sm, michol);
205  return ovl (sm);
206  }
207  else
208  {
210  = Ftril (args(0))(0).sparse_complex_matrix_value ();
212  ichol_checkpivot_complex> (sm, michol);
213  return ovl (sm);
214  }
215 }
216 
217 template <typename octave_matrix_t, typename T, T (*ichol_mult) (T, T),
218  bool (*ichol_checkpivot) (T)>
219 void ichol_t (const octave_matrix_t& sm, octave_matrix_t& L, const T* cols_norm,
220  const T droptol, const std::string michol = "off")
221 {
222 
223  const octave_idx_type n = sm.cols ();
224  octave_idx_type j, jrow, jend, jjrow, i, k, jj, total_len,
225  w_len, max_len, ind;
226  char opt;
227  enum {OFF, ON};
228  if (michol == "on")
229  opt = ON;
230  else
231  opt = OFF;
232 
233  // Input matrix pointers
234  octave_idx_type *cidx = sm.cidx ();
235  octave_idx_type *ridx = sm.ridx ();
236  T* data = sm.data ();
237 
238  // Output matrix data structures. Because the final zero pattern pattern of
239  // the output matrix is not known due to fill-in elements, a heuristic
240  // approach has been adopted for memory allocation. The size of ridx_out_l
241  // and data_out_l is incremented 10% of their actual size (nnz (A) in the
242  // beginning). If that amount is less than n, their size is just incremented
243  // in n elements. This way the number of reallocations decreases throughout
244  // the process, obtaining a good performance.
245  max_len = sm.nnz ();
246  max_len += (0.1 * max_len) > n ? 0.1 * max_len : n;
247  Array <octave_idx_type> cidx_out_l (dim_vector (n + 1, 1));
248  octave_idx_type *cidx_l = cidx_out_l.fortran_vec ();
249  Array <octave_idx_type> ridx_out_l (dim_vector (max_len, 1));
250  octave_idx_type *ridx_l = ridx_out_l.fortran_vec ();
251  Array <T> data_out_l (dim_vector (max_len, 1));
252  T* data_l = data_out_l.fortran_vec ();
253 
254  // Working arrays
255  OCTAVE_LOCAL_BUFFER (T, w_data, n);
258  OCTAVE_LOCAL_BUFFER (T, col_drops, n);
259  std::vector<octave_idx_type> vec (n, 0);
260  std::vector<bool> mark (n, false);
261 
262  T zero = T (0);
263  cidx_l[0] = cidx[0];
264  for (i = 0; i < n; i++)
265  {
266  Llist[i] = -1;
267  Lfirst[i] = -1;
268  w_data[i] = 0;
269  col_drops[i] = zero;
270  }
271 
272  total_len = 0;
273  for (k = 0; k < n; k++)
274  {
275  ind = 0;
276  for (j = cidx[k]; j < cidx[k+1]; j++)
277  {
278  w_data[ridx[j]] = data[j];
279  // Mark column index, intentionally outside the if-clause to ensure
280  // that mark[k] will be set to true as well.
281  mark[ridx[j]] = true;
282  if (ridx[j] != k)
283  {
284  vec[ind] = ridx[j];
285  ind++;
286  }
287  }
288  jrow = Llist[k];
289  while (jrow != -1)
290  {
291  jjrow = Lfirst[jrow];
292  jend = cidx_l[jrow+1];
293  for (jj = jjrow; jj < jend; jj++)
294  {
295  j = ridx_l[jj];
296  // If the element in the j position of the row is zero,
297  // then it will become non-zero, so we add it to the
298  // vector that tracks non-zero elements in the working row.
299  if (! mark[j])
300  {
301  mark[j] = true;
302  vec[ind] = j;
303  ind++;
304  }
305  w_data[j] -= ichol_mult (data_l[jj], data_l[jjrow]);
306  }
307  // Update the actual column first element and
308  // update the linked list of the jrow row.
309  if ((jjrow + 1) < jend)
310  {
311  Lfirst[jrow]++;
312  j = jrow;
313  jrow = Llist[jrow];
314  Llist[j] = Llist[ridx_l[Lfirst[j]]];
315  Llist[ridx_l[Lfirst[j]]] = j;
316  }
317  else
318  jrow = Llist[jrow];
319  }
320 
321  // Resizing output arrays
322  if ((max_len - total_len) < n)
323  {
324  max_len += (0.1 * max_len) > n ? 0.1 * max_len : n;
325  data_out_l.resize (dim_vector (max_len, 1));
326  data_l = data_out_l.fortran_vec ();
327  ridx_out_l.resize (dim_vector (max_len, 1));
328  ridx_l = ridx_out_l.fortran_vec ();
329  }
330 
331  // The sorting of the non-zero elements of the working column can be
332  // handled in a couple of ways. The most efficient two I found, are
333  // keeping the elements in an ordered binary search tree dynamically or
334  // keep them unsorted in a vector and at the end of the outer iteration
335  // order them. The last approach exhibits lower execution times.
336  std::sort (vec.begin (), vec.begin () + ind);
337 
338  data_l[total_len] = w_data[k];
339  ridx_l[total_len] = k;
340  w_len = 1;
341 
342  // Extract the non-zero elements of working column and
343  // drop the elements that are lower than droptol * cols_norm[k].
344  for (i = 0; i < ind ; i++)
345  {
346  jrow = vec[i];
347  if (w_data[jrow] != zero)
348  {
349  if (std::abs (w_data[jrow]) < (droptol * cols_norm[k]))
350  {
351  if (opt == ON)
352  {
353  col_drops[k] += w_data[jrow];
354  col_drops[jrow] += w_data[jrow];
355  }
356  }
357  else
358  {
359  data_l[total_len + w_len] = w_data[jrow];
360  ridx_l[total_len + w_len] = jrow;
361  w_len++;
362  }
363  }
364  // Clear mark, vec, and w_data. However, mark[k] is not set to zero.
365  mark[jrow] = false;
366  vec[i] = 0;
367  w_data[jrow] = zero;
368  }
369 
370  // Compensate column sums --> michol option
371  if (opt == ON)
372  data_l[total_len] += col_drops[k];
373 
374  if (data_l[total_len] == zero)
375  error ("ichol: encountered a pivot equal to 0");
376 
377  if (! ichol_checkpivot (data_l[total_len]))
378  break;
379 
380  // Once elements are dropped and compensation of column sums are done,
381  // scale the elements by the pivot.
382  data_l[total_len] = std::sqrt (data_l[total_len]);
383  for (jj = total_len + 1; jj < (total_len + w_len); jj++)
384  data_l[jj] /= data_l[total_len];
385  total_len += w_len;
386  // Check if there are too many elements to be indexed with
387  // octave_idx_type type due to fill-in during the process.
388  if (total_len < 0)
389  error ("ichol: integer overflow. Too many fill-in elements in L");
390 
391  cidx_l[k+1] = cidx_l[k] - cidx_l[0] + w_len;
392 
393  // Update Llist and Lfirst with the k-column information.
394  if (k < (n - 1))
395  {
396  Lfirst[k] = cidx_l[k];
397  if ((Lfirst[k] + 1) < cidx_l[k+1])
398  {
399  Lfirst[k]++;
400  jjrow = ridx_l[Lfirst[k]];
401  Llist[k] = Llist[jjrow];
402  Llist[jjrow] = k;
403  }
404  }
405  }
406 
407  // Build the output matrices
408  L = octave_matrix_t (n, n, total_len);
409 
410  for (i = 0; i <= n; i++)
411  L.cidx (i) = cidx_l[i];
412 
413  for (i = 0; i < total_len; i++)
414  {
415  L.ridx (i) = ridx_l[i];
416  L.data (i) = data_l[i];
417  }
418 }
419 
420 DEFUN (__icholt__, args, ,
421  doc: /* -*- texinfo -*-
422 @deftypefn {} {@var{L} =} __icholt__ (@var{A}, @var{droptol}, @var{michol})
423 Undocumented internal function.
424 @end deftypefn */)
425 {
426  if (args.length () != 3)
427  print_usage ();
428 
429  double droptol = args(1).double_value ();
430  std::string michol = args(2).string_value ();
431 
432  if (! args(0).iscomplex ())
433  {
434  SparseMatrix L;
435  SparseMatrix sm_l = Ftril (args(0))(0).sparse_matrix_value ();
438  (sm_l, L, xcolnorms (sm_l, 1).fortran_vec (), droptol, michol);
439 
440  return ovl (L);
441  }
442  else
443  {
446  = Ftril (args(0))(0).sparse_complex_matrix_value ();
447  Array <Complex> cols_norm = xcolnorms (sm_l, 1);
450  (sm_l, L, cols_norm.fortran_vec (),
451  Complex (droptol), michol);
452 
453  return ovl (L);
454  }
455 }
456 
457 /*
458 %!test <*51736>
459 %! k = 4;
460 %! n = 2^k;
461 %! Afull = diag (ones (n,1)) / ...
462 %! 2+triu ([zeros(n,2), [n.^-[1;1;2]*ones(1,n-2);zeros(n-3,n-2)]], 1);
463 %! A = sparse (Afull + Afull.');
464 %! opts.type = "ict";
465 %! opts.droptol = 0;
466 %! L = ichol (A, opts);
467 %! assert (norm (A - L*L.'), 0, 2*eps);
468 */
bool ichol_checkpivot_complex(Complex pivot)
Definition: __ichol__.cc:56
bool ichol_checkpivot_real(double pivot)
Definition: __ichol__.cc:66
double ichol_mult_real(double a, double b)
Definition: __ichol__.cc:51
void ichol_t(const octave_matrix_t &sm, octave_matrix_t &L, const T *cols_norm, const T droptol, const std::string michol="off")
Definition: __ichol__.cc:219
void ichol_0(octave_matrix_t &sm, const std::string michol="off")
Definition: __ichol__.cc:76
Complex ichol_mult_complex(Complex a, Complex b)
Definition: __ichol__.cc:39
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array.cc:1011
const T * fortran_vec(void) const
Size of the specified dimension.
Definition: Array.h:583
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
OCTINTERP_API void print_usage(void)
Definition: defun.cc:53
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void error(const char *fmt,...)
Definition: error.cc:968
octave_idx_type n
Definition: mx-inlines.cc:753
T * r
Definition: mx-inlines.cc:773
std::complex< double > Complex
Definition: oct-cmplx.h:33
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:44
OCTAVE_API RowVector xcolnorms(const Matrix &m, double p)
Definition: oct-norm.cc:594
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211
static T abs(T x)
Definition: pr-output.cc:1678
OCTAVE_EXPORT octave_value_list Ftril(const octave_value_list &args, int)
Definition: tril.cc:376