mathkit  1.5
 All Classes Namespaces Functions Variables Typedefs
Classes | Typedefs | Functions | Variables
mathkit Namespace Reference

All functions of mathkit are in the mathkit namespace. More...

Classes

class  Epsilon
 Epsilon functor (test whether |x| < eps). More...
 
struct  LineParam
 Parameters (slope and intercept) of regression line. More...
 
class  Matrix
 Matrix class. More...
 

Typedefs

typedef vector< double > Vector
 Vector is just a convenient alias for vector<double>.
 
typedef vector< vector< double > > Table
 Table is just a convenient alias for vector<vector<double> >.
 
typedef pair< double, double > Pair
 Pair is just a convenient alias for pair<double, double>.
 
typedef vector< pair< double,
double > > 
Pairs
 Pairs is just a convenient alias for vector<pair<double, double> >.
 
typedef pair< int, int > Dimen
 Dimen is just a convenient alias for pair<int, int>.
 
typedef vector< MatrixMatrices
 Matrices is just a convenient alias for vector<Matrix>.
 

Functions

double pv (double amount, double rate, double period)
 Present value of compound interest. More...
 
double fv (double amount, double rate, double period)
 Future value of compound interest. More...
 
double pv_coef (double rate, double period)
 Present value coefficient of compound interest. More...
 
double fv_coef (double rate, double period)
 Future value coefficient of compound interest. More...
 
double apv (double annuity, double rate, int period, bool prepaid=false)
 Present value of annuity. More...
 
double afv (double annuity, double rate, int period, bool prepaid=false)
 Future value of annuity. More...
 
double apv_coef (double rate, int period, bool prepaid=false)
 Present value coefficient of annuity. More...
 
double afv_coef (double rate, int period, bool prepaid=false)
 Future value coefficient of annuity. More...
 
double spv (const Vector &amount, double rate, bool prepaid=false)
 Present value of a series of various payments. More...
 
double sfv (const Vector &amount, double rate, bool prepaid=false)
 Future value of a series of various payments. More...
 
double comp_rate (double pval, double fval, double period)
 Compound interest/growth rate. More...
 
double mean (const Vector &data)
 Arithmatic mean. More...
 
double median (const Vector &data, bool sorted=false)
 Median of a sequence of data. More...
 
double var (const Vector &data, bool sample=true)
 Variance of the population or sample data. More...
 
double sd (const Vector &data, bool sample=true)
 Standard deviation. More...
 
double cov (const Vector &data1, const Vector &data2, bool sample=true)
 Covariance of two groups of data. More...
 
double cor (const Vector &data1, const Vector &data2)
 Correlation coefficient. More...
 
double moment (const Vector &data, int k, bool central=true)
 Moment. More...
 
double skew (const Vector &data)
 Skewness. More...
 
double kurt (const Vector &data, bool excess=true)
 Kurtosis. More...
 
double dnorm (double x, double mu=0, double sigma=1)
 Probability density of normal distribution. More...
 
double pnorm (double x, double mu=0, double sigma=1, const Epsilon &eps=Epsilon())
 Cumulative probability (CDF) of normal distribution. More...
 
double qnorm (double p, double mu=0, double sigma=1, const Epsilon &eps=Epsilon())
 Quantile of normal distribution. More...
 
double dt (double x, int n)
 Probability density of t distribution. More...
 
double pt (double x, int n, const Epsilon &eps=Epsilon())
 Cumulative probability (CDF) of t distribution. More...
 
double qt (double p, int n, const Epsilon &eps=Epsilon())
 Quantile of t distribution. More...
 
double pois (double lmd, int k, bool cum=false)
 Probability of poisson distribution. More...
 
double binom (int n, double p, int k, bool cum=false)
 Probability of binomial distribution. More...
 
double expo (double theta, double x)
 Cumulative probability of exponential distribution. More...
 
double gamma (double x)
 Gamma function. More...
 
LineParam linregress (const Vector &xdata, const Vector &ydata)
 Linear regression. More...
 
Vector seq (double from, double to, double step=1)
 Generate a vector from a sequence of numbers. More...
 
Vector linspace (double start, double end, int count=100)
 Generate a vector from a sequence of numbers uniformly spread a linear range. More...
 
double max (const Vector &data)
 Find the maximum value of the data. More...
 
double min (const Vector &data)
 Find the minimum value of the data. More...
 
double sum (const Vector &data)
 Calculate the sum of a number sequence. More...
 
double prod (const Vector &data)
 Calculate the product of a number sequence. More...
 
Vector add (const Vector &vec1, const Vector &vec2)
 Add two vectors of same dimension. More...
 
Vector add (const Vector &vec, double scalar)
 Add a vector and a scalar elementwise. More...
 
Vector operator+ (const Vector &vec1, const Vector &vec2)
 Add two vectors of same dimension. More...
 
Vector operator+ (const Vector &vec, double scalar)
 Add a vector and a scalar elementwise. More...
 
Vector operator+ (double scalar, const Vector &vec)
 Add a vector and a scalar elementwise. More...
 
Vector sub (const Vector &vec1, const Vector &vec2)
 Subtract two vectors of same dimension. More...
 
Vector sub (const Vector &vec, double scalar, bool dir=true)
 Subtract a vector and a scalar elementwise. More...
 
Vector operator- (const Vector &vec1, const Vector &vec2)
 Subtract two vectors of same dimension. More...
 
Vector operator- (const Vector &vec, double scalar)
 Subtract a vector and a scalar elementwise. More...
 
Vector operator- (double scalar, const Vector &vec)
 Subtract a vector and a scalar elementwise. More...
 
Vector mul (const Vector &vec1, const Vector &vec2)
 Multiply two vectors of same dimension elementwise. More...
 
Vector mul (const Vector &vec, double scalar)
 Multiply a vector and a scalar elementwise. More...
 
Vector operator* (const Vector &vec1, const Vector &vec2)
 Multiply two vectors of same dimension elementwise. More...
 
Vector operator* (const Vector &vec, double scalar)
 Multiply a vector and a scalar elementwise. More...
 
Vector operator* (double scalar, const Vector &vec)
 Multiply a vector and a scalar elementwise. More...
 
Vector div (const Vector &vec1, const Vector &vec2)
 Divide two vectors of same dimension elementwise. More...
 
Vector div (const Vector &vec, double scalar, bool dir=true)
 Divide a vector and a scalar elementwise. More...
 
Vector operator/ (const Vector &vec1, const Vector &vec2)
 Divide two vectors of same dimension elementwise. More...
 
Vector operator/ (const Vector &vec, double scalar)
 Divide a vector and a scalar elementwise. More...
 
Vector operator/ (double scalar, const Vector &vec)
 Divide a vector and a scalar elementwise. More...
 
double dot_prod (const Vector &vec1, const Vector &vec2)
 Dot product of two vectors. More...
 
Vector cross_prod (const Vector &vec1, const Vector &vec2)
 Cross product of two 3-dimensional vectors. More...
 
double norm (const Vector &vec)
 The Euclidean norm (length) of a vector. More...
 
string to_str (const Vector &vec, string sep=" ", string delim="()")
 Generate the string representation of a vector. More...
 
Vector make_vec (int n,...)
 Make a vector. More...
 
Vector load (istream &ins, string delim=" ")
 Load data from an input stream. More...
 
Table load (istream &ins, int nrow, int ncol, string delim=" ")
 Load table/matrix data from an input stream. More...
 
void save (ostream &outs, const Vector &data, string delim=" ")
 Save data to an output stream. More...
 
void save (ostream &outs, const Table &data, string delim=" ")
 Save table/matrix data to an output stream. More...
 
ostream & operator<< (ostream &outs, const Vector &vec)
 Stream insertion operator for vector. More...
 
istream & operator>> (istream &ins, Vector &vec)
 Stream extraction operator for vector. More...
 
double randf (double low, double high)
 Floating point random number in rang [low, high] inclusive. More...
 
Vector randf (double low, double high, int n)
 Floating point random numbers in rang [low, high] inclusive. More...
 
int randi (int low, int high)
 Integer random number in range [low, high] inclusive. More...
 
Vector randi (int low, int high, int n)
 Integer random numbers in range [low, high] inclusive. More...
 
bool randp (double p)
 0~1 distribution. More...
 
void randseed ()
 Set seed for pseudo-random number generator. More...
 
void randseed (unsigned int s)
 Set seed for pseudo-random number generator. More...
 
double rnorm (double mu=0, double sigma=1)
 Random number of normal distribution. More...
 
Vector rnorm (int n, double mu=0, double sigma=1)
 Random numbers of normal distribution. More...
 
int rpois (double lmd)
 Random number of poisson distribution. More...
 
Vector rpois (double lmd, int n)
 Random numbers of poisson distribution. More...
 
int rbinom (int n, double p)
 Random number of binomial distribution. More...
 
Vector rbinom (int n, double p, int count)
 Random numbers of binomial distribution. More...
 
double rexp (double theta)
 Random number of exponential distribution. More...
 
Vector rexp (double theta, int n)
 Random numbers of exponential distribution. More...
 
double prec (double num, int ndec=2)
 Round a number to specific decimal digits. More...
 
Vector prec (const Vector &vec, int ndec=4)
 Round every component of a vector to specific decimal digits. More...
 
double fac (unsigned int n)
 Factorial of integer n (n!). More...
 
double perm (unsigned int m, unsigned int n)
 Permutation number (mPn). More...
 
double comb (unsigned int m, unsigned int n)
 Combination number (mCn). More...
 
unsigned int gcd (unsigned int a, unsigned int b)
 Greatest common divisor. More...
 
unsigned int lcm (unsigned int a, unsigned int b)
 Least common multiple. More...
 
template<typename Func >
Vector each (Func func, const Vector &data)
 Apply a function to each component of a vector. More...
 
template<typename Func >
Vector filter (Func func, const Vector &data)
 Filter a vector of data. More...
 
template<typename Func >
bool probe (Func func, Pair &guess, double factor=1.6, int ntry=50)
 Find a region contains at least one root. More...
 
template<typename Func >
Pairs scan (Func func, Pair scope, int nr)
 Scan a specific scope to find sub-regions contain root. More...
 
template<typename Func >
double solve (Func func, Pair region, const Epsilon &eps=Epsilon())
 Find a root in a specific region using binary search algorithm. More...
 
template<typename Func >
double integrate (Func func, Pair region, const Epsilon &eps=Epsilon())
 Numerical integration. More...
 
template<typename T >
Vector a2vec (T data[], int len)
 Convert a C array to a C++ vector. More...
 
Matrix operator* (const Matrix &mat, double scalar)
 Scalar multiplication for matrix. More...
 
Matrix operator* (double scalar, const Matrix &mat)
 Scalar multiplication for matrix. More...
 
ostream & operator<< (ostream &outs, const Matrix &mat)
 Stream insertion operator for matrix. More...
 
istream & operator>> (istream &ins, Matrix &mat)
 Stream extraction operator for matrix. More...
 
Matrix t (const Matrix &mat)
 Transpose matrix. More...
 
Matrix eye (int n)
 Generate an identity matrix (n by n). More...
 
Vector diag (const Matrix &mat)
 Get the diagonal elements of a square matrix. More...
 
Matrix diag (const Vector &vec)
 Generate a diagonal matrix with specific diagonal elements. More...
 
double det (const Matrix &mat, const Epsilon &eps=Epsilon())
 Determinant. More...
 
Matrix inv (const Matrix &mat, const Epsilon &eps=Epsilon())
 Inverse matrix. More...
 
double trace (const Matrix &mat, const Epsilon &eps=Epsilon())
 Trace (sum of main diagonal elements) of square matrix. More...
 
bool positive (const Matrix &mat, const Epsilon &eps=Epsilon())
 Check whether a symmetric matrix is positive definite. More...
 
Table table (const Vector &data, Dimen dimen, bool byrow=true)
 Generate a table from a vector. More...
 
Matrix rowmat (const Vector &data)
 Generate an one row matrix (row vector in linear algebra). More...
 
Matrix colmat (const Vector &data)
 Generate an one column matrix (column vector in linear algebra). More...
 
Matrix cbind (const Matrix &mat1, const Matrix &mat2)
 Construct a matrix by column binding. More...
 
Matrix rbind (const Matrix &mat1, const Matrix &mat2)
 Construct a matrix by row binding. More...
 
Matrices lu (const Matrix &mat, const Epsilon &eps=Epsilon())
 LU decomposition. More...
 
Matrices qr (const Matrix &mat, const Epsilon &eps=Epsilon())
 QR decomposition. More...
 
Vector linsolve (const Matrix &mat, const Vector &vec, const Epsilon &eps=Epsilon())
 Solve a linear system (Ax = b). More...
 
Vector lusolve (const Matrices &lup, const Vector &vec, const Epsilon &eps=Epsilon())
 Solve a linear system represented by L, U, P (Ax = b, PA = LU). More...
 
Vector qrsolve (const Matrices &qrmat, const Vector &vec, const Epsilon &eps=Epsilon())
 Solve a linear system represented by Q, R (Ax = b, A = QR). More...
 
string to_str (const Matrix &mat, bool byrow=true, string delim="[]")
 The string representation of a matrix. More...
 

Variables

const double pi = 4 * atan(1.0)
 Constant pi.
 

Detailed Description

All functions of mathkit are in the mathkit namespace.

Function Documentation

double mathkit::pv ( double  amount,
double  rate,
double  period 
)

Present value of compound interest.

Parameters
amountThe terminal/final value.
rateThe compound interest rate.
periodThe number of periods spanned.
Returns
The present value.

Definition at line 21 of file mathkit.cpp.

double mathkit::fv ( double  amount,
double  rate,
double  period 
)

Future value of compound interest.

Parameters
amountThe present value (principal).
rateThe compound interest rate.
periodThe number of periods spanned.
Returns
The future/final value.

Definition at line 25 of file mathkit.cpp.

double mathkit::pv_coef ( double  rate,
double  period 
)

Present value coefficient of compound interest.

Parameters
rateThe compound interest rate.
periodThe number of periods spanned.
Returns
The present value coefficient.

Definition at line 29 of file mathkit.cpp.

double mathkit::fv_coef ( double  rate,
double  period 
)

Future value coefficient of compound interest.

Parameters
rateThe compound interest rate.
periodThe number of periods spanned.
Returns
The future value coefficient.

Definition at line 33 of file mathkit.cpp.

double mathkit::apv ( double  annuity,
double  rate,
int  period,
bool  prepaid = false 
)

Present value of annuity.

Parameters
annuityThe payment of every period.
rateThe compound interest (discount) rate.
periodThe number of periods spanned.
prepaidWhether the annuity is an ordinary annuity or a prepaid annuity.
Returns
The present value of annuity.

Definition at line 37 of file mathkit.cpp.

double mathkit::afv ( double  annuity,
double  rate,
int  period,
bool  prepaid = false 
)

Future value of annuity.

Parameters
annuityThe payment of every period.
rateThe compound interest rate.
periodThe number of periods spanned.
prepaidWhether the annuity is an ordinary annuity or a prepaid annuity.
Returns
The future/final value of annuity.

Definition at line 41 of file mathkit.cpp.

double mathkit::apv_coef ( double  rate,
int  period,
bool  prepaid = false 
)

Present value coefficient of annuity.

Parameters
rateThe compound interest (discount) rate.
periodThe number of periods spanned.
prepaidWhether the annuity is an ordinary annuity or a prepaid annuity.
Returns
The present value coefficient of annuity.

Definition at line 45 of file mathkit.cpp.

double mathkit::afv_coef ( double  rate,
int  period,
bool  prepaid = false 
)

Future value coefficient of annuity.

Parameters
rateThe compound interest rate.
periodThe number of periods spanned.
prepaidWhether the annuity is an ordinary annuity or a prepaid annuity.
Returns
The future value coefficient of annuity.

Definition at line 52 of file mathkit.cpp.

double mathkit::spv ( const Vector &  amount,
double  rate,
bool  prepaid = false 
)

Present value of a series of various payments.

This function can also be used to calculate net present value (NPV).

Parameters
amountThe payment of every period.
rateThe compound interest (discount) rate.
prepaidWhether these payments are ordinary or prepaid (paid at the beginning of every period).
Returns
The present value of the payments.

Definition at line 59 of file mathkit.cpp.

double mathkit::sfv ( const Vector &  amount,
double  rate,
bool  prepaid = false 
)

Future value of a series of various payments.

Parameters
amountThe payment of every period.
rateThe compound interest rate.
prepaidWhether these payments are ordinary or prepaid (paid at the beginning of every period).
Returns
The future/final value of the payments.

Definition at line 75 of file mathkit.cpp.

double mathkit::comp_rate ( double  pval,
double  fval,
double  period 
)

Compound interest/growth rate.

This function can also be used to calculate geometric mean because their formulas are the same.

Parameters
pvalThe present value (principal).
fvalThe future/final value.
periodThe number of periods spanned.
Returns
The compound interest/growth rate.

Definition at line 91 of file mathkit.cpp.

double mathkit::mean ( const Vector &  data)

Arithmatic mean.

Parameters
dataThe population or sample data.
Returns
The arithmatic mean.

Definition at line 95 of file mathkit.cpp.

double mathkit::median ( const Vector &  data,
bool  sorted = false 
)

Median of a sequence of data.

Parameters
dataThe population or sample data.
sortedWhether the data are sorted.
Returns
The median value.

Definition at line 102 of file mathkit.cpp.

double mathkit::var ( const Vector &  data,
bool  sample = true 
)

Variance of the population or sample data.

For population the variance is normalized by n. And for sample data it is normalized by n - 1.

Parameters
dataThe population or sample data.
sampleWhether the data are population data or sample data.
Returns
The variance value.

Definition at line 112 of file mathkit.cpp.

double mathkit::sd ( const Vector &  data,
bool  sample = true 
)

Standard deviation.

For population the standard deviation is normalized by n. And for sample data it is normalized by n - 1.

Parameters
dataThe population or sample data.
sampleWhether the data are population data or sample data.
Returns
The standard deviation value.

Definition at line 121 of file mathkit.cpp.

double mathkit::cov ( const Vector &  data1,
const Vector &  data2,
bool  sample = true 
)

Covariance of two groups of data.

For population the covariance is normalized by n. And for sample data it is normalized by n - 1.

Parameters
data1The population or sample data of one group.
data2The population or sample data of another group.
sampleWhether the data are population data or sample data.
Returns
The covariance value.

Definition at line 125 of file mathkit.cpp.

double mathkit::cor ( const Vector &  data1,
const Vector &  data2 
)

Correlation coefficient.

Parameters
data1The data of one group.
data2The data of another group.
Returns
The correlation coefficient.

Definition at line 141 of file mathkit.cpp.

double mathkit::moment ( const Vector &  data,
int  k,
bool  central = true 
)

Moment.

Parameters
dataA vector contains the data.
kThe degree of the moment.
centralCentral moment if true, else original moment.
Returns
The moment.

Definition at line 145 of file mathkit.cpp.

double mathkit::skew ( const Vector &  data)

Skewness.

Parameters
dataA vector contains the data.
Returns
The skewness.

Definition at line 160 of file mathkit.cpp.

double mathkit::kurt ( const Vector &  data,
bool  excess = true 
)

Kurtosis.

Parameters
dataA vector contains the data.
excessWhether to compute excess kurtosis.
Returns
The kurtosis.

Definition at line 164 of file mathkit.cpp.

double mathkit::dnorm ( double  x,
double  mu = 0,
double  sigma = 1 
)

Probability density of normal distribution.

Parameters
xA specific value of the normal variate.
muThe mean.
sigmaThe standard deviation.
Returns
The value of the probability density function at x.

Definition at line 170 of file mathkit.cpp.

double mathkit::pnorm ( double  x,
double  mu = 0,
double  sigma = 1,
const Epsilon &  eps = Epsilon() 
)

Cumulative probability (CDF) of normal distribution.

Parameters
xA specific value of the normal variate.
muThe mean.
sigmaThe standard deviation.
epsAn Epsilon functor to control accuracy.
Returns
P(X <= x).

Definition at line 175 of file mathkit.cpp.

double mathkit::qnorm ( double  p,
double  mu = 0,
double  sigma = 1,
const Epsilon &  eps = Epsilon() 
)

Quantile of normal distribution.

Parameters
pThe cumulative probability.
muThe mean.
sigmaThe standard deviation.
epsAn Epsilon functor to control accuracy.
Returns
The quantile x makes P(X <= x) == p.

Definition at line 195 of file mathkit.cpp.

double mathkit::dt ( double  x,
int  n 
)

Probability density of t distribution.

Parameters
xA specific value of the variate.
nThe degrees of freedom.
Returns
The value of the probability density function at x.

Definition at line 220 of file mathkit.cpp.

double mathkit::pt ( double  x,
int  n,
const Epsilon &  eps = Epsilon() 
)

Cumulative probability (CDF) of t distribution.

Parameters
xA specific value of the variate.
nThe degrees of freedom.
epsAn Epsilon functor to control accuracy.
Returns
P(X <= x).

Definition at line 224 of file mathkit.cpp.

double mathkit::qt ( double  p,
int  n,
const Epsilon &  eps = Epsilon() 
)

Quantile of t distribution.

Parameters
pThe cumulative probability.
nThe degrees of freedom.
epsAn Epsilon functor to control accuracy.
Returns
The quantile x makes P(X <= x) == p.

Definition at line 238 of file mathkit.cpp.

double mathkit::pois ( double  lmd,
int  k,
bool  cum = false 
)

Probability of poisson distribution.

Parameters
lmdThe lambda parameter (> 0).
kThe occurrence number of specific event (k = 0, 1, 2, ...).
cumCalculate P(X <= k) if cum is true, else calculate P(X = k).
Returns
P(X <= k) if cum is true, else P(X = k).

Definition at line 263 of file mathkit.cpp.

double mathkit::binom ( int  n,
double  p,
int  k,
bool  cum = false 
)

Probability of binomial distribution.

Parameters
nThe number of independent experiments.
pThe occurrence probability of specific event in one experiment.
kThe number of occurrences of the specific event in the n experiments.
cumCalculate P(X <= k) if cum is true, else calculate P(X = k).
Returns
P(X <= k) if cum is true, else P(X = k).

Definition at line 270 of file mathkit.cpp.

double mathkit::expo ( double  theta,
double  x 
)

Cumulative probability of exponential distribution.

Parameters
thetaThe parameter of exponential distribution (1/rate).
xA specific value of this exponential variate.
Returns
P(X <= x).

Definition at line 277 of file mathkit.cpp.

double mathkit::gamma ( double  x)

Gamma function.

Parameters
xThe parameter of gamma function.
Returns
The value of gamma function.

Definition at line 282 of file mathkit.cpp.

LineParam mathkit::linregress ( const Vector &  xdata,
const Vector &  ydata 
)

Linear regression.

Parameters
xdataThe data of independent variable.
ydataThe data of dependent variable.
Returns
A LineParam structure encapsulating the slope and intercept of the regression line.
See Also
LineParam

Definition at line 293 of file mathkit.cpp.

Vector mathkit::seq ( double  from,
double  to,
double  step = 1 
)

Generate a vector from a sequence of numbers.

Parameters
fromThe start point of the sequence.
toThe end point (may exclude) of the sequence.
stepThe step length of every progress.
Returns
The vector contains the number sequence.

Definition at line 300 of file mathkit.cpp.

Vector mathkit::linspace ( double  start,
double  end,
int  count = 100 
)

Generate a vector from a sequence of numbers uniformly spread a linear range.

Parameters
startThe start point of the range.
endThe end point of the range.
countThe count of numbers in the sequence (including the start and end point).
Returns
The vector contains the number sequence.

Definition at line 319 of file mathkit.cpp.

double mathkit::max ( const Vector &  data)

Find the maximum value of the data.

Parameters
dataA vector contains the data.
Returns
The maximum value.

Definition at line 338 of file mathkit.cpp.

double mathkit::min ( const Vector &  data)

Find the minimum value of the data.

Parameters
dataA vector contains the data.
Returns
The minimum value.

Definition at line 342 of file mathkit.cpp.

double mathkit::sum ( const Vector &  data)

Calculate the sum of a number sequence.

Parameters
dataThe sequence of numbers.
Returns
The sum of every number in the sequence.

Definition at line 346 of file mathkit.cpp.

double mathkit::prod ( const Vector &  data)

Calculate the product of a number sequence.

Parameters
dataThe sequence of numbers.
Returns
The product of every number in the sequence.

Definition at line 350 of file mathkit.cpp.

Vector mathkit::add ( const Vector &  vec1,
const Vector &  vec2 
)

Add two vectors of same dimension.

Parameters
vec1The first vector.
vec2The second vector.
Returns
A vector contains the result.

Definition at line 354 of file mathkit.cpp.

Vector mathkit::add ( const Vector &  vec,
double  scalar 
)

Add a vector and a scalar elementwise.

This function add the scalar to every component of the vector.

Parameters
vecThe vector.
scalarThe scalar value.
Returns
A vector contains the result.

Definition at line 367 of file mathkit.cpp.

Vector mathkit::operator+ ( const Vector &  vec1,
const Vector &  vec2 
)

Add two vectors of same dimension.

Parameters
vec1The first vector.
vec2The second vector.
Returns
A vector contains the result.

Definition at line 374 of file mathkit.cpp.

Vector mathkit::operator+ ( const Vector &  vec,
double  scalar 
)

Add a vector and a scalar elementwise.

This function add the scalar to every component of the vector.

Parameters
vecThe vector.
scalarThe scalar value.
Returns
A vector contains the result.

Definition at line 378 of file mathkit.cpp.

Vector mathkit::operator+ ( double  scalar,
const Vector &  vec 
)

Add a vector and a scalar elementwise.

This function add the scalar to every component of the vector.

Parameters
scalarThe scalar value.
vecThe vector.
Returns
A vector contains the result.

Definition at line 382 of file mathkit.cpp.

Vector mathkit::sub ( const Vector &  vec1,
const Vector &  vec2 
)

Subtract two vectors of same dimension.

Parameters
vec1The first vector.
vec2The second vector.
Returns
A vector contains the result.

Definition at line 386 of file mathkit.cpp.

Vector mathkit::sub ( const Vector &  vec,
double  scalar,
bool  dir = true 
)

Subtract a vector and a scalar elementwise.

This function subtract the scalar to every component of the vector.

Parameters
vecThe vector.
scalarThe scalar value.
dirIf dir is true compute vec - scalar else compute scalar - vec.
Returns
A vector contains the result.

Definition at line 399 of file mathkit.cpp.

Vector mathkit::operator- ( const Vector &  vec1,
const Vector &  vec2 
)

Subtract two vectors of same dimension.

Parameters
vec1The first vector.
vec2The second vector.
Returns
A vector contains the result.

Definition at line 410 of file mathkit.cpp.

Vector mathkit::operator- ( const Vector &  vec,
double  scalar 
)

Subtract a vector and a scalar elementwise.

This function subtract the scalar to every component of the vector.

Parameters
vecThe vector.
scalarThe scalar value.
Returns
A vector contains the result.

Definition at line 414 of file mathkit.cpp.

Vector mathkit::operator- ( double  scalar,
const Vector &  vec 
)

Subtract a vector and a scalar elementwise.

This function subtract the scalar to every component of the vector.

Parameters
scalarThe scalar value.
vecThe vector.
Returns
A vector contains the result.

Definition at line 418 of file mathkit.cpp.

Vector mathkit::mul ( const Vector &  vec1,
const Vector &  vec2 
)

Multiply two vectors of same dimension elementwise.

Parameters
vec1The first vector.
vec2The second vector.
Returns
A vector contains the result.

Definition at line 422 of file mathkit.cpp.

Vector mathkit::mul ( const Vector &  vec,
double  scalar 
)

Multiply a vector and a scalar elementwise.

Parameters
vecThe vector.
scalarThe scalar value.
Returns
A vector contains the result.

Definition at line 435 of file mathkit.cpp.

Vector mathkit::operator* ( const Vector &  vec1,
const Vector &  vec2 
)

Multiply two vectors of same dimension elementwise.

Parameters
vec1The first vector.
vec2The second vector.
Returns
A vector contains the result.

Definition at line 442 of file mathkit.cpp.

Vector mathkit::operator* ( const Vector &  vec,
double  scalar 
)

Multiply a vector and a scalar elementwise.

Parameters
vecThe vector.
scalarThe scalar value.
Returns
A vector contains the result.

Definition at line 446 of file mathkit.cpp.

Vector mathkit::operator* ( double  scalar,
const Vector &  vec 
)

Multiply a vector and a scalar elementwise.

Parameters
scalarThe scalar value.
vecThe vector.
Returns
A vector contains the result.

Definition at line 450 of file mathkit.cpp.

Vector mathkit::div ( const Vector &  vec1,
const Vector &  vec2 
)

Divide two vectors of same dimension elementwise.

Parameters
vec1The first vector.
vec2The second vector.
Returns
A vector contains the result.

Definition at line 454 of file mathkit.cpp.

Vector mathkit::div ( const Vector &  vec,
double  scalar,
bool  dir = true 
)

Divide a vector and a scalar elementwise.

Parameters
vecThe vector.
scalarThe scalar value.
dirIf dir is true compute vec / scalar else compute scalar / vec.
Returns
A vector contains the result.

Definition at line 467 of file mathkit.cpp.

Vector mathkit::operator/ ( const Vector &  vec1,
const Vector &  vec2 
)

Divide two vectors of same dimension elementwise.

Parameters
vec1The first vector.
vec2The second vector.
Returns
A vector contains the result.

Definition at line 477 of file mathkit.cpp.

Vector mathkit::operator/ ( const Vector &  vec,
double  scalar 
)

Divide a vector and a scalar elementwise.

Parameters
vecThe vector.
scalarThe scalar value.
Returns
A vector contains the result.

Definition at line 481 of file mathkit.cpp.

Vector mathkit::operator/ ( double  scalar,
const Vector &  vec 
)

Divide a vector and a scalar elementwise.

Parameters
scalarThe scalar value.
vecThe vector.
Returns
A vector contains the result.

Definition at line 485 of file mathkit.cpp.

double mathkit::dot_prod ( const Vector &  vec1,
const Vector &  vec2 
)

Dot product of two vectors.

The dot product of (a1, a2, a3) and (b1, b2, b3) is a1*b1 + a2*b2 + a3*b3.

Parameters
vec1The first vector.
vec2The second vector.
Returns
The dot product of these two vectors.

Definition at line 489 of file mathkit.cpp.

Vector mathkit::cross_prod ( const Vector &  vec1,
const Vector &  vec2 
)

Cross product of two 3-dimensional vectors.

Parameters
vec1The first 3-dimensional vector.
vec2The second 3-dimensional vector.
Returns
A 3-dimensional vector represents the cross product of these two vectors.

Definition at line 502 of file mathkit.cpp.

double mathkit::norm ( const Vector &  vec)

The Euclidean norm (length) of a vector.

Parameters
vecThe vector.
Returns
The Euclidean norm.

Definition at line 510 of file mathkit.cpp.

string mathkit::to_str ( const Vector &  vec,
string  sep = " ",
string  delim = "()" 
)

Generate the string representation of a vector.

Parameters
vecThe vector.
sepThe separator string.
delimThe delimiter string.
Returns
The string representation of the vector.

Definition at line 514 of file mathkit.cpp.

Vector mathkit::make_vec ( int  n,
  ... 
)

Make a vector.

The components must be double type, so integers should add a trailing dot.
To make a vector (1 2 9): make_vec(3, 1., 2., 9.)

Parameters
nThe number of components.
...The components (double type) of the vector.
Returns
A vector contains the specific components.

Definition at line 527 of file mathkit.cpp.

Vector mathkit::load ( istream &  ins,
string  delim = " " 
)

Load data from an input stream.

Parameters
insThe input stream.
delimThe delimiter string.
Returns
A vector contains the data from the input stream.

Definition at line 536 of file mathkit.cpp.

Table mathkit::load ( istream &  ins,
int  nrow,
int  ncol,
string  delim = " " 
)

Load table/matrix data from an input stream.

Parameters
insThe input stream.
nrowThe number of rows.
ncolThe number of columns.
delimThe delimiter string.
Returns
A vector of vector contains the data from the input stream.

Definition at line 554 of file mathkit.cpp.

void mathkit::save ( ostream &  outs,
const Vector &  data,
string  delim = " " 
)

Save data to an output stream.

Parameters
outsThe output stream.
dataThe data to be saved.
delimThe delimiter string.

Definition at line 563 of file mathkit.cpp.

void mathkit::save ( ostream &  outs,
const Table &  data,
string  delim = " " 
)

Save table/matrix data to an output stream.

Parameters
outsThe output stream.
dataThe data to be saved.
delimThe delimiter string.

Definition at line 576 of file mathkit.cpp.

ostream & mathkit::operator<< ( ostream &  outs,
const Vector &  vec 
)

Stream insertion operator for vector.

Parameters
outsThe output stream.
vecThe vector.
Returns
The reference of the output stream.

Definition at line 586 of file mathkit.cpp.

istream & mathkit::operator>> ( istream &  ins,
Vector &  vec 
)

Stream extraction operator for vector.

Parameters
insThe input stream.
vecThe vector.
Returns
The reference of the input stream.

Definition at line 591 of file mathkit.cpp.

double mathkit::randf ( double  low,
double  high 
)

Floating point random number in rang [low, high] inclusive.

Parameters
lowThe low boundary of the range.
highThe high boundary of the range.
Returns
A floating point random number in range [low, high] inclusive.

Definition at line 596 of file mathkit.cpp.

Vector mathkit::randf ( double  low,
double  high,
int  n 
)

Floating point random numbers in rang [low, high] inclusive.

Parameters
lowThe low boundary of the range.
highThe high boundary of the range.
nThe count of random numbers to generate.
Returns
A vector contains random numbers in range [low, high] inclusive.

Definition at line 600 of file mathkit.cpp.

int mathkit::randi ( int  low,
int  high 
)

Integer random number in range [low, high] inclusive.

Parameters
lowThe low boundary of the range.
highThe high boundary of the range.
Returns
An integer random number in range [low, high] inclusive.

Definition at line 606 of file mathkit.cpp.

Vector mathkit::randi ( int  low,
int  high,
int  n 
)

Integer random numbers in range [low, high] inclusive.

Parameters
lowThe low boundary of the range.
highThe high boundary of the range.
nThe count of random numbers to generate.
Returns
A vector contains integer random numbers in range [low, high] inclusive.

Definition at line 610 of file mathkit.cpp.

bool mathkit::randp ( double  p)

0~1 distribution.

This function simulate a boolean random variable whose probability to be true is p. So if you set p to 0.5 and run this function 100 times, the times it returns true is approximately 50.

Parameters
pThe probability between 0 and 1.
Returns
A boolean random value. It's probability to be true is p.

Definition at line 616 of file mathkit.cpp.

void mathkit::randseed ( )

Set seed for pseudo-random number generator.

This function set the seed according to the current system date-time.

Definition at line 620 of file mathkit.cpp.

void mathkit::randseed ( unsigned int  s)

Set seed for pseudo-random number generator.

Parameters
sThe seed.

Definition at line 624 of file mathkit.cpp.

double mathkit::rnorm ( double  mu = 0,
double  sigma = 1 
)

Random number of normal distribution.

Parameters
muThe mean.
sigmaThe standard deviation.
Returns
A random number of normal distribution.

Definition at line 628 of file mathkit.cpp.

Vector mathkit::rnorm ( int  n,
double  mu = 0,
double  sigma = 1 
)

Random numbers of normal distribution.

Parameters
nThe count of random numbers.
muThe mean.
sigmaThe standard deviation.
Returns
A vector contains random numbers of normal distribution.

Definition at line 635 of file mathkit.cpp.

int mathkit::rpois ( double  lmd)

Random number of poisson distribution.

Parameters
lmdThe lambda parameter (> 0).
Returns
An integer random number of poisson distribution.

Definition at line 641 of file mathkit.cpp.

Vector mathkit::rpois ( double  lmd,
int  n 
)

Random numbers of poisson distribution.

Parameters
lmdThe lambda parameter (> 0).
nThe count of random numbers.
Returns
A vector contains random numbers of poisson distribution.

Definition at line 652 of file mathkit.cpp.

int mathkit::rbinom ( int  n,
double  p 
)

Random number of binomial distribution.

Parameters
nThe number of independent experiments.
pThe occurrence probability of specific event in one experiment.
Returns
An integer random number of binomial distribution.

Definition at line 658 of file mathkit.cpp.

Vector mathkit::rbinom ( int  n,
double  p,
int  count 
)

Random numbers of binomial distribution.

Parameters
nThe number of independent experiments.
pThe occurrence probability of specific event in one experiment.
countThe count of random numbers.
Returns
A vector contains random numbers of binomial distribution.

Definition at line 665 of file mathkit.cpp.

double mathkit::rexp ( double  theta)

Random number of exponential distribution.

Parameters
thetaThe parameter of exponential distribution (1/rate).
Returns
A random number of exponential distribution.

Definition at line 671 of file mathkit.cpp.

Vector mathkit::rexp ( double  theta,
int  n 
)

Random numbers of exponential distribution.

Parameters
thetaThe parameter of exponential distribution (1/rate).
nThe count of random numbers.
Returns
A vector contains random numbers of exponential distribution.

Definition at line 675 of file mathkit.cpp.

double mathkit::prec ( double  num,
int  ndec = 2 
)

Round a number to specific decimal digits.

Parameters
numThe number to round.
ndecThe number of decimal digits.
Returns
A number with specific decimal digitals.

Definition at line 681 of file mathkit.cpp.

Vector mathkit::prec ( const Vector &  vec,
int  ndec = 4 
)

Round every component of a vector to specific decimal digits.

Parameters
vecThe vector.
ndecThe number of decimal digits.
Returns
A vector contains the rounded data.

Definition at line 692 of file mathkit.cpp.

double mathkit::fac ( unsigned int  n)

Factorial of integer n (n!).

Parameters
nThe integer number.
Returns
The factorial (n!).

Definition at line 700 of file mathkit.cpp.

double mathkit::perm ( unsigned int  m,
unsigned int  n 
)

Permutation number (mPn).

mPn = m! / (m - n)!

Parameters
mThe total number of all probable events.
nThe selected number of events.
Returns
The permutation number.

Definition at line 712 of file mathkit.cpp.

double mathkit::comb ( unsigned int  m,
unsigned int  n 
)

Combination number (mCn).

mCn = mPn / n!

Parameters
mThe total number of all probable events.
nThe selected number of events.
Returns
The combination number.

Definition at line 722 of file mathkit.cpp.

unsigned int mathkit::gcd ( unsigned int  a,
unsigned int  b 
)

Greatest common divisor.

Parameters
aAn integer.
bAnother integer.
Returns
The greatest common divisor of these two integers.

Definition at line 727 of file mathkit.cpp.

unsigned int mathkit::lcm ( unsigned int  a,
unsigned int  b 
)

Least common multiple.

Parameters
aAn integer.
bAnother integer.
Returns
The least common multiple of these two integers.

Definition at line 736 of file mathkit.cpp.

template<typename Func >
Vector mathkit::each ( Func  func,
const Vector &  data 
)

Apply a function to each component of a vector.

Parameters
funcThe function to apply (double func(double)).
dataThe vector.
Returns
A vector contains the results.

Definition at line 3 of file mathkit.hpp.

template<typename Func >
Vector mathkit::filter ( Func  func,
const Vector &  data 
)

Filter a vector of data.

Parameters
funcThe filter function (bool func(double)).
dataThe vector of data to filter.
Returns
A vector contains the data that make the filter function returns true.

Definition at line 11 of file mathkit.hpp.

template<typename Func >
bool mathkit::probe ( Func  func,
Pair &  guess,
double  factor = 1.6,
int  ntry = 50 
)

Find a region contains at least one root.

Parameters
funcA function represents the equation func(x) == 0.
guessA guessed region as input, the real region as output if the probe succeeds.
factorThe scale factor used to adjust the region in every iteration.
ntryThe maximum times to be tried.
Returns
A boolean value indicates whether the probe succeeds.

Definition at line 20 of file mathkit.hpp.

template<typename Func >
Pairs mathkit::scan ( Func  func,
Pair  scope,
int  nr 
)

Scan a specific scope to find sub-regions contain root.

Parameters
funcA function represents the equation func(x) == 0.
scopeThe scope to scan.
nrThe number of sub-regions by which the scope will be sliced.
Returns
A vector holds the sub-regions contain root.

Definition at line 39 of file mathkit.hpp.

template<typename Func >
double mathkit::solve ( Func  func,
Pair  region,
const Epsilon &  eps = Epsilon() 
)

Find a root in a specific region using binary search algorithm.

Parameters
funcA function represents the equation func(x) == 0.
regionThe region to search.
epsAn Epsilon functor to control accuracy.
Returns
The root found.

Definition at line 49 of file mathkit.hpp.

template<typename Func >
double mathkit::integrate ( Func  func,
Pair  region,
const Epsilon &  eps = Epsilon() 
)

Numerical integration.

Parameters
funcThe integrand function (double func(double)).
regionThe definite integral region.
epsAn Epsilon functor to control accurary.
Returns
The definite integration.

Definition at line 63 of file mathkit.hpp.

template<typename T >
Vector mathkit::a2vec ( data[],
int  len 
)

Convert a C array to a C++ vector.

Parameters
dataThe array contains the data.
lenThe length/size (number of elements) of the array.
Returns
A vector contains the same data of the array.

Definition at line 98 of file mathkit.hpp.

Matrix mathkit::operator* ( const Matrix &  mat,
double  scalar 
)

Scalar multiplication for matrix.

Parameters
matThe matrix.
scalarThe scalar value.
Returns
A matrix contains the result.

Definition at line 224 of file Matrix.cpp.

Matrix mathkit::operator* ( double  scalar,
const Matrix &  mat 
)

Scalar multiplication for matrix.

Parameters
scalarThe scalar value.
matThe matrix.
Returns
A matrix contains the result.

Definition at line 231 of file Matrix.cpp.

ostream & mathkit::operator<< ( ostream &  outs,
const Matrix &  mat 
)

Stream insertion operator for matrix.

Parameters
outsThe output stream.
matThe matrix.
Returns
The reference of the output stream.

Definition at line 235 of file Matrix.cpp.

istream & mathkit::operator>> ( istream &  ins,
Matrix &  mat 
)

Stream extraction operator for matrix.

Parameters
insThe input stream.
matThe matrix.
Returns
The reference of the input stream.

Definition at line 240 of file Matrix.cpp.

Matrix mathkit::t ( const Matrix &  mat)

Transpose matrix.

Parameters
matThe original matrix.
Returns
The transposed matrix.

Definition at line 246 of file Matrix.cpp.

Matrix mathkit::eye ( int  n)

Generate an identity matrix (n by n).

Parameters
nThe number of rows/columns of the matrix.
Returns
The identity matrix.

Definition at line 250 of file Matrix.cpp.

Vector mathkit::diag ( const Matrix &  mat)

Get the diagonal elements of a square matrix.

Parameters
matThe square matrix.
Returns
A vector contains the diagonal elements.

Definition at line 256 of file Matrix.cpp.

Matrix mathkit::diag ( const Vector &  vec)

Generate a diagonal matrix with specific diagonal elements.

Parameters
vecA vector contains the diagonal elements.
Returns
The diagonal matrix.

Definition at line 263 of file Matrix.cpp.

double mathkit::det ( const Matrix &  mat,
const Epsilon &  eps = Epsilon() 
)

Determinant.

Parameters
matThe square matrix to calculate determinant.
epsAn Epsilon functor to test zero elements.
Returns
The determinant of the square matrix.

Definition at line 270 of file Matrix.cpp.

Matrix mathkit::inv ( const Matrix &  mat,
const Epsilon &  eps = Epsilon() 
)

Inverse matrix.

Parameters
matThe original matrix.
epsAn Epsilon functor to test zero elements.
Returns
The inverse matrix.

Definition at line 285 of file Matrix.cpp.

double mathkit::trace ( const Matrix &  mat,
const Epsilon &  eps = Epsilon() 
)

Trace (sum of main diagonal elements) of square matrix.

Parameters
matThe square matrix.
epsAn Epsilon functor to test zero elements.
Returns
The trace of the square matrix.

Definition at line 299 of file Matrix.cpp.

bool mathkit::positive ( const Matrix &  mat,
const Epsilon &  eps = Epsilon() 
)

Check whether a symmetric matrix is positive definite.

Parameters
matThe symmetric matrix.
epsAn Epsilon functor to test zero elements.
Returns
True if the matrix is positive definite else false.

Definition at line 308 of file Matrix.cpp.

Table mathkit::table ( const Vector &  data,
Dimen  dimen,
bool  byrow = true 
)

Generate a table from a vector.

Parameters
dataA vector contains the table data.
dimenThe dimension of the table.
byrowThe data is in row major or column major form.
Returns
A table.

Definition at line 319 of file Matrix.cpp.

Matrix mathkit::rowmat ( const Vector &  data)

Generate an one row matrix (row vector in linear algebra).

Parameters
dataA vector contains the data.
Returns
An one row matrix.

Definition at line 336 of file Matrix.cpp.

Matrix mathkit::colmat ( const Vector &  data)

Generate an one column matrix (column vector in linear algebra).

Parameters
dataA vector contains the data.
Returns
An one column matrix.

Definition at line 340 of file Matrix.cpp.

Matrix mathkit::cbind ( const Matrix &  mat1,
const Matrix &  mat2 
)

Construct a matrix by column binding.

Parameters
mat1The first matrix.
mat2The second matrix.
Returns
A matrix binds the columns of mat1 and mat2.

Definition at line 344 of file Matrix.cpp.

Matrix mathkit::rbind ( const Matrix &  mat1,
const Matrix &  mat2 
)

Construct a matrix by row binding.

Parameters
mat1The first matrix.
mat2The second matrix.
Returns
A matrix binds the rows of mat1 and mat2.

Definition at line 352 of file Matrix.cpp.

Matrices mathkit::lu ( const Matrix &  mat,
const Epsilon &  eps = Epsilon() 
)

LU decomposition.

Parameters
matThe matrix to decomposite.
epsAn Epsilon functor to test zero elements.
Returns
A vector contains the L (lower triangular), U (upper triangular) and P (permutation) matrices if succeed, else an empty vector.

Definition at line 367 of file Matrix.cpp.

Matrices mathkit::qr ( const Matrix &  mat,
const Epsilon &  eps = Epsilon() 
)

QR decomposition.

Parameters
matThe matrix to decomposite.
epsAn Epsilon functor to test zero elements.
Returns
If succeed return a vector contains the matrices Q and R, else return an empty vector.

Definition at line 405 of file Matrix.cpp.

Vector mathkit::linsolve ( const Matrix &  mat,
const Vector &  vec,
const Epsilon &  eps = Epsilon() 
)

Solve a linear system (Ax = b).

Parameters
matThe coefficients matrix of the linear system.
vecThe column vector on the right side.
epsAn Epsilon functor to test zero elements.
Returns
If succeed return a vector contains the solutions else return an empty vector.

Definition at line 425 of file Matrix.cpp.

Vector mathkit::lusolve ( const Matrices &  lup,
const Vector &  vec,
const Epsilon &  eps = Epsilon() 
)

Solve a linear system represented by L, U, P (Ax = b, PA = LU).

Parameters
lupA vector contains the L, U and P matrices.
vecThe column vector on the right side.
epsAn Epsilon functor to test zero elements.
Returns
A vector contains the solutions if succeed, else an empty vector.

Definition at line 429 of file Matrix.cpp.

Vector mathkit::qrsolve ( const Matrices &  qrmat,
const Vector &  vec,
const Epsilon &  eps = Epsilon() 
)

Solve a linear system represented by Q, R (Ax = b, A = QR).

Parameters
qrmatA vector contains the Q and R matrices.
vecThe column vector on the right side.
epsAn Epsilon functor to test zero elements.
Returns
If succeed return a vector contains the solutions, else return an empty vector.

Definition at line 453 of file Matrix.cpp.

string mathkit::to_str ( const Matrix &  mat,
bool  byrow = true,
string  delim = "[]" 
)

The string representation of a matrix.

Parameters
matThe matrix.
byrowWhether the string representation is in row major or column major form.
delimThe delimiter string.
Returns
The string representation of the matrix.

Definition at line 471 of file Matrix.cpp.