Bsoft 2.1.4
Bernard's software package
math_util.cpp File Reference

Mathematics utility functions. More...

#include "math_util.h"
#include "utilities.h"

Functions

double bfloor (double value, int places)
 Truncates a value to a specified number of decimal places. More...
 
double bround (double value, int places)
 Rounds a value to a specified number of decimal places. More...
 
double sinc (double d)
 Returns the normalized cardinal sine. More...
 
double factorial (int n)
 Calculates the factorial of n. More...
 
double number_of_combinations (int n, int r)
 Calculates the number of combinations of size r in set of size n. More...
 
int partition (vector< double > &a, int n, int k)
 Determines the index k'th value in the array. More...
 
long * prime_factors (long number, long &n)
 Finds all the prime factor for the input number. More...
 
long smallest_prime (long number)
 Finds the smallest prime number factor of the input number. More...
 
int next_permutation (Bstring &s)
 Produces the next permutation in lexical order. More...
 
double fishers_z_transform (double value)
 Calculates Fisher's z-transform. More...
 
double betacf (double a, double b, double x)
 Evaluates the continued fraction for the incomplete beta function. More...
 
double betai (double a, double b, double x)
 Calculates the incomplete beta function Ix(a,b). More...
 
double kmeans_threshold (long n, double *v)
 Finds a threshold that partitions an array into foreground and background clusters. More...
 

Variables

int verbose
 

Detailed Description

Mathematics utility functions.

Author
Bernard Heymann
Date
Created: 20030414
Modified: 20220524

Function Documentation

◆ betacf()

double betacf ( double  a,
double  b,
double  x 
)

Evaluates the continued fraction for the incomplete beta function.

Parameters
afirst parameter.
bsecond parameter.
xdomain variable [0,1].
Returns
double
Lentz's method.
Reference: Press W.H. et al (1992) Numerical Recipes in C.

◆ betai()

double betai ( double  a,
double  b,
double  x 
)

Calculates the incomplete beta function Ix(a,b).

Parameters
afirst parameter.
bsecond parameter.
xdomain variable [0,1].
Returns
double
Limiting values: I0(a,b) = 0, I1(a,b) = 1.
Symmetry relation: Ix(a,b) = 1 - I1-x(b,a).
Reference: Press W.H. et al (1992) Numerical Recipes in C.

◆ bfloor()

double bfloor ( double  value,
int  places 
)

Truncates a value to a specified number of decimal places.

Parameters
valuevalue to be truncated.
placesnumber of decimal places.
Returns
int 0.

◆ bround()

double bround ( double  value,
int  places 
)

Rounds a value to a specified number of decimal places.

Parameters
valuevalue to be rounded.
placesnumber of decimal places.
Returns
double rounded value.

◆ factorial()

double factorial ( int  n)

Calculates the factorial of n.

Parameters
ninteger.
Returns
double factorial of n, <0 on error.
All values of n less than 1 returns 1.
An exact calculation is done for 1 < n <= 50.
The Lancos approximation is used for n > 50.
Factorials of integers larger than 170 exceeds the capacity of a 
double and causes program termination.
The largest relative error is for 170: 1.22378e-13.
Reference: Press W.H. et al (1992) Numerical Recipes in C.

◆ fishers_z_transform()

double fishers_z_transform ( double  value)

Calculates Fisher's z-transform.

Parameters
valuea value.
Returns
double z value.
Fisher's z-transform is given by:
    z = 0.5*log((1+v)/(1-v))
Reference: Press W.H. et al (1992) Numerical Recipes in C.

◆ kmeans_threshold()

double kmeans_threshold ( long  n,
double *  v 
)

Finds a threshold that partitions an array into foreground and background clusters.

Parameters
nnumber of array elements.
varray.
Returns
double threshold.
Limiting values: I0(a,b) = 0, I1(a,b) = 1.
Symmetry relation: Ix(a,b) = 1 - I1-x(b,a).
Reference: Press W.H. et al (1992) Numerical Recipes in C.

◆ next_permutation()

int next_permutation ( Bstring s)

Produces the next permutation in lexical order.

Parameters
&sstring of symbols to permute.
Returns
int 1 = success, 0 = no next permutation.
To get all permutations, the first string needs
to be ordered in ascending order.
Reference: Press W.H. et al (1992) Numerical Recipes in C.

◆ number_of_combinations()

double number_of_combinations ( int  n,
int  r 
)

Calculates the number of combinations of size r in set of size n.

Parameters
nnumber in set.
rnumber in subset.
Returns
double number of combinations, <0 on error.
All values of n less than 1 returns 1.
An exact calculation is done for 1 < n <= 50.
The Lancos approximation is used for n > 50.
Reference: Press W.H. et al (1992) Numerical Recipes in C.

◆ partition()

int partition ( vector< double > &  a,
int  n,
int  k 
)

Determines the index k'th value in the array.

Parameters
*aarray.
nnumber of array elements.
krank index to look for.
Returns
0
The array is partioned into 2 sides, with the left side lower or equal 
to and the right side higher or equal to the k'th element.
This is useful to determine the median without full sorting.
Reference: Press W.H. et al (1992) Numerical Recipes in C.

◆ prime_factors()

long * prime_factors ( long  number,
long &  n 
)

Finds all the prime factor for the input number.

Parameters
numberinteger.
&nnumber of prime factors.
Returns
long* array of prime factors (can be NULL).
Calculates the prime factors from the smallest to the largest.
Reference: Press W.H. et al (1992) Numerical Recipes in C.

◆ sinc()

double sinc ( double  d)

Returns the normalized cardinal sine.

Parameters
dvalue.
Returns
double sinc value.

◆ smallest_prime()

long smallest_prime ( long  number)

Finds the smallest prime number factor of the input number.

Parameters
numberinteger.
Returns
long smallest prime factor.
Tries to divide the given positive integer number by 
primes from 2 to the square root of the integer.
Returns the first prime divisor found, which may be the 
input number if it is prime.
Reference: Press W.H. et al (1992) Numerical Recipes in C.

Variable Documentation

◆ verbose

int verbose
extern