Changeset 328
- Timestamp:
- May 31, 2005, 1:04:20 AM (18 years ago)
- Location:
- trunk
- Files:
-
- 4 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/gslapi/vector.cc
r310 r328 2 2 3 3 #include <c++_tools/gslapi/vector.h> 4 #include <c++_tools/ gslapi/matrix.h>4 #include <c++_tools/utility/stl_utility.h> 5 5 6 6 #include <iostream> 7 #include <sstream>8 #include <string>9 7 #include <vector> 10 8 #include <utility> 11 12 13 14 9 15 10 … … 62 57 : view_(NULL) 63 58 { 64 matrix m(is); 65 66 // convert the data to a gsl vector and check that data file is a 67 // column vector or a row vector 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) ); 59 // read the data file and store in stl vectors (dynamically 60 // expandable) 61 std::vector<std::vector<double> > data_matrix; 62 u_int nof_columns=0; 63 u_int nof_rows=0; 64 std::vector<double> v; 65 for (nof_rows = 0; utility::read_to_double(is, v); nof_rows++) { 66 // Ignoring empty lines 67 if (!v.size()) { 68 nof_rows--; 69 continue; 70 } 71 if (!nof_columns) 72 nof_columns=v.size(); 73 else if ((nof_rows>1) && (nof_columns>1)) { 74 std::cerr << "vector::vector(std::istream&) data file error:\n" 75 << "file has " << nof_rows << " rows and " << nof_columns 76 << " columns; expected a row vector or a column vector." 77 << std::endl; 78 exit(1); 79 } 80 else if(v.size()!=nof_columns) { 81 std::cerr << "vector::vector(std::istream&) data file error: " 82 << "line" << nof_rows+1 << " has " << v.size() 83 << " columns; expected " << nof_columns << " columns" 84 << std::endl; 85 exit(1); 86 } 87 data_matrix.push_back(v); 72 88 } 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) ); 77 } 78 else { 79 // Jari. change to throw exception, remove unnecessary includes 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; 84 exit(1); 85 } 89 90 // manipulate the state of the stream to be good 91 is.clear(std::ios::goodbit); 92 // convert the data to a gsl vector 93 v_ = gsl_vector_alloc(nof_rows*nof_columns); 94 size_t n=0; 95 for (size_t i=0; i<nof_rows; i++) 96 for (size_t j=0; j<nof_columns; j++) 97 gsl_vector_set( v_, n++, data_matrix[i][j] ); 86 98 } 87 99 -
trunk/test/vector_test.cc
r301 r328 1 1 // $Id$ 2 2 3 #include <c++_tools/utility/FileIO.h> 3 4 #include <c++_tools/gslapi/vector.h> 4 5 #include <c++_tools/gslapi/utility.h> 5 6 7 #include <fstream> 8 6 9 using namespace theplu; 10 11 void check_file_access(std::string& str) 12 { 13 if (utility::FileIO().access_rights(str,"r")) { 14 std::cerr << "test_nni: Cannot access file " << str << std::endl; 15 exit(-1); 16 } 17 } 7 18 8 19 int main(const int argc,const char* argv[]) … … 57 68 ok=false; 58 69 70 // checking that reading vectors from file works 71 std::string data1("data/vector1.data"); 72 std::string data2("data/vector2.data"); 73 std::string data3("data/vector3.data"); 74 std::string data4("data/vector4.data"); 75 check_file_access(data1); 76 check_file_access(data2); 77 check_file_access(data3); 78 check_file_access(data4); 79 std::ifstream data_stream1(data1.c_str()); 80 std::ifstream data_stream2(data2.c_str()); 81 std::ifstream data_stream3(data3.c_str()); 82 std::ifstream data_stream4(data4.c_str()); 83 vec=gslapi::vector(data_stream1); 84 if (vec.size()!=9) 85 ok=false; 86 vec=gslapi::vector(data_stream2); 87 if (vec.size()!=9) 88 ok=false; 89 vec=gslapi::vector(data_stream3); 90 if (vec.size()!=12) 91 ok=false; 92 vec=gslapi::vector(data_stream4); 93 if (vec.size()!=12) 94 ok=false; 95 96 std::cout << "ok: " << ok << std::endl; 97 59 98 if (ok) 60 99 return 0;
Note: See TracChangeset
for help on using the changeset viewer.