C++ main module for mmsd Package  1.0
LAP_Vector.hpp
Go to the documentation of this file.
1 #ifndef LAP_Vector_HPP
2 #define LAP_Vector_HPP
3 
4 
5 using namespace std;
6 
7 
8 
9 template<class T>
11 template<class T>
13 
14 template<class T>
16  tLVectorIndex n=getSize();
17  tLVectorIncrement incx =getIncrement();
18  const T* v=&(*this)(0);
19  tLVectorIndex k=0;
20  if (n==0) return 0;
21  T maxValue=*v;
22  i=0;
23  v++;
24  for (k=1;k<n;k++) {
25  if (*v>maxValue) {
26  maxValue=*v;
27  i=k;
28  }
29  v+=incx;
30  }
31  return maxValue;
32 }
33 
34 template<class T>
36  tLVectorIndex n=getSize();
37  tLVectorIncrement incx =getIncrement();
38  const T* v=&(*this)(0);
39  tLVectorIndex k=0;
40  if (n==0) return 0;
41  T minValue=*v;
42  i=0;
43  v++;
44  for (k=1;k<n;k++) {
45  if (*v<minValue) {
46  minValue=*v;
47  i=k;
48  }
49  v+=incx;
50  }
51  return minValue;
52 }
53 
54 
55 template<class T>
57  tLVectorIndex n = getSize();
58  tLVectorIndex sx=getStart();
59  tLVectorIncrement incx = getIncrement();
60  tLVectorIndex idamin=0;
61  tLVectorIndex i=0,index=sx;
62  T v,vmin;
63  if ((n<=1) || (incx==0)) return idamin;
64  if (incx==1) {
65  vmin=fabs((*this)[index++]);
66  for (i=1;i<n;i++) {
67  v=fabs((*this)[index]);
68  if (v<=vmin) {
69  idamin=i;
70  vmin=v;
71  }
72  index++;
73 
74  }
75  } else {
76  vmin=fabs((*this)[index]);
77  index+=incx;
78  for (i=1;i<n;i++) {
79  v=fabs((*this)[index]);
80  if (v<=vmin) {
81  idamin=i;
82  vmin=v;
83  }
84  index+=incx;
85 
86  }
87  }
88  return idamin;
89 }
90 
91 
95 template<class T>
96 void LAP_Vector<T>::merge(const T &alpha,const LAP_Vector<T>& x,
97  const T &beta,const LAP_Vector<T>& y) {
98  tLVectorIndex i,n=x.getSize();
99  tLVectorIndex p=y.getSize();
100  setSize(n+p);
101 
102  T *vs=&(*this)(0);
103  const T *xs=&x(0);
104  const T *ys=&y(0);
105 
106  tLVectorIndex inc=getIncrement();
109 
110  for (i=0;i<n-1;i++) {
111  *vs=*xs*alpha;
112  vs+=inc;
113  xs+=incx;
114  }
115  //i=n
116  *vs=*xs*alpha;
117  if (p>0) {
118  vs+=inc;
119  for (i=0;i<p-1;i++) {
120  *vs=*ys*beta;
121  vs+=inc;
122  ys+=incy;
123  }
124  *vs=*ys*beta;
125  }
126 
127 }
128 
129 
130 template<class T>
132  if (a.getSize()!=b.getSize())
133  throw LAP_Exception("math/linalg/core",
134  "LAP_Vector::distance2",
135  "incompatible dimension of vectors");
136  tReal scale,ssq,distance=0;
137  tLVectorIndex n=a.getSize();
138  tLVectorIndex sx=a.getStart();
139  tLVectorIndex sy=b.getStart();
142  tLVectorIndex i,ix,iy;
143  double d,absd;
144  if ((incx==0) || incy==0) distance=0;
145  else if (n==1) distance=fabs(a[sx]-b[sy]);
146  else {
147  scale=0;
148  ssq=1;
149  ix=sx;
150  iy=sy;
151  for (i=0;i<n;i++) {
152  d=a[ix]-b[iy];
153  if (d!=0) {
154  absd=fabs(d);
155  if (scale <absd) {
156  ssq=1+ssq*(scale*scale/(absd*absd));
157  } else {
158  ssq+=(absd*absd)/(scale*scale);
159  }
160  }
161  ix+=incx;
162  iy+=incy;
163  }
164  distance=scale*sqrt(ssq);
165  }
166  return distance;
167 }
168 
169 template<class T>
171 
172  tLVectorIndex n=getSize();
173  if (n==0) return 0;
174  tLVectorIndex sx=getStart();
175  tLVectorIncrement incx=getIncrement();
176  T s=0;
177  const T *iter=&(*this)[sx];
178  const T *end=&(*this)[sx+n-1];
179  if (incx==1) {
180  while( iter<=end) {
181  s+=*iter;
182  iter++;
183  }
184  } else {
185  while( iter<=end) {
186  s+=*iter;
187  iter+=incx;
188  }
189  }
190  return s;
191 }
192 
193 
196 template<class T>
197 void LAP_Vector<T>::sort(const tFlag& order) {
198 
199  tLVectorIndex h=1;
200  tLVectorIndex i,j;
201  T v;
202  tLVectorIndex N=getSize();
203  T *values=&(*this)(0);
204  tLVectorIncrement inc=getIncrement();
205 
206  do {
207  h=3*h+1;
208  } while (h<=N);
209 
210  do {
211  h/=3;
212  for (i=h;i<N;i++) {
213  if (compare(values[i*inc],
214  values[(i-h)*inc],
215  order)) {
216  v=values[i*inc];
217  j=i;
218  do {
219  j=j*inc;
220  values[j*inc]=values[(j-h)*inc];
221  j=j-h;
222  } while ((j>=h) &&
223  (!compare(values[(j-h)*inc],
224  v,
225  order)));
226  (*this)[j]=v;
227  }
228  }
229  } while (h>1);
230 }
231 
232 template<class T>
234  int n=getSize();
235  if (n<=1) return;
236  quickSort(&(*this)(0),getIncrement(),0,n-1);
237 }
238 
239 template<class T>
240 void LAP_Vector<T>::quickSort(T *v,
241  const tLVectorIncrement& incv,
242  const tLVectorIndex& from,
243  const tLVectorIndex& to) {
244  // no need to sort a vector of zero or one elements
245  if (from>=to) return;
246 
247 
248  // select the pivot value
249  tLVectorIndex pivotLLVectorIndex = (from+to) / 2;
250  T pivotValue=v[pivotLLVectorIndex*incv];
251  // partition the vector
252  tLVectorIndex left=from;
253  tLVectorIndex right=to;
254  while (left<=right) {
255  while ((left<=to) && (v[left*incv]>pivotValue)) left++;
256  while ((right>from) && (v[right*incv]<pivotValue)) right--;
257  if (left<=right) {
258  std::swap(v[left*incv],v[right*incv]);
259  left++;
260  if (right>0) right--;
261  }
262  }
263 
264  // sort the two sub vectors
265  quickSort(v, incv,from, right);
266  quickSort(v, incv,left, to);
267 }
268 
269 template<class T>
271 
272  tLVectorIndex n =getSize();
273  tLVectorIndex sx= getStart();
274  tLVectorIncrement incx = getIncrement();
275  tLVectorIndex i=sx,k=sx+(n-1)*incx;
276  T temp;
277  //i=sx+i*incx
278  while (i<k) {
279  temp=(*this)[k];
280  (*this)[k]=(*this)[i];
281  (*this)[i]=temp;
282  i+=incx;
283  k-=incx;
284  }
285 }
286 
287 #endif
this class describes the exceptions raised for LAP package
Definition: LAP_Exception.h:14
const tLVectorIndex & getStart() const
get the start of the vector
Definition: LAP_Vector.h:534
T sum() const
compute the sum of the elements
Definition: LAP_Vector.hpp:170
T maxValue(tLVectorIndex &i) const
return the max value of the vector and the corresponding index
Definition: LAP_Vector.hpp:15
const tLVectorIncrement & getIncrement() const
get the increment of the vector
Definition: LAP_Vector.h:529
tLVectorIndex getSize() const
get the size of the vector
Definition: LAP_Vector.h:519
tReal distance2(const LAP_Vector< T > &a) const
compute the distance between this and a
Definition: LAP_Vector.h:583
void merge(const T &alpha, const LAP_Vector< T > &x, const T &beta, const LAP_Vector< T > &y)
this is the merging of 2 vectors This = [alpha.x,beta.y]
Definition: LAP_Vector.hpp:96
void quickSort()
quit sort the vector in an increasing order
Definition: LAP_Vector.hpp:233
this class describes a vector of double
Definition: LAP_Vector.h:16
#define tLVectorIndex
Definition: lapack_types.h:13
void sort()
sort in ascent order view untaken into account
Definition: LAP_Vector.h:614
tLVectorIndex indexMin() const
retun the index min of th evector
Definition: LAP_Vector.hpp:56
#define tLVectorIncrement
Definition: lapack_types.h:16
T minValue(tLVectorIndex &i) const
return the min value of the vector and the corresponding index
Definition: LAP_Vector.hpp:35
#define tReal
Definition: types.h:18
void reverse()
reverse the vector
Definition: LAP_Vector.hpp:270
#define tFlag
Definition: types.h:14