GNU Octave  3.8.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-2013 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  {
52  (*current_liboctave_warning_handler)("warning %i, at line %i in file %s",
53  status, line, file);
54 
55  (*current_liboctave_warning_handler)(message);
56  }
57 }
58 
59 int
60 SparseCholPrint (const char *fmt, ...)
61 {
62  va_list args;
63  va_start (args, fmt);
64  int ret = gnulib::vfprintf (stderr, fmt, args);
65  gnulib::fflush (stderr);
66  va_end (args);
67  return ret;
68 }
69 
70 #endif //HAVE_CHOLMOD
71 
72 bool
74  octave_idx_type nrows, octave_idx_type ncols,
75  octave_idx_type nnz)
76 {
77  if (nnz > 0)
78  {
79  if (c[0] != 0)
80  {
81  (*current_liboctave_error_handler)
82  ("invalid sparse matrix: cidx[0] must be zero");
83  return false;
84  }
85 
86  octave_idx_type jold = 0;
87 
88  for (octave_idx_type j = 1; j < ncols+1; j++)
89  {
90  if (c[j] < c[j-1])
91  {
92  (*current_liboctave_error_handler)
93  ("invalid sparse matrix: cidx elements must appear in ascending order");
94  return false;
95  }
96 
97  if (c[j] > nnz)
98  {
99  (*current_liboctave_error_handler)
100  ("invalid sparse matrix: cidx[%d] = %d exceeds number of nonzero elements",
101  j, c[j]+1);
102  return false;
103  }
104 
105  if (c[j] != jold)
106  {
107  for (octave_idx_type i = jold+1; i < c[j]; i++)
108  {
109  if (r[i] < r[i-1])
110  {
111  (*current_liboctave_error_handler)
112  ("invalid sparse matrix: ridx elements must appear in ascending order for each column");
113  return false;
114  }
115 
116  if (r[i] >= nrows)
117  {
118  (*current_liboctave_error_handler)
119  ("invalid sparse matrix: ridx[%d] = %d out of range",
120  i, r[i]+1);
121  return false;
122  }
123  }
124 
125  jold = c[j];
126  }
127  }
128  }
129 
130  return true;
131 }