Changeset 275
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/matrix.h
r272 r275 242 242 243 243 /// 244 /// The output operator for the vectorclass.244 /// The output operator for the matrix class. 245 245 /// 246 246 std::ostream& operator<< (std::ostream& s, const matrix&); -
trunk/src/vector.cc
r259 r275 2 2 3 3 #include "vector.h" 4 #include "matrix.h" 4 5 5 6 #include <iostream> … … 61 62 : view_(NULL) 62 63 { 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 109 66 // convert the data to a gsl vector and check that data file is a 110 67 // 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) ); 120 77 } 121 78 else { 122 79 // 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_columns125 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; 127 84 exit(1); 128 85 }
Note: See TracChangeset
for help on using the changeset viewer.