00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 2009, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ------------------------------------------------------------------- 00022 */ 00023 #ifndef O2SCL_VECTOR_H 00024 #define O2SCL_VECTOR_H 00025 00026 /** \file vector.h 00027 \brief File for generic vector functions 00028 00029 For a more general discussion of vectors and matrices in \o2, see 00030 the \ref vecmat_section of the User's Guide. 00031 00032 For overloaded operators involving vectors and matrices, see \ref 00033 vec_arith.h . For statistics operations not included here, see 00034 \ref vec_stats.h in the directory \c src/other . For functions and 00035 classes which are specific to C-style arrays, see \ref array.h . 00036 Also related are the matrix output functions, \ref matrix_out(), 00037 \ref matrix_cx_out_paren(), and \ref matrix_out_paren() which are 00038 defined in \ref columnify.h because they utilize the class \ref 00039 columnify to format the output. 00040 00041 For functions which search for a value in an ordered vector, 00042 see the class \ref search_vec . 00043 */ 00044 00045 #include <iostream> 00046 #include <cmath> 00047 #include <string> 00048 #include <fstream> 00049 #include <sstream> 00050 #include <o2scl/err_hnd.h> 00051 #include <gsl/gsl_ieee_utils.h> 00052 #include <gsl/gsl_sort.h> 00053 00054 #ifndef DOXYGENP 00055 namespace o2scl 00056 { 00057 #endif 00058 00059 /** 00060 \brief Naive vector copy 00061 00062 \note This ordering is reversed from the GSL function 00063 \c gsl_vector_memcpy . This is to be used with 00064 \code 00065 vector_copy(N,source,destination); 00066 \endcode 00067 instead of 00068 \code 00069 gsl_vector_memcpy(destination,source); 00070 \endcode 00071 */ 00072 template<class vec_t, class vec2_t> 00073 void vector_copy(size_t N, vec_t &src, vec2_t &dest) { 00074 for(size_t i=0;i<N;i++) dest[i]=src[i]; 00075 } 00076 00077 /** 00078 \brief Naive matrix copy 00079 00080 \note This ordering is reversed from the GSL function 00081 \c gsl_matrix_memcpy . This is to be used with 00082 \code 00083 matrix_copy(N,source,destination); 00084 \endcode 00085 instead of 00086 \code 00087 gsl_matrix_memcpy(destination,source); 00088 \endcode 00089 */ 00090 template<class mat_t, class mat2_t> 00091 void matrix_copy(size_t M, size_t N, mat_t &src, mat2_t &dest) { 00092 for(size_t i=0;i<M;i++) { 00093 for(size_t j=0;j<N;j++) { 00094 dest[i][j]=src[i][j]; 00095 } 00096 } 00097 } 00098 00099 /** 00100 \brief GSL complex vector copy 00101 00102 \future At present this works only with complex types based 00103 directly on the GSL complex format. This could be improved. 00104 */ 00105 template<class vec_t, class vec2_t> 00106 void vector_cx_copy_gsl(size_t N, vec_t &src, vec2_t &dest) { 00107 for(size_t i=0;i<N;i++) { 00108 dest[i].dat[0]=src[i].dat[0]; 00109 dest[i].dat[1]=src[i].dat[1]; 00110 } 00111 } 00112 00113 /** 00114 \brief GSL complex matrix copy 00115 00116 \future At present this works only with complex types based 00117 directly on the GSL complex format. This could be improved. 00118 */ 00119 template<class mat_t, class mat2_t> 00120 void matrix_cx_copy_gsl(size_t M, size_t N, mat_t &src, mat2_t &dest) { 00121 for(size_t i=0;i<M;i++) { 00122 for(size_t j=0;j<N;j++) { 00123 dest[i][j].dat[0]=src[i][j].dat[0]; 00124 dest[i][j].dat[1]=src[i][j].dat[1]; 00125 } 00126 } 00127 } 00128 00129 /** 00130 \brief Output a vector to a stream 00131 00132 No trailing space is output after the last element, and an 00133 endline is output only if \c endline is set to \c true. If the 00134 parameter \c n is zero, this function silently does nothing. 00135 00136 Note that the \o2 vector classes also have their own 00137 \c operator<<() defined for them. 00138 */ 00139 template<class vec_t> 00140 int vector_out(std::ostream &os, size_t n, vec_t &v, 00141 bool endline=false) { 00142 // This next line is important since n-1 is not well-defined if n=0 00143 if (n==0) return 0; 00144 for(size_t i=0;i<n-1;i++) os << v[i] << " "; 00145 os << v[n-1]; 00146 if (endline) os << std::endl; 00147 return 0; 00148 } 00149 00150 /** 00151 \brief Provide a downheap() function for vector_sort() 00152 */ 00153 template<class data_t, class vec_t> 00154 void sort_downheap(vec_t &data, const size_t N, size_t k) { 00155 00156 data_t v=data[k]; 00157 00158 while (k<=N/2) { 00159 size_t j=2*k; 00160 00161 if (j<N && data[j] < data[j+1]) j++; 00162 if (!(v < data[j])) break; 00163 data[k]=data[j]; 00164 k=j; 00165 } 00166 00167 data[k]=v; 00168 } 00169 00170 /** 00171 \brief Sort a vector 00172 00173 This is a generic sorting template function. It will work for 00174 any types \c data_t and \c vec_t for which 00175 - \c data_t has an <tt>operator=</tt> 00176 - \c data_t has a less than operator to compare elements 00177 - <tt>vec_t::operator[]</tt> returns a reference 00178 to an object of type \c data_t 00179 00180 In particular, it will work with \ref ovector, \ref uvector, 00181 \ref ovector_int, \ref uvector_int (and other related \o2 00182 vector classes), the STL template class <tt>std::vector</tt>, 00183 and arrays and pointers of numeric, character, and string 00184 objects. 00185 00186 For example, 00187 \code 00188 std::string list[3]={"dog","cat","fox"}; 00189 vector_sort<std::string, std::string[3]>(3,list); 00190 \endcode 00191 */ 00192 template<class data_t, class vec_t> 00193 int vector_sort(const size_t n, vec_t &data) { 00194 00195 size_t N; 00196 size_t k; 00197 00198 if (n==0) return 0; 00199 00200 N=n-1; 00201 k=N/2; 00202 k++; 00203 do { 00204 k--; 00205 sort_downheap<data_t,vec_t>(data,N,k); 00206 } while (k > 0); 00207 00208 while (N > 0) { 00209 data_t tmp=data[0]; 00210 data[0]=data[N]; 00211 data[N]=tmp; 00212 N--; 00213 sort_downheap<data_t,vec_t>(data,N,0); 00214 } 00215 00216 return 0; 00217 } 00218 00219 /** 00220 \brief "Rotate" a vector so that the kth element is now the beginning 00221 00222 This is a generic template function which will work for 00223 any types \c data_t and \c vec_t for which 00224 - \c data_t has an <tt>operator=</tt> 00225 - <tt>vec_t::operator[]</tt> returns a reference 00226 to an object of type \c data_t 00227 */ 00228 template<class data_t, class vec_t> 00229 int vector_rotate(const size_t n, vec_t &data, size_t k) { 00230 00231 data_t *tmp=new data_t[n]; 00232 for(size_t i=0;i<n;i++) { 00233 tmp[i]=data[(i+k)%n]; 00234 } 00235 for(size_t i=0;i<n;i++) { 00236 data[i]=tmp[i]; 00237 } 00238 delete[] tmp; 00239 00240 return 0; 00241 } 00242 00243 /// Compute the maximum of the first \c n elements of a vector 00244 template<class vec_t> 00245 double vector_max(const size_t n, vec_t &data) { 00246 00247 if (n==0) { 00248 O2SCL_ERR_RET("Sent size=0 to vector_max().",gsl_efailed); 00249 } 00250 double max=data[0]; 00251 for(size_t i=1;i<n;i++) { 00252 if (data[i]>max) { 00253 max=data[i]; 00254 } 00255 } 00256 return max; 00257 } 00258 00259 /// Compute the minimum of the first \c n elements of a vector 00260 template<class vec_t> 00261 double vector_min(const size_t n, vec_t &data) { 00262 00263 if (n==0) { 00264 O2SCL_ERR_RET("Sent size=0 to vector_min().",gsl_efailed); 00265 } 00266 double min=data[0]; 00267 for(size_t i=1;i<n;i++) { 00268 if (data[i]<min) { 00269 min=data[i]; 00270 } 00271 } 00272 return min; 00273 } 00274 00275 /// Compute the minimum and maximum of the first \c n elements of a vector 00276 template<class vec_t> 00277 int vector_minmax(const size_t n, vec_t &data, double &min, double &max) { 00278 00279 if (n==0) { 00280 O2SCL_ERR_RET("Sent size=0 to vector_min().",gsl_efailed); 00281 } 00282 min=data[0]; 00283 max=min; 00284 for(size_t i=1;i<n;i++) { 00285 if (data[i]<min) { 00286 min=data[i]; 00287 } 00288 if (data[i]>max) { 00289 max=data[i]; 00290 } 00291 } 00292 return 0; 00293 } 00294 00295 /// Compute the maximum of the first \c n elements of a vector 00296 template<class vec_t> 00297 size_t vector_max_index(const size_t n, vec_t &data, double &max) { 00298 00299 if (n==0) { 00300 O2SCL_ERR_RET("Sent size=0 to vector_max().",gsl_efailed); 00301 } 00302 size_t ix=0; 00303 max=data[0]; 00304 for(size_t i=1;i<n;i++) { 00305 if (data[i]>max) { 00306 max=data[i]; 00307 ix=i; 00308 } 00309 } 00310 return ix; 00311 } 00312 00313 /// Compute the minimum of the first \c n elements of a vector 00314 template<class vec_t> 00315 int vector_min_index(const size_t n, vec_t &data, double &min) { 00316 00317 if (n==0) { 00318 O2SCL_ERR_RET("Sent size=0 to vector_min().",gsl_efailed); 00319 } 00320 size_t ix=0; 00321 min=data[0]; 00322 for(size_t i=1;i<n;i++) { 00323 if (data[i]<min) { 00324 min=data[i]; 00325 ix=i; 00326 } 00327 } 00328 return ix; 00329 } 00330 00331 /// Compute the minimum and maximum of the first \c n elements of a vector 00332 template<class vec_t> 00333 int vector_minmax_index(const size_t n, vec_t &data, double &min, 00334 size_t &ix, double &max, size_t &ix2) { 00335 00336 if (n==0) { 00337 O2SCL_ERR_RET("Sent size=0 to vector_min().",gsl_efailed); 00338 } 00339 ix=0; 00340 ix2=0; 00341 min=data[0]; 00342 max=min; 00343 for(size_t i=1;i<n;i++) { 00344 if (data[i]<min) { 00345 min=data[i]; 00346 ix=i; 00347 } 00348 if (data[i]>max) { 00349 max=data[i]; 00350 ix2=i; 00351 } 00352 } 00353 return 0; 00354 } 00355 00356 /** 00357 \brief Compute the sum of the first \c n elements of a vector 00358 00359 If \c n is zero, this will set \c avg to zero and return 00360 \ref gsl_success. 00361 */ 00362 template<class vec_t> 00363 double vector_sum(const size_t n, vec_t &data) { 00364 00365 double sum=0; 00366 for(size_t i=0;i<n;i++) { 00367 sum+=data[i]; 00368 } 00369 return sum; 00370 } 00371 00372 /** \brief Reverse a vector 00373 */ 00374 template<class data_t, class vec_t> 00375 int vector_reverse(const size_t n, vec_t &data) { 00376 data_t tmp; 00377 00378 for(size_t i=0;i<n/2;i++) { 00379 tmp=data[n-1-i]; 00380 data[n-1-i]=data[i]; 00381 data[i]=tmp; 00382 } 00383 return 0; 00384 } 00385 00386 #ifndef DOXYGENP 00387 } 00388 #endif 00389 00390 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page