Changeset 1927


Ignore:
Timestamp:
Apr 30, 2009, 5:16:31 PM (12 years ago)
Author:
Peter
Message:

refs #510 using load function in Matrix istream constructor. Behavior should be as before except that when Matrix is invalid the error message no longer contains info on which row and column that failed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/Matrix.cc

    r1920 r1927  
    7777    // expandable)
    7878    std::vector<std::vector<double> > data_matrix;
     79    try {
     80      load(is, data_matrix, sep);
     81    }
     82    catch (std::runtime_error& e) {
     83      std::stringstream ss(e.what());
     84      ss << "\nMatrix.cc: invalid matrix element\n";
     85      throw IO_error(ss.str());
     86    }
     87
    7988    unsigned int nof_columns=0;
    8089    unsigned int nof_rows = 0;
    81     std::string line;
    82     while(getline(is, line, '\n')){
     90    for (size_t row=0; row<data_matrix.size(); ++row) {
    8391      // Ignoring empty lines
    84       if (!line.size()) {
     92      if (!data_matrix[row].size()) {
    8593        continue;
    8694      }
    87       nof_rows++;
    88       data_matrix.resize(data_matrix.size()+1);
    89       std::vector<double>& v=data_matrix.back();
    90       v.reserve(nof_columns);
    91       std::string element;
    92       std::stringstream ss(line);
    93      
    94       bool ok=true;
    95       while(ok) {
    96         if(sep=='\0')
    97           ok=(ss>>element);
    98         else
    99           ok=getline(ss, element, sep);
    100         if(!ok)
    101           break;
    102        
    103         if (!element.size())
    104           v.push_back(std::numeric_limits<double>::quiet_NaN());
    105         else {
    106           try {
    107             v.push_back(convert<double>(element));
    108           }
    109           catch (std::runtime_error& e) {
    110             std::stringstream ss(e.what());
    111             ss << "\nMatrix.cc: " << element
    112                << " is not accepted as a matrix element\n";
    113             throw IO_error(ss.str());
    114           }
    115         }
    116       }           
    117       if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator
    118           v.push_back(std::numeric_limits<double>::quiet_NaN());
     95      ++nof_rows;
    11996      if (!nof_columns)
    120         nof_columns=v.size();
    121       else if (v.size()!=nof_columns) {
     97        nof_columns=data_matrix[row].size();
     98      else if (data_matrix[row].size()!=nof_columns) {
    12299        std::ostringstream s;
    123100        s << "Matrix::Matrix(std::istream&, char) data file error: "
    124           << "line " << nof_rows << " has " << v.size()
     101          << "line " << nof_rows << " has " << data_matrix[row].size()
    125102          << " columns; expected " << nof_columns << " columns.";
    126103        throw utility::IO_error(s.str());
     
    128105    }
    129106
    130     // manipulate the state of the stream to be good
    131     is.clear(std::ios::goodbit);
    132    
    133107    // if stream was empty, create nothing
    134108    if (!nof_columns || !nof_rows)
     
    142116    // if gsl error handler disabled, out of bounds index will not
    143117    // abort the program.
    144     for(size_t i=0;i<nof_rows;i++)
    145       for(size_t j=0;j<nof_columns;j++)
    146         gsl_matrix_set( m_, i, j, data_matrix[i][j] );
     118    size_t row=0;
     119    for (size_t i=0; i<data_matrix.size(); ++i) {
     120      if (data_matrix[i].empty()) // ignore empty lines
     121        continue;
     122      assert(data_matrix[i].size()==columns());
     123      assert(row<rows());
     124      std::copy(data_matrix[i].begin(), data_matrix[i].end(), begin_row(row));
     125      ++row;
     126    }
    147127  }
    148128
Note: See TracChangeset for help on using the changeset viewer.