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< Matrix > | Matrices |
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. | |
All functions of mathkit are in the mathkit namespace.
double mathkit::pv | ( | double | amount, |
double | rate, | ||
double | period | ||
) |
Present value of compound interest.
amount | The terminal/final value. |
rate | The compound interest rate. |
period | The number of periods spanned. |
Definition at line 21 of file mathkit.cpp.
double mathkit::fv | ( | double | amount, |
double | rate, | ||
double | period | ||
) |
Future value of compound interest.
amount | The present value (principal). |
rate | The compound interest rate. |
period | The number of periods spanned. |
Definition at line 25 of file mathkit.cpp.
double mathkit::pv_coef | ( | double | rate, |
double | period | ||
) |
Present value coefficient of compound interest.
rate | The compound interest rate. |
period | The number of periods spanned. |
Definition at line 29 of file mathkit.cpp.
double mathkit::fv_coef | ( | double | rate, |
double | period | ||
) |
Future value coefficient of compound interest.
rate | The compound interest rate. |
period | The number of periods spanned. |
Definition at line 33 of file mathkit.cpp.
double mathkit::apv | ( | double | annuity, |
double | rate, | ||
int | period, | ||
bool | prepaid = false |
||
) |
Present value of annuity.
annuity | The payment of every period. |
rate | The compound interest (discount) rate. |
period | The number of periods spanned. |
prepaid | Whether the annuity is an ordinary annuity or a prepaid 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.
annuity | The payment of every period. |
rate | The compound interest rate. |
period | The number of periods spanned. |
prepaid | Whether the annuity is an ordinary annuity or a prepaid 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.
rate | The compound interest (discount) rate. |
period | The number of periods spanned. |
prepaid | Whether the annuity is an ordinary annuity or a prepaid 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.
rate | The compound interest rate. |
period | The number of periods spanned. |
prepaid | Whether the annuity is an ordinary annuity or a prepaid 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).
amount | The payment of every period. |
rate | The compound interest (discount) rate. |
prepaid | Whether these payments are ordinary or prepaid (paid at the beginning of every period). |
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.
amount | The payment of every period. |
rate | The compound interest rate. |
prepaid | Whether these payments are ordinary or prepaid (paid at the beginning of every period). |
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.
pval | The present value (principal). |
fval | The future/final value. |
period | The number of periods spanned. |
Definition at line 91 of file mathkit.cpp.
double mathkit::mean | ( | const Vector & | data) |
Arithmatic mean.
data | The population or sample data. |
Definition at line 95 of file mathkit.cpp.
double mathkit::median | ( | const Vector & | data, |
bool | sorted = false |
||
) |
Median of a sequence of data.
data | The population or sample data. |
sorted | Whether the data are sorted. |
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.
data | The population or sample data. |
sample | Whether the data are population data or sample data. |
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.
data | The population or sample data. |
sample | Whether the data are population data or sample data. |
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.
data1 | The population or sample data of one group. |
data2 | The population or sample data of another group. |
sample | Whether the data are population data or sample data. |
Definition at line 125 of file mathkit.cpp.
double mathkit::cor | ( | const Vector & | data1, |
const Vector & | data2 | ||
) |
Correlation coefficient.
data1 | The data of one group. |
data2 | The data of another group. |
Definition at line 141 of file mathkit.cpp.
double mathkit::moment | ( | const Vector & | data, |
int | k, | ||
bool | central = true |
||
) |
Moment.
data | A vector contains the data. |
k | The degree of the moment. |
central | Central moment if true, else original moment. |
Definition at line 145 of file mathkit.cpp.
double mathkit::skew | ( | const Vector & | data) |
Skewness.
data | A vector contains the data. |
Definition at line 160 of file mathkit.cpp.
double mathkit::kurt | ( | const Vector & | data, |
bool | excess = true |
||
) |
Kurtosis.
data | A vector contains the data. |
excess | Whether to compute excess 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.
x | A specific value of the normal variate. |
mu | The mean. |
sigma | The standard deviation. |
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.
x | A specific value of the normal variate. |
mu | The mean. |
sigma | The standard deviation. |
eps | An Epsilon functor to control accuracy. |
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.
p | The cumulative probability. |
mu | The mean. |
sigma | The standard deviation. |
eps | An Epsilon functor to control accuracy. |
Definition at line 195 of file mathkit.cpp.
double mathkit::dt | ( | double | x, |
int | n | ||
) |
Probability density of t distribution.
x | A specific value of the variate. |
n | The degrees of freedom. |
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.
x | A specific value of the variate. |
n | The degrees of freedom. |
eps | An Epsilon functor to control accuracy. |
Definition at line 224 of file mathkit.cpp.
double mathkit::qt | ( | double | p, |
int | n, | ||
const Epsilon & | eps = Epsilon() |
||
) |
Quantile of t distribution.
p | The cumulative probability. |
n | The degrees of freedom. |
eps | An Epsilon functor to control accuracy. |
Definition at line 238 of file mathkit.cpp.
double mathkit::pois | ( | double | lmd, |
int | k, | ||
bool | cum = false |
||
) |
Probability of poisson distribution.
lmd | The lambda parameter (> 0). |
k | The occurrence number of specific event (k = 0, 1, 2, ...). |
cum | Calculate P(X <= k) if cum is true, else calculate 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.
n | The number of independent experiments. |
p | The occurrence probability of specific event in one experiment. |
k | The number of occurrences of the specific event in the n experiments. |
cum | Calculate P(X <= k) if cum is true, else calculate P(X = k). |
Definition at line 270 of file mathkit.cpp.
double mathkit::expo | ( | double | theta, |
double | x | ||
) |
Cumulative probability of exponential distribution.
theta | The parameter of exponential distribution (1/rate). |
x | A specific value of this exponential variate. |
Definition at line 277 of file mathkit.cpp.
double mathkit::gamma | ( | double | x) |
Gamma function.
x | The parameter of gamma function. |
Definition at line 282 of file mathkit.cpp.
LineParam mathkit::linregress | ( | const Vector & | xdata, |
const Vector & | ydata | ||
) |
Linear regression.
xdata | The data of independent variable. |
ydata | The data of dependent variable. |
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.
from | The start point of the sequence. |
to | The end point (may exclude) of the sequence. |
step | The step length of every progress. |
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.
start | The start point of the range. |
end | The end point of the range. |
count | The count of numbers in the sequence (including the start and end point). |
Definition at line 319 of file mathkit.cpp.
double mathkit::max | ( | const Vector & | data) |
Find the maximum value of the data.
data | A vector contains the data. |
Definition at line 338 of file mathkit.cpp.
double mathkit::min | ( | const Vector & | data) |
Find the minimum value of the data.
data | A vector contains the data. |
Definition at line 342 of file mathkit.cpp.
double mathkit::sum | ( | const Vector & | data) |
Calculate the sum of a number sequence.
data | The sequence of numbers. |
Definition at line 346 of file mathkit.cpp.
double mathkit::prod | ( | const Vector & | data) |
Calculate the product of a number sequence.
data | The sequence of numbers. |
Definition at line 350 of file mathkit.cpp.
Vector mathkit::add | ( | const Vector & | vec1, |
const Vector & | vec2 | ||
) |
Add two vectors of same dimension.
vec1 | The first vector. |
vec2 | The second vector. |
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.
vec | The vector. |
scalar | The scalar value. |
Definition at line 367 of file mathkit.cpp.
Vector mathkit::operator+ | ( | const Vector & | vec1, |
const Vector & | vec2 | ||
) |
Add two vectors of same dimension.
vec1 | The first vector. |
vec2 | The second vector. |
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.
vec | The vector. |
scalar | The scalar value. |
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.
scalar | The scalar value. |
vec | The vector. |
Definition at line 382 of file mathkit.cpp.
Vector mathkit::sub | ( | const Vector & | vec1, |
const Vector & | vec2 | ||
) |
Subtract two vectors of same dimension.
vec1 | The first vector. |
vec2 | The second vector. |
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.
vec | The vector. |
scalar | The scalar value. |
dir | If dir is true compute vec - scalar else compute scalar - vec. |
Definition at line 399 of file mathkit.cpp.
Vector mathkit::operator- | ( | const Vector & | vec1, |
const Vector & | vec2 | ||
) |
Subtract two vectors of same dimension.
vec1 | The first vector. |
vec2 | The second vector. |
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.
vec | The vector. |
scalar | The scalar value. |
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.
scalar | The scalar value. |
vec | The vector. |
Definition at line 418 of file mathkit.cpp.
Vector mathkit::mul | ( | const Vector & | vec1, |
const Vector & | vec2 | ||
) |
Multiply two vectors of same dimension elementwise.
vec1 | The first vector. |
vec2 | The second vector. |
Definition at line 422 of file mathkit.cpp.
Vector mathkit::mul | ( | const Vector & | vec, |
double | scalar | ||
) |
Multiply a vector and a scalar elementwise.
vec | The vector. |
scalar | The scalar value. |
Definition at line 435 of file mathkit.cpp.
Vector mathkit::operator* | ( | const Vector & | vec1, |
const Vector & | vec2 | ||
) |
Multiply two vectors of same dimension elementwise.
vec1 | The first vector. |
vec2 | The second vector. |
Definition at line 442 of file mathkit.cpp.
Vector mathkit::operator* | ( | const Vector & | vec, |
double | scalar | ||
) |
Multiply a vector and a scalar elementwise.
vec | The vector. |
scalar | The scalar value. |
Definition at line 446 of file mathkit.cpp.
Vector mathkit::operator* | ( | double | scalar, |
const Vector & | vec | ||
) |
Multiply a vector and a scalar elementwise.
scalar | The scalar value. |
vec | The vector. |
Definition at line 450 of file mathkit.cpp.
Vector mathkit::div | ( | const Vector & | vec1, |
const Vector & | vec2 | ||
) |
Divide two vectors of same dimension elementwise.
vec1 | The first vector. |
vec2 | The second vector. |
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.
vec | The vector. |
scalar | The scalar value. |
dir | If dir is true compute vec / scalar else compute scalar / vec. |
Definition at line 467 of file mathkit.cpp.
Vector mathkit::operator/ | ( | const Vector & | vec1, |
const Vector & | vec2 | ||
) |
Divide two vectors of same dimension elementwise.
vec1 | The first vector. |
vec2 | The second vector. |
Definition at line 477 of file mathkit.cpp.
Vector mathkit::operator/ | ( | const Vector & | vec, |
double | scalar | ||
) |
Divide a vector and a scalar elementwise.
vec | The vector. |
scalar | The scalar value. |
Definition at line 481 of file mathkit.cpp.
Vector mathkit::operator/ | ( | double | scalar, |
const Vector & | vec | ||
) |
Divide a vector and a scalar elementwise.
scalar | The scalar value. |
vec | The vector. |
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.
vec1 | The first vector. |
vec2 | The second vector. |
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.
vec1 | The first 3-dimensional vector. |
vec2 | The second 3-dimensional vector. |
Definition at line 502 of file mathkit.cpp.
double mathkit::norm | ( | const Vector & | vec) |
The Euclidean norm (length) of a vector.
vec | The vector. |
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.
vec | The vector. |
sep | The separator string. |
delim | The delimiter string. |
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.)
n | The number of components. |
... | The components (double type) of the vector. |
Definition at line 527 of file mathkit.cpp.
Vector mathkit::load | ( | istream & | ins, |
string | delim = " " |
||
) |
Load data from an input stream.
ins | The input stream. |
delim | The delimiter string. |
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.
ins | The input stream. |
nrow | The number of rows. |
ncol | The number of columns. |
delim | The delimiter string. |
Definition at line 554 of file mathkit.cpp.
void mathkit::save | ( | ostream & | outs, |
const Vector & | data, | ||
string | delim = " " |
||
) |
Save data to an output stream.
outs | The output stream. |
data | The data to be saved. |
delim | The 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.
outs | The output stream. |
data | The data to be saved. |
delim | The delimiter string. |
Definition at line 576 of file mathkit.cpp.
ostream & mathkit::operator<< | ( | ostream & | outs, |
const Vector & | vec | ||
) |
Stream insertion operator for vector.
outs | The output stream. |
vec | The vector. |
Definition at line 586 of file mathkit.cpp.
istream & mathkit::operator>> | ( | istream & | ins, |
Vector & | vec | ||
) |
Stream extraction operator for vector.
ins | The input stream. |
vec | The vector. |
Definition at line 591 of file mathkit.cpp.
double mathkit::randf | ( | double | low, |
double | high | ||
) |
Floating point random number in rang [low, high] inclusive.
low | The low boundary of the range. |
high | The high boundary of the range. |
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.
low | The low boundary of the range. |
high | The high boundary of the range. |
n | The count of random numbers to generate. |
Definition at line 600 of file mathkit.cpp.
int mathkit::randi | ( | int | low, |
int | high | ||
) |
Integer random number in range [low, high] inclusive.
low | The low boundary of the range. |
high | The high boundary of the range. |
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.
low | The low boundary of the range. |
high | The high boundary of the range. |
n | The count of random numbers to generate. |
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.
p | The probability between 0 and 1. |
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.
s | The seed. |
Definition at line 624 of file mathkit.cpp.
double mathkit::rnorm | ( | double | mu = 0 , |
double | sigma = 1 |
||
) |
Random number of normal distribution.
mu | The mean. |
sigma | The standard deviation. |
Definition at line 628 of file mathkit.cpp.
Vector mathkit::rnorm | ( | int | n, |
double | mu = 0 , |
||
double | sigma = 1 |
||
) |
Random numbers of normal distribution.
n | The count of random numbers. |
mu | The mean. |
sigma | The standard deviation. |
Definition at line 635 of file mathkit.cpp.
int mathkit::rpois | ( | double | lmd) |
Random number of poisson distribution.
lmd | The lambda parameter (> 0). |
Definition at line 641 of file mathkit.cpp.
Vector mathkit::rpois | ( | double | lmd, |
int | n | ||
) |
Random numbers of poisson distribution.
lmd | The lambda parameter (> 0). |
n | The count of random numbers. |
Definition at line 652 of file mathkit.cpp.
int mathkit::rbinom | ( | int | n, |
double | p | ||
) |
Random number of binomial distribution.
n | The number of independent experiments. |
p | The occurrence probability of specific event in one experiment. |
Definition at line 658 of file mathkit.cpp.
Vector mathkit::rbinom | ( | int | n, |
double | p, | ||
int | count | ||
) |
Random numbers of binomial distribution.
n | The number of independent experiments. |
p | The occurrence probability of specific event in one experiment. |
count | The count of random numbers. |
Definition at line 665 of file mathkit.cpp.
double mathkit::rexp | ( | double | theta) |
Random number of exponential distribution.
theta | The parameter of exponential distribution (1/rate). |
Definition at line 671 of file mathkit.cpp.
Vector mathkit::rexp | ( | double | theta, |
int | n | ||
) |
Random numbers of exponential distribution.
theta | The parameter of exponential distribution (1/rate). |
n | The count of random numbers. |
Definition at line 675 of file mathkit.cpp.
double mathkit::prec | ( | double | num, |
int | ndec = 2 |
||
) |
Round a number to specific decimal digits.
num | The number to round. |
ndec | The number of decimal digits. |
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.
vec | The vector. |
ndec | The number of decimal digits. |
Definition at line 692 of file mathkit.cpp.
double mathkit::fac | ( | unsigned int | n) |
Factorial of integer n (n!).
n | The integer number. |
Definition at line 700 of file mathkit.cpp.
double mathkit::perm | ( | unsigned int | m, |
unsigned int | n | ||
) |
Permutation number (mPn).
mPn = m! / (m - n)!
m | The total number of all probable events. |
n | The selected number of events. |
Definition at line 712 of file mathkit.cpp.
double mathkit::comb | ( | unsigned int | m, |
unsigned int | n | ||
) |
Combination number (mCn).
mCn = mPn / n!
m | The total number of all probable events. |
n | The selected number of events. |
Definition at line 722 of file mathkit.cpp.
unsigned int mathkit::gcd | ( | unsigned int | a, |
unsigned int | b | ||
) |
Greatest common divisor.
a | An integer. |
b | Another integer. |
Definition at line 727 of file mathkit.cpp.
unsigned int mathkit::lcm | ( | unsigned int | a, |
unsigned int | b | ||
) |
Least common multiple.
a | An integer. |
b | Another integer. |
Definition at line 736 of file mathkit.cpp.
Vector mathkit::each | ( | Func | func, |
const Vector & | data | ||
) |
Apply a function to each component of a vector.
func | The function to apply (double func(double)). |
data | The vector. |
Definition at line 3 of file mathkit.hpp.
Vector mathkit::filter | ( | Func | func, |
const Vector & | data | ||
) |
Filter a vector of data.
func | The filter function (bool func(double)). |
data | The vector of data to filter. |
Definition at line 11 of file mathkit.hpp.
bool mathkit::probe | ( | Func | func, |
Pair & | guess, | ||
double | factor = 1.6 , |
||
int | ntry = 50 |
||
) |
Find a region contains at least one root.
func | A function represents the equation func(x) == 0. |
guess | A guessed region as input, the real region as output if the probe succeeds. |
factor | The scale factor used to adjust the region in every iteration. |
ntry | The maximum times to be tried. |
Definition at line 20 of file mathkit.hpp.
Pairs mathkit::scan | ( | Func | func, |
Pair | scope, | ||
int | nr | ||
) |
Scan a specific scope to find sub-regions contain root.
func | A function represents the equation func(x) == 0. |
scope | The scope to scan. |
nr | The number of sub-regions by which the scope will be sliced. |
Definition at line 39 of file mathkit.hpp.
double mathkit::solve | ( | Func | func, |
Pair | region, | ||
const Epsilon & | eps = Epsilon() |
||
) |
Find a root in a specific region using binary search algorithm.
func | A function represents the equation func(x) == 0. |
region | The region to search. |
eps | An Epsilon functor to control accuracy. |
Definition at line 49 of file mathkit.hpp.
double mathkit::integrate | ( | Func | func, |
Pair | region, | ||
const Epsilon & | eps = Epsilon() |
||
) |
Numerical integration.
func | The integrand function (double func(double)). |
region | The definite integral region. |
eps | An Epsilon functor to control accurary. |
Definition at line 63 of file mathkit.hpp.
Vector mathkit::a2vec | ( | T | data[], |
int | len | ||
) |
Convert a C array to a C++ vector.
data | The array contains the data. |
len | The length/size (number of elements) of the array. |
Definition at line 98 of file mathkit.hpp.
Matrix mathkit::operator* | ( | const Matrix & | mat, |
double | scalar | ||
) |
Scalar multiplication for matrix.
mat | The matrix. |
scalar | The scalar value. |
Definition at line 224 of file Matrix.cpp.
Matrix mathkit::operator* | ( | double | scalar, |
const Matrix & | mat | ||
) |
Scalar multiplication for matrix.
scalar | The scalar value. |
mat | The matrix. |
Definition at line 231 of file Matrix.cpp.
ostream & mathkit::operator<< | ( | ostream & | outs, |
const Matrix & | mat | ||
) |
Stream insertion operator for matrix.
outs | The output stream. |
mat | The matrix. |
Definition at line 235 of file Matrix.cpp.
istream & mathkit::operator>> | ( | istream & | ins, |
Matrix & | mat | ||
) |
Stream extraction operator for matrix.
ins | The input stream. |
mat | The matrix. |
Definition at line 240 of file Matrix.cpp.
Matrix mathkit::t | ( | const Matrix & | mat) |
Transpose matrix.
mat | The original matrix. |
Definition at line 246 of file Matrix.cpp.
Matrix mathkit::eye | ( | int | n) |
Generate an identity matrix (n by n).
n | The number of rows/columns of the matrix. |
Definition at line 250 of file Matrix.cpp.
Vector mathkit::diag | ( | const Matrix & | mat) |
Get the diagonal elements of a square matrix.
mat | The square matrix. |
Definition at line 256 of file Matrix.cpp.
Matrix mathkit::diag | ( | const Vector & | vec) |
Generate a diagonal matrix with specific diagonal elements.
vec | A vector contains the diagonal elements. |
Definition at line 263 of file Matrix.cpp.
double mathkit::det | ( | const Matrix & | mat, |
const Epsilon & | eps = Epsilon() |
||
) |
Determinant.
mat | The square matrix to calculate determinant. |
eps | An Epsilon functor to test zero elements. |
Definition at line 270 of file Matrix.cpp.
Matrix mathkit::inv | ( | const Matrix & | mat, |
const Epsilon & | eps = Epsilon() |
||
) |
Inverse matrix.
mat | The original matrix. |
eps | An Epsilon functor to test zero elements. |
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.
mat | The square matrix. |
eps | An Epsilon functor to test zero elements. |
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.
mat | The symmetric matrix. |
eps | An Epsilon functor to test zero elements. |
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.
data | A vector contains the table data. |
dimen | The dimension of the table. |
byrow | The data is in row major or column major form. |
Definition at line 319 of file Matrix.cpp.
Matrix mathkit::rowmat | ( | const Vector & | data) |
Generate an one row matrix (row vector in linear algebra).
data | A vector contains the data. |
Definition at line 336 of file Matrix.cpp.
Matrix mathkit::colmat | ( | const Vector & | data) |
Generate an one column matrix (column vector in linear algebra).
data | A vector contains the data. |
Definition at line 340 of file Matrix.cpp.
Matrix mathkit::cbind | ( | const Matrix & | mat1, |
const Matrix & | mat2 | ||
) |
Construct a matrix by column binding.
mat1 | The first matrix. |
mat2 | The second matrix. |
Definition at line 344 of file Matrix.cpp.
Matrix mathkit::rbind | ( | const Matrix & | mat1, |
const Matrix & | mat2 | ||
) |
Construct a matrix by row binding.
mat1 | The first matrix. |
mat2 | The second matrix. |
Definition at line 352 of file Matrix.cpp.
Matrices mathkit::lu | ( | const Matrix & | mat, |
const Epsilon & | eps = Epsilon() |
||
) |
LU decomposition.
mat | The matrix to decomposite. |
eps | An Epsilon functor to test zero elements. |
Definition at line 367 of file Matrix.cpp.
Matrices mathkit::qr | ( | const Matrix & | mat, |
const Epsilon & | eps = Epsilon() |
||
) |
QR decomposition.
mat | The matrix to decomposite. |
eps | An Epsilon functor to test zero elements. |
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).
mat | The coefficients matrix of the linear system. |
vec | The column vector on the right side. |
eps | An Epsilon functor to test zero elements. |
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).
lup | A vector contains the L, U and P matrices. |
vec | The column vector on the right side. |
eps | An Epsilon functor to test zero elements. |
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).
qrmat | A vector contains the Q and R matrices. |
vec | The column vector on the right side. |
eps | An Epsilon functor to test zero elements. |
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.
mat | The matrix. |
byrow | Whether the string representation is in row major or column major form. |
delim | The delimiter string. |
Definition at line 471 of file Matrix.cpp.