VectorMath.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #define VecFunc(fnName) \
23 template<typename T, typename Allocator> \
24 inline pFlow::Vector<T, Allocator> pFlow::fnName(const Vector<T,Allocator>& v) \
25 { \
26  Vector<T, Allocator> res(v.capacity(), Logical()); \
27  for(auto& e:v) \
28  { \
29  res.push_back( fnName(e) ); \
30  } \
31  return std::move(res); \
32 } \
33 template<typename T, typename Allocator, typename indexFunc> \
34 inline pFlow::Vector<T, Allocator> pFlow::fnName(const Vector<T, Allocator>& v, indexFunc iFn) \
35 { \
36  Vector<T, Allocator> res(v.capacity(), Logical()); \
37  for(label i=0; i<v.size(); i++) \
38  { \
39  if( iFn(i) ) \
40  res.push_back(fnName(v[i])); \
41  else \
42  res.push_back(v[i]); \
43  } \
44  return std::move(res); \
45 }
46 
47 #define VecFunc2(fnName) \
48 template<typename T, typename Allocator> \
49 inline pFlow::Vector<T, Allocator> pFlow::fnName(const Vector<T, Allocator>& v1, const Vector<T, Allocator>& v2) \
50 { \
51  Vector<T, Allocator> res(v1.capacity(), Logical()); \
52  for(label i=0; i<v1.size(); i++) \
53  { \
54  res.push_back( fnName(v1[i], v2[i])); \
55  } \
56  return std::move(res); \
57 } \
58 template<typename T, typename Allocator, typename indexFunc> \
59 inline pFlow::Vector<T, Allocator> pFlow::fnName(const Vector<T, Allocator>& v1, const Vector<T, Allocator>& v2, indexFunc iFn) \
60 { \
61  Vector<T, Allocator> res(v1.capacity(), Logical()); \
62  for(label i=0; i<v1.size(); i++) \
63  { \
64  if( iFn(i) ) \
65  res.push_back(fnName(v1[i], v2[i])); \
66  else \
67  res.push_back(v1[i]); \
68  } \
69  return std::move(res); \
70 }
71 
72 
73 //* * * * * * * * * * * List of functinos * * * * * * * * //
74 // abs, mod, exp, log, log10, pow, sqrt, cbrt
75 // sin, cos, tan, asin, acos, atan, atan2
76 // sinh, cosh, tanh, asinh, acosh, atanh
77 // min, max
78 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * //
79 
80 VecFunc(abs);
81 VecFunc2(mod);
82 VecFunc(exp);
83 VecFunc(log);
84 VecFunc(log10);
85 VecFunc2(pow);
86 VecFunc(sqrt);
87 VecFunc(cbrt);
88 VecFunc(sin);
89 VecFunc(cos);
90 VecFunc(tan);
91 VecFunc(asin);
92 VecFunc(acos);
93 VecFunc(atan);
95 VecFunc(sinh);
96 VecFunc(cosh);
97 VecFunc(tanh);
98 VecFunc(asinh);
99 VecFunc(acosh);
100 VecFunc(atanh);
101 
102 #undef VecFunc
103 
105 namespace pFlow
106 {
107 
108 template<typename T, typename Allocator>
110 {
112  for(auto& elm:v)
113  {
114  res.push_back(pow(elm,e));
115  }
116  return std::move(res);
117 }
118 
119 template<typename T, typename Allocator, typename indexFunc>
120 inline Vector<T,Allocator> pow(const Vector<T,Allocator>& v, T e, indexFunc iFn)
121 {
123  for(label i=0; i<v.size(); i++)
124  {
125  if(iFn(i))
126  {
127  res.push_back(pow(v[i],e));
128  }
129  else
130  {
131  res.push_back(v[i]);
132  }
133  }
134  return std::move(res);
135 }
136 
137 template<typename T, typename Allocator>
138 inline T min(const Vector<T, Allocator>& v)
139 {
140  T minVal(largestPositive<T>());
141  for(auto& elm:v)
142  {
143  minVal = min(elm, minVal);
144  }
145  return minVal;
146 }
147 
148 template<typename T, typename Allocator, typename indexFunc>
149 inline T min(const Vector<T, Allocator>& v, indexFunc iFn)
150 {
151  T minVal(largestPositive<T>());
152  for(label i=0; i<v.size(); i++)
153  {
154  if(iFn(i))
155  {
156  minVal = min(v[i], minVal);
157  }
158 
159  }
160  return minVal;
161 }
162 
163 template<typename T, typename Allocator>
164 inline T max(const Vector<T, Allocator>& v)
165 {
166  T maxVal(largestNegative<T>());
167  for(auto& elm:v)
168  {
169  maxVal = max(elm, maxVal);
170  }
171  return maxVal;
172 }
173 
174 template<typename T, typename Allocator ,typename indexFunc>
175 inline T max(const Vector<T, Allocator>& v, indexFunc iFn)
176 {
177  T maxVal(largestNegative<T>());
178  for(label i=0; i<v.size(); i++)
179  {
180  if(iFn(i))
181  {
182  maxVal = max(v[i], maxVal);
183  }
184 
185  }
186  return maxVal;
187 }
188 
189 template<typename T, typename Allocator>
190 inline T sum(const Vector<T, Allocator>& v)
191 {
192  T s = static_cast<T>(0);
193  for(auto& elm:v)
194  {
195  s += elm;
196  }
197  return s;
198 }
199 
200 template<typename T, typename Allocator, typename indexFunc>
201 inline T sum(const Vector<T, Allocator>& v, indexFunc iFn)
202 {
203  T s = static_cast<T>(0);
204  for(label i=0; i<v.size(); ++i)
205  {
206  if(iFn(i))
207  s += v[i];
208  }
209  return s;
210 }
211 
212 }
pFlow::atan
INLINE_FUNCTION_HD real atan(real x)
Definition: math.hpp:218
VecFunc
#define VecFunc(fnName)
Definition: VectorMath.hpp:22
pFlow::atan2
INLINE_FUNCTION_HD real atan2(real y, real x)
Definition: math.hpp:229
pFlow::atanh
INLINE_FUNCTION_HD real atanh(real x)
Definition: math.hpp:286
pFlow::cos
INLINE_FUNCTION_HD real cos(real x)
Definition: math.hpp:178
pFlow::sin
INLINE_FUNCTION_HD real sin(real x)
Definition: math.hpp:168
pFlow::cosh
INLINE_FUNCTION_HD real cosh(real x)
Definition: math.hpp:249
pFlow::asin
INLINE_FUNCTION_HD real asin(real x)
Definition: math.hpp:197
pFlow::Vector::size
auto size() const
Definition: Vector.hpp:299
pFlow::log10
INLINE_FUNCTION_HD real log10(real x)
Definition: math.hpp:128
pFlow::log
INLINE_FUNCTION_HD real log(real x)
Definition: math.hpp:119
pFlow::tan
INLINE_FUNCTION_HD real tan(real x)
Definition: math.hpp:188
pFlow
Definition: demComponent.hpp:28
pFlow::sinh
INLINE_FUNCTION_HD real sinh(real x)
Definition: math.hpp:239
pFlow::Vector::capacity
auto capacity() const
Definition: Vector.hpp:304
pFlow::pow
Vector< T, Allocator > pow(const Vector< T, Allocator > &v, T e)
Definition: VectorMath.hpp:109
pFlow::pow
Vector< T, Allocator > pow(const Vector< T, Allocator > &v, T e, indexFunc iFn)
Definition: VectorMath.hpp:120
VecFunc2
#define VecFunc2(fnName)
Definition: VectorMath.hpp:47
pFlow::abs
INLINE_FUNCTION_HD real abs(real x)
Definition: math.hpp:43
pFlow::acos
INLINE_FUNCTION_HD real acos(real x)
Definition: math.hpp:208
pFlow::mod
INLINE_FUNCTION_HD real mod(real x, real y)
Definition: math.hpp:72
pFlow::tanh
INLINE_FUNCTION_HD real tanh(real x)
Definition: math.hpp:259
pFlow::exp
INLINE_FUNCTION_HD real exp(real x)
Definition: math.hpp:110
pFlow::sum
T sum(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:190
pFlow::max
T max(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:164
pFlow::asinh
INLINE_FUNCTION_HD real asinh(real x)
Definition: math.hpp:268
pFlow::Logical
Definition: Logical.hpp:35
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::acosh
INLINE_FUNCTION_HD real acosh(real x)
Definition: math.hpp:277
pFlow::sqrt
INLINE_FUNCTION_HD real sqrt(real x)
Definition: math.hpp:148
pFlow::cbrt
INLINE_FUNCTION_HD real cbrt(real x)
Definition: math.hpp:158
pFlow::Vector
Definition: Vector.hpp:46
pFlow::min
T min(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:138