Bsoft  2.1.0
Bernard's software package
math_util.cpp
Go to the documentation of this file.
1 
9 #include "math_util.h"
10 #include "utilities.h"
11 
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14 
21 double bfloor(double value, int places)
22 {
23  int i;
24  double mult(1);
25 
26  if ( places < 0 ) places = 0;
27 
28  for ( i=0; i<places; i++ ) mult *= 10;
29 
30  return floor(mult*value)/mult;
31 }
32 
39 double bround(double value, int places)
40 {
41  int i;
42  double mult(1);
43 
44  if ( places < 0 ) places = 0;
45 
46  for ( i=0; i<places; i++ ) mult *= 10;
47 
48  return floor(mult*value + 0.5)/mult;
49 }
50 
66 double factorial(int n)
67 {
68  double fn;
69 
70  if ( n > 170 ) {
71  error_show("Error in factorial", __FILE__, __LINE__);
72  cerr << "Factorial of %d exceeds maximum value of a double!" << n << endl;
73  return -1;
74  }
75 
76  if ( n <= 1 ) return 1;
77 
78  if ( n > 50 ) return exp(lgamma(n+1.0L));
79 
80  for ( fn = 1; n > 1; n-- ) fn *= n;
81 
82  return fn;
83 }
84 
97 double number_of_combinations(int n, int r)
98 {
99  double nc(0);
100 
101  if ( n <= 0 ) return nc;
102 
103  if ( n > 50 ) nc = floor(0.5 + exp(lgamma(n+1.0L) - lgamma(r+1.0L) - lgamma(n-r+1.0L)));
104 
105  else nc = factorial(n)/(factorial(r)*factorial(n-r));
106 
107  return nc;
108 }
109 
123 int partition(vector<double>& a, int n, int k)
124 {
125  int i(0), j(n - 1);
126 
127  while ( i < j ) {
128  while ( i<k && a[i] <= a[k] ) i++;
129  while ( j>k && a[j] >= a[k] ) j--;
130  if ( i < j ) {
131  swap(a[i], a[j]);
132  if ( i == k || j==k ) {
133  i = 0;
134  j = n - 1;
135  }
136  }
137  }
138 
139  return 0;
140 }
141 
152 long* prime_factors(long number, long& n)
153 {
154  long divisor, pn[1024], i;
155 
156  if ( verbose & VERB_DEBUG )
157  cout << "DEBUG prime_factors: for number " << number << ": ";
158 
159  for ( n=0; number > 1; n++ ) {
160  divisor = smallest_prime(number);
161  if ( divisor < 1 ) break;
162  pn[n] = divisor;
163  number /= divisor;
164  }
165 
166  if ( n < 1 ) return NULL;
167 
168  long* prime = new long[n];
169  for ( i=0; i<n; i++ ) prime[i] = pn[i];
170 
171  if ( verbose & VERB_DEBUG ) {
172  for ( i=0; i<n; i++ ) cout << " " << pn[i];
173  cout << endl;
174  }
175 
176  return prime;
177 }
178 
191 long smallest_prime(long number)
192 {
193  if ( number < 2 ) {
194  cout << "Zero and one are neither composite nor prime numbers!" << endl;
195  return 1;
196  }
197 
198  if ( number%2 == 0 ) return 2;
199  if ( number%3 == 0 ) return 3;
200 
201  /* Primes generated by alternatingly adding 2 or 4 */
202  long i, inc(2);
203  double max = sqrt((double)number);
204  for( i=5; i<=max; i+=inc, inc=6-inc)
205  if ( number%i == 0 ) return i;
206 
207  return number;
208 }
209 
221 {
222  int i, j;
223  long len = s.length();
224 
225  for ( i=len-2; i>=0 && s[i] >= s[i+1]; i-- ) ;
226  if ( i < 0 ) return 0;
227 
228  for ( j=len-1; j>=0 && s[i] >= s[j]; j-- ) ;
229  s = s.swap(i, j);
230 
231  for ( i++, j=len-1; i<j; i++, j-- ) s = s.swap(i, j);
232 
233  if ( verbose & VERB_DEBUG )
234  cout << "DEBUG next_permutation: " << s << endl;
235 
236  return 1;
237 }
238 
249 double fishers_z_transform(double value)
250 {
251  double vmin(1e-30); // Will regularize the unusual case of complete correlation
252 
253  return 0.5*log((1 + value + vmin)/(1.0 - value + vmin));
254 }
255 
268 double betacf(double a, double b, double x)
269 {
270  int m,m2;
271  double aa, c, d, del, h, qab, qam, qap;
272  double fpmin(1.0e-30); //Number near the smallest representative
273  //floating-point number
274  double eps(3.0e-7); //Relative accuracy
275  int maxit(100); //Maximum allowed number of iterations
276 
277  qab=a+b; // These q's will be used in factors that occur
278  qap=a+1.0; // in the coefficients (6.4.6).
279  qam=a-1.0;
280  c=1.0;
281  d=1.0-qab*x/qap;
282  if (fabs(d) < fpmin) d=fpmin;
283  d=1.0/d;
284  h=d;
285  for (m=1; m<=maxit;m++){
286  m2=2*m;
287  aa=m*(b-m)*x/((qam+m2)*(a+m2));
288  d=1.0+aa*d;
289  if (fabs(d) < fpmin) d=fpmin;
290  c=1.0+aa/c;
291  if (fabs(c) < fpmin) c=fpmin;
292  d=1.0/d;
293  h *= d*c;
294  aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
295  d=1.0+aa*d; // Next step of the recurrence (the odd one).
296  if (fabs(d) < fpmin) d=fpmin;
297  c=1.0+aa/c;
298  if (fabs(c) < fpmin) c=fpmin;
299  d=1.0/d;
300  del=d*c;
301  h *= del;
302  if (fabs(del-1.0) < eps) break; // Are we done?
303  }
304  if (m > maxit) cerr << "Error: a or b too big, or maxit too small in betacf" << endl;
305  return h;
306 }
307 
320 double betai (double a, double b, double x)
321 {
322  double bt;
323 
324  if(x < 0.0 || x > 1.0) cerr << "Error: Bad x in routine betai" << endl;
325  if(x == 0.0 || x == 1.0) bt=0.0;
326  else //Factors in front of the continued fraction.
327  bt=exp(lgamma(a+b)-lgamma(a)-lgamma(b)+a*log(x)+b*log(1.0-x));
328  if(x < (a+1.0)/(a+b+2.0)) //Use continued fraction directly.
329  return bt*betacf(a,b,x)/a;
330  else //Use continued fraction after making the sym
331  return 1.0-bt*betacf(b,a,1.0-x)/b; //metry transformation
332 }
333 
345 double kmeans_threshold(long n, double* v)
346 {
347  long i, nfg, nbg;
348  double t, vfg(v[0]), vbg(v[1]), dvfg(1), dvbg(1), dt(0.001), sumfg, sumbg;
349 
350  for ( i=0; i<n && vfg == vbg; i++ ); // Ensure at least one value on each class
351 
352  while ( fabs(dvfg) > dt || fabs(dvbg) > dt ) { // Exit on no more change
353  nfg = nbg = 0;
354  sumfg = sumbg = 0;
355  t = (vbg + vfg)/2;
356  for ( i=0; i<n; i++ ) { // Sum into two classes
357  if ( v[i] > t ) {
358  sumfg += v[i];
359  nfg++;
360  } else {
361  sumbg += v[i];
362  nbg++;
363  }
364  }
365  if ( nfg ) sumfg /= nfg;
366  if ( nbg ) sumbg /= nbg;
367  dvfg = sumfg - vfg;
368  dvbg = sumbg - vbg;
369  vfg = sumfg;
370  vbg = sumbg;
371  }
372 
373  return t;
374 }
375 
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bstring::swap
Bstring swap(const long i, const long j)
Definition: Bstring.cpp:485
number_of_combinations
double number_of_combinations(int n, int r)
Calculates the number of combinations of size r in set of size n.
Definition: math_util.cpp:97
smallest_prime
long smallest_prime(long number)
Finds the smallest prime number factor of the input number.
Definition: math_util.cpp:191
Bstring
Definition: Bstring.h:29
partition
int partition(vector< double > &a, int n, int k)
Determines the index k'th value in the array.
Definition: math_util.cpp:123
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
kmeans_threshold
double kmeans_threshold(long n, double *v)
Finds a threshold that partitions an array into foreground and background clusters.
Definition: math_util.cpp:345
factorial
double factorial(int n)
Calculates the factorial of n.
Definition: math_util.cpp:66
betacf
double betacf(double a, double b, double x)
Evaluates the continued fraction for the incomplete beta function.
Definition: math_util.cpp:268
prime_factors
long * prime_factors(long number, long &n)
Finds all the prime factor for the input number.
Definition: math_util.cpp:152
betai
double betai(double a, double b, double x)
Calculates the incomplete beta function Ix(a,b).
Definition: math_util.cpp:320
bfloor
double bfloor(double value, int places)
Truncates a value to a specified number of decimal places.
Definition: math_util.cpp:21
bround
double bround(double value, int places)
Rounds a value to a specified number of decimal places.
Definition: math_util.cpp:39
next_permutation
int next_permutation(Bstring &s)
Produces the next permutation in lexical order.
Definition: math_util.cpp:220
utilities.h
Header file for general utilities.
verbose
int verbose
Bstring::length
long length() const
Definition: Bstring.cpp:263
math_util.h
Header file for general utilities.
fishers_z_transform
double fishers_z_transform(double value)
Calculates Fisher's z-transform.
Definition: math_util.cpp:249