Changeset 273


Ignore:
Timestamp:
Apr 14, 2005, 6:42:33 PM (16 years ago)
Author:
Peter
Message:

modified istream constructor to use function in stl_utility and modified output operator to not end with a newline

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/matrix.cc

    r272 r273  
    11// $Id$
     2
     3#include "matrix.h"
     4
     5#include "stl_utility.h"
     6#include "vector.h"
    27
    38#include <iostream>
     
    914#include <gsl/gsl_linalg.h>
    1015
    11 #include "matrix.h"
    12 #include "vector.h"
    13 
    1416
    1517namespace theplu {
     
    4345  matrix::matrix(std::istream& is)
    4446  {
    45     using namespace std;
    46 
    4747    // read the data file and store in stl vectors (dynamically
    4848    // expandable)
     
    5050    u_int nof_columns=0;
    5151    u_int nof_rows = 0;
    52     string s;
    53     for (nof_rows = 0; getline(is, s, '\n'); nof_rows++) {
    54       istringstream line(s);
    55       std::vector<double> v;
    56       string tmp_string;
    57       while (line >> tmp_string) {
    58         if(!is.good()) {
    59           cerr << "matrix::matrix(std::istream&): "
    60                << "error reading data file!" << endl;
    61           exit(1);
    62         }
    63         double t=atof(tmp_string.c_str());
    64         // Peter, should check that it actually was a double!!!!!
    65         v.push_back(t);
    66       }
     52    std::vector<double> v;
     53    for (nof_rows = 0; cpptools::read_to_double(is, v); nof_rows++) {
    6754   
    6855      // Ignoring empty lines
     
    7562        nof_columns=v.size();
    7663      else if(v.size()!=nof_columns) {
    77         cerr << "matrix::matrix(std::istream&) data file error: "
    78              << "line" << nof_rows+1 << " has " << v.size()
    79              << " columns; expected " << nof_columns
    80              << " columns"
    81              << endl;
     64        std::cerr << "matrix::matrix(std::istream&) data file error: "
     65                  << "line" << nof_rows+1 << " has " << v.size()
     66                  << " columns; expected " << nof_columns
     67                  << " columns"
     68                  << std::endl;
    8269        exit(1);
    8370      }
    8471      data_matrix.push_back(v);
    8572    }
     73
    8674    // manipulate the state of the stream to be good
    8775    is.clear(std::ios::goodbit);
     
    203191
    204192
    205   std::ostream& operator<<(std::ostream& s, const matrix& a)
     193  std::ostream& operator<<(std::ostream& s, const matrix& m)
    206194  {
    207195    using namespace std;
     
    209197    s.precision(12);
    210198    // Peter, end with an "\n" to make it compatible with stream constructor
    211     for(size_t i=0, j=0; i<a.rows(); i++) {
    212       for (j=0; j<a.columns()-1; j++)
    213         s << a(i,j) << "\t";
    214       s << a(i,j) << "\n";
    215     }
     199    for(size_t i=0, j=0; i<m.rows(); i++)
     200      for (j=0; j<m.columns(); j++) {
     201        s << m(i,j);
     202        if (j<m.columns()-1)
     203          s << "\t";
     204        else if (i<m.rows()-1)
     205          s << "\n";
     206      }
    216207    return s;
    217208  }
Note: See TracChangeset for help on using the changeset viewer.