Changeset 110 for trunk/se/lu/thep/wenni/lib/c++_tools/gslapi/vector.cc
 Timestamp:
 Jun 14, 2006, 12:03:40 AM (17 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/se/lu/thep/wenni/lib/c++_tools/gslapi/vector.cc
r95 r110 27 27 28 28 #include <c++_tools/gslapi/vector.h> 29 #include <c++_tools/gslapi/matrix.h> 29 30 #include <c++_tools/utility/stl_utility.h> 31 #include <c++_tools/utility/utility.h> 32 30 33 31 34 #include <iostream> 32 35 #include <sstream> 33 #include <string>34 36 #include <vector> 35 37 #include <utility> … … 41 43 42 44 43 vector::vector(void)44 : v_(NULL), view_(NULL)45 {46 }47 48 49 50 vector::vector(const size_t n,const double init_value)51 : view_(NULL)52 {53 v_ = gsl_vector_alloc(n);54 set_all(init_value);55 }56 57 58 59 vector::vector(const vector& other)60 : view_(NULL)61 {62 v_ = other.TEMP_gsl_vector_copy();63 }64 65 66 67 45 vector::vector(vector& v, size_t offset, size_t n, size_t stride) 68 { 46 : const_view_(NULL) 47 { 48 // Jari, exception handling needed here. Failure in setting up a 49 // proper gsl_vector_view is communicated by NULL pointer in the 50 // view structure (cf. GSL manual). How about GSL error state? 69 51 view_ = new gsl_vector_view(gsl_vector_subvector_with_stride(v.v_,offset, 70 52 stride,n)); … … 74 56 75 57 76 vector::vector(gsl_vector* vec) 77 : v_(vec), view_(NULL) 78 { 79 } 80 81 82 83 vector::vector(std::istream& is) throw (utility::IO_error,std::exception) 58 vector::vector(const vector& v, size_t offset, size_t n, size_t stride) 84 59 : view_(NULL) 60 { 61 // Jari, exception handling needed here. Failure in setting up a 62 // proper gsl_vector_view is communicated by NULL pointer in the 63 // view structure (cf. GSL manual). How about GSL error state? 64 const_view_ = new gsl_vector_const_view( 65 gsl_vector_const_subvector_with_stride(v.v_,offset,stride,n)); 66 v_ = const_cast<gsl_vector*>(&(const_view_>vector)); 67 } 68 69 70 71 vector::vector(matrix& m, size_t i, bool row) 72 : const_view_(NULL) 73 { 74 view_=new gsl_vector_view(row ? 75 gsl_matrix_row (m.gsl_matrix_p(),i) : 76 gsl_matrix_column(m.gsl_matrix_p(),i) ); 77 v_ = &(view_>vector); 78 } 79 80 81 82 vector::vector(const matrix& m, size_t i, bool row) 83 : view_(NULL) 84 { 85 const_view_ = new gsl_vector_const_view(row ? 86 gsl_matrix_const_row (m.gsl_matrix_p(),i) : 87 gsl_matrix_const_column(m.gsl_matrix_p(),i) ); 88 v_ = const_cast<gsl_vector*>(&(const_view_>vector)); 89 } 90 91 92 93 vector::vector(std::istream& is, char sep) 94 throw (utility::IO_error,std::exception) 95 : view_(NULL), const_view_(NULL) 85 96 { 86 97 // read the data file and store in stl vectors (dynamically … … 89 100 u_int nof_columns=0; 90 101 u_int nof_rows=0; 91 std::vector<double> v; 92 for (nof_rows = 0; utility::read_to_double(is, v); nof_rows++) { 93 // Ignoring empty lines 94 if (!v.size()) { 95 nof_rows; 96 continue; 97 } 102 std::string line; 103 while(getline(is, line, '\n')) { 104 // Empty lines 105 if (!line.size()) 106 continue; 107 nof_rows++; 108 109 std::vector<double> v; 110 std::string element; 111 std::stringstream ss(line); 112 bool ok=true; 113 while(ok) { 114 if(sep=='\0') 115 ok=(ss>>element); 116 else 117 ok=getline(ss, element, sep); 118 if(!ok) 119 break; 120 121 if(utility::is_double(element)) { 122 v.push_back(atof(element.c_str())); 123 } 124 else if (!element.size()  utility::is_nan(element)) { 125 v.push_back(std::numeric_limits<double>::quiet_NaN()); 126 } 127 else { 128 // Jari, this should be communicated with as an exception. 129 // std::cerr << "Warning: '" << element 130 // << "' is not an integer." << std::endl; 131 } 132 } 133 if(sep!='\0' && line[line.size()1]==sep) // add NaN for final separator 134 v.push_back(std::numeric_limits<double>::quiet_NaN()); 98 135 if (!nof_columns) 99 136 nof_columns=v.size(); … … 101 138 std::ostringstream s; 102 139 s << "vector::vector(std::istream&) data file error:\n" 103 << " File has inconsistent number of rows (" << nof_rows +1140 << " File has inconsistent number of rows (" << nof_rows 104 141 << ") and columns (" << nof_columns 105 142 << ").\n Expected a row or a column vector."; 106 std::string m=s.str(); 107 throw utility::IO_error(m); 143 throw utility::IO_error(s.str()); 108 144 } 109 else if (v.size()!=nof_columns) {145 else if (v.size()!=nof_columns) { 110 146 std::ostringstream s; 111 147 s << "vector::vector(std::istream&) data file error:\n" 112 << " Line " << (nof_rows+1)<< " has " << v.size()148 << " Line " << nof_rows << " has " << v.size() 113 149 << " columns; expected " << nof_columns << " column."; 114 std::string m=s.str(); 115 throw utility::IO_error(m); 150 throw utility::IO_error(s.str()); 116 151 } 117 152 data_matrix.push_back(v); … … 134 169 if (view_) 135 170 delete view_; 171 else if (const_view_) 172 delete const_view_; 136 173 else if (v_) 137 174 gsl_vector_free(v_); … … 141 178 142 179 143 gsl_vector* vector:: TEMP_gsl_vector_copy(void) const180 gsl_vector* vector::create_gsl_vector_copy(void) const 144 181 { 145 182 gsl_vector* vec = gsl_vector_alloc(size()); … … 172 209 double sum = 0; 173 210 for (size_t i=0; i<size(); i++) 174 sum += gsl_vector_get( v_, i);211 sum += (*this)(i); 175 212 return( sum ); 176 213 } 177 178 179 180 double vector::operator*( const vector &other ) const181 {182 // Jari, check for gsl_support183 double res = 0.0;;184 for ( size_t i = 0; i < size(); ++i )185 res += other[i] * (*this)[i];186 return res;187 }188 189 190 191 vector vector::operator+(const vector &other) const192 {193 vector res(*this);194 gsl_vector_add(res.v_,other.v_);195 return res;196 }197 198 199 200 vector vector::operator( const vector &other ) const201 {202 vector res( *this );203 gsl_vector_sub(res.v_,other.v_);204 return res;205 }206 207 208 214 209 215 bool vector::operator==(const vector& a) const … … 219 225 220 226 227 double vector::operator*( const vector &other ) const 228 { 229 double res = 0.0;; 230 for ( size_t i = 0; i < size(); ++i ) 231 res += other(i) * (*this)(i); 232 return res; 233 } 234 235 236 221 237 const vector& vector::operator=( const vector& other ) 222 238 { 223 239 if( this != &other ) { 224 if ( v_ ) 240 if (view_) 241 delete view_; 242 else if (const_view_) 243 delete const_view_; 244 else if ( v_ ) 225 245 gsl_vector_free( v_ ); 226 v_ = other. TEMP_gsl_vector_copy();246 v_ = other.create_gsl_vector_copy(); 227 247 } 228 248 return *this; … … 233 253 std::ostream& operator<<(std::ostream& s, const vector& a) 234 254 { 235 s.setf(std::ios:: fixed);255 s.setf(std::ios::dec); 236 256 s.precision(12); 237 257 for (size_t j = 0; j < a.size(); ++j) {
Note: See TracChangeset
for help on using the changeset viewer.