-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyVector.hxx
292 lines (214 loc) · 9.3 KB
/
TinyVector.hxx
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef LINALG_FILE_TINY_VECTOR_HXX
#include "TinyVectorExpression.hxx"
namespace linalg
{
template<int n>
class TinyVectorLoop;
//! Class storing a tiny vector whose size is known at compilation time
template <class T, int m_>
class TinyVector : public TinyVectorExpression<T, m_, TinyVector<T, m_> >
{
public :
typedef T value_type;
template<int n>
friend class TinyVectorLoop;
//! threshold used to compare points
static constexpr double threshold = 1e-12;
protected:
// pointer data_;
T data_[m_];
public :
TinyVector();
explicit TinyVector(const T& a);
explicit TinyVector(const T& a, const T& b);
TinyVector(const T& a, const T& b, const T& c);
TinyVector(const T& a, const T& b, const T& c, const T& d);
TinyVector(const T& a, const T& b, const T& c, const T& d, const T& e);
template<class E>
TinyVector(const TinyVectorExpression<T, m_, E> & u);
void Init(const T & a);
void Init(const T & a, const T& b);
void Init(const T & a, const T& b, const T& c);
void Init(const T & a, const T& b, const T& c, const T& d);
void Init(const T & a, const T& b, const T& c, const T& d, const T& e);
// Basic Functions
int GetM() const;
int GetSize() const;
// Element access and affection
const T& operator()(int i) const;
T& operator()(int i);
// Convenient Functions
void Zero() ; // we put zero on coordinates
void Fill(const T& a); // all values are set to a
void WriteText(ostream& out); // the vector is written in the stream without brackets and commas
// operation this = u
template<class T1, class E>
TinyVector<T, m_>& operator =(const TinyVectorExpression<T1, m_, E> & u);
// this += u
template<class T1, class E>
TinyVector<T, m_>& operator +=(const TinyVectorExpression<T1, m_, E> & u);
// this -= u
template<class T1, class E>
TinyVector<T, m_>& operator -=(const TinyVectorExpression<T1, m_, E> & u);
// fills vector with x
TinyVector<T, m_>& operator =(const T& x);
// operation this = a*this where a is scalar
template<class T1>
TinyVector<T, m_>& operator *=( const T1& a );
// operation this = 1/a*this where a is scalar
template<class T1>
TinyVector<T, m_>& operator /=( const T1& a );
};
template<class T, int m, class E1, class E2>
bool operator==(const TinyVectorExpression<T, m, E1>& u,
const TinyVectorExpression<T, m, E2>& v);
template<class T, int m, class E1, class E2>
bool operator!=(const TinyVectorExpression<T, m, E1> & u,
const TinyVectorExpression<T, m, E2>& v);
template<class T, int m, class E1, class E2>
bool operator<=(const TinyVectorExpression<T, m, E1> & u,
const TinyVectorExpression<T, m, E2>& v);
template<class T, int m, class E1, class E2>
bool operator<(const TinyVectorExpression<T, m, E1> & u,
const TinyVectorExpression<T, m, E2>& v);
template<class T, int m, class E1, class E2>
bool operator>=(const TinyVectorExpression<int, m, E1> & u,
const TinyVectorExpression<int, m, E2>& v);
template<class T, int m, class E1, class E2>
bool operator>(const TinyVectorExpression<T, m, E1> & u,
const TinyVectorExpression<T, m, E2>& v);
template <class T, int m, class E>
ostream& operator <<(ostream& out, const TinyVectorExpression<T, m, E>& V);
template <class T, int m>
istream& operator >>(istream& out, TinyVector<T, m>& V);
//! class used for unrolling loops for operators/functions of TinyVector
template<int n>
class TinyVectorLoop
{
public :
template<int m, class T0>
static void Zero(TinyVector<T0, m>& x);
template<int m, class T0>
static void FillGreater(TinyVector<T0, m>& x, const T0& a);
template<int m, class T0>
static void InitValue(TinyVector<T0, m>& x, const T0& a);
template<int m, class T0, class T1>
static void Fill(TinyVector<T0, m>& x, const T1& a);
template<int m, class T1, class E, class T0>
static void Copy(const TinyVectorExpression<T1, m, E>& x, TinyVector<T0, m>& y);
template<int m, class T1, class E, class T0>
static void AddCopy(const TinyVectorExpression<T1, m, E>& x, TinyVector<T0, m>& y);
template<int m, class T1, class E, class T0>
static void DiffCopy(const TinyVectorExpression<T1, m, E>& x, TinyVector<T0, m>& y);
template<int m, class T0, class T1, class E0, class E1>
static bool Equal(const TinyVectorExpression<T0, m, E0>& x,
const TinyVectorExpression<T1, m, E1>& y);
template<int m, class T0, class T1, class E0, class E1>
static bool LessOrEqual(const TinyVectorExpression<T0, m, E0>& x,
const TinyVectorExpression<T1, m, E1>& y);
template<int m, class T0, class T1, class E0, class E1>
static bool Less(const TinyVectorExpression<T0, m, E0>& x,
const TinyVectorExpression<T1, m, E1>& y);
template<int m, class T0, class T1>
static void Mlt(const T1& alpha, TinyVector<T0, m>& x);
template<int m, class T0, class T1, class T2, class E0, class E1>
static void DotProd(const TinyVectorExpression<T0, m, E0>& u,
const TinyVectorExpression<T1, m, E1>& v, T2& scal);
template<int m, class T0, class T1, class E0>
static void AbsSquare(const TinyVectorExpression<T0, m, E0>& u, T1& scal);
template<int m, class T, class E>
static void Print(const TinyVectorExpression<T, m, E>& V, ostream& out);
template<int m, class T, class E>
static ostream& WriteText(const TinyVectorExpression<T, m, E>& V, ostream& out);
template<int m, class T>
static istream& ReadText(TinyVector<T, m>& V, istream& in);
};
//! Class used to terminate the loop
template<>
class TinyVectorLoop<0>
{
public :
template<int m, class T0>
static inline void Zero(TinyVector<T0, m>& x) {}
template<int m, class T0>
static inline bool IsZero(const TinyVector<T0, m>& x) { return true; }
template<int m, class T0>
static inline void FillGreater(TinyVector<T0, m>& x, const T0& a){}
template<int m, class T0>
static inline void InitValue(TinyVector<T0, m>& x, const T0& a){}
template<int m, class T0, class T1>
static inline void Fill(TinyVector<T0, m>& x, const T1& a){}
template<int m, class T1, class E, class T0>
static inline void Copy(const TinyVectorExpression<T1, m, E>& x, TinyVector<T0, m>& y) {}
template<int m, class T1, class E, class T0>
static inline void AddCopy(const TinyVectorExpression<T1, m, E>& x, TinyVector<T0, m>& y) {}
template<int m, class T1, class E, class T0>
static inline void DiffCopy(const TinyVectorExpression<T1, m, E>& x, TinyVector<T0, m>& y) {}
template<int m, class T0, class T1, class E0, class E1>
static inline bool Equal(const TinyVectorExpression<T0, m, E0>& x,
const TinyVectorExpression<T1, m, E1>& y)
{
return true;
}
template<int m, class T0, class T1, class E0, class E1>
static inline bool LessOrEqual(const TinyVectorExpression<T0, m, E0>& x,
const TinyVectorExpression<T1, m, E1>& y)
{
return true;
}
template<int m, class T0, class T1, class E0, class E1>
static inline bool Less(const TinyVectorExpression<T0, m, E0>& x,
const TinyVectorExpression<T1, m, E1>& y)
{
return false;
}
template<int m, class T0, class T1>
static inline void Mlt(const T1& alpha, TinyVector<T0, m>& x) {}
template<int m, class T0, class T1, class T2, class E0, class E1>
static inline void DotProd(const TinyVectorExpression<T0, m, E0>& u,
const TinyVectorExpression<T1, m, E1>& v, T2& scal) {}
template<int m, class T0, class T1, class E0>
static inline void AbsSquare(const TinyVectorExpression<T0, m, E0>& u, T1& scal) {}
template<int m, class T, class E>
static inline void Print(const TinyVectorExpression<T, m, E>& V, ostream& out) { }
template<int m, class T, class E>
static inline ostream& WriteText(const TinyVectorExpression<T, m, E>& V, ostream& out) { return out; }
template<int m, class T>
static inline istream& ReadText(const TinyVector<T, m>& V, istream& in) { return in; }
};
template<class T>
T absSquare(const T& x);
template<class T>
T absSquare(const complex<T>& x);
// returns (u,v)
template<class T, int m, class E1, class E2>
T DotProd(const TinyVectorExpression<T, m, E1> & u,
const TinyVectorExpression<T, m, E2> & v);
// returns (u,v) where v is complex and u real
template<class T, int m, class E1, class E2>
complex<T> DotProd(const TinyVectorExpression<T, m, E1> & u,
const TinyVectorExpression<complex<T>, m, E2> & v);
template<class T, int m, class E1, class E2>
complex<T> DotProd(const TinyVectorExpression<complex<T>, m, E1> & u,
const TinyVectorExpression<T, m, E2> & v);
template<class T>
class ClassComplexType
{
public:
typedef T Treal;
};
template<class T>
class ClassComplexType<complex<T> >
{
public:
typedef T Treal;
};
// returns || u ||^2
template<class T, int m, class E>
typename ClassComplexType<T>::Treal AbsSquare(const TinyVectorExpression<T, m, E> & u);
// returns || u ||
template<class T, int m, class E>
typename ClassComplexType<T>::Treal Norm2(const TinyVectorExpression<T, m, E> & v);
} // end namespace
#define LINALG_FILE_TINY_VECTOR_HXX
#endif