Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
667 khor 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
7
/*!
8
	\file Vector.h
9
	\brief The Vector type
10
*/
11
#if !defined(VECTOR_H__HAA_AGUST_2001)
12
#define VECTOR_H__HAA_AGUST_2001
13
 
14
#include <cassert>
15
#include <iostream>
16
#include <cmath>
17
#include "../CGLA/Vec2f.h"
18
#include "../CGLA/Vec3f.h"
19
 
20
namespace LinAlg
21
{
22
	template <class T> class CMatrixType;  
23
 
24
	/*!
25
		\brief The Vector type.
26
 
27
		This vector teplate is one of the basic linear algebra types. In 
28
		principle it an overloaded Array of type T. The typedef Vector with 
29
		T set to double
30
 
31
		typedef CVectorType<double> CVector;
32
 
33
		is the delclaration intended to be used as the matrix type outside 
34
		the this and related files. The reson for making a template is 
35
		twofold, first it allows for an esay change of precision in this
36
		linear algebre package. Secondly it allows for other Vector types 
37
		in special cases.
38
 
39
		The pointer to the data area can be obtained by operator [] (int i) 
40
		with i=0, e.g. 
41
 
42
		CVector A;
43
 
44
		T* pData=A[0];  
45
 
46
		will  make pData point to the data.
47
 
48
		\see LapackFunc.h
49
		\see CMatrixType  
50
		\see Additional vector operators (located in this file Vector.h)
51
		\see CMatrix
52
		\see CVector
53
		\author Henrik Aanęs
54
		\version Aug 2001
55
	*/
56
 
57
	template <class T>
58
	class CVectorType  
59
	{
60
 
61
		friend class CMatrixType<T>;
62
 
63
	private:
64
 
65
		///The number of elements in the vector
66
		int nElems;
67
 
68
		///The pointer to the vector data
69
		T*Data;
70
 
71
		///Sets all the elements in the vector to a scalar
72
		void SetScalar(const T& Scalar) 
73
		{
74
			T* Itt=Data;
75
			T* Stop=&(Data[nElems]);
76
			while(Itt!=Stop)
77
				{
78
					*Itt=Scalar;
79
					++Itt;
80
				}
81
		}; 
82
 
83
		///Frees the used memory
84
		void CleanUp(void)  {if(Data!=NULL) delete [] Data;};
85
		///Allocate the needed memory. Note that nElems should be initialized correctly first.
86
 
87
		void Allocate(void) { Data= new T[nElems];};
88
 
89
 
90
	public:
91
 
92
		/// \name Constructurs and Destructors
93
		//@{
94
		///Creats a vector a size 0.
95
		CVectorType<T>():nElems(0),Data(NULL) {};
96
 
97
		///Creates a vector of the specified Length
98
		explicit CVectorType<T>(const int Length): nElems(Length) {Allocate();}
99
 
100
		///Creates a vector of the specified Length, and sets all the values to a scalar.
101
		CVectorType<T>(const int Length,const T& Scalar): nElems(Length) {Allocate();SetScalar(Scalar);}
102
 
103
		///Copy constructor
104
		CVectorType<T>(const CVectorType<T>& Rhs): nElems(Rhs.nElems)
105
		{
106
			Allocate();
107
			memcpy(Data,Rhs.Data,nElems*sizeof(T));
108
		}
109
 
110
		///Copy constructor from Vec2Type
111
		template <class TT, class V, unsigned int N> 
112
		CVectorType<T>(const CGLA::ArithVec<TT,V,N>& Rhs):nElems(N)
113
		{
114
			Allocate(); 
115
			for(int i=0;i<N;++i)
116
				Data[i]=Rhs[i];
117
		}
118
 
119
		///
120
		virtual ~CVectorType<T>(){ CleanUp();};
121
		//@}
122
 
123
 
124
		/*!\name Asignment operators. 
125
			Note that the vector is resized if it does not have the correct size.
126
		*/
127
		//@{
128
		CVectorType<T>& operator=(const T& Rhs) {SetScalar(Rhs);return *this;}
129
 
130
		template <class TT, class V, unsigned int N> 
131
		CVectorType<T>& operator=(const CGLA::ArithVec<TT,V,N>& Rhs)
132
		{
133
			Resize(N);
134
			for(int i=0;i<N;++i)
135
				Data[i]=Rhs[i];
136
		}
137
 
138
 
139
		CVectorType<T>& operator=(const CVectorType<T>& Rhs)
140
		{
141
			Resize(Rhs.nElems);
142
			memcpy(Data,Rhs.Data,nElems*sizeof(T));
143
			return *this;
144
		}
145
		//@}
146
 
147
		/// \name Dimension functions
148
		//@{
149
		///Returns the length of the vector.
150
		const int Length(void) const {return nElems;}
151
		///Realocates memory IF the vector does not already hve the correct size.
152
		void Resize(const int Length)
153
		{
154
			if(Length!=nElems)
155
				{
156
					CleanUp();
157
					nElems=Length;
158
					Data= new T[nElems];
159
				}
160
		}
161
		//@}
162
 
163
		/*!
164
			\name Acces functions and operators.
165
			The [] operators are the ones intaended for usual use. The get 
166
			and set functions are added to allow for genral vector function 
167
			templates.
168
		*/
169
		//@{
170
		const T& get(const int i)const 
171
		{
172
			assert(i>=0);
173
			assert(i<nElems);
174
 
175
			return Data[i];
176
		}
177
 
178
		void set(const int i,const T& val)
179
		{
180
			assert(i>=0);
181
			assert(i<nElems);
182
 
183
			Data[i]=val;
184
		}
185
 
186
		T& operator[](const int i) 
187
		{
188
			assert(i>=0);
189
			assert(i<nElems);
190
 
191
			return Data[i];
192
		}
193
 
194
		const T& operator[](const int i) const 
195
		{
196
			assert(i>=0);
197
			assert(i<nElems);
198
 
199
			return Data[i];
200
		}
201
		//@}
202
 
203
		/// \name aritmethic operators
204
		//@{
205
		CVectorType<T> operator+(const CVectorType<T>& Rhs) const {CVectorType<T>Ret(*this); return Ret+=Rhs;};
206
		CVectorType<T> operator-(const CVectorType<T>& Rhs) const {CVectorType<T>Ret(*this); return Ret-=Rhs;};
207
		CVectorType<T> operator+(const T& Rhs) const {CVectorType<T>Ret(*this); return Ret+=Rhs;};
208
		CVectorType<T> operator-(const T& Rhs) const {CVectorType<T>Ret(*this); return Ret-=Rhs;};
209
		CVectorType<T> operator*(const T& Rhs) const {CVectorType<T>Ret(*this); return Ret*=Rhs;};
210
		CVectorType<T> operator/(const T& Rhs) const {CVectorType<T>Ret(*this); return Ret/=Rhs;};
211
 
212
		CVectorType<T>& operator+=(const CVectorType<T>& Rhs)
213
		{
214
			assert(nElems==Rhs.nElems);
215
 
216
			T* IttRhs=Rhs.Data;
217
			T* Itt=Data;
218
			T* Stop=&(Data[nElems]);
219
			while(Itt!=Stop)
220
				{
221
					(*Itt)+=(*IttRhs);
222
					++Itt;
223
					++IttRhs;
224
				}
225
			return *this; 
226
		}
227
 
228
		CVectorType<T>& operator-=(const CVectorType<T>& Rhs)
229
		{
230
			assert(nElems==Rhs.nElems);
231
 
232
			T* IttRhs=Rhs.Data;
233
			T* Itt=Data;
234
			T* Stop=&(Data[nElems]);
235
			while(Itt!=Stop)
236
				{
237
					(*Itt)-=(*IttRhs);
238
					++Itt;
239
					++IttRhs;
240
				}
241
			return *this; 
242
		}
243
 
244
		CVectorType<T>& operator+=(const T& Rhs)
245
		{
246
			T* Itt=Data;
247
			T* Stop=&(Data[nElems]);
248
			while(Itt!=Stop)
249
				{
250
					(*Itt)+=Rhs;
251
					++Itt;
252
				}
253
			return *this; 
254
		}
255
 
256
		CVectorType<T>& operator-=(const T& Rhs)
257
		{
258
			T* Itt=Data;
259
			T* Stop=&(Data[nElems]);
260
			while(Itt!=Stop)
261
				{
262
					(*Itt)-=Rhs;
263
					++Itt;
264
				}
265
			return *this; 
266
		}
267
 
268
		CVectorType<T>& operator*=(const T& Rhs)
269
		{
270
			T* Itt=Data;
271
			T* Stop=&(Data[nElems]);
272
			while(Itt!=Stop)
273
				{
274
					(*Itt)*=Rhs;
275
					++Itt;
276
				}
277
			return *this; 
278
		}
279
 
280
		CVectorType<T>& operator/=(const T& Rhs)
281
		{
282
			T* Itt=Data;
283
			T* Stop=&(Data[nElems]);
284
			while(Itt!=Stop)
285
				{
286
					(*Itt)/=Rhs;
287
					++Itt;
288
				}
289
			return *this; 
290
		}
291
 
292
		T operator*(const CVectorType<T>& Rhs) const
293
		{
294
			assert(nElems==Rhs.nElems);
295
			T Ret=0;
296
 
297
			T* IttRhs=Rhs.Data;
298
			T* Itt=Data;
299
			T* Stop=&(Data[nElems]);
300
			while(Itt!=Stop)
301
				{
302
					Ret+=(*Itt)*(*IttRhs);
303
					++Itt;
304
					++IttRhs;
305
				}
306
			return Ret; 
307
		}
308
 
309
		CVectorType<T> operator*(const CMatrixType<T>& Rhs) const
310
		{
311
			assert(nElems==Rhs.Rows());
312
			CVectorType<T> Ret(Rhs.Cols(),0);
313
 
314
			T* ThisItt=Data;
315
			const T* RhsItt=Rhs.Data;
316
			const T* Stop=&(Rhs.Data[Rhs.nElems]);
317
			T* RetItt;
318
 
319
			while(RhsItt!=Stop)
320
				{
321
					RetItt=Ret.Data;
322
					for(int cCol=Rhs.Cols()-1;cCol>=0;--cCol)
323
						{
324
							(*RetItt)+=(*ThisItt)*(*RhsItt);
325
							++RetItt;
326
							++RhsItt;
327
						}
328
					++ThisItt;
329
				}
330
			return Ret;
331
		}
332
		//@}
333
 
334
		/// \name Norms
335
		//@{
336
		const T Norm(void) const {return sqrt((*this)*(*this));}
337
		const T NormMax(void) const
338
		{
339
			T Ret=0;
340
			T Abs;
341
			const T* Itt=Data;
342
			const T* Stop=&(Data[nElems]);
343
			while(Itt!=Stop)
344
				{
345
					Abs=(*Itt)>0?(*Itt):-(*Itt);
346
					if(Ret<Abs)
347
						Ret=Abs;
348
 
349
					++Itt;
350
				}
351
			return Ret;
352
		}
353
		//@}
354
 
355
		/// Sum of all the elements in the vector
356
		const T Sum(void) const
357
		{
358
			T Ret=0;
359
			const T* Itt=Data;
360
			const T* Stop=&(Data[nElems]);
361
			while(Itt!=Stop)
362
				{
363
					Ret+=(*Itt);
364
					++Itt;
365
				}
366
		}
367
 
368
 
369
		/*!
370
			\name Elementwise functions.
371
			Functions the perform some elementary operation on each 
372
			of the elements of the vector.
373
		*/
374
		//@{
375
		///The elements of the vector squared. Equvivalent to MatLAb's .^2
376
		void ElemSqr(void)
377
		{
378
			T* Itt=Data;
379
			T* Stop=&(Data[nElems]);
380
			while(Itt!=Stop)
381
				{
382
					(*Itt)*=(*Itt);
383
					++Itt;
384
				}
385
		}
386
 
387
 
388
	};
389
 
390
 
391
	/*!
392
		\name Additional vector operators
393
		These are operators heavily associated with CVectorType, but not included
394
		in the class definition it self.
395
	*/
396
	//@{
397
	template<class T>
398
	inline CVectorType<T> operator+(const T& Lhs,const CVectorType<T>&Rhs ) 
399
	{
400
		return Rhs+Lhs;
401
	}
402
 
403
	template<class T>
404
	inline CVectorType<T> operator*(const T& Lhs,const CVectorType<T>&Rhs ) 
405
	{
406
		return Rhs*Lhs;
407
	}
408
 
409
	template <class T>
410
	std::ostream& operator<<(std::ostream &s, const CVectorType<T> &a)
411
	{
412
		int nElems=a.Length();
413
 
414
		for (int cElem=0; cElem<nElems; cElem++)
415
			{
416
				s << a[cElem] << " ";
417
			}
418
		s << "\n";
419
 
420
		return s;
421
	}
422
 
423
 
424
	template <class T>
425
	std::istream& operator>>(std::istream &s, CVectorType<T> &a)
426
	{
427
		int nElems;
428
 
429
		s >> nElems;
430
 
431
		if ( nElems!=a.Length())
432
			a.Resize(nElems);
433
 
434
 
435
		for (int cElem=0; cElem<a.Length();cElem++)
436
			{
437
				s >>  a[cElem];
438
			}
439
 
440
		return s;
441
	}
442
	//@}
443
 
444
	/// The Vector annotation intended for use.
445
	typedef CVectorType<double> CVector;
446
}
447
#endif // !defined(VECTOR_H__HAA_AGUST_2001)