Ignore:
Timestamp:
Dec 15, 2005, 12:23:46 AM (17 years ago)
Author:
Markus Ringnér
Message:

Modified istream constructors to support both the old format and CSV files. Also support has been added for NaN:s

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/gslapi/matrix.cc

    r421 r434  
    55#include <c++_tools/gslapi/vector.h>
    66#include <c++_tools/utility/stl_utility.h>
     7#include <c++_tools/utility/utility.h>
    78
    89#include <cmath>
     
    3132
    3233
    33 
    3434  // Constructor that gets data from istream
    35   matrix::matrix(std::istream& is) throw (utility::IO_error,std::exception)
     35  matrix::matrix(std::istream& is, char sep)
     36    throw (utility::IO_error,std::exception)
    3637    : view_(NULL)
    3738  {
     39    // Markus to Jari, somewhere we should check that quiet_NaNs are supported
     40    // std::numeric_limits<double>::has_quiet_NaN has to be true.
     41    // Also in vector
     42
    3843    // read the data file and store in stl vectors (dynamically
    3944    // expandable)
     
    4146    u_int nof_columns=0;
    4247    u_int nof_rows = 0;
    43     std::vector<double> v;
    44     for (nof_rows = 0; utility::read_to_double(is, v); nof_rows++) {
     48    std::string line;
     49    while(getline(is, line, '\n')){
    4550      // Ignoring empty lines
    46       if (!v.size()) {
    47         nof_rows--;
     51      if (!line.size()) {
    4852        continue;
    4953      }
     54      nof_rows++;
     55      std::vector<double> v;
     56      std::string element;
     57      std::stringstream ss(line);
     58      bool ok=true;
     59      while(ok) {
     60        if(sep=='\0')
     61          ok=(ss>>element);
     62        else
     63          ok=getline(ss, element, sep);
     64        if(!ok)
     65          break;
     66
     67        if(utility::is_double(element)) {
     68          v.push_back(atof(element.c_str()));
     69        }
     70        else if (!element.size() || utility::is_nan(element)) {
     71          v.push_back(std::numeric_limits<double>::quiet_NaN());
     72        }
     73        else {
     74          // Jari, this should be communicated with as an exception.
     75          //          std::cerr << "Warning: '" << vec_str[i]
     76          //                    << "' is not an integer." << std::endl;
     77        }
     78      }           
    5079      if (!nof_columns)
    5180        nof_columns=v.size();
    5281      else if (v.size()!=nof_columns) {
    5382        std::ostringstream s;
    54         s << "matrix::matrix(std::istream&) data file error: "
     83        s << "matrix::matrix(std::istream&, char) data file error: "
    5584          << "line" << nof_rows+1 << " has " << v.size()
    5685          << " columns; expected " << nof_columns << " columns.";
     
    6897        gsl_matrix_set( m_, i, j, data_matrix[i][j] );
    6998  }
    70 
    7199
    72100
Note: See TracChangeset for help on using the changeset viewer.