You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ABACUS_Utils.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**********************************************************
  2. This software is part of J.-S. Caux's ABACUS library.
  3. Copyright (c) J.-S. Caux.
  4. -----------------------------------------------------------
  5. File: ABACUS_util.h
  6. Purpose: Defines basic math functions.
  7. ***********************************************************/
  8. #ifndef ABACUS_UTIL_H
  9. #define ABACUS_UTIL_H
  10. #include "ABACUS.h"
  11. typedef double DP;
  12. // Global constants
  13. const double PI = 3.141592653589793238462643;
  14. const double sqrtPI = sqrt(PI);
  15. const double twoPI = 2.0*PI;
  16. const double logtwoPI = log(twoPI);
  17. const double Euler_Mascheroni = 0.577215664901532860606;
  18. const double Gamma_min_0p5 = -2.0 * sqrt(PI);
  19. const std::complex<double> II(0.0,1.0); // Shorthand for i
  20. const DP MACHINE_EPS = std::numeric_limits<DP>::epsilon();
  21. const DP MACHINE_EPS_SQ = pow(MACHINE_EPS, 2.0);
  22. // Now for some basic math utilities:
  23. namespace ABACUS {
  24. // File checks:
  25. inline bool file_exists (const char* filename)
  26. {
  27. std::fstream file;
  28. file.open(filename);
  29. bool exists = !file.fail();
  30. file.close();
  31. return(exists);
  32. }
  33. // Error handler:
  34. inline void ABACUSerror (const std::string error_text)
  35. // my error handler
  36. {
  37. std::cerr << "Run-time error... " << std::endl;
  38. std::cerr << error_text << std::endl;
  39. std::cerr << "Exiting to system..." << std::endl;
  40. exit(1);
  41. }
  42. struct Divide_by_zero {};
  43. // Basics: min, max, fabs
  44. template<class T>
  45. inline const T max (const T& a, const T& b) { return a > b ? (a) : (b); }
  46. template<class T>
  47. inline const T min (const T& a, const T& b) { return a > b ? (b) : (a); }
  48. template<class T>
  49. inline const T fabs (const T& a) { return a >= 0 ? (a) : (-a); }
  50. inline long long int pow_lli (const long long int& base, const int& exp)
  51. {
  52. long long int answer = base;
  53. if (exp == 0) answer = 1LL;
  54. else for (int i = 1; i < exp; ++i) answer *= base;
  55. return(answer);
  56. }
  57. inline unsigned long long int pow_ulli (const unsigned long long int& base, const int& exp)
  58. {
  59. unsigned long long int answer = base;
  60. if (exp == 0) answer = 1ULL;
  61. for (int i = 1; i < exp; ++i) answer *= base;
  62. return(answer);
  63. }
  64. inline int fact (const int& N)
  65. {
  66. int ans = 0;
  67. if (N < 0) {
  68. std::cerr << "Error: factorial of negative number. Exited." << std::endl;
  69. exit(1);
  70. }
  71. else if ( N == 1 || N == 0) ans = 1;
  72. else ans = N * fact(N-1);
  73. return(ans);
  74. }
  75. inline DP ln_fact (const int& N)
  76. {
  77. DP ans = 0.0;
  78. if (N < 0) {
  79. std::cerr << "Error: factorial of negative number. Exited." << std::endl;
  80. exit(1);
  81. }
  82. else if ( N == 1 || N == 0) ans = 0.0;
  83. else ans = log(DP(N)) + ln_fact(N-1);
  84. return(ans);
  85. }
  86. inline long long int fact_lli (const int& N)
  87. {
  88. long long int ans = 0;
  89. if (N < 0) {
  90. std::cerr << "Error: factorial of negative number. Exited." << std::endl;
  91. exit(1);
  92. }
  93. else if ( N == 1 || N == 0) ans = 1;
  94. else ans = fact_lli(N-1) * N;
  95. return(ans);
  96. }
  97. inline long long int fact_ulli (const int& N)
  98. {
  99. unsigned long long int ans = 0;
  100. if (N < 0) {
  101. std::cerr << "Error: factorial of negative number. Exited." << std::endl;
  102. exit(1);
  103. }
  104. else if ( N == 1 || N == 0) ans = 1;
  105. else ans = fact_ulli(N-1) * N;
  106. return(ans);
  107. }
  108. inline int choose (const int& N1, const int& N2)
  109. {
  110. // returns N1 choose N2
  111. int ans = 0;
  112. if (N1 < N2) {
  113. std::cout << "Error: N1 smaller than N2 in choose. Exited." << std::endl;
  114. exit(1);
  115. }
  116. else if (N1 == N2) ans = 1;
  117. else if (N1 < 12) ans = fact(N1)/(fact(N2) * fact(N1 - N2));
  118. else {
  119. ans = 1;
  120. int mult = N1;
  121. while (mult > max(N2, N1 - N2)) ans *= mult--;
  122. ans /= fact(min(N2, N1 - N2));
  123. }
  124. return(ans);
  125. }
  126. inline DP ln_choose (const int& N1, const int& N2)
  127. {
  128. // returns the log of N1 choose N2
  129. DP ans = 0.0;
  130. if (N1 < N2) {
  131. std::cout << "Error: N1 smaller than N2 in choose. Exited." << std::endl;
  132. exit(1);
  133. }
  134. else if (N1 == N2) ans = 0.0;
  135. else ans = ln_fact(N1) - ln_fact(N2) - ln_fact(N1 - N2);
  136. return(ans);
  137. }
  138. inline long long int choose_lli (const int& N1, const int& N2)
  139. {
  140. // returns N1 choose N2
  141. long long int ans = 0;
  142. if (N1 < N2) {
  143. std::cout << "Error: N1 smaller than N2 in choose. Exited." << std::endl;
  144. exit(1);
  145. }
  146. else if (N1 == N2) ans = 1;
  147. else if (N1 < 12) ans = fact_lli(N1)/(fact_lli(N2) * fact_lli(N1 - N2));
  148. else {
  149. // Make sure that N2 is less than or equal to N1/2; if not, just switch...
  150. int N2_min = min(N2, N1 - N2);
  151. ans = 1;
  152. for (int i = 0; i < N2_min; ++i) {
  153. ans *= (N1 - i);
  154. ans /= i + 1;
  155. }
  156. }
  157. return(ans);
  158. }
  159. inline unsigned long long int choose_ulli (const int& N1, const int& N2)
  160. {
  161. // returns N1 choose N2
  162. unsigned long long int ans = 0;
  163. if (N1 < N2) {
  164. std::cout << "Error: N1 smaller than N2 in choose. Exited." << std::endl;
  165. exit(1);
  166. }
  167. else if (N1 == N2) ans = 1;
  168. else if (N1 < 12) ans = fact_ulli(N1)/(fact_ulli(N2) * fact_ulli(N1 - N2));
  169. else {
  170. // Make sure that N2 is less than or equal to N1/2; if not, just switch...
  171. int N2_min = min(N2, N1 - N2);
  172. ans = 1;
  173. for (int i = 0; i < N2_min; ++i) {
  174. ans *= (N1 - i);
  175. ans /= i + 1;
  176. }
  177. }
  178. return(ans);
  179. }
  180. inline DP SIGN (const DP &a, const DP &b)
  181. {
  182. return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a);
  183. }
  184. inline DP sign_of (const DP& a)
  185. {
  186. return (a >= 0.0 ? 1.0 : -1.0);
  187. }
  188. inline int sgn_int (const int& a)
  189. {
  190. return (a >= 0) ? 1 : -1;
  191. }
  192. inline int sgn_DP (const DP& a)
  193. {
  194. return (a >= 0) ? 1 : -1;
  195. }
  196. template<class T>
  197. inline void SWAP (T& a, T& b) {T dum = a; a = b; b = dum;}
  198. inline int kronecker (int a, int b)
  199. {
  200. return a == b ? 1 : 0;
  201. }
  202. template<class T>
  203. inline bool is_nan (const T& a)
  204. {
  205. return(!((a < T(0.0)) || (a >= T(0.0))));
  206. }
  207. inline std::complex<DP> atan_cx(const std::complex<DP>& x)
  208. {
  209. return(-0.5 * II * log((1.0 + II* x)/(1.0 - II* x)));
  210. }
  211. /**************** Gamma function *******************/
  212. inline std::complex<double> ln_Gamma (std::complex<double> z)
  213. {
  214. // Implementation of Lanczos method with g = 9.
  215. // Coefficients from Godfrey 2001.
  216. if (real(z) < 0.5) return(log(PI/(sin(PI*z))) - ln_Gamma(1.0 - z));
  217. else {
  218. std::complex<double> series = 1.000000000000000174663
  219. + 5716.400188274341379136/z
  220. - 14815.30426768413909044/(z + 1.0)
  221. + 14291.49277657478554025/(z + 2.0)
  222. - 6348.160217641458813289/(z + 3.0)
  223. + 1301.608286058321874105/(z + 4.0)
  224. - 108.1767053514369634679/(z + 5.0)
  225. + 2.605696505611755827729/(z + 6.0)
  226. - 0.7423452510201416151527e-2 / (z + 7.0)
  227. + 0.5384136432509564062961e-7 / (z + 8.0)
  228. - 0.4023533141268236372067e-8 / (z + 9.0);
  229. return(0.5 * logtwoPI + (z - 0.5) * log(z + 8.5) - (z + 8.5) + log(series));
  230. }
  231. return(log(0.0)); // never called
  232. }
  233. inline std::complex<double> ln_Gamma_old (std::complex<double> z)
  234. {
  235. // Implementation of Lanczos method with g = 9.
  236. // Coefficients from Godfrey 2001.
  237. if (real(z) < 0.5) return(log(PI/(sin(PI*z))) - ln_Gamma(1.0 - z));
  238. else {
  239. int g = 9;
  240. double p[11] = { 1.000000000000000174663,
  241. 5716.400188274341379136,
  242. -14815.30426768413909044,
  243. 14291.49277657478554025,
  244. -6348.160217641458813289,
  245. 1301.608286058321874105,
  246. -108.1767053514369634679,
  247. 2.605696505611755827729,
  248. -0.7423452510201416151527e-2,
  249. 0.5384136432509564062961e-7,
  250. -0.4023533141268236372067e-8 };
  251. std::complex<double> z_min_1 = z - 1.0;
  252. std::complex<double> series = p[0];
  253. for (int i = 1; i < g+2; ++i)
  254. series += p[i]/(z_min_1 + std::complex<double>(i));
  255. return(0.5 * logtwoPI + (z_min_1 + 0.5) * log(z_min_1 + std::complex<double>(g) + 0.5)
  256. - (z_min_1 + std::complex<double>(g) + 0.5) + log(series));
  257. }
  258. return(log(0.0)); // never called
  259. }
  260. inline std::complex<double> ln_Gamma_2 (std::complex<double> z)
  261. {
  262. // Implementation of Lanczos method with g = 7.
  263. if (real(z) < 0.5) return(log(PI/(sin(PI*z)) - ln_Gamma(1.0 - z)));
  264. else {
  265. int g = 7;
  266. double p[9] = { 0.99999999999980993, 676.5203681218851, -1259.1392167224028,
  267. 771.32342877765313, -176.61502916214059, 12.507343278686905,
  268. -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7};
  269. std::complex<double> z_min_1 = z - 1.0;
  270. std::complex<double> series = p[0];
  271. for (int i = 1; i < g+2; ++i)
  272. series += p[i]/(z_min_1 + std::complex<double>(i));
  273. return(0.5 * logtwoPI + (z_min_1 + 0.5) * log(z_min_1 + std::complex<double>(g) + 0.5)
  274. - (z_min_1 + std::complex<double>(g) + 0.5) + log(series));
  275. }
  276. return(log(0.0)); // never called
  277. }
  278. /********** Partition numbers **********/
  279. inline long long int Partition_Function (int n)
  280. {
  281. // Returns the value of the partition function p(n), giving the number of partitions of n into integers.
  282. if (n < 0) ABACUSerror("Calling Partition_Function for n < 0.");
  283. else if (n == 0 || n == 1) return(1LL);
  284. else if (n == 2) return(2LL);
  285. else if (n == 3) return(3LL);
  286. else { // do recursion using pentagonal numbers
  287. long long int pn = 0LL;
  288. int pentnrplus, pentnrmin; // pentagonal numbers
  289. for (int i = 1; true; ++i) {
  290. pentnrplus = (i * (3*i - 1))/2;
  291. pentnrmin = (i * (3*i + 1))/2;
  292. if (n - pentnrplus >= 0) pn += (i % 2 ? 1LL : -1LL) * Partition_Function (n - pentnrplus);
  293. if (n - pentnrmin >= 0) pn += (i % 2 ? 1LL : -1LL) * Partition_Function (n - pentnrmin);
  294. else break;
  295. }
  296. return(pn);
  297. }
  298. return(-1LL); // never called
  299. }
  300. /********** Sorting **********/
  301. template <class T>
  302. void QuickSort (T* V, int l, int r)
  303. {
  304. int i = l, j = r;
  305. T pivot = V[l + (r-l)/2];
  306. while (i <= j) {
  307. while (V[i] < pivot) i++;
  308. while (V[j] > pivot) j--;
  309. if (i <= j) {
  310. std::swap(V[i],V[j]);
  311. i++;
  312. j--;
  313. }
  314. };
  315. if (l < j) QuickSort(V, l, j);
  316. if (i < r) QuickSort(V, i, r);
  317. }
  318. template <class T>
  319. void QuickSort (T* V, int* index, int l, int r)
  320. {
  321. int i = l, j = r;
  322. T pivot = V[l + (r-l)/2];
  323. while (i <= j) {
  324. while (V[i] < pivot) i++;
  325. while (V[j] > pivot) j--;
  326. if (i <= j) {
  327. std::swap(V[i],V[j]);
  328. std::swap(index[i],index[j]);
  329. i++;
  330. j--;
  331. }
  332. };
  333. if (l < j) QuickSort(V, index, l, j);
  334. if (i < r) QuickSort(V, index, i, r);
  335. }
  336. } // namespace ABACUS
  337. #endif