ug4
math_vector_functions.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 ��7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., N��gel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__COMMON__MATH_VECTOR_FUNCTIONS__
34 #define __H__COMMON__MATH_VECTOR_FUNCTIONS__
35 
36 #include "math_vector.h"
37 
38 namespace ug
39 {
40 
43 
46 
49 template <typename vector_target_t, typename vector_source_t>
50 void VecCopy(vector_target_t& target, const vector_source_t& source,
52 
54 // Append Vector
55 
57 // vOut += v1
58 template <typename vector_t>
59 inline
60 void
61 VecAppend(vector_t& vOut, const vector_t& v1);
62 
64 // vOut = v1 + v2
65 template <typename vector_t>
66 inline
67 void
68 VecAppend(vector_t& vOut, const vector_t& v1, const vector_t& v2);
69 
71 // vOut += v1 + v2 + v3
72 template <typename vector_t>
73 inline
74 void
75 VecAppend(vector_t& vOut, const vector_t& v1, const vector_t& v2,
76  const vector_t& v3);
77 
79 // vOut += v1 + v2 + v3 + v4
80 template <typename vector_t>
81 inline
82 void
83 VecAppend(vector_t& vOut, const vector_t& v1, const vector_t& v2,
84  const vector_t& v3, const vector_t& v4);
85 
87 // Scale and Append Vectors
88 
90 // vOut += s1*v1
91 template <typename vector_t>
92 inline
93 void
94 VecScaleAppend(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1);
95 
97 // vOut += s1*v1 + s2*v2
98 template <typename vector_t>
99 inline
100 void
101 VecScaleAppend(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
102  typename vector_t::value_type s2, const vector_t& v2);
103 
105 // vOut += s1*v1 + s2*v2 + s3*v3
106 template <typename vector_t>
107 inline
108 void
109 VecScaleAppend(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
110  typename vector_t::value_type s2, const vector_t& v2,
111  typename vector_t::value_type s3, const vector_t& v3);
112 
114 // vOut += s1*v1 + s2*v2 + s3*v3 + s4*v4
115 template <typename vector_t>
116 inline
117 void
118 VecScaleAppend(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
119  typename vector_t::value_type s2, const vector_t& v2,
120  typename vector_t::value_type s3, const vector_t& v3,
121  typename vector_t::value_type s4, const vector_t& v4);
122 
123 
125 // Addition of Vectors
126 
128 // vOut = v1 + v2
129 template <typename vector_t>
130 inline
131 void
132 VecAdd(vector_t& vOut, const vector_t& v1, const vector_t& v2);
133 
135 // vOut = v1 + v2 + v3
136 template <typename vector_t>
137 inline
138 void
139 VecAdd(vector_t& vOut, const vector_t& v1, const vector_t& v2,
140  const vector_t& v3);
141 
143 // vOut = v1 + v2 + v3 + v4
144 template <typename vector_t>
145 inline
146 void
147 VecAdd(vector_t& vOut, const vector_t& v1, const vector_t& v2,
148  const vector_t& v3, const vector_t& v4);
149 
151 // Subtraction of Vectors
152 
154 // vOut = v1 - v2
155 template <typename vector_t>
156 inline
157 void
158 VecSubtract(vector_t& vOut, const vector_t& v1, const vector_t& v2);
159 
161 // Component-wise exponentiation of a vector
162 template <typename vector_t>
163 inline
164 void
165 VecPow(vector_t& vOut, const vector_t& v1, typename vector_t::value_type s);
166 
167 
169 // Scaling of a Vector
170 
172 // vOut = s * v
173 template <typename vector_t>
174 inline
175 void
176 VecScale(vector_t& vOut, const vector_t& v, typename vector_t::value_type s);
177 
179 // Scaled Addition of Vectors
180 
182 // vOut = s1*v1 + s2*v2
183 template <typename vector_t>
184 inline
185 void
186 VecScaleAdd(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
187  typename vector_t::value_type s2, const vector_t& v2);
188 
190 // vOut = s1*v1 + s2*v2 + s3*v3
191 template <typename vector_t>
192 inline
193 void
194 VecScaleAdd(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
195  typename vector_t::value_type s2, const vector_t& v2,
196  typename vector_t::value_type s3, const vector_t& v3);
197 
199 // vOut = s1*v1 + s2*v2 + s3*v3 + s4*v4
200 template <typename vector_t>
201 inline
202 void
203 VecScaleAdd(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
204  typename vector_t::value_type s2, const vector_t& v2,
205  typename vector_t::value_type s3, const vector_t& v3,
206  typename vector_t::value_type s4, const vector_t& v4);
207 
209 // Interpolation of Vectors
210 
211 // performs linear interpolation between two Vectors.
212 template <typename vector_t>
213 inline
214 void
215 VecInterpolateLinear(vector_t& vOut, const vector_t& v1, const vector_t& v2,
216  typename vector_t::value_type interpAmount);
217 
219 // Length of Vectors
220 
222 template <typename vector_t>
223 inline
224 typename vector_t::value_type
225 VecLengthSq(const vector_t& v);
226 
228 template <typename vector_t>
229 inline
230 typename vector_t::value_type
231 VecLength(const vector_t& v);
232 
234 // Distance of Vectors
235 
237 template <typename vector_t>
238 inline
239 typename vector_t::value_type
240 VecDistanceSq(const vector_t& v1, const vector_t& v2);
241 
243 template <typename vector_t>
244 inline
245 typename vector_t::value_type
246 VecDistance(const vector_t& v1, const vector_t& v2);
247 
248 
250 // Dot Product
252 template <typename vector_t>
253 inline
254 typename vector_t::value_type
255 VecDot(const vector_t& v1, const vector_t& v2);
256 
258 // Angle
260 template <typename vector_t>
261 inline
262 typename vector_t::value_type
263 VecAngle(const vector_t& v1, const vector_t& v2);
264 
266 // Angle
268 
270 template <typename vector_t>
271 inline
272 typename vector_t::value_type
273 VecAngleNorm(const vector_t& v1, const vector_t& v2);
274 
276 // Cross Product
278 template <typename vector_t>
279 inline
280 void
281 VecCross(vector_t& vOut, const vector_t& v1, const vector_t& v2);
282 
284 // Generalized cross Product
286 template <size_t dim>
287 inline void GenVecCross
288 (
289  MathVector<dim> & result,
290  const MathVector<dim> & v_1, const MathVector<dim> & v_2
291 );
292 
294 // Normalize a Vector
296 template <typename vector_t>
297 inline
298 void
299 VecNormalize(vector_t& vOut, const vector_t& v);
300 
302 // CalculateTriangleNormalNoNormalize
304 //TODO: This method should not be part of the core-math methods.
305 // Instead it should be moved to a seperate math-util file.
306 template <typename vector_t>
307 inline
308 void
309 CalculateTriangleNormalNoNormalize(vector_t& vOut, const vector_t& v1,
310  const vector_t& v2, const vector_t& v3);
311 
313 // CalculateTriangleNormal
315 //TODO: This method should not be part of the core-math methods.
316 // Instead it should be moved to a seperate math-util file.
317 template <typename vector_t>
318 inline
319 void
320 CalculateTriangleNormal(vector_t& vOut, const vector_t& v1,
321  const vector_t& v2, const vector_t& v3);
322 
323 
325 // Operations with scalar
326 
328 template <typename vector_t>
329 inline
330 void
331 VecSet(vector_t& vInOut, typename vector_t::value_type s);
332 
334 template <typename vector_t>
335 inline
336 void
337 VecAdd(vector_t& vOut, const vector_t& v, typename vector_t::value_type s);
338 
340 template <typename vector_t>
341 inline
342 void
343 VecSubtract(vector_t& vOut, const vector_t& v, typename vector_t::value_type s);
344 
346 // Norms
347 template <typename vector_t>
348 inline
349 typename vector_t::value_type
350 VecTwoNorm(const vector_t& v);
351 
352 template <typename vector_t>
353 inline
354 typename vector_t::value_type
355 VecTwoNormSq(const vector_t& v);
356 
357 template <typename vector_t>
358 inline
359 typename vector_t::value_type
360 VecOneNorm(const vector_t& v);
361 
362 template <typename vector_t>
363 inline
364 typename vector_t::value_type
365 VecPNorm(const vector_t& v, unsigned int p);
366 
367 template <typename vector_t>
368 inline
369 typename vector_t::value_type
370 VecMaxNorm(const vector_t& v);
371 
372 template <typename vector_t>
373 inline
374 typename vector_t::value_type
375 VecInftyNorm(const vector_t& v);
376 
377 // end group vectors
379 
380 
382 // Elementwise operations
383 
385 template <typename vector_t>
386 inline
387 void
388 VecElemProd(vector_t& vOut, const vector_t& v1, const vector_t& v2);
389 
391 template <typename vector_t>
392 inline
393 void
394 VecElemSqrt(vector_t& vOut, const vector_t& v1);
395 
397 template <typename vector_t>
398 inline
399 bool
400 VecAbsIsLess(const vector_t& v1, const vector_t& v2);
401 
403 template <typename vector_t>
404 inline
405 bool
406 VecAbsIsLess(const vector_t& v1, const typename vector_t::value_type s);
407 
409 template <typename vector_t>
410 inline
411 bool
412 VecIsInBB(const vector_t& v, const vector_t& low, const vector_t& high);
413 
414 }// end of namespace
415 
417 // include a general, but not very fast implementation of the declared methods above.
419 
420 #endif /* __H__COMMON__MATH_MathVector_FUNCTIONS__ */
function util fill(N, c)
vector_t::value_type VecLength(const vector_t &v)
returns the length of v. Slower than VecLengthSq.
Definition: math_vector_functions_common_impl.hpp:341
void VecScaleAppend(vector_t &vOut, typename vector_t::value_type s1, const vector_t &v1)
Scales a Vector and adds it to a second vector.
Definition: math_vector_functions_common_impl.hpp:126
void VecCopy(vector_target_t &target, const vector_source_t &source, typename vector_target_t::value_type fill)
Copy contents between vectors of possibly different types.
Definition: math_vector_functions_common_impl.hpp:56
vector_t::value_type VecMaxNorm(const vector_t &v)
Definition: math_vector_functions_common_impl.hpp:625
void GenVecCross(MathVector< dim > &result, const MathVector< dim > &v_1, const MathVector< dim > &v_2)
calculates the usual cross product in 3d, and the (det, 0) vector as a cross product in 2d
Definition: math_vector_functions_common_impl.hpp:465
void VecSet(vector_t &vInOut, typename vector_t::value_type s)
Set each vector component to scalar (componentwise)
Definition: math_vector_functions_common_impl.hpp:539
void VecScaleAdd(vector_t &vOut, typename vector_t::value_type s1, const vector_t &v1, typename vector_t::value_type s2, const vector_t &v2)
Scales two Vectors, adds them and returns the sum in a third vector.
Definition: math_vector_functions_common_impl.hpp:265
vector_t::value_type VecTwoNormSq(const vector_t &v)
Definition: math_vector_functions_common_impl.hpp:585
vector_t::value_type VecOneNorm(const vector_t &v)
Definition: math_vector_functions_common_impl.hpp:593
void VecNormalize(vector_t &vOut, const vector_t &v)
scales a vector_t to unit length
Definition: math_vector_functions_common_impl.hpp:501
void CalculateTriangleNormalNoNormalize(vector_t &vOut, const vector_t &v1, const vector_t &v2, const vector_t &v3)
Calculates a triangle-normal in 3d (no normalization is performed).
Definition: math_vector_functions_common_impl.hpp:514
void VecSubtract(vector_t &vOut, const vector_t &v1, const vector_t &v2)
subtracts v2 from v1 and stores the result in a vOut
Definition: math_vector_functions_common_impl.hpp:226
vector_t::value_type VecAngle(const vector_t &v1, const vector_t &v2)
returns the angle between two vectors in radiants
Definition: math_vector_functions_common_impl.hpp:401
vector_t::value_type VecDistanceSq(const vector_t &v1, const vector_t &v2)
returns the squared distance of two vector_ts.
Definition: math_vector_functions_common_impl.hpp:351
void CalculateTriangleNormal(vector_t &vOut, const vector_t &v1, const vector_t &v2, const vector_t &v3)
Calculates a triangle-normal in 3d (output has length 1).
Definition: math_vector_functions_common_impl.hpp:528
void VecAppend(vector_t &vOut, const vector_t &v1)
adds a MathVector<N> to a second one
Definition: math_vector_functions_common_impl.hpp:72
vector_t::value_type VecLengthSq(const vector_t &v)
returns the squared length of v. Faster than VecLength.
Definition: math_vector_functions_common_impl.hpp:324
vector_t::value_type VecDistance(const vector_t &v1, const vector_t &v2)
returns the distance of two vector_ts.
Definition: math_vector_functions_common_impl.hpp:375
void VecAdd(vector_t &vOut, const vector_t &v1, const vector_t &v2)
adds two MathVector<N>s and stores the result in a third one
Definition: math_vector_functions_common_impl.hpp:185
void VecScale(vector_t &vOut, const vector_t &v, typename vector_t::value_type s)
scales a MathVector<N>
Definition: math_vector_functions_common_impl.hpp:252
vector_t::value_type VecTwoNorm(const vector_t &v)
Definition: math_vector_functions_common_impl.hpp:577
void VecCross(vector_t &vOut, const vector_t &v1, const vector_t &v2)
calculates the cross product of two Vectors of dimension 3. It makes no sense to use VecCross for vec...
Definition: math_vector_functions_common_impl.hpp:437
void VecPow(vector_t &vOut, const vector_t &v1, typename vector_t::value_type s)
component-wise exponentiation of a vector
Definition: math_vector_functions_common_impl.hpp:239
vector_t::value_type VecAngleNorm(const vector_t &v1, const vector_t &v2)
returns the angle between two vectors of length 1 in radiants
Definition: math_vector_functions_common_impl.hpp:421
void VecInterpolateLinear(vector_t &vOut, const vector_t &v1, const vector_t &v2, typename vector_t::value_type interpAmount)
Definition: math_vector_functions_common_impl.hpp:310
vector_t::value_type VecDot(const vector_t &v1, const vector_t &v2)
returns the dot-product of two vector_ts
Definition: math_vector_functions_common_impl.hpp:385
vector_t::value_type VecInftyNorm(const vector_t &v)
Definition: math_vector_functions_common_impl.hpp:641
vector_t::value_type VecPNorm(const vector_t &v, unsigned int p)
Definition: math_vector_functions_common_impl.hpp:609
size_t target(SM_edge< typename T::value_type > const &e, ug::BidirectionalMatrix< T > const &m)
Definition: bidirectional_boost.h:100
size_t source(SM_edge< typename T::value_type > const &e, ug::BidirectionalMatrix< T > const &)
Definition: bidirectional_boost.h:94
the ug namespace
bool VecIsInBB(const vector_t &v, const vector_t &low, const vector_t &high)
checks if the given point is in the bounding box given by two other points
Definition: math_vector_functions_common_impl.hpp:701
void VecElemProd(vector_t &vOut, const vector_t &v1, const vector_t &v2)
component-wise product: vOut_i = v1_i*v2_i
Definition: math_vector_functions_common_impl.hpp:651
bool VecAbsIsLess(const vector_t &v1, const vector_t &v2)
component-wise comparison of two vectors (in the absolute values)
Definition: math_vector_functions_common_impl.hpp:677
void VecElemSqrt(vector_t &vOut, const vector_t &v1)
component-wise square root:
Definition: math_vector_functions_common_impl.hpp:664
T value_type
Definition: sparsematrix_interface.h:2