GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
sparse-util.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2005-2015 David Bateman
4 Copyright (C) 1998-2005 Andy Adler
5 
6 This file is part of Octave.
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
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
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
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include "lo-error.h"
31 #include "oct-sparse.h"
32 #include "sparse-util.h"
33 
34 // FIXME: this overload is here due to API change in SuiteSparse (3.1 -> 3.2)
35 
36 #ifdef HAVE_CHOLMOD
37 
38 void
39 SparseCholError (int status, char *file, int line, char *message)
40 {
41  SparseCholError (status, file, line, message);
42 }
43 
44 void
45 SparseCholError (int status, const char *file, int line, const char *message)
46 {
47 
48  // Ignore CHOLMOD_NOT_POSDEF, since we handle that in Fchol as an
49  // error or exit status.
50  if (status != CHOLMOD_NOT_POSDEF)
51  (*current_liboctave_warning_with_id_handler)
52  ("Octave:cholmod-message", "warning %i, at line %i in file %s: %s",
53  status, line, file, message);
54 }
55 
56 int
57 SparseCholPrint (const char *fmt, ...)
58 {
59  va_list args;
60  va_start (args, fmt);
61  int ret = gnulib::vfprintf (stderr, fmt, args);
62  gnulib::fflush (stderr);
63  va_end (args);
64  return ret;
65 }
66 
67 #endif //HAVE_CHOLMOD
68 
69 bool
71  octave_idx_type nrows, octave_idx_type ncols,
72  octave_idx_type nnz)
73 {
74  if (nnz > 0)
75  {
76  if (c[0] != 0)
77  {
78  (*current_liboctave_error_handler)
79  ("invalid sparse matrix: cidx[0] must be zero");
80  return false;
81  }
82 
83  octave_idx_type jold = 0;
84 
85  for (octave_idx_type j = 1; j < ncols+1; j++)
86  {
87  if (c[j] < c[j-1])
88  {
89  (*current_liboctave_error_handler)
90  ("invalid sparse matrix: cidx elements must appear in ascending order");
91  return false;
92  }
93 
94  if (c[j] > nnz)
95  {
96  (*current_liboctave_error_handler)
97  ("invalid sparse matrix: cidx[%d] = %d exceeds number of nonzero elements",
98  j, c[j]+1);
99  return false;
100  }
101 
102  if (c[j] != jold)
103  {
104  for (octave_idx_type i = jold+1; i < c[j]; i++)
105  {
106  if (r[i] < r[i-1])
107  {
108  (*current_liboctave_error_handler)
109  ("invalid sparse matrix: ridx elements must appear in ascending order for each column");
110  return false;
111  }
112 
113  if (r[i] >= nrows)
114  {
115  (*current_liboctave_error_handler)
116  ("invalid sparse matrix: ridx[%d] = %d out of range",
117  i, r[i]+1);
118  return false;
119  }
120  }
121 
122  jold = c[j];
123  }
124  }
125  }
126 
127  return true;
128 }
void SparseCholError(int status, char *file, int line, char *message)
Definition: sparse-util.cc:39
bool sparse_indices_ok(octave_idx_type *r, octave_idx_type *c, octave_idx_type nrows, octave_idx_type ncols, octave_idx_type nnz)
Definition: sparse-util.cc:70
void message(const char *name, const char *fmt,...)
Definition: error.cc:380
int SparseCholPrint(const char *fmt,...)
Definition: sparse-util.cc:57