1 // $Id: vectorx.h,v 1.22 2011/03/07 06:08:51 bobgian Exp $
2
3 /*
4 Copyright 2002 Peter Beerli, Mary Kuhner, Jon Yamato and Joseph Felsenstein
5
6 This software is distributed free of charge for non-commercial use
7 and is copyrighted. Of course, we do not guarantee that the software
8 works, and are not responsible for any damage you may cause or have.
9 */
10
11 // Vector utilities
12 //
13 // VECTOR SETTERS
14 // - Sets n-dimensional vector to an initial value,
15 // assumes that the vector has allocated elements
16 // VECTOR CREATION
17 // - Creates 1-D and 2-D vectors with an initial value
18 // VECTOR TYPES
19 // - Typedefs for n-dimensional vectors
20 // VECTOR MATH UTILITIES
21 // - vector = Log(vector) in save and unsave (sic!) versions of it.
22 // VECTOR TRANSMOGRIFIER
23 // - turn a 1D into a 2D vector (must be square!)
24
25 #ifndef VECTORX_H
26 #define VECTORX_H
27
28 #include <vector>
29 #include <algorithm>
30 #include <string>
31 #include "constants.h"
32 #include "definitions.h"
33 #include "ui_id.h"
34
35 using std::vector;
36 using std::string;
37
38 // This should be in mathx.h except that mathx.h includes
39 // this file, and we use this below so we can't
40 long LongSquareRootOfLong(long x);
41
42 class Force;
43
44 // VECTOR CREATION ------------------------------------------------------
45 // a set of vector of vectors setters:
46 // there is no error checking, these will *return* a new vector and
47
48 template <class T>
CreateVec1d(long n,T initial)49 vector <T> CreateVec1d(long n, T initial)
50 {
51 vector<T> one(static_cast<typename vector<T>::size_type> (n),initial);
52 return one;
53 }
54
55 template <class T>
CreateVec2d(long n,unsigned long m,T initial)56 vector < vector <T> > CreateVec2d(long n, unsigned long m, T initial)
57 {
58 vector<T> one(static_cast<typename vector<T>::size_type> (m),initial);
59 vector < vector <T> > two(n,one);
60 return two;
61 }
62
63 // VECTOR DEFINITIONS ---------------------------------------------------
64 // Typedefs for commonly used multidimensional vectors
65 // WARNING: Some version of g++ refuse to accept a 4+
66 // dimensional vector of vectors unless they have already, in
67 // compiling that source file, found a smaller vector. To work
68 // around this bug it may be necessary to declare a dummy vector.
69
70 typedef vector<string> StringVec1d;
71 typedef vector<StringVec1d> StringVec2d;
72 typedef vector<StringVec2d> StringVec3d;
73 typedef vector<StringVec3d> StringVec4d;
74 typedef vector<StringVec4d> StringVec5d;
75
76 typedef vector<double> DoubleVec1d;
77 typedef vector<DoubleVec1d> DoubleVec2d;
78 typedef vector<DoubleVec2d> DoubleVec3d;
79 typedef vector<DoubleVec3d> DoubleVec4d;
80 typedef vector<DoubleVec4d> DoubleVec5d;
81
82 typedef vector<Force*> ForceVec;
83
84 typedef vector<long> LongVec1d;
85 typedef vector<LongVec1d> LongVec2d;
86 typedef vector<LongVec2d> LongVec3d;
87 typedef vector<LongVec3d> LongVec4d;
88 typedef vector<LongVec4d> LongVec5d;
89
90 typedef vector<unsigned long> ULongVec1d; //like the tea.
91 typedef vector<ULongVec1d> ULongVec2d;
92 typedef vector<ULongVec2d> ULongVec3d;
93
94 typedef vector<int> IntVec1d;
95 typedef vector<IntVec1d> IntVec2d;
96 typedef vector<IntVec2d> IntVec3d;
97 typedef vector<IntVec3d> IntVec4d;
98 typedef vector<IntVec4d> IntVec5d;
99
100 typedef vector<model_type> ModelTypeVec1d;
101
102 typedef vector<method_type> MethodTypeVec1d;
103 typedef vector<MethodTypeVec1d> MethodTypeVec2d;
104
105 typedef vector<proftype> ProftypeVec1d;
106 typedef vector<force_type> ForceTypeVec1d;
107
108 typedef vector<UIId> UIIdVec1d;
109 typedef vector<UIIdVec1d> UIIdVec2d;
110
111 typedef vector<data_source> DataSourceVec1d;
112
113 //------------------------------------------------------------------------------------
114 //------------------------------------------------------------------------------------
115
116 // VectorAppend() puts "vec2" onto the end of "vec1"
117 template <class T>
VectorAppend(const vector<T> & vec1,const vector<T> & vec2)118 vector<T> VectorAppend(const vector<T>& vec1, const vector<T>& vec2)
119 {
120 vector<T> vec = vec1;
121 typename vector<T>::const_iterator vit;
122 for(vit = vec2.begin(); vit != vec2.end(); ++vit)
123 vec.push_back(*vit);
124
125 return(vec);
126 } // VectorAppend
127
128 //------------------------------------------------------------------------------------
129 //------------------------------------------------------------------------------------
130
131 // VECTOR MATH UTILITIES ------------------------------------------------------
132 //
133 void LogVec0(const vector<double> &in, vector<double> &out);
134
135 //------------------------------------------------------------------------------------
136 // This function takes a linear vector and turns it into
137 // a square two-dimensional vector. It will throw an exception
138 // if the size of the linear vector is not a perfect square.
139 // It assumes that diagonal entries ARE PRESENT.
140 //
141 // It is templated on the type contained in the vector.
142
143 template<class T>
SquareOffVector(const vector<T> & src)144 vector<vector<T> > SquareOffVector(const vector<T>& src)
145 {
146 long dim = LongSquareRootOfLong(src.size());
147
148 // convert linear matrix into square matrix
149
150 vector<T> vec1D;
151 vector<vector<T> > vec2D;
152 typename vector<T>::const_iterator it = src.begin();
153
154 vec1D.reserve(dim); // for speed
155 vec2D.reserve(dim);
156
157 long i;
158 long j;
159
160 for (i = 0; i < dim; i++)
161 {
162 for (j = 0; j < dim; ++j, ++it)
163 {
164 vec1D.push_back(*it);
165 }
166
167 vec2D.push_back(vec1D);
168 vec1D.clear();
169 }
170
171 return vec2D;
172
173 } // SquareOffVector
174
175 //------------------------------------------------------------------------------------
176 // vector comparison with scalar
177 //
178 template < class T >
vec_leq(vector<T> v,T comparison)179 bool vec_leq(vector < T > v, T comparison)
180 {
181 typename vector< T > :: iterator vecit;
182 for(vecit = v.begin(); vecit != v.end(); vecit++)
183 {
184 if(*vecit > comparison)
185 return false;
186 }
187 return true;
188 }
189
190 template <class T>
vec_greater(vector<T> v,T comparison)191 bool vec_greater(vector < T > v, T comparison)
192 {
193 typename vector< T > :: iterator vecit;
194 for(vecit = v.begin(); vecit != v.end(); vecit++)
195 {
196 if(*vecit <= comparison)
197 return false;
198 }
199 return true;
200 }
201
202 //------------------------------------------------------------------------------------
203 // convenience wrapper for find
204 //
205
206 template < class T >
Contains(const std::vector<T> & collection,const T & item)207 bool Contains(const std::vector<T>& collection, const T& item)
208 {
209 typename std::vector<T>::const_iterator it =
210 std::find(collection.begin(), collection.end(), item);
211 return (it != collection.end());
212 }
213
214
215 #endif // VECTORX_H
216
217 //____________________________________________________________________________________
218