Bsoft  2.1.0
Bernard's software package
math_util.h File Reference

Header file for general utilities. More...

#include <stdlib.h>
#include <cmath>
#include "Bstring.h"
#include "utilities.h"

Go to the source code of this file.

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 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...
 
int partition (double *a, int n, int k)
 
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...
 

Detailed Description

Header file for general utilities.

Author
Bernard Heymann
Date
Created: 19990722
Modified: 20151203

Definition in file math_util.h.

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.

Definition at line 268 of file math_util.cpp.

◆ 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.

Definition at line 320 of file math_util.cpp.

◆ 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.

Definition at line 21 of file math_util.cpp.

◆ 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.

Definition at line 39 of file math_util.cpp.

◆ factorial()

double factorial ( int  n)

Calculates the factorial of n.

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.

Parameters
ninteger.
Returns
double factorial of n, <0 on error.

Definition at line 66 of file math_util.cpp.

◆ 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.

Definition at line 249 of file math_util.cpp.

◆ 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.

Definition at line 345 of file math_util.cpp.

◆ 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.

Definition at line 220 of file math_util.cpp.

◆ 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.

Definition at line 97 of file math_util.cpp.

◆ partition() [1/2]

int partition ( double *  a,
int  n,
int  k 
)

◆ partition() [2/2]

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.

Definition at line 123 of file math_util.cpp.

◆ 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.

Definition at line 152 of file math_util.cpp.

◆ 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.

Definition at line 191 of file math_util.cpp.