OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
Point_operators_functions.hpp
1 /*
2  * Point_operators_functions.hpp
3  *
4  * Created on: Jun 20, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_SPACE_SHAPE_POINT_OPERATORS_FUNCTIONS_HPP_
9 #define OPENFPM_DATA_SRC_SPACE_SHAPE_POINT_OPERATORS_FUNCTIONS_HPP_
10 
11 #define CREATE_ARG_FUNC_CLASS(fun_base,fun_name,OP_ID) \
12 \
13 template <typename orig, typename exp1, typename exp2>\
14 class point_expression_op<orig,exp1,exp2, OP_ID >\
15 {\
16  const exp1 o1;\
17 \
18  mutable typename std::remove_const<typename orig::coord_type>::type scal;\
19 \
20 public:\
21 \
22  typedef orig orig_type;\
23 \
24  typedef int is_expression;\
25 \
26  typedef int has_init;\
27 \
28  typedef typename orig::coord_type return_type;\
29 \
30  static const unsigned int nvals = exp1::nvals;\
31 \
32  inline point_expression_op(const exp1 & o1)\
33  :o1(o1),scal(0)\
34  {}\
35 \
36  inline void init() const\
37  {\
38  o1.init();\
39  }\
40 \
41  template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > inline r_type value(size_t k) const\
42  {\
43  return fun_base(o1.value(k));\
44  }\
45 \
46  template <typename T>operator T() const\
47  {\
48  init();\
49  return fun_base(o1.value(0));\
50  }\
51 };
52 
59 #define CREATE_ARG_FUNC(fun_base,fun_name,OP_ID) \
60 \
61 template<typename orig,typename exp1, typename exp_2, unsigned int op1>\
62 inline point_expression_op<orig,point_expression_op<orig,exp1,exp_2,op1>,void, OP_ID >\
63 fun_name(const point_expression_op<orig,exp1,exp_2,op1> & va)\
64 {\
65  point_expression_op<orig,point_expression_op<orig,exp1,exp_2,op1>,void,OP_ID> exp_sum(va);\
66 \
67  return exp_sum;\
68 }\
69 \
70 \
71 template<unsigned int dim, typename T>\
72 inline point_expression_op<Point<dim,T>,Point<dim,T>,void, OP_ID >\
73 fun_name(const Point<dim,T> & va)\
74 {\
75  point_expression_op<Point<dim,T>,Point<dim,T>,void,OP_ID> exp_sum(va);\
76 \
77  return exp_sum;\
78 }
79 
80 
89 template <typename orig, typename exp1, typename exp2>
90 class point_expression_op<orig,exp1,exp2,POINT_NORM>
91 {
92  const exp1 o1;
93 
95  mutable typename std::remove_const<typename orig::coord_type>::type scal;
96 
97 public:
98 
100  typedef orig orig_type;
101 
103  typedef int is_expression;
104 
106  typedef int has_init;
107 
109  typedef typename orig::coord_type return_type;
110 
112  static const unsigned int nvals = 1;
113 
115  inline point_expression_op(const exp1 & o1)
116  :o1(o1),scal(0.0)
117  {}
118 
124  inline void init() const
125  {
126  scal = 0.0;
127 
128  for (size_t i = 0 ; i < orig::dims ; i++)
129  scal += o1.value(i) * o1.value(i);
130 
131  scal = sqrt(scal);
132 
133  o1.init();
134  }
135 
141  template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > inline r_type value(size_t k) const
142  {
143  return scal;
144  }
145 
146  template <typename T>operator T() const
147  {
148  init();
149  return scal;
150  }
151 };
152 
161 template <typename orig, typename exp1, typename exp2>
163 {
165  const exp1 o1;
166 
168  mutable typename std::remove_const<typename orig::coord_type>::type scal;
169 
170 public:
171 
172  typedef orig orig_type;
173 
175  typedef int has_init;
176 
178  typedef int is_expression;
179 
181  typedef typename orig::coord_type return_type;
182 
184  static const unsigned int nvals = 1;
185 
187  inline point_expression_op(const exp1 & o1)
188  :o1(o1),scal(0.0)
189  {}
190 
196  inline void init() const
197  {
198  scal = 0.0;
199 
200  for (size_t i = 0 ; i < orig::dims ; i++)
201  scal += o1.value(i) * o1.value(i);
202 
203  o1.init();
204  }
205 
211  template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > inline r_type value(size_t k) const
212  {
213  return scal;
214  }
215 
217  template <typename T>operator T() const
218  {
219  init();
220  return scal;
221  }
222 };
223 
224 
225 CREATE_ARG_FUNC_CLASS(std::abs,abs,POINT_ABS)
226 CREATE_ARG_FUNC_CLASS(std::exp,exp,POINT_EXP)
227 CREATE_ARG_FUNC_CLASS(std::exp2,exp2,POINT_EXP2)
228 CREATE_ARG_FUNC_CLASS(std::expm1,expm1,POINT_EXPM1)
229 CREATE_ARG_FUNC_CLASS(std::log,log,POINT_LOG)
230 CREATE_ARG_FUNC_CLASS(std::log10,log10,POINT_LOG10)
231 CREATE_ARG_FUNC_CLASS(std::log2,log2,POINT_LOG2)
232 CREATE_ARG_FUNC_CLASS(std::log1p,log1p,POINT_LOG1P)
233 CREATE_ARG_FUNC_CLASS(std::sqrt,sqrt,POINT_SQRT)
234 CREATE_ARG_FUNC_CLASS(std::cbrt,cbrt,POINT_CBRT)
235 CREATE_ARG_FUNC_CLASS(std::sin,sin,POINT_SIN)
236 CREATE_ARG_FUNC_CLASS(std::cos,cos,POINT_COS)
237 CREATE_ARG_FUNC_CLASS(std::tan,tan,POINT_TAN)
238 CREATE_ARG_FUNC_CLASS(std::asin,asin,POINT_ASIN)
239 CREATE_ARG_FUNC_CLASS(std::acos,acos,POINT_ACOS)
240 CREATE_ARG_FUNC_CLASS(std::atan,atan,POINT_ATAN)
241 CREATE_ARG_FUNC_CLASS(std::sinh,sinh,POINT_SINH)
242 CREATE_ARG_FUNC_CLASS(std::cosh,cosh,POINT_COSH)
243 CREATE_ARG_FUNC_CLASS(std::tanh,tanh,POINT_TANH)
244 CREATE_ARG_FUNC_CLASS(std::asinh,asinh,POINT_ASINH)
245 CREATE_ARG_FUNC_CLASS(std::acosh,acosh,POINT_ACOSH)
246 CREATE_ARG_FUNC_CLASS(std::atanh,atanh,POINT_ATANH)
247 CREATE_ARG_FUNC_CLASS(std::erf,erf,POINT_ERF)
248 CREATE_ARG_FUNC_CLASS(std::erfc,erfc,POINT_ERFC)
249 CREATE_ARG_FUNC_CLASS(std::tgamma,tgamma,POINT_TGAMMA)
250 CREATE_ARG_FUNC_CLASS(std::lgamma,lgamma,POINT_LGAMMA)
251 CREATE_ARG_FUNC_CLASS(std::ceil,ceil,POINT_CEIL)
252 CREATE_ARG_FUNC_CLASS(std::floor,floor,POINT_FLOOR)
253 CREATE_ARG_FUNC_CLASS(std::trunc,trunc,POINT_TRUNC)
254 CREATE_ARG_FUNC_CLASS(std::round,round,POINT_ROUND)
255 CREATE_ARG_FUNC_CLASS(std::nearbyint,nearbyint,POINT_NEARBYINT)
256 CREATE_ARG_FUNC_CLASS(std::rint,rint,POINT_RINT)
257 
259 
260 /* \brief Calculate the norm of the point
261  *
262  * \param va point expression one
263  *
264  * \return an object that encapsulate the expression
265  *
266  */
267 template<typename orig,typename exp1, typename exp2, unsigned int op1>
268 inline point_expression_op<orig,point_expression_op<orig,exp1,exp2,op1>,void,POINT_NORM>
269 norm(const point_expression_op<orig,exp1,exp2,op1> & va)
270 {
272 
273  return exp_sum;
274 }
275 
276 
277 /* \brief norm of a scalar
278  *
279  * \param d scalar double or float
280  *
281  * \return d
282  *
283  */
284 template <typename T>T norm(T d)
285 {
286  return d;
287 }
288 
289 
290 /* \brief Calculate the norm of the point
291  *
292  * \param va point expression one
293  *
294  * \return an object that encapsulate the expression
295  *
296  */
297 template<unsigned int dim, typename T>
299 norm(const point_expression<T[dim]> & d)
300 {
301  point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM> exp_sum( (point_expression<T[dim]>(d)) );
302 
303  return exp_sum;
304 }
305 
306 
307 /* \brief Divide two points expression
308  *
309  * \param va point expression one
310  * \param vb point expression two
311  *
312  * \return an object that encapsulate the expression
313  *
314  */
315 template<unsigned int dim, typename T>
316 inline point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM>
317 norm(const Point<dim,T> & va)
318 {
319  point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM> exp_sum(va);
320 
321  return exp_sum;
322 }
323 
324 
326 
327 /* \brief Calculate the norm of the point
328  *
329  * \param va point expression one
330  *
331  * \return an object that encapsulate the expression
332  *
333  */
334 template<typename orig,typename exp1, typename exp2, unsigned int op1>
337 {
338  point_expression_op<orig,point_expression_op<orig,exp1,exp2,op1>,void,POINT_NORM2> exp_sum(va);
339 
340  return exp_sum;
341 }
342 
343 
344 /* \brief norm of a scalar
345  *
346  * \param d scalar double or float
347  *
348  * \return d
349  *
350  */
351 template <typename T>T norm2(T d)
352 {
353  return d*d;
354 }
355 
356 
357 /* \brief Calculate the norm of the point
358  *
359  * \param va point expression one
360  *
361  * \return an object that encapsulate the expression
362  *
363  */
364 template<unsigned int dim, typename T>
365 inline point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM2>
366 norm2(const point_expression<T[dim]> & d)
367 {
368  point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM2> exp_sum( (point_expression<T[dim]>(d)) );
369 
370  return exp_sum;
371 }
372 
373 
374 /* \brief Divide two points expression
375  *
376  * \param va point expression one
377  * \param vb point expression two
378  *
379  * \return an object that encapsulate the expression
380  *
381  */
382 template<unsigned int dim, typename T>
383 inline point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM2>
384 norm2(const Point<dim,T> & va)
385 {
386  point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM2> exp_sum(va);
387 
388  return exp_sum;
389 }
390 
391 
396 template <typename T, typename P> auto distance(T exp1, P exp2) -> decltype(norm(exp1 - exp2))
397 {
398  return norm(exp1 - exp2);
399 }
400 
401 
403 
404 CREATE_ARG_FUNC(std::abs,abs,POINT_ABS)
405 CREATE_ARG_FUNC(std::exp,exp,POINT_EXP)
406 CREATE_ARG_FUNC(std::exp2,exp2,POINT_EXP2)
407 CREATE_ARG_FUNC(std::expm1,expm1,POINT_EXPM1)
408 CREATE_ARG_FUNC(std::log,log,POINT_LOG)
409 CREATE_ARG_FUNC(std::log10,log10,POINT_LOG10)
410 CREATE_ARG_FUNC(std::log2,log2,POINT_LOG2)
411 CREATE_ARG_FUNC(std::log1p,log1p,POINT_LOG1P)
412 CREATE_ARG_FUNC(std::sqrt,sqrt,POINT_SQRT)
413 CREATE_ARG_FUNC(std::cbrt,cbrt,POINT_CBRT)
414 CREATE_ARG_FUNC(std::sin,sin,POINT_SIN)
415 CREATE_ARG_FUNC(std::cos,cos,POINT_COS)
416 CREATE_ARG_FUNC(std::tan,tan,POINT_TAN)
417 CREATE_ARG_FUNC(std::asin,asin,POINT_ASIN)
418 CREATE_ARG_FUNC(std::acos,acos,POINT_ACOS)
419 CREATE_ARG_FUNC(std::atan,atan,POINT_ATAN)
420 CREATE_ARG_FUNC(std::sinh,sinh,POINT_SINH)
421 CREATE_ARG_FUNC(std::cosh,cosh,POINT_COSH)
422 CREATE_ARG_FUNC(std::tanh,tanh,POINT_TANH)
423 CREATE_ARG_FUNC(std::asinh,asinh,POINT_ASINH)
424 CREATE_ARG_FUNC(std::acosh,acosh,POINT_ACOSH)
425 CREATE_ARG_FUNC(std::atanh,atanh,POINT_ATANH)
426 CREATE_ARG_FUNC(std::erf,erf,POINT_ERF)
427 CREATE_ARG_FUNC(std::erfc,erfc,POINT_ERFC)
428 CREATE_ARG_FUNC(std::tgamma,tgamma,POINT_TGAMMA)
429 CREATE_ARG_FUNC(std::lgamma,lgamma,POINT_LGAMMA)
430 CREATE_ARG_FUNC(std::ceil,ceil,POINT_CEIL)
431 CREATE_ARG_FUNC(std::floor,floor,POINT_FLOOR)
432 CREATE_ARG_FUNC(std::trunc,trunc,POINT_TRUNC)
433 CREATE_ARG_FUNC(std::round,round,POINT_ROUND)
434 CREATE_ARG_FUNC(std::nearbyint,nearbyint,POINT_NEARBYINT)
435 CREATE_ARG_FUNC(std::rint,rint,POINT_RINT)
436 
437 
438 #endif /* OPENFPM_DATA_SRC_SPACE_SHAPE_POINT_OPERATORS_FUNCTIONS_HPP_ */
void init() const
This function must be called before value.
point_expression_op(const exp1 &o1)
Constructor from expression.
int is_expression
indicate that this class encapsulate an expression
orig::coord_type return_type
return type of the expression
Specialization for an array of dimension dim as expression.
orig::coord_type return_type
return type of the expression
Unknown operation specialization.
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
Main class that encapsulate a constant number used in a point expression.
int has_init
indicate that init must be called before value
int is_expression
indicate that this class encapsulate an expression
std::remove_const< typename orig::coord_type >::type scal
result of the expression
void init() const
This function must be called before value.
r_type value(size_t k) const
Evaluate the expression.
r_type value(size_t k) const
Evaluate the expression.
int has_init
indicate that init must be called before value
Test structure used for several test.
Definition: Point_test.hpp:105
std::remove_const< typename orig::coord_type >::type scal
Scalar value.
point_expression_op(const exp1 &o1)
constructor from an expression