OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
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\
13template <typename orig, typename exp1, typename exp2>\
14class 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\
20public:\
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 typedef typename first_or_second_pt<has_coordtype<exp1>::value,exp1,exp2>::coord_type coord_type;\
33 \
34\
35 __device__ __host__ inline explicit point_expression_op(const exp1 & o1)\
36 :o1(o1),scal(0)\
37 {}\
38\
39 __device__ __host__ inline void init() const\
40 {\
41 o1.init();\
42 }\
43\
44 template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > __device__ __host__ inline r_type value(size_t k) const\
45 {\
46 return fun_base(o1.value(k));\
47 }\
48\
49 template <typename T> __device__ __host__ operator T() const\
50 {\
51 init();\
52 return fun_base(o1.value(0));\
53 }\
54};
55
62#define CREATE_ARG_FUNC(fun_base,fun_name,OP_ID) \
63\
64template<typename orig,typename exp1, typename exp_2, unsigned int op1>\
65__device__ __host__ inline point_expression_op<orig,point_expression_op<orig,exp1,exp_2,op1>,void, OP_ID >\
66fun_name(const point_expression_op<orig,exp1,exp_2,op1> & va)\
67{\
68 point_expression_op<orig,point_expression_op<orig,exp1,exp_2,op1>,void,OP_ID> exp_sum(va);\
69\
70 return exp_sum;\
71}\
72\
73\
74template<unsigned int dim, typename T>\
75__device__ __host__ inline point_expression_op<Point<dim,T>,Point<dim,T>,void, OP_ID >\
76fun_name(const Point<dim,T> & va)\
77{\
78 point_expression_op<Point<dim,T>,Point<dim,T>,void,OP_ID> exp_sum(va);\
79\
80 return exp_sum;\
81}
82
91template <typename orig, typename exp1, typename exp2>
92class point_expression_op<orig,exp1,exp2,POINT_NORM_INF>
93{
94 const exp1 o1;
95
97 mutable typename std::remove_const<typename orig::coord_type>::type scal;
98
99public:
100
102 typedef orig orig_type;
103
105 typedef int is_expression;
106
108 typedef int has_init;
109
111 typedef typename orig::coord_type return_type;
112
114 static const unsigned int nvals = 1;
115
116 typedef typename exp1::coord_type coord_type;
117
119 __device__ __host__ inline explicit point_expression_op(const exp1 & o1)
120 :o1(o1),scal(0.0)
121 {}
122
128 __device__ __host__ inline void init() const
129 {
130 scal = 0.0;
131
132 for (size_t i = 0 ; i < orig::dims ; i++) {
133 if (fabs(o1.value(i)) > scal) {
134 scal=fabs(o1.value(i));
135 }
136 }
137 //scal += o1.value(i) * o1.value(i);
138
139 //scal = sqrt(scal);
140
141 o1.init();
142 }
143
149 template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > __device__ __host__ inline r_type value(size_t k) const
150 {
151 return scal;
152 }
153
154 template <typename T>__device__ __host__ operator T() const
155 {
156 init();
157 return scal;
158 }
159};
160
161
170template <typename orig, typename exp1, typename exp2>
172{
173 const exp1 o1;
174
176 mutable typename std::remove_const<typename orig::coord_type>::type scal;
177
178public:
179
181 typedef orig orig_type;
182
184 typedef int is_expression;
185
187 typedef int has_init;
188
190 typedef typename orig::coord_type return_type;
191
193 static const unsigned int nvals = 1;
194
195 typedef typename exp1::coord_type coord_type;
196
198 __device__ __host__ inline explicit point_expression_op(const exp1 & o1)
199 :o1(o1),scal(0.0)
200 {}
201
207 __device__ __host__ inline void init() const
208 {
209 scal = 0.0;
210
211 for (size_t i = 0 ; i < orig::dims ; i++)
212 scal += o1.value(i) * o1.value(i);
213
214 scal = sqrt(scal);
215
216 o1.init();
217 }
218
224 template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > __device__ __host__ inline r_type value(size_t k) const
225 {
226 return scal;
227 }
228
229 template <typename T>__device__ __host__ operator T() const
230 {
231 init();
232 return scal;
233 }
234};
235
244template <typename orig, typename exp1, typename exp2>
246{
248 const exp1 o1;
249
251 mutable typename std::remove_const<typename orig::coord_type>::type scal;
252
253public:
254
255 typedef orig orig_type;
256
258 typedef int has_init;
259
261 typedef int is_expression;
262
264 typedef typename orig::coord_type return_type;
265
267 static const unsigned int nvals = 1;
268
269 typedef typename exp1::coord_type coord_type;
270
272 __device__ __host__ inline explicit point_expression_op(const exp1 & o1)
273 :o1(o1),scal(0.0)
274 {}
275
281 __device__ __host__ inline void init() const
282 {
283 scal = 0.0;
284
285 for (size_t i = 0 ; i < orig::dims ; i++)
286 scal += o1.value(i) * o1.value(i);
287
288 o1.init();
289 }
290
296 template<typename r_type=typename std::remove_reference<decltype(o1.value(0))>::type > __device__ __host__ inline r_type value(size_t k) const
297 {
298 return scal;
299 }
300
302 template <typename T> __device__ __host__ operator T() const
303 {
304 init();
305 return scal;
306 }
307};
308
309
310CREATE_ARG_FUNC_CLASS(std::abs,abs,POINT_ABS)
311CREATE_ARG_FUNC_CLASS(std::exp,exp,POINT_EXP)
312CREATE_ARG_FUNC_CLASS(std::exp2,exp2,POINT_EXP2)
313CREATE_ARG_FUNC_CLASS(std::expm1,expm1,POINT_EXPM1)
314CREATE_ARG_FUNC_CLASS(std::log,log,POINT_LOG)
315CREATE_ARG_FUNC_CLASS(std::log10,log10,POINT_LOG10)
316CREATE_ARG_FUNC_CLASS(std::log2,log2,POINT_LOG2)
317CREATE_ARG_FUNC_CLASS(std::log1p,log1p,POINT_LOG1P)
318CREATE_ARG_FUNC_CLASS(std::sqrt,sqrt,POINT_SQRT)
319CREATE_ARG_FUNC_CLASS(std::cbrt,cbrt,POINT_CBRT)
320CREATE_ARG_FUNC_CLASS(std::sin,sin,POINT_SIN)
321CREATE_ARG_FUNC_CLASS(std::cos,cos,POINT_COS)
322CREATE_ARG_FUNC_CLASS(std::tan,tan,POINT_TAN)
323CREATE_ARG_FUNC_CLASS(std::asin,asin,POINT_ASIN)
324CREATE_ARG_FUNC_CLASS(std::acos,acos,POINT_ACOS)
325CREATE_ARG_FUNC_CLASS(std::atan,atan,POINT_ATAN)
326CREATE_ARG_FUNC_CLASS(std::sinh,sinh,POINT_SINH)
327CREATE_ARG_FUNC_CLASS(std::cosh,cosh,POINT_COSH)
328CREATE_ARG_FUNC_CLASS(std::tanh,tanh,POINT_TANH)
329CREATE_ARG_FUNC_CLASS(std::asinh,asinh,POINT_ASINH)
330CREATE_ARG_FUNC_CLASS(std::acosh,acosh,POINT_ACOSH)
331CREATE_ARG_FUNC_CLASS(std::atanh,atanh,POINT_ATANH)
332CREATE_ARG_FUNC_CLASS(std::erf,erf,POINT_ERF)
333CREATE_ARG_FUNC_CLASS(std::erfc,erfc,POINT_ERFC)
334CREATE_ARG_FUNC_CLASS(std::tgamma,tgamma,POINT_TGAMMA)
335CREATE_ARG_FUNC_CLASS(std::lgamma,lgamma,POINT_LGAMMA)
336CREATE_ARG_FUNC_CLASS(std::ceil,ceil,POINT_CEIL)
337CREATE_ARG_FUNC_CLASS(std::floor,floor,POINT_FLOOR)
338CREATE_ARG_FUNC_CLASS(std::trunc,trunc,POINT_TRUNC)
339CREATE_ARG_FUNC_CLASS(std::round,round,POINT_ROUND)
340CREATE_ARG_FUNC_CLASS(std::nearbyint,nearbyint,POINT_NEARBYINT)
341CREATE_ARG_FUNC_CLASS(std::rint,rint,POINT_RINT)
342
343
344
345/* \brief Calculate the norm of the point
346 *
347 * \param va point expression one
348 *
349 * \return an object that encapsulate the expression
350 *
351 */
352template<typename orig,typename exp1, typename exp2, unsigned int op1>
353__device__ __host__ inline point_expression_op<orig,point_expression_op<orig,exp1,exp2,op1>,void,POINT_NORM>
355{
357
358 return exp_sum;
359}
360
361
362/* \brief norm of a scalar
363 *
364 * \param d scalar double or float
365 *
366 * \return d
367 *
368 */
369template <typename T>
370__device__ __host__ T norm(T d)
371{
372 return d;
373}
374
375/* \brief Calculate the norm of the point
376 *
377 * \param va point expression one
378 *
379 * \return an object that encapsulate the expression
380 *
381 */
382template<typename T, typename vmpl>
385 void,
386 POINT_NORM>
388{
391 void,
393
394 return exp_sum;
395}
396
397/* \brief Calculate the norm of the point
398 *
399 * \param va point expression one
400 *
401 * \return an object that encapsulate the expression
402 *
403 */
404template<unsigned int dim, typename T>
405__device__ __host__ inline point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM>
406norm(const point_expression<T[dim]> & d)
407{
408 point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM> exp_sum( (point_expression<T[dim]>(d)) );
409
410 return exp_sum;
411}
412
413
414/* \brief Divide two points expression
415 *
416 * \param va point expression one
417 * \param vb point expression two
418 *
419 * \return an object that encapsulate the expression
420 *
421 */
422template<unsigned int dim, typename T>
423__device__ __host__ inline point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM>
424norm(const Point<dim,T> & va)
425{
426 point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM> exp_sum(va);
427
428 return exp_sum;
429}
430
431
433
434/* \brief Calculate the norm of the point
435 *
436 * \param va point expression one
437 *
438 * \return an object that encapsulate the expression
439 *
440 */
441template<typename orig,typename exp1, typename exp2, unsigned int op1>
442__device__ __host__ inline point_expression_op<orig,point_expression_op<orig,exp1,exp2,op1>,void,POINT_NORM2>
444{
446
447 return exp_sum;
448}
449
450
451/* \brief norm of a scalar
452 *
453 * \param d scalar double or float
454 *
455 * \return d
456 *
457 */
458template <typename T>__device__ __host__ T norm2(T d)
459{
460 return d*d;
461}
462
463
464/* \brief Calculate the norm of the point
465 *
466 * \param va point expression one
467 *
468 * \return an object that encapsulate the expression
469 *
470 */
471template<unsigned int dim, typename T>
472__device__ __host__ inline point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM2>
473norm2(const point_expression<T[dim]> & d)
474{
475 point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM2> exp_sum( (point_expression<T[dim]>(d)) );
476
477 return exp_sum;
478}
479
480
481/* \brief Divide two points expression
482 *
483 * \param va point expression one
484 * \param vb point expression two
485 *
486 * \return an object that encapsulate the expression
487 *
488 */
489template<unsigned int dim, typename T>
490__device__ __host__ inline point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM2>
491norm2(const Point<dim,T> & va)
492{
493 point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM2> exp_sum(va);
494
495 return exp_sum;
496}
497
498
503template <typename T, typename P> __device__ __host__ auto distance(T exp1, P exp2) -> decltype(norm(exp1 - exp2))
504{
505 return norm(exp1 - exp2);
506}
507
508
509
511
512/* \brief Calculate the norm of the point
513 *
514 * \param va point expression one
515 *
516 * \return an object that encapsulate the expression
517 *
518 */
519template<typename orig,typename exp1, typename exp2, unsigned int op1>
520__device__ __host__ inline point_expression_op<orig,point_expression_op<orig,exp1,exp2,op1>,void,POINT_NORM_INF>
522{
524
525 return exp_sum;
526}
527
528
529/* \brief norm of a scalar
530 *
531 * \param d scalar double or float
532 *
533 * \return d
534 *
535 */
536template <typename T>__device__ __host__ T norm_inf(T d)
537{
538 return abs(d);
539}
540
541
542/* \brief Calculate the norm of the point
543 *
544 * \param va point expression one
545 *
546 * \return an object that encapsulate the expression
547 *
548 */
549template<unsigned int dim, typename T>
550__device__ __host__ inline point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM_INF>
551norm_inf(const point_expression<T[dim]> & d)
552{
553 point_expression_op<Point<dim,T>,point_expression<T[dim]>,void,POINT_NORM_INF> exp_sum( (point_expression<T[dim]>(d)) );
554
555 return exp_sum;
556}
557
558
559/* \brief Divide two points expression
560 *
561 * \param va point expression one
562 * \param vb point expression two
563 *
564 * \return an object that encapsulate the expression
565 *
566 */
567template<unsigned int dim, typename T>
568__device__ __host__ inline point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM_INF>
569norm_inf(const Point<dim,T> & va)
570{
571 point_expression_op<Point<dim,T>,Point<dim,T>,void,POINT_NORM_INF> exp_sum(va);
572
573 return exp_sum;
574}
575
576
581template <typename T, typename P> __device__ __host__ auto distance_inf(T exp1, P exp2) -> decltype(norm_inf(exp1 - exp2))
582{
583 return norm_inf(exp1 - exp2);
584}
585
586
588
589CREATE_ARG_FUNC(std::abs,abs,POINT_ABS)
590CREATE_ARG_FUNC(std::exp,exp,POINT_EXP)
591CREATE_ARG_FUNC(std::exp2,exp2,POINT_EXP2)
592CREATE_ARG_FUNC(std::expm1,expm1,POINT_EXPM1)
593CREATE_ARG_FUNC(std::log,log,POINT_LOG)
594CREATE_ARG_FUNC(std::log10,log10,POINT_LOG10)
595CREATE_ARG_FUNC(std::log2,log2,POINT_LOG2)
596CREATE_ARG_FUNC(std::log1p,log1p,POINT_LOG1P)
597CREATE_ARG_FUNC(std::sqrt,sqrt,POINT_SQRT)
598CREATE_ARG_FUNC(std::cbrt,cbrt,POINT_CBRT)
599CREATE_ARG_FUNC(std::sin,sin,POINT_SIN)
600CREATE_ARG_FUNC(std::cos,cos,POINT_COS)
601CREATE_ARG_FUNC(std::tan,tan,POINT_TAN)
602CREATE_ARG_FUNC(std::asin,asin,POINT_ASIN)
603CREATE_ARG_FUNC(std::acos,acos,POINT_ACOS)
604CREATE_ARG_FUNC(std::atan,atan,POINT_ATAN)
605CREATE_ARG_FUNC(std::sinh,sinh,POINT_SINH)
606CREATE_ARG_FUNC(std::cosh,cosh,POINT_COSH)
607CREATE_ARG_FUNC(std::tanh,tanh,POINT_TANH)
608CREATE_ARG_FUNC(std::asinh,asinh,POINT_ASINH)
609CREATE_ARG_FUNC(std::acosh,acosh,POINT_ACOSH)
610CREATE_ARG_FUNC(std::atanh,atanh,POINT_ATANH)
611CREATE_ARG_FUNC(std::erf,erf,POINT_ERF)
612CREATE_ARG_FUNC(std::erfc,erfc,POINT_ERFC)
613CREATE_ARG_FUNC(std::tgamma,tgamma,POINT_TGAMMA)
614CREATE_ARG_FUNC(std::lgamma,lgamma,POINT_LGAMMA)
615CREATE_ARG_FUNC(std::ceil,ceil,POINT_CEIL)
616CREATE_ARG_FUNC(std::floor,floor,POINT_FLOOR)
617CREATE_ARG_FUNC(std::trunc,trunc,POINT_TRUNC)
618CREATE_ARG_FUNC(std::round,round,POINT_ROUND)
619CREATE_ARG_FUNC(std::nearbyint,nearbyint,POINT_NEARBYINT)
620CREATE_ARG_FUNC(std::rint,rint,POINT_RINT)
621
622
623#endif /* OPENFPM_DATA_SRC_SPACE_SHAPE_POINT_OPERATORS_FUNCTIONS_HPP_ */
Test structure used for several test.
This class implement the point shape in an N-dimensional space.
Definition Point.hpp:28
int is_expression
indicate that this class encapsulate an expression
int has_init
indicate that init must be called before value
std::remove_const< typenameorig::coord_type >::type scal
result of the expression
__device__ __host__ void init() const
This function must be called before value.
__device__ __host__ point_expression_op(const exp1 &o1)
constructor from an expression
__device__ __host__ r_type value(size_t k) const
Evaluate the expression.
orig::coord_type return_type
return type of the expression
int is_expression
indicate that this class encapsulate an expression
int has_init
indicate that init must be called before value
__device__ __host__ void init() const
This function must be called before value.
__device__ __host__ r_type value(size_t k) const
Evaluate the expression.
orig::coord_type return_type
return type of the expression
std::remove_const< typenameorig::coord_type >::type scal
Scalar value.
__device__ __host__ point_expression_op(const exp1 &o1)
Constructor from expression.
int is_expression
indicate that this class encapsulate an expression
std::remove_const< typenameorig::coord_type >::type scal
Scalar value.
int has_init
indicate that init must be called before value
__device__ __host__ r_type value(size_t k) const
Evaluate the expression.
orig::coord_type return_type
return type of the expression
__device__ __host__ point_expression_op(const exp1 &o1)
Constructor from expression.
__device__ __host__ void init() const
This function must be called before value.
Unknown operation specialization.
Main class that encapsulate a constant number used in a point expression.