MathUtils Namespace Reference

## Functions

Real round (Real x)

Real sign (Real x)

Real poly1Log (Real x, Real tol, int deriv)

Real poly2Log (Real x, Real tol, int deriv)

Real poly3Log (Real x, Real tol, int order)

Real poly4Log (Real x, Real tol, int order)

Real taylorLog (Real x)

Real pow (Real x, int e)

Real poly (std::vector< Real > c, const Real x, const bool deriv)

Real heavyside (Real x)

Real positivePart (Real x)

Real negativePart (Real x)

template<typename T , typename T2 , typename T3 , typename std::enable_if< ScalarTraits< T >::value &&ScalarTraits< T2 >::value &&ScalarTraits< T3 >::value, int >::type = 0>
void addScaled (const T &a, const T2 &b, T3 &result)

template<typename T , typename T2 , typename T3 , typename std::enable_if< ScalarTraits< T >::value, int >::type = 0>
void addScaled (const T &scalar, const NumericVector< T2 > &numeric_vector, NumericVector< T3 > &result)

template<typename T , typename T2 , template< typename > class W, template< typename > class W2, typename std::enable_if< std::is_same< typename W< T >::index_type, unsigned int >::value &&std::is_same< typename W2< T2 >::index_type, unsigned int >::value, int >::type = 0>
CompareTypes< T, T2 >::supertype dotProduct (const W< T > &a, const W2< T2 > &b)

template<typename T >
poly (std::vector< Real > c, const T x, const bool derivative)

template<typename T >
clamp (const T &x, Real lowerlimit, Real upperlimit)

template<typename T >
smootherStep (T x, T start, T end, bool derivative=false)

## Function Documentation

template<typename T , typename T2 , typename T3 , typename std::enable_if< ScalarTraits< T >::value &&ScalarTraits< T2 >::value &&ScalarTraits< T3 >::value, int >::type = 0>
 void MathUtils::addScaled ( const T & a, const T2 & b, T3 & result )

Definition at line 65 of file MathUtils.h.

66 {
67  result += a * b;
68 }

template<typename T , typename T2 , typename T3 , typename std::enable_if< ScalarTraits< T >::value, int >::type = 0>
 void MathUtils::addScaled ( const T & scalar, const NumericVector< T2 > & numeric_vector, NumericVector< T3 > & result )

Definition at line 75 of file MathUtils.h.

76 {
78 }

## ◆ clamp()

template<typename T >
 T MathUtils::clamp ( const T & x, Real lowerlimit, Real upperlimit )

Definition at line 135 of file MathUtils.h.

Referenced by smootherStep().

136 {
137  if (x < lowerlimit)
138  return lowerlimit;
139  if (x > upperlimit)
140  return upperlimit;
141  return x;
142 }
static PetscErrorCode Vec x

## ◆ dotProduct()

template<typename T , typename T2 , template< typename > class W, template< typename > class W2, typename std::enable_if< std::is_same< typename W< T >::index_type, unsigned int >::value &&std::is_same< typename W2< T2 >::index_type, unsigned int >::value, int >::type = 0>
 CompareTypes< T, T2 >::supertype MathUtils::dotProduct ( const W< T > & a, const W2< T2 > & b )

Definition at line 89 of file MathUtils.h.

90 {
91  return a * b;
92 }

## ◆ heavyside()

 Real MathUtils::heavyside ( Real x )
inline

Definition at line 43 of file MathUtils.h.

44 {
45  return x < 0.0 ? 0.0 : 1.0;
46 }
static PetscErrorCode Vec x

## ◆ negativePart()

 Real MathUtils::negativePart ( Real x )
inline

Definition at line 53 of file MathUtils.h.

54 {
55  return x < 0.0 ? x : 0.0;
56 }
static PetscErrorCode Vec x

## ◆ poly() [1/2]

 Real MathUtils::poly ( std::vector< Real > c, const Real x, const bool deriv )

## ◆ poly() [2/2]

template<typename T >
 T MathUtils::poly ( std::vector< Real > c, const T x, const bool derivative )

Definition at line 111 of file MathUtils.h.

112 {
113  const unsigned int size = c.size();
114  if (size == 0)
115  return 0.0;
116
117  T value = c[0];
118  if (derivative)
119  {
120  value *= size - 1;
121  for (unsigned int i = 1; i < size - 1; i++)
122  value = value * x + c[i] * (size - i - 1);
123  }
124  else
125  {
126  for (unsigned int i = 1; i < size; i++)
127  value = value * x + c[i];
128  }
129
130  return value;
131 }
static PetscErrorCode Vec x

## ◆ poly1Log()

 Real MathUtils::poly1Log ( Real x, Real tol, int deriv )

Definition at line 17 of file MathUtils.C.

18 {
19  Real c1 = 1.0 / tol;
20  Real c2 = std::log(tol) - 1.0;
21
22  Real value = 0.0;
23
24  if (deriv == 0)
25  {
26  if (x < tol)
27  value = c1 * x + c2;
28  else
29  value = std::log(x);
30  }
31  else if (deriv == 1)
32  {
33  if (x < tol)
34  value = c1;
35  else
36  value = 1.0 / x;
37  }
38  else if (deriv == 2)
39  {
40  if (x < tol)
41  value = 0.0;
42  else
43  value = -1.0 / (x * x);
44  }
45  else if (deriv == 3)
46  {
47  if (x < tol)
48  value = 0.0;
49  else
50  value = 2.0 / (x * x * x);
51  }
52
53  return value;
54 }
static PetscErrorCode Vec x

## ◆ poly2Log()

 Real MathUtils::poly2Log ( Real x, Real tol, int deriv )

Definition at line 57 of file MathUtils.C.

58 {
59  Real c1 = -0.5 / (tol * tol);
60  Real c2 = 2.0 / tol;
61  Real c3 = std::log(tol) - 3.0 / 2.0;
62
63  Real value = 0.0;
64
65  if (deriv == 0)
66  {
67  if (x < tol)
68  value = c1 * x * x + c2 * x + c3;
69  else
70  value = std::log(x);
71  }
72  else if (deriv == 1)
73  {
74  if (x < tol)
75  value = 2.0 * c1 * x + c2;
76  else
77  value = 1.0 / x;
78  }
79  else if (deriv == 2)
80  {
81  if (x < tol)
82  value = 2.0 * c1;
83  else
84  value = -1.0 / (x * x);
85  }
86  else if (deriv == 3)
87  {
88  if (x < tol)
89  value = 0.0;
90  else
91  value = 2.0 / (x * x * x);
92  }
93
94  return value;
95 }
static PetscErrorCode Vec x

## ◆ poly3Log()

 Real MathUtils::poly3Log ( Real x, Real tol, int order )

Definition at line 98 of file MathUtils.C.

99 {
100  Real c1 = 1.0 / (3.0 * tol * tol * tol);
101  Real c2 = -3.0 / (2.0 * tol * tol);
102  Real c3 = 3.0 / tol;
103  Real c4 = std::log(tol) - 11.0 / 6.0;
104
105  Real value = 0.0;
106
107  if (order == 0)
108  {
109  if (x < tol)
110  value = c1 * x * x * x + c2 * x * x + c3 * x + c4;
111  else
112  value = std::log(x);
113  }
114  else if (order == 1)
115  {
116  if (x < tol)
117  value = 3.0 * c1 * x * x + 2.0 * c2 * x + c3;
118  else
119  value = 1.0 / x;
120  }
121  else if (order == 2)
122  {
123  if (x < tol)
124  value = 6.0 * c1 * x + 2.0 * c2;
125  else
126  value = -1.0 / (x * x);
127  }
128  else if (order == 3)
129  {
130  if (x < tol)
131  value = 6.0 * c1;
132  else
133  value = 2.0 / (x * x * x);
134  }
135  return value;
136 }
static PetscErrorCode Vec x

## ◆ poly4Log()

 Real MathUtils::poly4Log ( Real x, Real tol, int order )

Definition at line 139 of file MathUtils.C.

140 {
141  Real value = 0.0;
142
143  if (order == 0)
144  {
145  if (x < tol)
146  value = std::log(tol) + (x - tol) / tol - (x - tol) * (x - tol) / (2.0 * tol * tol) +
147  (x - tol) * (x - tol) * (x - tol) / (3.0 * tol * tol * tol) -
148  (x - tol) * (x - tol) * (x - tol) * (x - tol) / (4.0 * tol * tol * tol * tol) +
149  (x - tol) * (x - tol) * (x - tol) * (x - tol) * (x - tol) /
150  (5.0 * tol * tol * tol * tol * tol) -
151  (x - tol) * (x - tol) * (x - tol) * (x - tol) * (x - tol) * (x - tol) /
152  (6.0 * tol * tol * tol * tol * tol * tol);
153  else
154  value = std::log(x);
155  }
156  else if (order == 1)
157  {
158  if (x < tol)
159  value = 1.0 / tol - 2.0 * (x - tol) / (2.0 * tol * tol) +
160  3.0 * (x - tol) * (x - tol) / (3.0 * tol * tol * tol) -
161  4.0 * (x - tol) * (x - tol) * (x - tol) / (4.0 * tol * tol * tol * tol) +
162  5.0 * (x - tol) * (x - tol) * (x - tol) * (x - tol) /
163  (5.0 * tol * tol * tol * tol * tol) -
164  6.0 * (x - tol) * (x - tol) * (x - tol) * (x - tol) * (x - tol) /
165  (6.0 * tol * tol * tol * tol * tol * tol);
166  else
167  value = 1.0 / x;
168  }
169  else if (order == 2)
170  {
171  if (x < tol)
172  value = -2.0 * 1.0 / (2.0 * tol * tol) + 3.0 * 2.0 * (x - tol) / (3.0 * tol * tol * tol) -
173  4.0 * 3.0 * (x - tol) * (x - tol) / (4.0 * tol * tol * tol * tol) +
174  5.0 * 4.0 * (x - tol) * (x - tol) * (x - tol) / (5.0 * tol * tol * tol * tol * tol) -
175  6.0 * 5.0 * (x - tol) * (x - tol) * (x - tol) * (x - tol) /
176  (6.0 * tol * tol * tol * tol * tol * tol);
177  else
178  value = -1.0 / (x * x);
179  }
180  else if (order == 3)
181  {
182  if (x < tol)
183  value = 3.0 * 2.0 * 1.0 / (3.0 * tol * tol * tol) -
184  4.0 * 3.0 * 2.0 * (x - tol) / (4.0 * tol * tol * tol * tol) +
185  5.0 * 4.0 * 3.0 * (x - tol) * (x - tol) / (5.0 * tol * tol * tol * tol * tol) -
186  6.0 * 5.0 * 4.0 * (x - tol) * (x - tol) * (x - tol) /
187  (6.0 * tol * tol * tol * tol * tol * tol);
188  else
189  value = 2.0 / (x * x * x);
190  }
191  return value;
192 }
static PetscErrorCode Vec x

## ◆ positivePart()

 Real MathUtils::positivePart ( Real x )
inline

Definition at line 48 of file MathUtils.h.

49 {
50  return x > 0.0 ? x : 0.0;
51 }
static PetscErrorCode Vec x

## ◆ pow()

 Real MathUtils::pow ( Real x, int e )

Definition at line 211 of file MathUtils.C.

212 {
213  bool neg = false;
214  Real result = 1.0;
215
216  if (e < 0)
217  {
218  neg = true;
219  e = -e;
220  }
221
222  while (e)
223  {
224  // if bit 0 is set multiply the current power of two factor of the exponent
225  if (e & 1)
226  result *= x;
227
228  // x is incrementally set to consecutive powers of powers of two
229  x *= x;
230
231  // bit shift the exponent down
232  e >>= 1;
233  }
234
235  return neg ? 1.0 / result : result;
236 }
static PetscErrorCode Vec x

## ◆ round()

 Real MathUtils::round ( Real x )
inline

Definition at line 22 of file MathUtils.h.

23 {
24  return ::round(x); // use round from math.h
25 }
Real round(Real x)
Definition: MathUtils.h:22
static PetscErrorCode Vec x

## ◆ sign()

 Real MathUtils::sign ( Real x )
inline

Definition at line 28 of file MathUtils.h.

Referenced by AutomaticMortarGeneration::computeNodalNormals().

29 {
30  return x >= 0.0 ? 1.0 : -1.0;
31 }
static PetscErrorCode Vec x

## ◆ smootherStep()

template<typename T >
 T MathUtils::smootherStep ( T x, T start, T end, bool derivative = false )

Definition at line 146 of file MathUtils.h.

147 {
148  if (end == start)
149  return 0.0;
150  x = clamp((x - start) / (end - start), 0.0, 1.0);
151  if (x == 0.0)
152  return 0.0;
153  if (derivative)
154  {
155  if (x == 1.0)
156  return 0.0;
157  return 30.0 * Utility::pow<2>(x) * (x * (x - 2.0) + 1.0) / (end - start);
158  }
159  if (x == 1.0)
160  return 1.0;
161  return Utility::pow<3>(x) * (x * (x * 6.0 - 15.0) + 10.0);
162 }
static PetscErrorCode Vec x
T clamp(const T &x, Real lowerlimit, Real upperlimit)
Definition: MathUtils.h:135

## ◆ taylorLog()

 Real MathUtils::taylorLog ( Real x )

Definition at line 196 of file MathUtils.C.

197 {
198  Real y = (x - 1.0) / (x + 1.0);
199  Real val = 1.0;
200  for (unsigned int i = 0; i < 5; ++i)
201  {
202  Real exponent = i + 2.0;
203  val += 1.0 / (2.0 * (i + 1.0) + 1.0) * std::pow(y, exponent);
204  }
205
206  return val * 2.0 * y;
207 }
static PetscErrorCode Vec x
Real pow(Real x, int e)
Definition: MathUtils.C:211