1 | // $Id: vector.cc 328 2005-05-30 23:04:20Z jari $ |
---|
2 | |
---|
3 | #include <c++_tools/gslapi/vector.h> |
---|
4 | #include <c++_tools/utility/stl_utility.h> |
---|
5 | |
---|
6 | #include <iostream> |
---|
7 | #include <vector> |
---|
8 | #include <utility> |
---|
9 | |
---|
10 | |
---|
11 | namespace theplu { |
---|
12 | namespace gslapi { |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | vector::vector(void) |
---|
17 | : v_(NULL), view_(NULL) |
---|
18 | { |
---|
19 | } |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | vector::vector(const size_t n,const double init_value) |
---|
24 | : view_(NULL) |
---|
25 | { |
---|
26 | v_ = gsl_vector_alloc(n); |
---|
27 | set_all(init_value); |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | |
---|
32 | vector::vector(const vector& other) |
---|
33 | : view_(NULL) |
---|
34 | { |
---|
35 | v_ = other.TEMP_gsl_vector_copy(); |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | vector::vector(vector& v, size_t offset, size_t n, size_t stride) |
---|
41 | { |
---|
42 | view_ = new gsl_vector_view(gsl_vector_subvector_with_stride(v.v_,offset, |
---|
43 | stride,n)); |
---|
44 | v_ = &(view_->vector); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | vector::vector(gsl_vector* vec) |
---|
50 | : v_(vec), view_(NULL) |
---|
51 | { |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | vector::vector(std::istream& is) |
---|
57 | : view_(NULL) |
---|
58 | { |
---|
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); |
---|
88 | } |
---|
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] ); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | vector::~vector(void) |
---|
103 | { |
---|
104 | if (view_) |
---|
105 | delete view_; |
---|
106 | else if (v_) |
---|
107 | gsl_vector_free(v_); |
---|
108 | v_=NULL; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | gsl_vector* vector::TEMP_gsl_vector_copy(void) const |
---|
114 | { |
---|
115 | gsl_vector* vec = gsl_vector_alloc(size()); |
---|
116 | gsl_vector_memcpy(vec,v_); |
---|
117 | return vec; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | std::pair<double,double> vector::minmax(void) const |
---|
123 | { |
---|
124 | double min, max; |
---|
125 | gsl_vector_minmax(v_,&min,&max); |
---|
126 | return std::pair<double,double>(min,max); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | std::pair<size_t,size_t> vector::minmax_index(void) const |
---|
132 | { |
---|
133 | size_t min_index, max_index; |
---|
134 | gsl_vector_minmax_index(v_,&min_index,&max_index); |
---|
135 | return std::pair<size_t,size_t>(min_index,max_index); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | double vector::sum(void) const |
---|
141 | { |
---|
142 | double sum = 0; |
---|
143 | for (size_t i=0; i<size(); i++) |
---|
144 | sum += gsl_vector_get( v_, i ); |
---|
145 | return( sum ); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | vector vector::operator-(void) const |
---|
151 | { |
---|
152 | vector res( *this ); |
---|
153 | gsl_vector_scale (res.v_,-1.); |
---|
154 | return res; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | double vector::operator*( const vector &other ) const |
---|
160 | { |
---|
161 | // Jari, check for gsl_support |
---|
162 | double res = 0.0;; |
---|
163 | for ( size_t i = 0; i < size(); ++i ) |
---|
164 | res += other[i] * (*this)[i]; |
---|
165 | return res; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | vector vector::operator+(const vector &other) const |
---|
171 | { |
---|
172 | vector res(*this); |
---|
173 | gsl_vector_add(res.v_,other.v_); |
---|
174 | return res; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | |
---|
179 | vector vector::operator-( const vector &other ) const |
---|
180 | { |
---|
181 | vector res( *this ); |
---|
182 | gsl_vector_sub(res.v_,other.v_); |
---|
183 | return res; |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | vector& vector::operator=( const vector& other ) |
---|
189 | { |
---|
190 | if( this != &other ) { |
---|
191 | if ( v_ ) |
---|
192 | gsl_vector_free( v_ ); |
---|
193 | v_ = other.TEMP_gsl_vector_copy(); |
---|
194 | } |
---|
195 | return *this; |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | std::ostream& operator<<(std::ostream& s, const vector& a) |
---|
201 | { |
---|
202 | s.setf(std::ios::fixed); |
---|
203 | |
---|
204 | for (size_t j = 0; j < a.size(); ++j) { |
---|
205 | s << a[j]; |
---|
206 | if ( (j+1)<a.size() ) |
---|
207 | s << " "; |
---|
208 | } |
---|
209 | |
---|
210 | return s; |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | }} // of namespace gslapi and namespace thep |
---|