File: vector.h

package info (click to toggle)
flying 6.20-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 752 kB
  • ctags: 1,873
  • sloc: cpp: 10,966; makefile: 189
file content (140 lines) | stat: -rw-r--r-- 3,031 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef _vector_h
#define _vector_h

#ifndef _real_h
#	include "real.h"
#endif

//
// -------------------------------------------------------------------------
// class Vector : Vektorklasse f�r beliebig dimensionierte Vektoren
// -------------------------------------------------------------------------
//

class Vec2;
class Vec3;

class Vector {
	protected:
		short	dim;
		Real	*data;
		static Real	ErrorZero;

	public:
		Vector()									{ dim=0; }
		Vector( int n, double d0, ... );
		Vector( const Vector &v );
#if (Vec2IsVector==0)
		Vector( const Vec2 &v );
#endif
#if (Vec3IsVector==0)
		Vector( const Vec3 &v );
#endif
		~Vector()								{ if (dim)	delete [] data; }

	void show(const char *str=0) const;

	protected:
		void Resize(int i);
		Real &GetRef(int i);
	public:
		const Real &operator()(int i) const
				{ return (i>=0&&i<dim)?data[i]:ErrorZero; }
		Real &operator[](int i)
				{ return (i>=0&&i<dim)?data[i]:GetRef(i); }

		int	IsZero() const;

		Real SqrNorm() const;
		Real Norm() const;
		Vector Norm1() const;


	const Vector& operator=(const Vector &v);		// Copy
	const Vector& operator<<=(Vector &v);			// Move

	inline Vector operator+(const Vector&) const;
	inline Vector operator-(const Vector&) const;

	friend Real operator*(const Vector&, const Vector&);
	inline Vector operator*(const Real&) const;
	friend inline Vector operator*(const Real&, const Vector&);
	inline Vector operator/(const Real&) const;

	friend int operator==(const Vector&, const Vector&);
	friend inline int operator!=(const Vector&, const Vector&);
	const Vector& operator+=(const Vector&);
	const Vector& operator-=(const Vector&);
	const Vector& operator*=(const Real&);
	const Vector& operator/=(const Real&);
	inline Vector operator+() const;
	inline Vector operator-() const;


friend class Matrix;
	friend Vector operator*(const class Matrix& m, const Vector& v);
	friend Vector operator*(const Vector& v, const class Matrix& m);
	friend int system_calc(class Matrix &a, Vector *x, Vector &b);
};

inline Vector Vector::operator+() const
{
	return *this;
}
inline Vector Vector::operator-() const
{
Vector	v(*this);
	return v*=Real(-1.0);
}


// Definitions of non-member binary operator functions

inline Vector Vector::operator+(const Vector& z2) const
{
	if (dim>z2.dim) {
		Vector	v(*this);
					return v+=z2;
	}
	else {
		Vector	v(z2);
					return v+=*this;
	}
}
inline Vector Vector::operator-(const Vector& z2) const
{
Vector	v(*this);
			return v-=z2;
}

inline Vector Vector::operator*(const Real& r) const
{
Vector	erg(*this);
			erg *= r;
			return erg;
}

inline Vector Vector::operator/(const Real& r) const
{
Vector	erg(*this);
			erg /= r;
			return erg;
}


inline Vector operator*(const Real& val, const Vector& z2)
{
		return z2*val;		// kommutativ
}

inline int operator!=(const Vector& z1, const Vector& z2)
{
		return !(z1==z2);
}

inline Real Vector::Norm() const					{ return sqrt(SqrNorm()); }
inline Vector Vector::Norm1() const				{ return *this / Norm(); }

extern Vector VectorZero;

#endif