1 | // $Id: vector.cc 48 2004-03-02 15:08:45Z jari $ |
---|
2 | |
---|
3 | #include <iostream> |
---|
4 | #include <sstream> |
---|
5 | #include <string> |
---|
6 | #include <vector> |
---|
7 | |
---|
8 | #include "vector.h" |
---|
9 | |
---|
10 | |
---|
11 | namespace theplu { |
---|
12 | namespace gslapi { |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | vector::vector(void) |
---|
17 | : v_(NULL) |
---|
18 | { |
---|
19 | } |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | vector::vector(const size_t n, const double init_value) |
---|
24 | { |
---|
25 | v_ = gsl_vector_alloc(n); |
---|
26 | set_all(init_value); |
---|
27 | } |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | vector::vector(const vector& other) |
---|
32 | { |
---|
33 | v_ = other.gsl_vector_copy(); |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | vector::vector(gsl_vector* vec) |
---|
39 | : v_(vec) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | vector::vector(std::istream& is) |
---|
46 | { |
---|
47 | using namespace std; |
---|
48 | |
---|
49 | // read the data file and store in stl vectors (dynamically expandable) |
---|
50 | std::vector<std::vector<double> > data_matrix; |
---|
51 | u_int nof_columns=0; |
---|
52 | u_int nof_rows = 0; |
---|
53 | string s; |
---|
54 | for (nof_rows = 0; getline(is, s, '\n'); nof_rows++) { |
---|
55 | istringstream line(s); |
---|
56 | std::vector<double> v; |
---|
57 | string tmp_string; |
---|
58 | while (line >> tmp_string) { |
---|
59 | if(!is.good()) { |
---|
60 | // Jari. change to throw exception, remove unnecessary includes |
---|
61 | cerr << "vector::vector(std::istream&): " |
---|
62 | << "error reading data file!" << endl; |
---|
63 | exit(1); |
---|
64 | } |
---|
65 | double t=atof(tmp_string.c_str()); |
---|
66 | // Here we should check that it actually was a double!!!!! |
---|
67 | v.push_back(t); |
---|
68 | } |
---|
69 | if(nof_columns==0) |
---|
70 | nof_columns=v.size(); |
---|
71 | else if(v.size()!=nof_columns) { |
---|
72 | // Jari. change to throw exception, remove unnecessary includes |
---|
73 | cerr << "vector::vector(std::istream&) data file error: " |
---|
74 | << "line" << nof_rows+1 << " has " << v.size() |
---|
75 | << " columns; expected " << nof_columns |
---|
76 | << " columns" |
---|
77 | << endl; |
---|
78 | exit(1); |
---|
79 | } |
---|
80 | data_matrix.push_back(v); |
---|
81 | } |
---|
82 | |
---|
83 | // manipulate the state of the stream to be good |
---|
84 | is.clear(std::ios::goodbit); |
---|
85 | |
---|
86 | // convert the data to a gsl vector and check that data file is a |
---|
87 | // column vector or a row vector |
---|
88 | if(nof_columns==1) { |
---|
89 | v_ = gsl_vector_alloc ( nof_rows ); |
---|
90 | for(u_int i=0;i<nof_rows;i++) |
---|
91 | gsl_vector_set( v_, i, data_matrix[i][0] ); |
---|
92 | } |
---|
93 | else if(nof_rows==1){ |
---|
94 | v_ = gsl_vector_alloc ( nof_columns ); |
---|
95 | for(u_int i=0;i<nof_columns;i++) |
---|
96 | gsl_vector_set( v_, i, data_matrix[0][i] ); |
---|
97 | } |
---|
98 | else { |
---|
99 | // Jari. change to throw exception, remove unnecessary includes |
---|
100 | cerr << "vector::vector(std::istream&) data file error: " |
---|
101 | << "file has " << nof_rows << " rows and " << nof_columns |
---|
102 | << " columns; expected a row vector or a column vector" |
---|
103 | << endl; |
---|
104 | exit(1); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | vector::~vector(void) |
---|
111 | { |
---|
112 | if (v_) { |
---|
113 | gsl_vector_free(v_); |
---|
114 | v_=NULL; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | gsl_vector* vector::gsl_vector_copy(void) const |
---|
121 | { |
---|
122 | gsl_vector* vec = gsl_vector_alloc(size()); |
---|
123 | gsl_vector_memcpy(vec,v_); |
---|
124 | return vec; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | vector vector::mul_elements( const vector& other ) const |
---|
130 | { |
---|
131 | vector res( *this ); |
---|
132 | gsl_vector_mul( res.v_, other.v_ ); |
---|
133 | return res; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | double vector::sum(void) const |
---|
139 | { |
---|
140 | double sum = 0; |
---|
141 | for (u_int i=0; i<size(); i++) |
---|
142 | sum += gsl_vector_get( v_, i ); |
---|
143 | return( sum ); |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | vector vector::operator-(void) const |
---|
149 | { |
---|
150 | vector res( *this ); |
---|
151 | gsl_vector_scale (res.v_,-1.); |
---|
152 | return res; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | double vector::operator*( const vector &other ) const |
---|
158 | { |
---|
159 | // Jari, check for gsl_support |
---|
160 | double res = 0.0;; |
---|
161 | for ( size_t i = 0; i < size(); ++i ) |
---|
162 | res += other[i] * (*this)[i]; |
---|
163 | return res; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | vector vector::operator+(const vector &other) const |
---|
169 | { |
---|
170 | vector res(*this); |
---|
171 | gsl_vector_add(res.v_,other.v_); |
---|
172 | return res; |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | vector vector::operator-( const vector &other ) const |
---|
178 | { |
---|
179 | vector res( *this ); |
---|
180 | gsl_vector_sub(res.v_,other.v_); |
---|
181 | return res; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | vector& vector::operator=( const vector& other ) |
---|
187 | { |
---|
188 | if( this != &other ) { |
---|
189 | if ( v_ ) |
---|
190 | gsl_vector_free( v_ ); |
---|
191 | v_ = other.gsl_vector_copy(); |
---|
192 | } |
---|
193 | return *this; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | std::ostream& theplu::gslapi::operator<<(std::ostream& s, const vector& a) |
---|
199 | { |
---|
200 | s.setf(std::ios::fixed); |
---|
201 | |
---|
202 | for (size_t j = 0; j < a.size(); ++j) { |
---|
203 | s << a[j]; |
---|
204 | if ( (j+1)<a.size() ) |
---|
205 | s << " "; |
---|
206 | } |
---|
207 | |
---|
208 | return s; |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | }} // of namespace gslapi and namespace thep |
---|