Changeset 275


Ignore:
Timestamp:
Apr 14, 2005, 7:07:56 PM (18 years ago)
Author:
Peter
Message:

modified istream constructor for vector class to use the matrix constructor

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/matrix.h

    r272 r275  
    242242
    243243  ///
    244   /// The output operator for the vector class.
     244  /// The output operator for the matrix class.
    245245  ///
    246246  std::ostream& operator<< (std::ostream& s, const matrix&);
  • trunk/src/vector.cc

    r259 r275  
    22
    33#include "vector.h"
     4#include "matrix.h"
    45
    56#include <iostream>
     
    6162    : view_(NULL)
    6263  {
    63     using namespace std;
    64  
    65     // read the data file and store in stl vectors (dynamically expandable)
    66     std::vector<std::vector<double> > data_matrix;
    67     u_int nof_columns=0;
    68     u_int nof_rows = 0;
    69     string s;
    70     for (nof_rows = 0; getline(is, s, '\n'); nof_rows++) {
    71       istringstream line(s);
    72       std::vector<double> v;
    73       string tmp_string;
    74       while (line >> tmp_string) {
    75         if(!is.good()) {
    76           // Jari. change to throw exception, remove unnecessary includes
    77           cerr << "vector::vector(std::istream&): "
    78                << "error reading data file!" << endl;
    79           exit(1);
    80         }
    81         double t=atof(tmp_string.c_str());
    82         // Here we should check that it actually was a double!!!!!
    83         v.push_back(t);
    84       }
    85 
    86       // Ignoring empty lines
    87       if(!v.size()){
    88         nof_rows--;
    89         continue;
    90       }
    91 
    92       if(nof_columns==0)
    93         nof_columns=v.size();
    94       else if(v.size()!=nof_columns) {
    95         // Jari. change to throw exception, remove unnecessary includes
    96         cerr << "vector::vector(std::istream&) data file error: "
    97              << "line" << nof_rows+1 << " has " << v.size()
    98              << " columns; expected " << nof_columns
    99              << " columns"
    100              << endl;
    101         exit(1);
    102       }
    103       data_matrix.push_back(v);
    104     }
    105  
    106     // manipulate the state of the stream to be good
    107     is.clear(std::ios::goodbit);
    108  
     64    matrix m(is);
     65   
    10966    // convert the data to a gsl vector and check that data file is a
    11067    // column vector or a row vector
    111     if(nof_columns==1) {
    112       v_ = gsl_vector_alloc ( nof_rows );
    113       for(u_int i=0;i<nof_rows;i++)
    114         gsl_vector_set( v_, i, data_matrix[i][0] );
    115     }
    116     else if(nof_rows==1){
    117       v_ = gsl_vector_alloc ( nof_columns );
    118       for(u_int i=0;i<nof_columns;i++)
    119         gsl_vector_set( v_, i, data_matrix[0][i] );
     68    if(m.columns()==1) {
     69      v_ = gsl_vector_alloc ( m.rows() );
     70      for(u_int i=0;i<m.rows();i++)
     71        gsl_vector_set( v_, i, m(i,0) );
     72    }
     73    else if(m.rows()==1){
     74      v_ = gsl_vector_alloc ( m.columns() );
     75      for(u_int i=0;i<m.columns();i++)
     76        gsl_vector_set( v_, i, m(0,i) );
    12077    }
    12178    else {
    12279      // Jari. change to throw exception, remove unnecessary includes
    123       cerr << "vector::vector(std::istream&) data file error: "
    124            << "file has " << nof_rows << " rows and " << nof_columns
    125            << " columns; expected a row vector or a column vector"
    126            << endl;
     80      std::cerr << "vector::vector(std::istream&) data file error: "
     81                << "file has " << m.rows() << " rows and " << m.columns()
     82                << " columns; expected a row vector or a column vector"
     83                << std::endl;
    12784      exit(1);
    12885    }
Note: See TracChangeset for help on using the changeset viewer.