www.cemf.ir
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 namespace pFlow
22 {
23 
24 #define VecFunc(fnName) \
25 template<typename T, typename Allocator> \
26 inline Vector<T, Allocator> fnName(const Vector<T,Allocator>& v) \
27 { \
28  Vector<T, Allocator> res(v.name(), v.capacity(), 0 ,RESERVE()); \
29  for(auto& e:v) \
30  { \
31  res.push_back( fnName(e) ); \
32  } \
33  return std::move(res); \
34 } \
35 template<typename T, typename Allocator, typename indexFunc> \
36 inline Vector<T, Allocator> fnName(const Vector<T, Allocator>& v, indexFunc iFn) \
37 { \
38  Vector<T, Allocator> res(v.name(), v.capacity(), 0, RESERVE()); \
39  for(size_t i=0; i<v.size(); i++) \
40  { \
41  if( iFn(i) ) \
42  res.push_back(fnName(v[i])); \
43  else \
44  res.push_back(v[i]); \
45  } \
46  return std::move(res); \
47 }
48 
49 #define VecFunc2(fnName) \
50 template<typename T, typename Allocator> \
51 inline Vector<T, Allocator> fnName(const Vector<T, Allocator>& v1, const Vector<T, Allocator>& v2) \
52 { \
53  Vector<T, Allocator> res(v1.name(), v1.capacity(), 0 ,RESERVE()); \
54  for(size_t i=0; i<v1.size(); i++) \
55  { \
56  res.push_back( fnName(v1[i], v2[i])); \
57  } \
58  return std::move(res); \
59 } \
60 template<typename T, typename Allocator, typename indexFunc> \
61 inline Vector<T, Allocator> fnName(const Vector<T, Allocator>& v1, const Vector<T, Allocator>& v2, indexFunc iFn) \
62 { \
63  Vector<T, Allocator> res(v1.name(), v1.capacity(), 0 ,RESERVE()); \
64  for(size_t i=0; i<v1.size(); i++) \
65  { \
66  if( iFn(i) ) \
67  res.push_back(fnName(v1[i], v2[i])); \
68  else \
69  res.push_back(v1[i]); \
70  } \
71  return std::move(res); \
72 }
73 
74 
75 //* * * * * * * * * * * List of functinos * * * * * * * * //
76 // abs, mod, exp, log, log10, pow, sqrt, cbrt
77 // sin, cos, tan, asin, acos, atan, atan2
78 // sinh, cosh, tanh, asinh, acosh, atanh
79 // min, max
80 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 
82 
83 
105 
106 #undef VecFunc
107 }
108 
109 
110 
112 namespace pFlow
113 {
114 
115 template<typename T, typename Allocator>
117 {
118  Vector<T, Allocator> res(v.name(), v.capacity(), 0 ,RESERVE());
119  for(auto& elm:v)
120  {
121  res.push_back(pow(elm,e));
122  }
123  return std::move(res);
124 }
125 
126 template<typename T, typename Allocator, typename indexFunc>
127 inline Vector<T,Allocator> pow(const Vector<T,Allocator>& v, T e, indexFunc iFn)
128 {
129  Vector<T, Allocator> res(v.name(), v.capacity(), 0 ,RESERVE());
130  for(size_t i=0; i<v.size(); i++)
131  {
132  if(iFn(i))
133  {
134  res.push_back(pow(v[i],e));
135  }
136  else
137  {
138  res.push_back(v[i]);
139  }
140  }
141  return std::move(res);
142 }
143 
144 template<typename T, typename Allocator>
145 inline T min(const Vector<T, Allocator>& v)
146 {
147  T minVal(largestPositive<T>());
148  for(auto& elm:v)
149  {
150  minVal = min(elm, minVal);
151  }
152  return minVal;
153 }
154 
155 template<typename T, typename Allocator, typename indexFunc>
156 inline T min(const Vector<T, Allocator>& v, indexFunc iFn)
157 {
158  T minVal(largestPositive<T>());
159  for(size_t i=0; i<v.size(); i++)
160  {
161  if(iFn(i))
162  {
163  minVal = min(v[i], minVal);
164  }
165 
166  }
167  return minVal;
168 }
169 
170 template<typename T, typename Allocator>
171 inline T max(const Vector<T, Allocator>& v)
172 {
173  T maxVal(largestNegative<T>());
174  for(auto& elm:v)
175  {
176  maxVal = max(elm, maxVal);
177  }
178  return maxVal;
179 }
180 
181 template<typename T, typename Allocator ,typename indexFunc>
182 inline T max(const Vector<T, Allocator>& v, indexFunc iFn)
183 {
184  T maxVal(largestNegative<T>());
185  for(size_t i=0; i<v.size(); i++)
186  {
187  if(iFn(i))
188  {
189  maxVal = max(v[i], maxVal);
190  }
191 
192  }
193  return maxVal;
194 }
195 
196 template<typename T, typename Allocator>
197 inline T sum(const Vector<T, Allocator>& v)
198 {
199  T s = static_cast<T>(0);
200  for(auto& elm:v)
201  {
202  s += elm;
203  }
204  return s;
205 }
206 
207 template<typename T, typename Allocator, typename indexFunc>
208 inline T sum(const Vector<T, Allocator>& v, indexFunc iFn)
209 {
210  T s = static_cast<T>(0);
211  for(size_t i=0; i<v.size(); ++i)
212  {
213  if(iFn(i))
214  s += v[i];
215  }
216  return s;
217 }
218 
219 }
pFlow::exp
Vector< T, Allocator > exp(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:86
pFlow::atan
Vector< T, Allocator > atan(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:97
pFlow::asin
Vector< T, Allocator > asin(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:95
pFlow::cbrt
Vector< T, Allocator > cbrt(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:91
VecFunc
#define VecFunc(fnName)
Definition: VectorMath.hpp:24
pFlow::sinh
Vector< T, Allocator > sinh(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:99
pFlow::pow
Vector< T, Allocator > pow(const Vector< T, Allocator > &v1, const Vector< T, Allocator > &v2)
Definition: VectorMath.hpp:89
pFlow::sqrt
Vector< T, Allocator > sqrt(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:90
pFlow::cosh
Vector< T, Allocator > cosh(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:100
pFlow::cos
Vector< T, Allocator > cos(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:93
pFlow::max
T max(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:79
pFlow::Vector::size
auto size() const
Size of the vector.
Definition: Vector.hpp:265
pFlow
Definition: demGeometry.hpp:27
pFlow::asinh
Vector< T, Allocator > asinh(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:102
RESERVE
Definition: Vector.hpp:40
pFlow::log10
Vector< T, Allocator > log10(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:88
pFlow::Vector::capacity
auto capacity() const
Capacity of the vector.
Definition: Vector.hpp:271
pFlow::log
Vector< T, Allocator > log(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:87
pFlow::mod
Vector< T, Allocator > mod(const Vector< T, Allocator > &v1, const Vector< T, Allocator > &v2)
Definition: VectorMath.hpp:85
VecFunc2
#define VecFunc2(fnName)
Definition: VectorMath.hpp:49
pFlow::abs
Vector< T, Allocator > abs(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:84
pFlow::min
T min(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:28
pFlow::sum
T sum(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:197
pFlow::sin
Vector< T, Allocator > sin(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:92
pFlow::Vector::name
const word & name() const
Name of the vector.
Definition: Vector.hpp:259
pFlow::atanh
Vector< T, Allocator > atanh(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:104
pFlow::tanh
Vector< T, Allocator > tanh(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:101
pFlow::atan2
Vector< T, Allocator > atan2(const Vector< T, Allocator > &v1, const Vector< T, Allocator > &v2)
Definition: VectorMath.hpp:98
pFlow::Vector
Definition: Vector.hpp:48
pFlow::tan
Vector< T, Allocator > tan(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:94
pFlow::acosh
Vector< T, Allocator > acosh(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:103
pFlow::acos
Vector< T, Allocator > acos(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:96