Subversion Repositories gelsvn

Rev

Rev 47 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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