source: src/base/helper.hpp@ 62f19a

Last change on this file since 62f19a was 62f19a, checked in by Michael Hofmann <michael.hofmann@…>, 14 years ago

vmg: minor fixes

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@942 5161e1c8-67bf-11de-9fd5-51895aff932f

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/**
2 * @file helper.hpp
3 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
4 * @date Tue Apr 5 21:03:47 2011
5 *
6 * @brief Provides various helper functions.
7 *
8 *
9 */
10
11#ifndef HELPER_HPP_
12#define HELPER_HPP_
13
14#include <cassert>
15#include <cstdio>
16#include <limits>
17
18namespace VMG
19{
20
21namespace Helper
22{
23
24 /**
25 * Checks a number for validity, i.e. it is neither nan nor inf.
26 *
27 */
28 inline bool CheckNumber(const vmg_float& number)
29 {
30 bool valid = true;
31
32 if (std::numeric_limits<vmg_float>::has_quiet_NaN) {
33 valid &= number != std::numeric_limits<vmg_float>::quiet_NaN();
34 assert(number != std::numeric_limits<vmg_float>::quiet_NaN());
35 }
36
37 if (std::numeric_limits<vmg_float>::has_signaling_NaN) {
38 valid &= number != std::numeric_limits<vmg_float>::signaling_NaN();
39 assert(number != std::numeric_limits<vmg_float>::signaling_NaN());
40 }
41
42 if (std::numeric_limits<vmg_float>::has_infinity) {
43 valid &= number != std::numeric_limits<vmg_float>::infinity();
44 valid &= number != -1 * std::numeric_limits<vmg_float>::infinity();
45 assert(number != std::numeric_limits<vmg_float>::infinity());
46 assert(number != -1 * std::numeric_limits<vmg_float>::infinity());
47 }
48
49 return valid;
50 }
51
52 inline int intpow(unsigned int base, unsigned int power)
53 {
54 assert (power >= 0);
55 unsigned int result = 1;
56 while (power != 0) {
57 if (power & 1)
58 result *= base;
59 base *= base;
60 power >>= 1;
61 }
62 return result;
63 }
64
65 /**
66 * Tests two arbitrary objects for equality and prints
67 * a warning if they differ.
68 */
69 template <class T>
70 bool IsEq(const T& val1, const T& val2, const char name[])
71 {
72 bool rval = (val1 == val2);
73
74 if (!rval)
75 printf("WARNING: Values are not equal (%s)\n", name);
76
77 assert(rval);
78
79 return rval;
80 }
81
82 inline void MM(int nn, vmg_float u, vmg_float *tbl)
83 {
84 int i, n, m;
85
86 assert(u >= 0.0 && u <= 1.0);
87
88 /* n = 1: */
89 /* i = 0: */
90 tbl[0] = 1.0;
91
92 for (n = 2; n <= nn; n++) {
93 m = n - 1;
94
95 /* i = n - 1: */
96 tbl[n-1] = (1 - u) * tbl[n-2] / m;
97
98 for (i = n - 2; i >= 1; i--)
99 tbl[i] = ((u + i) * tbl[i] + (n - (u + i)) * tbl[i-1]) / m;
100
101 /* i = 0: */
102 tbl[0] *= u / m;
103 }
104 }
105
106 inline void MdM(int nn, vmg_float u,
107 vmg_float * restrict tbl, vmg_float * restrict dtbl)
108 {
109 int i, n, m;
110
111 assert(nn >= 2);
112 assert(u >= 0.0 && u <= 1.0);
113
114 /* n = 1: */
115 /* i = 0: */
116 tbl[0] = 1.0;
117
118 for (n = 2; n <= nn; n++) {
119 m = n - 1;
120
121 /* differentiation */
122 if (n == nn) {
123 dtbl[n-1] = - tbl[n-2];
124
125 for (i = n - 2; i >= 1; i--)
126 dtbl[i] = tbl[i] - tbl[i-1];
127
128 dtbl[0] = tbl[0];
129 }
130
131 /* i = n - 1: */
132 tbl[n-1] = (1 - u) * tbl[n-2] / m;
133
134 for (i = n - 2; i >= 1; i--)
135 tbl[i] = ((u + i) * tbl[i] + (n - (u + i)) * tbl[i-1]) / m;
136
137 /* i = 0: */
138 tbl[0] *= u / m;
139
140 }
141 }
142
143}
144
145}
146
147#endif /* HELPER_HPP_ */
Note: See TracBrowser for help on using the repository browser.