ASL  0.1.7
Advanced Simulation Library
aslVectorsDynamicLengthOperations.h
Go to the documentation of this file.
1 /*
2  * Advanced Simulation Library <http://asl.org.il>
3  *
4  * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5  *
6  *
7  * This file is part of Advanced Simulation Library (ASL).
8  *
9  * ASL is free software: you can redistribute it and/or modify it
10  * under the terms of the GNU Affero General Public License as
11  * published by the Free Software Foundation, version 3 of the License.
12  *
13  * ASL is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 
25 
26 #ifndef ASLVECTORSDYNAMICLENGTHOPERATIONS_H
27 #define ASLVECTORSDYNAMICLENGTHOPERATIONS_H
28 
29 
30 #include "../aslUtilities.h"
31 #include <cmath>
32 
33 
34 namespace asl
35 {
36 
38  template <typename T1, typename T2>
39  inline bool compatibleSizes(AVec<T1> a, AVec<T2> b);
40 
42  template <typename T>
43  inline const T l2(const AVec<T> & a);
44 
46  template <typename T>
47  inline const AVec<> normalize(const AVec<T> & a);
48 
50  template <typename T>
51  inline const AVec<T> operator+(const AVec<T> & a, const AVec<T> & b);
53  template <typename T>
54  inline const AVec<T> operator-(const AVec<T> & a, const AVec<T> & b);
56  template <typename T>
57  inline const AVec<T> operator-(const AVec<T> & a);
59  template <typename T>
60  inline const T operator*(const AVec<T> & a, const AVec<T> & b);
62  template <typename T>
63  inline const AVec<T> operator*(const T & a, const AVec<T> & b);
65  template <typename T>
66  inline const AVec<T> operator*(const AVec<T> & a, const T & b);
68  template <typename T>
69  inline const AVec<T> operator/(const AVec<T> & a, const T & b);
70 
72  template <typename T> inline const AVec<T> & operator+=(AVec<T> & a,
73  const AVec<T> & b);
75  template <typename T> inline const AVec<T> & operator-=(AVec<T> & a,
76  const AVec<T> & b);
78  template <typename T> inline const AVec<T> & operator*=(AVec<T> & a,
79  const T & b);
80 
82  template <typename T> inline const bool operator==(const AVec<T> & a,
83  const AVec<T> & b);
84 
85 
87  template <typename T> inline const bool operator!=(const AVec<T> & a,
88  const AVec<T> & b);
89 
90 
92  template <typename T>
93  inline const AVec<T> crossProduct(const AVec<T> & a, const AVec<T> & b);
94 
96  template <typename T> inline const T minComponent(const AVec<T> & a);
98  template <typename T> inline const T maxComponent(const AVec<T> & a);
99 
101  template <typename T> inline T sumOfElements(const AVec<T> & a);
103  template <typename T> inline T productOfElements(const AVec<T> & a);
104 
106  template <typename T> inline const AVec<T> productOfElements(const AVec<T> & a,
107  const AVec<T> & b);
109  template <typename T> inline const AVec<T> divisionOfElements(const AVec<T> & a,
110  const AVec<T> & b);
111 
113  template <typename T> inline const bool positive(const AVec<T> & a);
114 
116  template <typename T> inline const bool nonNegative(const AVec<T> & a);
117 
119  inline const AVec<int> floor(const AVec<> & a);
120 
122  inline const AVec<int> round(const AVec<> & a);
123 
125 
129  inline double computePolynom(double x, AVec<> &coefs);
130 
132  template <typename T>
133  inline std::ostream & operator<<(std::ostream & output, const AVec<T> & a);
134 
136  inline const AVec<> swapXZ(const AVec<> & a);
137 
138 //---------------- Implementation ----------------
139 
140  template <typename T1, typename T2>
142  {
143  return a.getSize() == b.getSize();
144  }
145 
146 
147  template <typename T> inline const T l2(const AVec<T> & a)
148  {
149  return a * a;
150  }
151 
152  template <typename T> inline const AVec<> normalize(const AVec<T> & a)
153  {
154  return AVec<>(a) / sqrt(l2(a));
155  }
156 
157  template <typename T> inline const AVec<T> operator+(const AVec<T> & a,
158  const AVec<T> & b){
159  if (!compatibleSizes(a, b))
160  errorMessage("(AVec; operator+) Vector sizes are incompatible");
161  AVec<T> c(a.getSize());
162  for (unsigned int i(0); i < a.getSize(); ++i)
163  c[i] = a[i] + b[i];
164  return c;
165  }
166 
167  template <typename T> inline const AVec<T> operator-(const AVec<T> & a, const AVec<T> & b)
168  {
169  if (!compatibleSizes (a,b))
170  errorMessage("(AVec; operator-) Vector sizes are incompatible");
171  AVec<T> c(a.getSize());
172  for (unsigned int i(0); i < a.getSize(); ++i)
173  c[i] =a[i]-b[i];
174  return c;
175  }
176 
177  template <typename T> inline const AVec<T> operator-(const AVec<T> & a)
178  {
179  AVec<T> c(a.getSize());
180  for (unsigned int i(0); i < a.getSize(); ++i)
181  c[i] =-a[i];
182  return c;
183  }
184 
185  template <typename T> inline const T operator*(const AVec<T> & a, const AVec<T> & b){
186  if (!compatibleSizes (a,b))
187  errorMessage("(AVec; operator*) Vector sizes are incompatible");
188  T s(0);
189  for (unsigned int i(0); i < a.getSize(); ++i)
190  s+=a[i]*b[i];
191  return s;
192  }
193 
194  template <typename T> inline const AVec<T> operator*(const AVec<T> & a, const T & b){
195  AVec<T> c(a.getSize());
196  for (unsigned int i(0); i < a.getSize(); ++i)
197  c[i] =a[i]*b;
198  return c;
199  }
200 
201  template <typename T> inline const AVec<T> operator*(const T & a, const AVec<T> & b){
202  return b*a;
203  }
204 
205  template <typename T> inline const AVec<T> operator/(const AVec<T> & a, const T & b){
206  AVec<T> c(a.getSize());
207  for (unsigned int i(0); i < a.getSize(); ++i)
208  c[i] =a[i]/b;
209  return c;
210  }
211 
212  template <typename T> inline const AVec<T> & operator+=(AVec<T> & a, const AVec<T> & b){
213  if (!compatibleSizes (a,b))
214  errorMessage("Vector sizes are incompatible");
215  for (unsigned int i(0); i < a.getSize(); ++i)
216  a[i]+=b[i];
217  return a;
218  }
219 
220  template <typename T> inline const AVec<T> & operator-=(AVec<T> & a, const AVec<T> & b){
221  if (!compatibleSizes (a,b))
222  errorMessage("Vector sizes are incompatible");
223  for (unsigned int i(0); i < a.size; ++i)
224  a[i]-=b[i];
225  return a;
226  }
227 
228  template <typename T> inline const AVec<T> & operator*=(AVec<T> & a, const T & b){
229  for (unsigned int i(0); i < a.getSize(); ++i)
230  a[i] *= b;
231  return a;
232  }
233 
234  template <typename T> inline const bool operator==(const AVec<T> & a,
235  const AVec<T> & b)
236  {
237  if (!compatibleSizes (a, b))
238  return false;
239  bool c(true);
240  for (unsigned int i(0); i < a.getSize(); ++i)
241  c = c && (a[i] == b[i]);
242  return c;
243  }
244 
245 
246  template <typename T> inline const bool operator!=(const AVec<T> & a,
247  const AVec<T> & b)
248  {
249  return !(a == b);
250  }
251 
252  template <typename T>
253  inline const AVec<T> crossProduct(const AVec<T> & a, const AVec<T> & b)
254  {
255  if (!compatibleSizes (a,b))
256  errorMessage("(AVec; crossProduct) Vector sizes are incompatible");
257  if (a.getSize()>3)
258  errorMessage("(AVec; crossProduct) number of components is more than 3");
259  if (a.getSize()<2)
260  errorMessage("(AVec; crossProduct) number of components is less than 2");
261 
262  AVec<T> res(1);
263  if(a.getSize() == 2)
264  {
265  res[0] = a[0]*b[1]-a[1]*b[0];
266  }
267  if(a.getSize() == 3)
268  {
269  res.resize(3);
270  res[0] = a[1]*b[2]-a[2]*b[1];
271  res[1] = a[2]*b[0]-a[0]*b[2];
272  res[2] = a[0]*b[1]-a[1]*b[0];
273  }
274  return res;
275  }
276 
277  template <typename T> inline const T minComponent(const AVec<T> & a)
278  {
279  T ma(a[0]);
280  for (unsigned int i(1); i < a.getSize(); ++i)
281  ma = std::min(ma, a[i]);
282  return ma;
283  }
284 
285  template <typename T> inline const T maxComponent(const AVec<T> & a)
286  {
287  if (a.getSize()<1) errorMessage("Vector size less than 1");
288  T ma(a[0]);
289  for (unsigned int i(1); i < a.getSize(); ++i)
290  ma=std::max(ma,a[i]);
291  return ma;
292  }
293 
294  template <typename T> inline AVec<T> subAVec(const AVec<T> & source,
295  unsigned int start, unsigned int end)
296  {
297  if (source.getSize() <= end )
298  errorMessage("subAVec: attempt to copy besides the vector range");
299 
300  AVec<T> destination(1 + end - start);
301  for (unsigned int i(start); i <= end; ++i)
302  destination[i - start] = source[i];
303  return destination;
304  }
305 
306  template <typename T> inline T sumOfElements(const AVec<T> & a)
307  {
308  T s(0);
309  for (unsigned int i(0); i < a.getSize(); ++i)
310  s += a[i];
311  return s;
312  }
313 
314  template <typename T> inline T productOfElements(const AVec<T> & a)
315  {
316  T p(1);
317  for (unsigned int i(0); i < a.getSize(); ++i)
318  p *= a[i];
319  return p;
320  }
321 
322  template <typename T> inline const AVec<T> productOfElements(const AVec<T> & a,
323  const AVec<T> & b)
324  {
325  if (!compatibleSizes (a, b))
326  errorMessage("(AVec; productOfElements) Vector sizes are incompatible");
327  AVec<T> c(a.getSize());
328  for (unsigned int i(0); i < a.getSize(); ++i)
329  c[i] = a[i] * b[i];
330  return c;
331  }
332 
333  template <typename T> inline const AVec<T> divisionOfElements(const AVec<T> & a, const AVec<T> & b)
334  {
335  if (!compatibleSizes (a, b))
336  errorMessage("(AVec; divisionOfElements) Vector sizes are incompatible");
337 
338  AVec<T> c(a.getSize());
339  for (unsigned int i(0); i < a.getSize(); ++i)
340  c[i] = a[i] / b[i];
341 
342  return c;
343  }
344 
345  template <typename T> inline const bool positive(const AVec<T> & a)
346  {
347  if (!a.getSize())
348  errorMessage("(AVec; positive) Vector size is zero");
349  bool res(a[0]>0);
350  for (unsigned int i(1); i < a.getSize(); ++i)
351  res &= a[i]>0;
352  return res;
353  }
354 
355  template <typename T> inline const bool nonNegative(const AVec<T> & a)
356  {
357  if (!a.getSize())
358  errorMessage("(AVec; positive) Vector size is zero");
359  bool res(a[0]>=0);
360  for (unsigned int i(1); i < a.getSize(); ++i)
361  res &= a[i]>=0;
362  return res;
363  }
364 
365  inline const AVec<int> floor(const AVec<> & a)
366  {
367  if (!a.getSize())
368  errorMessage("(AVec; floor) Vector size is zero");
369  AVec<int> res(a.getSize());
370  for (unsigned int i(0); i < a.getSize(); ++i)
371  res[i] = std::floor(a[i]);
372  return res;
373  }
374 
375  inline const AVec<int> round(const AVec<> & a)
376  {
377  if (!a.getSize())
378  errorMessage("(AVec; round) Vector size is zero");
379  AVec<int> res(a.getSize());
380  for (unsigned int i(0); i < a.getSize(); ++i)
381  res[i] = std::round(a[i]);
382  return res;
383  }
384 
385 
386  inline double computePolynom (double x, AVec<> &coefs)
387  {
388  if (coefs.getSize() < 1)
389  errorMessage("Error: (asl::computePolynom) size of \"coefs\" less than 1");
390  double p;
391  p=coefs[0];
392  for (unsigned int i(1); i < coefs.getSize(); ++i)
393  p=p*x+coefs[i];
394  return p;
395  }
396 
398  inline const AVec<> swapXZ(const AVec<> & a)
399  {
400  if (a.getSize()<3)
401  errorMessage("(AVec; swapXZ) Vector size less than 3");
402  AVec<> res(a);
403  std::swap(res[0],res[2]);
404  return res;
405  }
406 
407 } // asl
408 
409 #endif //ASLVECTORSDYNAMICLENGTH_H
bool compatibleSizes(AVec< T1 > a, AVec< T2 > b)
double computePolynom(double x, AVec<> &coefs)
const AVec< T > divisionOfElements(const AVec< T > &a, const AVec< T > &b)
Advanced Simulation Library.
Definition: aslDataInc.h:30
const AVec< T > & operator*=(AVec< T > &a, const T &b)
SPDistanceFunction normalize(SPDistanceFunction a, double dx)
void errorMessage(cl_int status, const char *errorMessage)
Prints errorMessage and exits depending on the status.
const AMatr< T > & operator+=(AMatr< T > &a, const AMatr< T > &b)
Definition: aslMatrices.h:245
const AVec< T > crossProduct(const AVec< T > &a, const AVec< T > &b)
Element max(Element a, Element b)
const T l2(const AVec< T > &a)
Element min(Element a, Element b)
bool operator==(const std::vector< T > &vector1, const std::vector< T > &vector2)
Compares two vectors.
Definition: aslUtilities.h:185
AVec< T > subAVec(const AVec< T > &source, unsigned int start, unsigned int end)
T productOfElements(const AVec< T > &a)
const T maxComponent(const AVec< T > &a)
const bool nonNegative(const AVec< T > &a)
std::ostream & operator<<(std::ostream &output, const std::vector< T > &vector)
Prints elements of the vector separated by space.
Definition: aslUtilities.h:173
const AVec< int > floor(const AVec<> &a)
T sumOfElements(const AVec< T > &a)
const AMatr< T > operator/(const AMatr< T > &b, const T &a)
Definition: aslMatrices.h:264
Element sqrt(Element e)
const AVec< T > & operator-=(AVec< T > &a, const AVec< T > &b)
SPPositionFunction operator*(SPPositionFunction a, SPPositionFunction b)
const AMatr< T > operator+(const AMatr< T > &a, const AMatr< T > &b)
Definition: aslMatrices.h:252
const T minComponent(const AVec< T > &a)
SPDistanceFunction operator-(SPDistanceFunction a)
const unsigned int & getSize() const
const bool positive(const AVec< T > &a)
const AVec swapXZ(const AVec<> &a)
returns true in case when all components of a more then 0
void resize(unsigned int newSize)
const AVec< int > round(const AVec<> &a)
const bool operator!=(const AVec< T > &a, const AVec< T > &b)