KrisLibrary  1.0.0
misc.h
Go to the documentation of this file.
1 #ifndef MATH_MISC_H
2 #define MATH_MISC_H
3 
4 #include "math.h"
5 
11 namespace Math {
12 
15 
18 int quadratic(double a, double b, double c, double& x1, double& x2);
21 int cubic(double a, double b, double c, double d, double x[3]);
23 double pythag(double a, double b);
25 double pythag_leg(double a,double c);
27 double Sinc(double x);
29 double Sinc_Dx(double x);
31 double dFactorial(unsigned int n);
33 double dLogFactorial(unsigned int n);
35 double dChoose(unsigned int n,unsigned int k);
37 double dLogChoose(unsigned int n,unsigned int k);
39 double TaylorCoeff(double x,unsigned int n);
41 double Gamma(double x);
43 double LogGamma(double x);
45 double GammaInv(double x);
47 double Beta(double a,double b);
49 double LogBeta(double a,double b);
51 double NormalizedIncompleteBeta(double a,double b,double x);
53 double Erf(double x);
55 double ErfComplimentary(double x);
56 
58 inline int FuzzySign(double x,double eps=dEpsilon) { return (x>eps?1:(x<-eps?-1:0)); }
61 inline bool OpposingSigns_pos(double a,double b) { return (a<dZero) != (b<dZero); }
63 inline bool OpposingSigns_neg(double a,double b) { return (a<=dZero) != (b<=dZero); }
64 
65 
66 int quadratic(float a, float b, float c, float& x1, float& x2);
67 int cubic(float a, float b, float c, float d, float x[3]);
68 float pythag(float a, float b);
69 float pythag_leg(float a,float c);
70 float Sinc(float x);
71 float Sinc_Dx(float x);
72 float fFactorial(unsigned int n);
73 float fLogFactorial(unsigned int n);
74 float fChoose(unsigned int n,unsigned int k);
75 float fLogChoose(unsigned int n,unsigned int k);
76 float TaylorCoeff(float x,unsigned int n);
77 float Gamma(float x);
78 float LogGamma(float x);
79 float GammaInv(float x);
80 float Beta(float a,float b);
81 float LogBeta(float a,float b);
82 float NormalizedIncompleteBeta(float a,float b,float x);
83 float Erf(float x);
84 float ErfComplimentary(float x);
85 
86 inline int FuzzySign(float x,float eps=fEpsilon) { return (x>eps?1:(x<-eps?-1:0)); }
87 inline bool OpposingSigns_pos(float a,float b) { return (a<fZero) != (b<fZero); }
88 inline bool OpposingSigns_neg(float a,float b) { return (a<=fZero) != (b<=fZero); }
89 
90 
91 
93 template <class T>
94 T IntegerPower(const T x, int i)
95 {
96  if(i<0) return 1.0/IntegerPower(x,-i);
97  if(i==0) return 1;
98  if(i&1) //odd
99  { T y=IntegerPower(x,(i-1)>>1); return x*y*y; }
100  else //even
101  { T y=IntegerPower(x,i>>1); return y*y; }
102 }
103 
105 inline unsigned int Factorial(unsigned int n)
106 {
107  //assert(n <= 12); //otherwise, it'll cause overflow with 32-bit arith
108  int x=1;
109  for(unsigned int i=1;i<=n;i++) x*=i;
110  return x;
111 }
112 
114 inline unsigned int FactorialTruncated(unsigned int n,unsigned int k)
115 {
116  unsigned int fact=1;
117  for(unsigned int j=0;j<k;j++) fact*=(n-j);
118  return fact;
119 }
120 
122 inline unsigned int Choose(unsigned int n,unsigned int k)
123 {
124  if(2*k > n) return FactorialTruncated(n,n-k)/Factorial(n-k);
125  else return FactorialTruncated(n,k)/Factorial(k);
126 }
127 
128 inline Real rFactorial(unsigned int n) { return dFactorial(n); }
129 inline Real LogFactorial(unsigned int n) { return dLogFactorial(n); }
130 inline Real rChoose(unsigned int n,unsigned int k) { return dChoose(n,k); }
131 inline Real LogChoose(unsigned int n,unsigned int k) { return dLogChoose(n,k); }
132 
134 } //namespace Math
135 
136 #endif
137 
int FuzzySign(double x, double eps=dEpsilon)
Returns -1 if x<eps, 0 if |x|<=eps, 1 if x>eps.
Definition: misc.h:58
Common math typedefs, constants, functions.
int cubic(double a, double b, double c, double d, double x[3])
Definition: misc.cpp:70
double dChoose(unsigned int n, unsigned int k)
Computes n choose k in double precision.
Definition: misc.cpp:166
double LogBeta(double a, double b)
Computes log B(a,b)
Definition: misc.cpp:173
double Beta(double a, double b)
Computes the beta function B(a,b)
Definition: misc.cpp:172
T IntegerPower(const T x, int i)
Calculates x^i where i is an integer (in O(log i) time)
Definition: misc.h:94
unsigned int Choose(unsigned int n, unsigned int k)
Returns `n choose k&#39; = n!/k!(n-k)! in O(k) time.
Definition: misc.h:122
double NormalizedIncompleteBeta(double a, double b, double x)
Computes the normalized incomplete beta function B(a,b,x)
Definition: misc.cpp:174
unsigned int Factorial(unsigned int n)
Returns n! in O(n) time.
Definition: misc.h:105
double Sinc(double x)
Computes the sinc function sin(x)/x (stable for small x)
Definition: misc.cpp:119
double TaylorCoeff(double x, unsigned int n)
Computes the taylor series coefficient x^n/n!
Definition: misc.cpp:168
bool OpposingSigns_neg(double a, double b)
Same as above, but treats 0 as a negative number.
Definition: misc.h:63
double Sinc_Dx(double x)
Computes the derivative of the sinc function.
Definition: misc.cpp:130
unsigned int FactorialTruncated(unsigned int n, unsigned int k)
Returns n!/(n-k)! in O(k) time.
Definition: misc.h:114
double pythag_leg(double a, double c)
Returns b where a^2+b^2=c^2.
Definition: misc.cpp:111
double dLogChoose(unsigned int n, unsigned int k)
Computes the log of n choose k.
Definition: misc.cpp:167
bool OpposingSigns_pos(double a, double b)
Definition: misc.h:61
int quadratic(double a, double b, double c, double &x1, double &x2)
Definition: misc.cpp:20
double Gamma(double x)
Computes the gamma function.
Definition: misc.cpp:169
double dLogFactorial(unsigned int n)
Computes the log of the factorial of n.
Definition: misc.cpp:165
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12
double dFactorial(unsigned int n)
Computes the factorial of n, with double precision.
Definition: misc.cpp:164
double ErfComplimentary(double x)
Computes the complimentary error function?
Definition: misc.cpp:176
double pythag(double a, double b)
Returns c where a^2+b^2=c^2.
Definition: misc.cpp:99
double Erf(double x)
Computes the error function.
Definition: misc.cpp:175
double LogGamma(double x)
Computes log gamma(x)
Definition: misc.cpp:170
double GammaInv(double x)
Computes 1 / gamma(x)
Definition: misc.cpp:171