1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1998-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 <cinttypes>
31 #include <cstdarg>
32 #include <cstdio>
33 
34 #include "lo-error.h"
35 #include "oct-sparse.h"
36 #include "sparse-util.h"
37 
38 static inline void
sparse_chol_error_internal(int status,const char * file,int line,const char * message)39 sparse_chol_error_internal (int status, const char *file,
40                             int line, const char *message)
41 {
42 #if defined (HAVE_CHOLMOD)
43 
44   // Ignore CHOLMOD_NOT_POSDEF, since we handle that in Fchol as an
45   // error or exit status.
46   if (status != CHOLMOD_NOT_POSDEF)
47     (*current_liboctave_warning_with_id_handler)
48       ("Octave:cholmod-message", "warning %i, at line %i in file %s: %s",
49        status, line, file, message);
50 
51 #else
52 
53   octave_unused_parameter (status);
54   octave_unused_parameter (file);
55   octave_unused_parameter (line);
56   octave_unused_parameter (message);
57 
58 #endif
59 }
60 
61 // FIXME: this overload is here due to API change in SuiteSparse (3.1 -> 3.2)
62 
63 void
SparseCholError(int status,char * file,int line,char * message)64 SparseCholError (int status, char *file, int line, char *message)
65 {
66   sparse_chol_error_internal (status, file, line, message);
67 }
68 
69 void
SparseCholError(int status,const char * file,int line,const char * message)70 SparseCholError (int status, const char *file, int line, const char *message)
71 {
72   sparse_chol_error_internal (status, file, line, message);
73 }
74 
75 int
SparseCholPrint(const char * fmt,...)76 SparseCholPrint (const char *fmt, ...)
77 {
78   va_list args;
79   va_start (args, fmt);
80   int ret = std::vfprintf (stderr, fmt, args);
81   std::fflush (stderr);
82   va_end (args);
83   return ret;
84 }
85 
86 bool
sparse_indices_ok(octave_idx_type * r,octave_idx_type * c,octave_idx_type nrows,octave_idx_type ncols,octave_idx_type nnz)87 sparse_indices_ok (octave_idx_type *r, octave_idx_type *c,
88                    octave_idx_type nrows, octave_idx_type ncols,
89                    octave_idx_type nnz)
90 {
91   if (nnz > 0)
92     {
93       if (c[0] != 0)
94         (*current_liboctave_error_handler)
95           ("invalid sparse matrix: cidx[0] must be zero");
96 
97       octave_idx_type jold = 0;
98 
99       for (octave_idx_type j = 1; j < ncols+1; j++)
100         {
101           if (c[j] < c[j-1])
102             (*current_liboctave_error_handler)
103               ("invalid sparse matrix: cidx elements must appear in ascending order");
104 
105           if (c[j] > nnz)
106             (*current_liboctave_error_handler)
107               ("invalid sparse matrix: cidx[%" OCTAVE_IDX_TYPE_FORMAT "] = "
108                "%" OCTAVE_IDX_TYPE_FORMAT " exceeds number of nonzero elements",
109                j, c[j]+1);
110 
111           if (c[j] != jold)
112             {
113               for (octave_idx_type i = jold+1; i < c[j]; i++)
114                 {
115                   if (r[i] < r[i-1])
116                     (*current_liboctave_error_handler)
117                       ("invalid sparse matrix: ridx elements must appear "
118                        "in ascending order for each column");
119 
120                   if (r[i] >= nrows)
121                     (*current_liboctave_error_handler)
122                       ("invalid sparse matrix: ridx[%" OCTAVE_IDX_TYPE_FORMAT
123                        "] = %" OCTAVE_IDX_TYPE_FORMAT " out of range",
124                        i, r[i]+1);
125                 }
126 
127               jold = c[j];
128             }
129         }
130     }
131 
132   return true;
133 }
134