mathkit  2.0
 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 of regression line. More...
 
class  Matrix
 Matrix class. More...
 
struct  Elim
 A structure represents the result of Gaussian elimination. More...
 
struct  Eigen
 Eigen values and eigen vectors. 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 dexp (double theta, double x)
 Probability density of exponential distribution. More...
 
double pexp (double theta, double x)
 Cumulative probability (CDF) of exponential distribution. More...
 
double qexp (double theta, double p, const Epsilon &eps=Epsilon())
 Quantile of exponential distribution. 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 dchisq (double x, int k)
 Probability density of chi square distribution. More...
 
double pchisq (double x, int k)
 Cumulative probability (CDF) of chi square distribution. More...
 
double qchisq (double p, int k, const Epsilon &eps=Epsilon())
 Quantile of chi square distribution. More...
 
double df (double x, int d1, int d2)
 Probability density of F distribution. More...
 
double pf (double x, int d1, int d2)
 Cumulative probability (CDF) of F distribution. More...
 
double qf (double p, int d1, int d2, const Epsilon &eps=Epsilon())
 Quantile of F 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 nb (int k, double p, int x, bool cum=false)
 Probability of negative binomial distribution. More...
 
double hg (int s, int k, int n, int x, bool cum=false)
 Probability of hypergeometric distribution. More...
 
double gamma (double x)
 Gamma function. More...
 
double lgamma (double x)
 Logarithm gamma function. More...
 
double igamma (double s, double x, const Epsilon &eps=Epsilon(1e-15))
 Incomplete (lower) gamma function. More...
 
double beta (double x, double y)
 Beta function. More...
 
double lbeta (double x, double y)
 Logarithm beta function. More...
 
double ibeta (double x, double a, double b, const Epsilon &eps=Epsilon(1e-15))
 Regularized incomplete beta function. More...
 
LineParam linregress (const Vector &xdata, const Vector &ydata)
 Linear regression. More...
 
Vector sample (const Vector &data, int n)
 Make a random sample without replacement. 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...
 
bool zero (const Vector &vec, const Epsilon &eps=Epsilon())
 Test if the vector is a zero vector (all elements are zero). 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...
 
double rexp (double theta)
 Random number of exponential distribution. More...
 
Vector rexp (double theta, int n)
 Random numbers of exponential 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...
 
int rnb (int k, double p)
 Random number of negative binomial distribution. More...
 
Vector rnb (int k, double p, int count)
 Random numbers of negative binomial distribution. More...
 
int rhg (int s, int k, int n)
 Random number of hypergeometric distribution. More...
 
Vector rhg (int s, int k, int n, int count)
 Random number of hypergeometric distribution. More...
 
double prec (double num, int ndec=4)
 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 (int n)
 Factorial of integer n (n!). More...
 
double perm (int m, int n)
 Permutation number (mPn). More...
 
double comb (int m, 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...
 
Vector eigval (const Matrix &mat, const Epsilon &eps=Epsilon())
 Eigen values of square matrix. More...
 
Eigen eigen (const Matrix &mat, const Epsilon &eps=Epsilon())
 Eigen values and vectors of symmetric matrix. More...
 
Matrices svd (const Matrix &mat, const Epsilon &eps=Epsilon())
 Singular value decomposition (A = UWV'). More...
 
Elim elim (const Matrix &mat, bool augment=false, const Epsilon &eps=Epsilon())
 Gaussian elimination. More...
 
Matrix nullbasis (const Matrix &mat, bool augment=false, const Epsilon &eps=Epsilon())
 Basis of null space. 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...
 
Vector upsolve (const Matrix &upmat, const Vector &vec, const Epsilon &eps=Epsilon())
 Solve a linear system Ux = b (U is upper triangular). 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 25 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 29 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 33 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 37 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 41 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 45 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 49 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 56 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 63 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 79 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 95 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 99 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 106 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 116 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 125 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 129 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 145 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 149 of file mathkit.cpp.

double mathkit::skew ( const Vector &  data)

Skewness.

Parameters
dataA vector contains the data.
Returns
The skewness.

Definition at line 164 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 168 of file mathkit.cpp.

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

Probability density of exponential distribution.

Parameters
thetaThe parameter of exponential distribution (1/rate).
xA specific value of this exponential variate.
Returns
The value of the probability density function at x.

Definition at line 174 of file mathkit.cpp.

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

Cumulative probability (CDF) 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 179 of file mathkit.cpp.

double mathkit::qexp ( double  theta,
double  p,
const Epsilon &  eps = Epsilon() 
)

Quantile of exponential distribution.

Parameters
thetaThe parameter of exponential distribution (1/rate).
pThe cumulative probability.
epsAn Epsilon functor to control accuracy.
Returns
The quantile x makes P(X <= x) == p.

Definition at line 184 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 200 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 205 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 225 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 250 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 254 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 268 of file mathkit.cpp.

double mathkit::dchisq ( double  x,
int  k 
)

Probability density of chi square distribution.

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

Definition at line 293 of file mathkit.cpp.

double mathkit::pchisq ( double  x,
int  k 
)

Cumulative probability (CDF) of chi square distribution.

Parameters
xA specific value of the variate.
kThe degrees of freedom.
Returns
P(X <= x).

Definition at line 297 of file mathkit.cpp.

double mathkit::qchisq ( double  p,
int  k,
const Epsilon &  eps = Epsilon() 
)

Quantile of chi square distribution.

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

Definition at line 302 of file mathkit.cpp.

double mathkit::df ( double  x,
int  d1,
int  d2 
)

Probability density of F distribution.

Parameters
xA specific value of the variate.
d1The first degrees of freedom.
d2The second degrees of freedom.
Returns
The value of the probability density function at x.

Definition at line 319 of file mathkit.cpp.

double mathkit::pf ( double  x,
int  d1,
int  d2 
)

Cumulative probability (CDF) of F distribution.

Parameters
xA specific value of the variate.
d1The first degrees of freedom.
d2The second degrees of freedom.
Returns
P(X <= x).

Definition at line 324 of file mathkit.cpp.

double mathkit::qf ( double  p,
int  d1,
int  d2,
const Epsilon &  eps = Epsilon() 
)

Quantile of F distribution.

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

Definition at line 329 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 346 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 353 of file mathkit.cpp.

double mathkit::nb ( int  k,
double  p,
int  x,
bool  cum = false 
)

Probability of negative binomial distribution.

Parameters
kThe target number of successes to be attained.
pThe success probability in one independent trial.
xThe number of failures obtained before the kth success.
cumCalculate P(X <= x) if cum is true, else calculate P(X = x).
Returns
P(X <= x) if cum is true, else P(X = x).

Definition at line 360 of file mathkit.cpp.

double mathkit::hg ( int  s,
int  k,
int  n,
int  x,
bool  cum = false 
)

Probability of hypergeometric distribution.

Parameters
sThe sample size.
kThe number of successes contained in population.
nThe population size.
xThe number of successes in a specific sample.
cumCalculate P(X <= x) if cum is true, else calculate P(X = x).
Returns
P(X <= x) if cum is true, else P(X = x).

Definition at line 367 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 374 of file mathkit.cpp.

double mathkit::lgamma ( double  x)

Logarithm gamma function.

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

Definition at line 378 of file mathkit.cpp.

double mathkit::igamma ( double  s,
double  x,
const Epsilon &  eps = Epsilon(1e-15) 
)

Incomplete (lower) gamma function.

Parameters
sThe first parameter of lower gamma function.
xThe second parameter of lower gamma function.
epsAn Epsilon functor to control accurary.
Returns
The value of lower gamma function.

Definition at line 393 of file mathkit.cpp.

double mathkit::beta ( double  x,
double  y 
)

Beta function.

Parameters
xThe first parameter of beta function.
yThe second parameter of beta function.
Returns
The value of beta function.

Definition at line 409 of file mathkit.cpp.

double mathkit::lbeta ( double  x,
double  y 
)

Logarithm beta function.

Parameters
xThe first parameter of logarithm beta function.
yThe second parameter of logarithm beta function.
Returns
The value of logarithm beta function.

Definition at line 413 of file mathkit.cpp.

double mathkit::ibeta ( double  x,
double  a,
double  b,
const Epsilon &  eps = Epsilon(1e-15) 
)

Regularized incomplete beta function.

Parameters
xThe argument of incomplete beta function (0 < x < 1).
aThe first parameter of incomplete beta function (a > 0).
bThe second parameter of incomplete beta function (b > 0).
epsAn Epsilon functor to control accurary.
Returns
The value of regularized incomplete beta function.

Definition at line 417 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 parameters of the regression line.
See Also
LineParam

Definition at line 436 of file mathkit.cpp.

Vector mathkit::sample ( const Vector &  data,
int  n 
)

Make a random sample without replacement.

Parameters
dataA vector contains the population data.
nThe sample size.
Returns
A vector contains the sample.

Definition at line 454 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 475 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 494 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 513 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 517 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 521 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 525 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 529 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 542 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 549 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 553 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 557 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 561 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 574 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 585 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 589 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 593 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 597 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 610 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 617 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 621 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 625 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 629 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 642 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 652 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 656 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 660 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 664 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 677 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 685 of file mathkit.cpp.

bool mathkit::zero ( const Vector &  vec,
const Epsilon &  eps = Epsilon() 
)

Test if the vector is a zero vector (all elements are zero).

Parameters
vecThe vector to test.
epsAn Epsilon functor to control accurary.
Returns
True if the vector is a zero vector else false.

Definition at line 689 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 696 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 710 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 719 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 737 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 746 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 759 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 769 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 774 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 779 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 783 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 789 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 793 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 799 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 803 of file mathkit.cpp.

void mathkit::randseed ( unsigned int  s)

Set seed for pseudo-random number generator.

Parameters
sThe seed.

Definition at line 807 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 811 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 818 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 824 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 828 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 834 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 845 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 851 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 858 of file mathkit.cpp.

int mathkit::rnb ( int  k,
double  p 
)

Random number of negative binomial distribution.

Parameters
kThe target number of successes to be attained.
pThe success probability in one independent trial.
Returns
An integer random number of negative binomial distribution.

Definition at line 864 of file mathkit.cpp.

Vector mathkit::rnb ( int  k,
double  p,
int  count 
)

Random numbers of negative binomial distribution.

Parameters
kThe target number of successes to be attained.
pThe success probability in one independent trial.
countThe count of random numbers.
Returns
A vector contains random numbers of negative binomial distribution.

Definition at line 871 of file mathkit.cpp.

int mathkit::rhg ( int  s,
int  k,
int  n 
)

Random number of hypergeometric distribution.

Parameters
sThe sample size.
kThe number of successes contained in population.
nThe population size.
Returns
An integer random number of hypergeometric distribution.

Definition at line 877 of file mathkit.cpp.

Vector mathkit::rhg ( int  s,
int  k,
int  n,
int  count 
)

Random number of hypergeometric distribution.

Parameters
sThe sample size.
kThe number of successes contained in population.
nThe population size.
countThe count of random numbers.
Returns
A vector contains random numbers of hypergeometric distribution.

Definition at line 885 of file mathkit.cpp.

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

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 891 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 908 of file mathkit.cpp.

double mathkit::fac ( int  n)

Factorial of integer n (n!).

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

Definition at line 916 of file mathkit.cpp.

double mathkit::perm ( int  m,
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 929 of file mathkit.cpp.

double mathkit::comb ( int  m,
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 940 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 947 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 956 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 257 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 264 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 268 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 273 of file Matrix.cpp.

Matrix mathkit::t ( const Matrix &  mat)

Transpose matrix.

Parameters
matThe original matrix.
Returns
The transposed matrix.

Definition at line 279 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 283 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 289 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 296 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 303 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 318 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 332 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 341 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 352 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 369 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 373 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 377 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 385 of file Matrix.cpp.

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

Eigen values of square matrix.

Parameters
matThe square matrix.
epsAn Epsilon functor to test zero elements.
Returns
A vector contains the eigen values.

Definition at line 400 of file Matrix.cpp.

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

Eigen values and vectors of symmetric matrix.

Parameters
matThe symmetric matrix.
epsAn Epsilon functor to control accuracy.
Returns
An Eigen structure contains the eigen values and vectors.

Definition at line 412 of file Matrix.cpp.

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

Singular value decomposition (A = UWV').

Parameters
matThe matrix to decompose.
epsAn Epsilon functor to control accuracy.
Returns
A vector contains the U, V and W matrices.

Definition at line 457 of file Matrix.cpp.

Elim mathkit::elim ( const Matrix &  mat,
bool  augment = false,
const Epsilon &  eps = Epsilon() 
)

Gaussian elimination.

Parameters
matThe coefficient matrix.
augmentWhether the matrix is an augment matrix.
epsAn Epsilon functor to test zero elements.
Returns
An Elim structure contains the result of Gaussian elimination.

Definition at line 483 of file Matrix.cpp.

Matrix mathkit::nullbasis ( const Matrix &  mat,
bool  augment = false,
const Epsilon &  eps = Epsilon() 
)

Basis of null space.

Parameters
matThe coefficient matrix.
augmentWhether the matrix is an augment matrix.
epsAn Epsilon functor to test zero elements.
Returns
A matrix whose columns consists of a basis of null space.

Definition at line 519 of file Matrix.cpp.

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

LU decomposition.

Parameters
matThe matrix to decompose.
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 544 of file Matrix.cpp.

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

QR decomposition.

Parameters
matThe matrix to decompose.
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 582 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 coefficient 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 605 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 609 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 633 of file Matrix.cpp.

Vector mathkit::upsolve ( const Matrix &  upmat,
const Vector &  vec,
const Epsilon &  eps = Epsilon() 
)

Solve a linear system Ux = b (U is upper triangular).

Parameters
upmatThe upper triangular coefficient matrix.
vecThe column vector on the right side.
epsAn Epsilon functor to test zero elements.
Returns
A vector contains the solutions.

Definition at line 652 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 667 of file Matrix.cpp.