Changeset dfed1c for src/base/helper.hpp
- Timestamp:
- Nov 22, 2011, 9:22:10 PM (14 years ago)
- Children:
- facba0
- Parents:
- 66f24d
- File:
-
- 1 edited
-
src/base/helper.hpp (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/base/helper.hpp
r66f24d rdfed1c 3 3 * @author Julian Iseringhausen <isering@ins.uni-bonn.de> 4 4 * @date Tue Apr 5 21:03:47 2011 5 * 5 * 6 6 * @brief Provides various helper functions. 7 * 8 * 7 * 9 8 */ 10 9 … … 15 14 #include <cstdio> 16 15 #include <limits> 16 #include <sstream> 17 #include <string> 17 18 18 19 namespace VMG 19 20 { 20 21 22 class Grid; 23 class Vector; 24 21 25 namespace Helper 22 26 { 27 char* GetCharArray(const std::string& str); 28 std::string ReplaceWhitespaces(const char* buffer, const char* replace); 29 30 template <class T> 31 std::string ToString(const T& val) 32 { 33 std::stringstream str; 34 str << std::scientific << val; 35 return str.str(); 36 } 37 38 template <class T> 39 T ToVal(const char* val_str) 40 { 41 T val; 42 std::stringstream str; 43 str << val_str; 44 str >> val; 45 46 return val; 47 } 48 49 template <class T> 50 T ToValWithDefault(const char* val_str, const T& def) 51 { 52 T val; 53 std::stringstream str(val_str); 54 str >> val; 55 56 if (str.fail() || str.bad() || !str.eof()) { 57 #ifdef DEBUG_VERBOSE 58 std::printf("VMG::Helper::ToValWithDefault: Using default value.\n"); 59 #endif 60 val = def; 61 } 62 63 return val; 64 } 23 65 24 66 /** 25 67 * Checks a number for validity, i.e. it is neither nan nor inf. 26 *27 68 */ 28 69 inline bool CheckNumber(const vmg_float& number) … … 50 91 } 51 92 52 inline int intpow( unsignedint base, unsigned int power)93 inline int intpow(int base, unsigned int power) 53 94 { 54 assert (power >= 0); 55 unsigned int result = 1; 95 int result = 1; 56 96 while (power != 0) { 57 97 if (power & 1) … … 62 102 return result; 63 103 } 104 105 inline vmg_float pow(vmg_float base, unsigned int power) 106 { 107 vmg_float result = 1.0; 108 while (power != 0) { 109 if (power & 1) 110 result *= base; 111 base *= base; 112 power >>= 1; 113 } 114 return result; 115 } 116 117 inline unsigned int fact(unsigned int number) 118 { 119 unsigned int result = 1; 120 for (unsigned int i=2; i<=number; ++i) 121 result *= i; 122 return result; 123 } 124 125 inline vmg_float pow_3(vmg_float val) 126 { 127 return val*val*val; 128 } 129 64 130 65 131 /** … … 72 138 bool rval = (val1 == val2); 73 139 140 #ifdef DEBUG_OUTPUT 74 141 if (!rval) 75 142 printf("WARNING: Values are not equal (%s)\n", name); 143 #endif /* DEBUG_OUTPUT */ 76 144 77 145 assert(rval); … … 80 148 } 81 149 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 } 150 vmg_float InterpolateTrilinear(const Vector& point, const Grid& grid); 142 151 143 152 }
Note:
See TracChangeset
for help on using the changeset viewer.
