C++11下的<random>与<cmath>
注:最近搞数据挖掘跟各种分布拟合、采样打交道,Matlab用着非常的爽,不过商业软件你懂得~~~本想C++调用.m未果,就只好.m调用.exe了,虽然实验挺顺的,但是想着这样不适合搞应用啊,就想找些第三方C++库来搞统计分布之类的东西,可搜“C++各大有名科学计算库”似乎GNU Scientific Library (linux)-GSL就够用的了,Boost中的math方法也不是很全,感觉与C++11中比较增加不是很多,以后的策略就是先看C++11中<cmath>及<random>中有没有,没有的话再调用GSL库。
安装g++ 4.7
g++ 4.7以上才支持C++11.
g++ 4.7要用-std=c++11
来开启对C++11的支持,如
$$ g++ -std=c++11 hello.cpp -o hello
Ubuntu
Ubuntu 12.04 LTS默认g++版本是4.6(Ubuntu 13.04默认4.7)
以下方法来自这里,还有这个方法,但没试过
-
You can add the repository using
$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
-
Then, to install it use
$ sudo apt-get update $ sudo apt-get install g++-4.7
-
To change the default compiler use update-alternatives
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7 $ sudo update-alternatives --config gcc
Win7
下载MinGW最新版安装即可,可自行google之
C++11中的随机数及各种分布
首先要包含头文件 #include <random>
- 均匀分布:
- uniform_int_distribution 整数均匀分布
- uniform_real_distribution 浮点数均匀分布
- 伯努利类型分布:(仅有yes/no两种结果,概率一个p,一个1-p)
- bernoulli_distribution 伯努利分布
- binomial_distribution 二项分布
- geometry_distribution 几何分布
- negative_biomial_distribution 负二项分布
- Rate-based distributions:
- poisson_distribution 泊松分布
- exponential_distribution 指数分布
- gamma_distribution 伽马分布
- weibull_distribution 威布尔分布
- extreme_value_distribution 极值分布
- 正态分布相关:
- normal_distribution 正态分布
- lognormal_distribution 对数正态分布
- chi_squared_distribution 卡方分布
- cauchy_distribution 柯西分布
- fisher_f_distribution 费歇尔F分布
- student_t_distribution t分布
- 分段分布相关:
- discrete_distribution 离散分布
- piecewise_constant_distribution 分段常数分布
- piecewise_linear_distribution 分段线性分布
<cmath>
<cmath>中包含计算Gamma函数的tgamma()
与计算log-gamma的lgamma()
(在C++11中添加的函数)
GSL
GSL Reference Manual
- Introduction:
- Using the library:
- Error Handling:
- Mathematical Functions:
- Complex Numbers:
- Polynomials:
- Special Functions:
- Vectors and Matrices:
- Permutations:
- Combinations:
- Multisets:
- Sorting:
- BLAS Support:
- Linear Algebra:
- Eigensystems:
- Fast Fourier Transforms:
- Numerical Integration:
- Random Number Generation:
- Quasi-Random Sequences:
- Random Number Distributions:
- Statistics:
- Histograms:
- N-tuples:
- Monte Carlo Integration:
- Simulated Annealing:
- Ordinary Differential Equations:
- Interpolation:
- Numerical Differentiation:
- Chebyshev Approximations:
- Series Acceleration:
- Wavelet Transforms:
- Discrete Hankel Transforms:
- One dimensional Root-Finding:
- One dimensional Minimization:
- Multidimensional Root-Finding:
- Multidimensional Minimization:
- Least-Squares Fitting:
- Nonlinear Least-Squares Fitting:
- Basis Splines:
- Physical Constants:
- IEEE floating-point arithmetic:
- Debugging Numerical Programs:
- Contributors to GSL:
- Autoconf Macros:
- GSL CBLAS Library:
- GNU General Public License:
- GNU Free Documentation License:
- Function Index:
- Variable Index:
- Type Index:
- Concept Index:
Special Functions
- Special Function Usage:
- The gsl_sf_result struct:
- Special Function Modes:
- Airy Functions and Derivatives:
- Bessel Functions:
- Clausen Functions:
- Coulomb Functions:
- Coupling Coefficients:
- Dawson Function:
- Debye Functions:
- Dilogarithm:
- Elementary Operations:
- Elliptic Integrals:
- Elliptic Functions (Jacobi):
- Error Functions:
- Exponential Functions:
- Exponential Integrals:
- Fermi-Dirac Function:
- Gamma and Beta Functions:
- Gegenbauer Functions:
- Hypergeometric Functions:
- Laguerre Functions:
- Lambert W Functions:
- Legendre Functions and Spherical Harmonics:
- Logarithm and Related Functions:
- Mathieu Functions:
- Power Function:
- Psi (Digamma) Function:
- Synchrotron Functions:
- Transport Functions:
- Trigonometric Functions:
- Zeta Functions:
- Special Functions Examples:
- Special Functions References and Further Reading:
Random Number Distributions
- Random Number Distribution Introduction:
- The Gaussian Distribution:
- The Gaussian Tail Distribution:
- The Bivariate Gaussian Distribution:
- The Exponential Distribution:
- The Laplace Distribution:
- The Exponential Power Distribution:
- The Cauchy Distribution:
- The Rayleigh Distribution:
- The Rayleigh Tail Distribution:
- The Landau Distribution:
- The Levy alpha-Stable Distributions:
- The Levy skew alpha-Stable Distribution:
- The Gamma Distribution:
- The Flat (Uniform) Distribution:
- The Lognormal Distribution:
- The Chi-squared Distribution:
- The F-distribution:
- The t-distribution:
- The Beta Distribution:
- The Logistic Distribution:
- The Pareto Distribution:
- Spherical Vector Distributions:
- The Weibull Distribution:
- The Type-1 Gumbel Distribution:
- The Type-2 Gumbel Distribution:
- The Dirichlet Distribution:
- General Discrete Distributions:
- The Poisson Distribution:
- The Bernoulli Distribution:
- The Binomial Distribution:
- The Multinomial Distribution:
- The Negative Binomial Distribution:
- The Pascal Distribution:
- The Geometric Distribution:
- The Hypergeometric Distribution:
- The Logarithmic Distribution:
- Shuffling and Sampling:
- Random Number Distribution Examples:
- Random Number Distribution References and Further Reading:
References
[1] GNU Scientific Library – Reference Manual
[3] C++11 <random>
[4] C++11 <cmath>