jmath.h 20.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
/***************************************************************************
 *   Copyright (C) 2005 by Jeff Ferr                                       *
 *   root@sat                                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 a***************************************************************************/
#ifndef J_MATH_H
#define J_MATH_H

#include "jobject.h"

#include <string>

#include <stdint.h>
#include <math.h>

namespace jmath {

/**
 * \brief
 * 
 * \author Jeff Ferr
 */
template<class T>class Math : public virtual jcommon::Object{

	public:
		// Fundamental Constants

		/** \brief PI  */
		static double PI;
		/** \brief  Base of natural log */
		static double e;
		/** \brief  Natural log of 10 */
		static double ln10;
		/** \brief  Log of e */
		static double logE;
		/** \brief  Velocity of light (m s^-1) */
		static double C;
		/** \brief  Error */
		static double Cuncertainty;
		/** \brief  Gravitational constant (m^3 kg^-1 s^-1) */
		static double G;
		/** \brief  Error */
		static double Guncertainty;
		/** \brief (GeV/c^-2)^-2 */
	 	static double GhbarC;
		/** \brief  Error */
		static double GhbarCuncertainty;
		/** \brief Standard acceleration of gravity (m s^-2) */
		static double Gn;
		/** \brief  Error */
		static double GnUncertainty;
		/** \brief Planck's constant (J s) */
		static double H;
		/** \brief   */
		static double HUncertainty;
		/** \brief h-bar (h over 2 pi) (J s) */
		static double Hbar;
		/** \brief  Error */
		static double HbarUncertainty;
		/** \brief Boltzmann's constant (J K^-1) */
		static double K;
		/** \brief   */
		static double KUncertainty;
		/** \brief Stefan-Boltzmann constant (W m^-2 K^-4) */
		static double Sigma;
		/** \brief  Error */
		static double SigmaUncertainty;
		/** \brief Avogadro constant (Avogadro's Number) (mol^-1) */
		static double Na;
		/** \brief  Error */
		static double NaUncertainty;
		/** \brief universal gas constant (Na * K) (J K^-1 mol^-1) */
		static double R;
		/** \brief   */
		static double RUncertainty;
		/** \brief Molecular weight of dry air (kg kmol^-1 (or gm mol^-1)) */
		static double MWair;
		/** \brief Dry Air Gas Constant (R / MWair) (J kg^-1 K^-1) */
		static double Rgair;
		/** \brief Elementary charge (C) */
		static double Qe;
		/** \brief  Error */
		static double QeUncertainty;
		
		/**
 		 * \brief Round to nearest integer. Rounds half integers to the nearest even integer.
		 *
		 */
		static int Nint(T x);
		
		/**
		 * \brief Verify finite
		 *
		 */
		static int Finite(double x);
		
		/**
		 * \brief Verify not a number
		 *
		 */
		static int IsNaN(double x)
		{
			return isnan(x); 
		}
		
		/**
		 * \brief
		 *
		 */
		static double Sin(double x)
		{
			return sin(x);
		}
		
		/**
		 * \brief
		 *
		 */
		static double Cos(double x)
		{
			return cos(x);
		}
		
		/**
		 * \brief
		 *
		 */
		static double Tan(double x)
		{
			return tan(x);
		}
		
		/**
		 * \brief
		 *
		 */
		static double SinH(double x)
		{
   		return sinh(x); 
		}
		
		/**
		 * \brief
		 *
		 */
		static double CosH(double x)
		{
   		return cosh(x); 
		}
		
		/**
		 * \brief
		 *
		 */
		static double TanH(double x)
		{
   		return tanh(x); 
		}
		
		/**
		 * \brief
		 *
		 */
		static double ASin(double x)
		{
			if (x < -1.) 
				return -Math<T>::PI/2.0;
			
			if (x >  1.) 
				return  Math<T>::PI/2.0;

			return asin(x);
		}
		
		/**
		 * \brief
		 *
		 */
		static double ACos(double x)
		{
			if (x < -1.) 
				return Math<T>::PI;
			
			if (x >  1.) 
				return 0;
			
			return acos(x);
		}
		
		/**
		 * \brief
		 *
		 */
		static double ATan(double x)
		{
   		return atan(x); 
		}
		
		/**
		 * \brief
		 *
		 */
		static double ATan2(double x, double y)
		{
			if (x != 0) 
				return  atan2(y, x);
			if (y == 0) 
				return  0;
			if (y >  0) 
				return  PI/2.0;
			
			return -PI/2.0;
		}
		
		/**
		 * \brief
		 *
		 */
		static double ASinH(double);
		
		/**
		 * \brief
		 *
		 */
		static double ACosH(double);
		
		/**
		 * \brief
		 *
		 */
		static double ATanH(double);
		
		/**
		 * \brief
		 *
		 */
		static double Hypot(double x, double y)
		{
			return hypot(x, y);
		}
		
		/**
		 * \brief
		 *
		 */
		static double Sqrt(double x)
		{
			return sqrt(x);
		}
		
		/**
		 * \brief
		 *
		 */
		static double Ceil(double x)
		{
   		return ceil(x); 
		}
		
		/**
		 * \brief
		 *
		 */
		static int CeilNint(double x)
		{
   		return Math<T>::Nint(ceil(x)); 
		}
		
		/**
		 * \brief
		 *
		 */
		static double Floor(double x)
		{
			return floor(x);
		}
		
		/**
		 * \brief
		 *
		 */
		static int FloorNint(double x)
		{
   		return Math<T>::Nint(floor(x)); 
		}
		
		/**
		 * \brief
		 *
		 */
		static double Exp(double x)
		{
			return exp(x);
		}
		
		/**
		 * \brief
		 *
		 */
		static double Ldexp(double x, int exp)
		{
   		return ldexp(x, exp); 
		}
		
		/**
		 * \brief
		 *
		 */
		static double Factorial(int i);
		
		/**
		 * \brief
		 *
		 */
		static double Power(double x, double y)
		{
   		return pow(x, y); 
		}
		
		/**
		 * \brief
		 *
		 */
		static double Log(double x)
		{
			return log(x);
		}
		
		/**
		 * \brief
		 *
		 */
		static double Log2(double x)
		{
  		return log(x)/log(2.0);
		}
		
		/**
		 * \brief
		 *
		 */
		static double Log10(double x)
		{
   		return log10(x); 
		}
		
		/**
		 * brief Return next prime number after x, unless x is a prime in which case x is returned.
		 *
		 */
		static int64_t NextPrime(int64_t x);
		
		/**
		 * \brief sqrt (px*px + py*py)
		 *
		 */
		static int64_t Hypot(int64_t x, int64_t y); 
		
		/**
		 * \brief Absolute value
		 *
		 */
		static T Abs(T d)
		{
   		return (d >= 0) ? d : -d; 
		}
		
		/** 
		 * \brief Sign value
		 *
		 */
		static T Sign(T a, T b)
		{
   		return (b >= 0) ? Abs(a) : -Abs(a); 
		}
		
		/**
		 * \brief Even
		 *
		 */
		static bool Even(int64_t a)
		{
			return ! (a & 1); 
		}
		
		/**
		 * \brief Odd
		 *
		 */
		static bool Odd(int64_t a)
		{
   		return (a & 1); 
		}
		
		/**
		 * \brief Min of two scalars
		 *
		 */
		static T Min(T a, T b)
		{
			return a <= b ? a : b; 
		}

		/**
		 * \brief Max of two scalars
		 *
		 */
		static T Max(T a, T b)
		{
			return a >= b ? a : b; 
		}
		
		/**
		 * \brief Min of an array
		 *
		 */
		static T MinElement(int64_t n, const T *a);
		
		/**
		 * \brief Max of an array
		 *
		 */
		static T MaxElement(int64_t n, const T *a);
		
		/**
		 * \brief Locate Min element number in an array
		 *
		 */
		static int64_t LocMin(int64_t n, const T *a);
		
		/**
		 * \brief Locate Max element number in an array
		 *
		 */
		static int64_t LocMax(int64_t n, const T *a);
		
		/**
		 * \brief Mean
		 *
		 */
		static double Mean(int64_t n, const T *a, const double *w = 0);
		
		/**
		 * \brief Geometric Mean
		 *
		 */
		static double GeometricMean(int64_t n, const T *a);
		
		/**
		 * \brief RMS
		 *
		 */
		static double  RMS(int64_t n, const T *a);
		
		/**
		 * \brief Return the median of the array a where each entry i has weight w[i].
		 * Both arrays have a length of at least n . The median is a number obtained
		 * from the sorted array a through median = (a[jl]+a[jh])/2.
		 * Where (using also the sorted index on the array w)
		 * sum_i=0,jl w[i] <= sumTot/2
		 * sum_i=0,jh w[i] >= sumTot/2
		 * sumTot = sum_i=0,n w[i]
		 *
		 * If w=0, the algorithm defaults to the median definition where it is
		 * a number that divides the sorted sequence into 2 halves.
		 * When n is odd or n > 1000, the median is kth element k = (n + 1) / 2.
		 * when n is even and n < 1000the median is a mean of the elements k = n/2 and k = n/2 + 1.
		 * If work is supplied, it is used to store the sorting index and assumed to be
		 * >= n . If work=0, local storage is used, either on the stack if n < kWorkMax
		 * or on the heap for n >= kWorkMax.
		 *
		 */
		static double  Median(int64_t n, const T *a,  const double *w=0, int64_t *work = 0);
		
		/**
		 * \brief
		 *
		 */
		static T KOrdStat(int64_t n, const T *a,  int64_t k, int64_t *work = 0);
		
		/**
		 * \brief Computes sample quantiles, corresponding to the given probabilities
		 * Parameters:
		 * x -the data sample
		 * n - its size
		 * quantiles - computed quantiles are returned in there
		 * prob - probabilities where to compute quantiles
		 * nprob - size of prob array
		 * isSorted - is the input array x sorted?
		 *
		 * NOTE, that when the input is not sorted, an array of integers of size n needs
		 * to be allocated. It can be passed by the user in parameter index,
		 * or, if not passed, it will be allocated inside the function
		 *
		 * type - method to compute (from 1 to 9). Following types are provided:
		 * Discontinuous:
		 * type=1 - inverse of the empirical distribution function
		 * type=2 - like type 1, but with averaging at discontinuities
		 * type=3 - SAS definition: nearest even order statistic
		 * Piecwise linear continuous:
		 * In this case, sample quantiles can be obtained by linear interpolation
		 * between the k-th order statistic and p(k).
		 * type=4 - linear interpolation of empirical cdf, p(k)=k/n;
		 * type=5 - a very popular definition, p(k) = (k-0.5)/n;
		 * type=6 - used by Minitab and SPSS, p(k) = k/(n+1);
		 * type=7 - used by S-Plus and R, p(k) = (k-1)/(n-1);
		 * type=8 - resulting sample quantiles are approximately median unbiased
		 * regardless of the distribution of x. p(k) = (k-1/3)/(n+1/3);
		 * type=9 - resulting sample quantiles are approximately unbiased, when
		 * the sample comes from Normal distribution. p(k)=(k-3/8)/(n+1/4);
		 * default type = 7
		 *
		 */
		static void Quantiles(int n, int nprob, double *x, double *quantiles, double *prob, bool isSorted=true, int *index = 0, int type = 7);
		
		/**
		 * \brief Range
		 *
		 */
		static T Range(T lb, T ub, T x)
		{
   		return x < lb ? lb : (x > ub ? ub : x); 
		}
		
		/**
		 * \brief Binary search
		 *
		 */
		static int64_t BinarySearch(int64_t n, const T *array, T value);
		
		/**
		 * \brief Binary search
		 *
		 */
		static int64_t BinarySearch(int64_t n, const T **array, T value);
		
		/**
		 * \brief Hashing
		 *
		 */
		static uint64_t Hash(const void *txt, int ntxt);
		
		/**
		 * \brief Hashing
		 *
		 */
		static uint64_t Hash(const char *str);
		
		/**
		 * brief IsInside
		 *
		 */
		static bool IsInside(T xp, T yp, int np, T *x, T *y);
		
		/**
		 * \brief Sorting
		 *
		 */
		static void Sort(int n,    const T *a,  int *index,    bool down=true);
		
		/**
		 * \brief Sorting
		 *
		 */
		static void Sort(int64_t n, const T *a,  int64_t *index, bool down=true);

		/**
		 * \brief Bubble Sorting
		 *
		 */
		static void BubbleHigh(int Narr, double *arr1, int *arr2);
		
		/**
		 * \brief Bubble Sorting
		 *
		 */
		static void BubbleLow (int Narr, double *arr1, int *arr2);
		
		/**
		 * \brief Calculate the cross product of two vectors
		 *
		 */
		static T * Cross(const T v1[3], const T v2[3], T out[3]);

		/**
		 * \brief Normalize a vector
		 *
		 */
	   	static float Normalize(float v[3]);

		/**
		 * \brief Normalize a vector
		 *
		 */
	   	static double Normalize(double v[3]);

		/**
		 * \brief Calculate the normalized cross product of two vectors
		 *
		 */
		static T NormCross(const T v1[3], const T v2[3], T out[3]); 

		/**
		 * \brief Calculate a normal vector of a plane
		 *
		 */
		static T * Normal2Plane(const T v1[3], const T v2[3], const T v3[3], T normal[3]);
		
		/**
		 * \brief Roots cubic
		 *
		 */
		static bool RootsCubic(const double coef[4], double &a, double &b, double &c);
		
		/**
		 * \brief
		 *
		 */
	  	static double  BreitWigner(double x, double mean = 0, double gamma = 1);
		
		/**
		 * \brief
		 *
		 */
		static double  Gaussian(double x, double mean = 0, double sigma = 1, bool norm = false);
		
		/**
		 * \brief The LANDAU function with mpv(most probable value) and sigma.
		 * This function has been adapted from the CERNLIB routine G110 denlan.
		 *
		 */
  		static double  Landau(double x, double mpv = 0, double sigma = 1, bool norm = false);
		
		/**
		 * \brief Computation of Voigt function (normalised).
		 * Voigt is a convolution of gauss(xx) = 1/(sqrt(2*pi)*sigma) * exp(xx*xx/(2*sigma*sigma)
		 * and lorentz(xx) = (1/pi) * (lg/2) / (xx*xx + g*g/4) functions.
		 *
		 * The Voigt function is known to be the real part of Faddeeva function also
		 * called complex error function [2].
		 * The algoritm was developed by J. Humlicek [1]. This code is based on 
		 * fortran code presented by R. J. Wells [2]. Translated and adapted by Miha D. Puc.
		 *
		 * To calculate the Faddeeva function with relative error less than 10^(-r).
		 * r can be set by the the user subject to the constraints 2 <= r <= 5. 
		 *
		 */
		static double  Voigt(double x, double sigma, double lg, int R = 4);
		
		/**
		 * \brief Bessel functions
		 *
		 */
		static double BesselI(int n,double x);  // integer order modified Bessel function I_n(x)
		
		/**
		 * \brief Integer order modified Bessel function K_n(x)
		 *
		 */
		static double BesselK(int n,double x); 
		
		/**
		 * \brief Modified Bessel function I_0(x)
		 *
		 */
	   	static double BesselI0(double x);
		
		/**
		 * \brief modified Bessel function K_0(x)
		 *
		 */
	 	static double BesselK0(double x);
   		
		/**
		 * \brief modified Bessel function I_1(x)
		 *
		 */
		static double BesselI1(double x);
 		
		/**
		 * \brief modified Bessel function K_1(x)
		 *
		 */
		static double BesselK1(double x);
		
		/**
		 * \brief Bessel function J0(x) for any real x
		 *
		 */
		static double BesselJ0(double x);
		
		/**
		 * \brief Bessel function J1(x) for any real x
		 *
		 */
		static double BesselJ1(double x);
		
		/**
		 * \brief Bessel function Y0(x) for positive x
		 *
		 */
		static double BesselY0(double x);
		
		/**
		 * \brief Bessel function Y1(x) for positive x
		 *
		 */
		static double BesselY1(double x);
		
		/**
		 * \brief Struve functions of order 0
		 *
		 */
		static double StruveH0(double x);
		
		/**
		 * \brief Struve functions of order 1
		 *
		 */
		static double StruveH1(double x);
		
		/**
		 * \brief Modified Struve functions of order 0
		 *
		 */
		static double StruveL0(double x);
		
		/**
		 * \brief Modified Struve functions of order 1
		 *
		 */
		static double StruveL1(double x);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double Beta(double p, double q);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double BetaCf(double x, double a, double b);
		
		/**
		 * \brief Statistics
		 *
		 */
	   	static double BetaDist(double x, double p, double q);
		
		/**
		 * \brief Statistics
		 *
		 */
	 	static double BetaDistI(double x, double p, double q);
		
		/**
		 * \brief Statistics
		 *
		 */
   		static double BetaIncomplete(double x, double a, double b);
		
		/**
		 * \brief Statistics
		 *
		 */
 		static double Binomial(int n,int k);  // Calculate the binomial coefficient n over k
		
		/**
		 * \brief Statistics
		 *
		 */
		static double BinomialI(double p, int n, int k);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double CauchyDist(double x, double t=0, double s=1);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double ChisquareQuantile(double p, double ndf);
		
		/**
		 * \brief The DiLogarithm function.
		 * Code translated by R.Brun from CERNLIB DILOG function C332.
		 *
		 */
		static double DiLog(double x);
		
		/**
		 * \brief Computation of the error function erf(x).
		 *
		 */
		static double Erf(double x);
		
		/**
		 * \brief Returns inverse error function.
		 *
		 */
		static double ErfInverse(double x);
		
		/**
		 * \brief Compute the complementary error function erfc(x).
		 *
		 */
		static double Erfc(double x);
		
		/**
		 * \brief Statistics
		 *
		 */
	 	static double ErfcInverse(double x) {
			return ErfInverse(1.0 - x);
		}
		
		/**
		 * \brief Statistics
		 *
		 */
   		static double FDist(double F, double N, double M);
		
		/**
		 * \brief Statistics
		 *
		 */
 		static double FDistI(double F, double N, double M);
		
		/**
		 * \brief Computation of the normal frequency function freq(x).
		 *
		 */
		static double Freq(double x);
		
		/**
		 * \brief Computation of gamma(z) for all z>0.
		 *
		 */
		static double Gamma(double z);
		
		/**
		 * \brief Computation of the upper incomplete gamma function P(a,x) as defined in the
		 * Handbook of Mathematical Functions by Abramowitz and Stegun, formula 6.5.1 on page 260.
		 * 
		 * Note that this is the version of the incomplete gamma function as used in statistics :
		 * its normalization is such that Math<T>::Gamma(a,+infinity) = 1.
		 *
		 */
		static double Gamma(double a,double x);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double GammaDist(double x, double gamma, double mu=0, double beta=1);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double GamCf(double a, double x);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double GamSer(double a, double x);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double KolmogorovProb(double z);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double LandauI(double x);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double LaplaceDist(double x, double alpha=0, double beta=1);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double LaplaceDistI(double x, double alpha=0, double beta=1);
		
		/**
		 * \brief Computation of ln[gamma(z)] for all z>0.
		 * 
		 * C.Lanczos, SIAM Journal of Numerical Analysis B1 (1964), 86.
		 * The accuracy of the result is better than 2e-10.
		 * Nve 14-nov-1998 UU-SAP Utrecht
		 * 
		 */
		static double LnGamma(double z)
		{
			if (z<=0) return 0;
			
			// Coefficients for the series expansion
			double c[7] = { 2.5066282746310005, 76.18009172947146, -86.50532032941677
					,24.01409824083091,  -1.231739572450155, 0.1208650973866179e-2
					,-0.5395239384953e-5};
			
			double x   = z;
			double y   = x;
			double tmp = x+5.5;
			tmp = (x+0.5)*Log(tmp)-tmp;
			double ser = 1.000000000190015;
			for (int i=1; i<7; i++) {
				y   += 1;
				ser += c[i]/y;
			}
			double v = tmp+Log(c[0]*ser/x);
			return v;
		}
		
		/**
		 * \brief Statistics
		 *
		 */
		static double LogNormal(double x, double sigma, double theta=0, double m=1);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double NormQuantile(double p);
		
		/**
		 * \brief Statistics
		 *
		 */
		static bool   Permute(int n, int *a); // Find permutations
		
		/**
		 * \brief Compute the Poisson distribution function for (x,par).
		 * The Poisson PDF is implemented by means of Euler's Gamma-function
		 * (for the factorial), so for all integer arguments it is correct.
		 * BUT for non-integer values it IS NOT equal to the Poisson distribution.
		 *
		 */
		static double Poisson(double x, double par);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double PoissonI(double x, double par);
		
		/**
		 * \brief Computation of the probability for a certain Chi-squared (chi2)
		 * and number of degrees of freedom (ndf).
		 * Calculations are based on the incomplete gamma function P(a,x), where a=ndf/2 and x=chi2/2.
		 *
		 * P(a,x) represents the probability that the observed Chi-squared
		 * for a correct model should be less than the value chi2.
		 *
		 * The returned probability corresponds to 1-P(a,x), which denotes the probability
		 * that an observed Chi-squared exceeds the value chi2 by chance, even for a correct model.
		 *
		 */
		static double Prob(double chi2,int ndf);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double Student(double t, double ndf);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double StudentI(double t, double ndf);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double StudentQuantile(double p, double ndf, bool lower_tail=true);
		
		/**
		 * \brief Statistics
		 *
		 */
		void VavilovSet(double rkappa, double beta2, bool mode, double *WCM, double *AC, double *HC, int &itype, int &npt);
		
		/**
		 * \brief Statistics
		 *
		 */
		double VavilovDenEval(double rlam, double *AC, double *HC, int itype);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double Vavilov(double x, double kappa, double beta2);
		
		/**
		 * \brief Statistics
		 *
		 */
		static double VavilovI(double x, double kappa, double beta2);

};

}

#endif