sparse-util.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2012 David Bateman
00004 Copyright (C) 1998-2005 Andy Adler
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include <stdio.h>
00029 #include <stdarg.h>
00030 #include "lo-error.h"
00031 #include "sparse-util.h"
00032 
00033 // FIXME this overload is here due to API change in SuiteSparse (3.1 -> 3.2)
00034 void
00035 SparseCholError (int status, char *file, int line, char *message)
00036 {
00037   SparseCholError (status, file, line, message);
00038 }
00039 
00040 void
00041 SparseCholError (int status, const char *file, int line, const char *message)
00042 {
00043   (*current_liboctave_warning_handler)("warning %i, at line %i in file %s",
00044                                      status, line, file);
00045 
00046   (*current_liboctave_warning_handler)(message);
00047 }
00048 
00049 int
00050 SparseCholPrint (const char *fmt, ...)
00051 {
00052   va_list args;
00053   va_start (args, fmt);
00054   int ret = gnulib::vfprintf (stderr, fmt, args);
00055   gnulib::fflush (stderr);
00056   va_end (args);
00057   return ret;
00058 }
00059 
00060 
00061 bool
00062 sparse_indices_ok (octave_idx_type *r, octave_idx_type *c,
00063                    octave_idx_type nrows, octave_idx_type ncols,
00064                    octave_idx_type nnz)
00065 {
00066   if (nnz > 0)
00067     {
00068       if (c[0] != 0)
00069         {
00070           (*current_liboctave_error_handler)
00071             ("invalid sparse matrix: cidx[0] must be zero");
00072           return false;
00073         }
00074 
00075       octave_idx_type jold = 0;
00076 
00077       for (octave_idx_type j = 1; j < ncols+1; j++)
00078         {
00079           if (c[j] < c[j-1])
00080             {
00081               (*current_liboctave_error_handler)
00082                 ("invalid sparse matrix: cidx elements must appear in ascending order");
00083               return false;
00084             }
00085 
00086           if (c[j] > nnz)
00087             {
00088               (*current_liboctave_error_handler)
00089                 ("invalid sparse matrix: cidx[%d] = %d exceeds number of nonzero elements", j, c[j]+1);
00090               return false;
00091             }
00092 
00093           if (c[j] != jold)
00094             {
00095               for (octave_idx_type i = jold+1; i < c[j]; i++)
00096                 {
00097                   if (r[i] < r[i-1])
00098                     {
00099                       (*current_liboctave_error_handler)
00100                         ("invalid sparse matrix: ridx elements must appear in ascending order for each column");
00101                       return false;
00102                     }
00103 
00104                   if (r[i] >= nrows)
00105                     {
00106                       (*current_liboctave_error_handler)
00107                         ("invalid sparse matrix: ridx[%d] = %d out of range",
00108                          i, r[i]+1);
00109                       return false;
00110                     }
00111                 }
00112 
00113               jold = c[j];
00114             }
00115         }
00116     }
00117 
00118   return true;
00119 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines