author | Francesco Abbate <francesco.bbt@gmail.com> | 2012年03月04日 21:38:00 +0100 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2012年03月04日 21:38:00 +0100 |
commit | fd34530d2b7058ebb80e195a33fc605ecc5935e1 (patch) | |
tree | 284bfbd2f459fc85514bde3e0584729eccf5ac31 | |
parent | 03603dc306f707c51c1088365f823329108aa762 (diff) | |
download | gsl-shell-fd34530d2b7058ebb80e195a33fc605ecc5935e1.tar.gz |
-rw-r--r-- | INSTALL | 108 | ||||
-rw-r--r-- | README | 38 | ||||
-rw-r--r-- | doc/user-manual/pdf.rst | 366 | ||||
-rw-r--r-- | doc/user-manual/randist.rst | 546 | ||||
-rw-r--r-- | str.c | 684 | ||||
-rw-r--r-- | str.h | 152 |
@@ -1,54 +1,54 @@ -
-GSL Shell 2.1
-
-** BUILD INSTRUCTION
-
-GSL Shell needs the following external libraries:
-
-- AGG library, version 2.5
-- GSL Library, version 1.14 or 1.15
-- GNU readline library (on Linux only)
-
-Please note that the Lua implementation used in GSL Shell is included with
-the source code so that you don't need any external Lua library or
-executable.
-
-Once you are sure that the AGG and GSL libraries are properly installed
-edit the file "makepackages". For the Linux platform locate the part
-between the "else" and "endif" keyword and modify the following variables:
-
-AGG_INCLUDES
-AGG_LIBS
-GSL_INCLUDES
-GSL_LIBS
-
-so that the AGG and GSL libraries can be located. Please note that if the
-libraries are installed in the standard locations you can leave empty the
-variables AGG_INCLUDES and GSL_INCLUDES.
-
-Also make sure that PREFIX (default: /usr/local) is correctly set in "makeconfig".
-
-Once you have modified the files "makepackage" and "makeconfig" you can just give
-
-> make
-
-to build the software and
-
-> make install
-
-to install it.
-
-** EXTERNAL REFERENCIES
-
-The GSL Library:
-
- http://www.gnu.org/software/gsl/
-
-The LuaJIT project:
-
- http://luajit.org
-
-The AGG library:
-
- http://www.antigrain.com/
-
+ +GSL Shell 2.1 + +** BUILD INSTRUCTION + +GSL Shell needs the following external libraries: + +- AGG library, version 2.5 +- GSL Library, version 1.14 or 1.15 +- GNU readline library (on Linux only) + +Please note that the Lua implementation used in GSL Shell is included with +the source code so that you don't need any external Lua library or +executable. + +Once you are sure that the AGG and GSL libraries are properly installed +edit the file "makepackages". For the Linux platform locate the part +between the "else" and "endif" keyword and modify the following variables: + +AGG_INCLUDES +AGG_LIBS +GSL_INCLUDES +GSL_LIBS + +so that the AGG and GSL libraries can be located. Please note that if the +libraries are installed in the standard locations you can leave empty the +variables AGG_INCLUDES and GSL_INCLUDES. + +Also make sure that PREFIX (default: /usr/local) is correctly set in "makeconfig". + +Once you have modified the files "makepackage" and "makeconfig" you can just give + +> make + +to build the software and + +> make install + +to install it. + +** EXTERNAL REFERENCIES + +The GSL Library: + + http://www.gnu.org/software/gsl/ + +The LuaJIT project: + + http://luajit.org + +The AGG library: + + http://www.antigrain.com/ + @@ -1,19 +1,19 @@ -
-* GSL shell 2.1
-* Copyright (C) 2009-2011 Francesco Abbate
-* Author: Francesco Abbate
-* Published under GNU GENERAL PUBLIC LICENSE, version 3
-
-GSL shell offers an interactive command-line interface that gives
-access to GSL collection of mathematical functions. GSL shell is based
-on the powerful and elegant scripting language Lua.
-
-GSL shell is not just a wrapper over the C API of GSL but does offer
-much more simple and expressive way to use GSL. The objective of GSL
-shell is to give the user the power of easily access GSL functions
-without having to write a complete C application.
-
-GSL Shell is based on LuaJIT2.
-LuaJIT is Copyright ゥ 2005-2011 Mike Pall.
-LuaJIT is open source software, released under the MIT license.
-More informations on the LuaJIT webpage http://luajit.org.
+ +* GSL shell 2.1 +* Copyright (C) 2009-2011 Francesco Abbate +* Author: Francesco Abbate +* Published under GNU GENERAL PUBLIC LICENSE, version 3 + +GSL shell offers an interactive command-line interface that gives +access to GSL collection of mathematical functions. GSL shell is based +on the powerful and elegant scripting language Lua. + +GSL shell is not just a wrapper over the C API of GSL but does offer +much more simple and expressive way to use GSL. The objective of GSL +shell is to give the user the power of easily access GSL functions +without having to write a complete C application. + +GSL Shell is based on LuaJIT2. +LuaJIT is Copyright ゥ 2005-2011 Mike Pall. +LuaJIT is open source software, released under the MIT license. +More informations on the LuaJIT webpage http://luajit.org. diff --git a/doc/user-manual/pdf.rst b/doc/user-manual/pdf.rst index 618b26fa..b89f88a9 100644 --- a/doc/user-manual/pdf.rst +++ b/doc/user-manual/pdf.rst @@ -1,183 +1,183 @@ -.. highlight:: lua
-
-.. include:: <isogrk1.txt>
-
-Probability Distribution Functions
-==================================
-
-.. module:: randist
-
-The module :mod:`randist` offer a set of functions that mirrors those available from the module :mod:`rnd`.
-Generally, for each kind of distribution a few functions are available to calculate the probability density, the cumulative probability and its inverse.
-
-Continuous random number distributions are defined by a probability density function, p(x), such that the probability of x occurring in the infinitesimal range x to x+dx is p dx.
-
-The cumulative distribution function for the lower tail P(x) is defined by the integral,
-
-.. math::
- P(x) = \int_{-\infty}^{x} dx' p(x')
-
-and gives the probability of a variate taking a value less than x.
-
-The cumulative distribution function for the upper tail Q(x) is defined by the integral,
-
-.. math::
- Q(x) = \int_{x}^{+\infty} dx' p(x')
-
-and gives the probability of a variate taking a value greater than x.
-
-The upper and lower cumulative distribution functions are related by :math:`P(x) + Q(x) = 1` and satisfy :math:`0 \le P(x) \le 1`, :math:`0 \le Q(x) \le 1`.
-
-The inverse cumulative distributions, :math:`x = P^{-1}(p)` and :math:`x = Q^{-1}(q)` give the values of x which correspond to a specific value of p or q.
-They can be used to find confidence limits from probability values.
-
-For discrete distributions the probability of sampling the integer value k is given by :math:`p(k)`, where :math:`\sum_k p(k) = 1`. The cumulative distribution for the lower tail P(k) of a discrete distribution is defined as,
-
-.. math::
- P(k) = \sum_{i \le k} p(i)
-
-where the sum is over the allowed range of the distribution less than or equal to k.
-
-The cumulative distribution for the upper tail of a discrete distribution Q(k) is defined as
-
-.. math::
- Q(k) = \sum_{i > k} p(i)
-
-giving the sum of probabilities for all values greater than k.
-These two definitions satisfy the identity :math:`P(k) + Q(k) = 1`.
-
-If the range of the distribution is 1 to n inclusive then :math:`P(n)=1`,
-:math:`Q(n)=0` while :math:`P(1) = p(1)`, :math:`Q(1)=1-p(1)`.
-
-Naming Conventions
-~~~~~~~~~~~~~~~~~~
-
-The probability functions are named following an uniform naming convention.
-The probability density function end with the suffix ``_pdf``.
-The cumulative functions :math:`P(x)` and :math:`Q(x)` ends with the suffix ``_P`` and ``_Q`` respectively.
-The inverse cumulative functions :math:`P^{-1}(x)` and :math:`Q^{-1}(x)` ends with the suffix ``_Pinv`` and ``_Qinv`` respectively.
-
-Functions Index
-~~~~~~~~~~~~~~~
-
- We present here the list of the available probability functions.
-
- .. note::
- Actually GSL Shell implements all the functions provided by the GSL library but some of them are not listed here.
- Please consult the GSL reference manual if you need a complete list of all the distributions available.
-
-.. function:: gaussian_pdf(x, sigma)
- gaussian_P(x, sigma)
- gaussian_Q(x, sigma)
- gaussian_Pinv(x, sigma)
- gaussian_Qinv(x, sigma)
-
- See :ref:`Gaussian distribution <rnd_gaussian>`.
-
-.. function:: exponential_pdf(x, mu)
- exponential_P(x, mu)
- exponential_Q(x, mu)
- exponential_Pinv(x, mu)
- exponential_Qinv(x, mu)
-
- See :ref:`Exponential Distribution <rnd_exponential>`.
-
-.. function:: chisq_pdf(x, nu)
- chisq_P(x, nu)
- chisq_Q(x, nu)
- chisq_Pinv(x, nu)
- chisq_Qinv(x, nu)
-
- See :ref:`Chi square Distribution <rnd_chisq>`.
-
-.. function:: laplace_pdf(x, a)
- laplace_P(x, a)
- laplace_Q(x, a)
- laplace_Pinv(x, a)
- laplace_Qinv(x, a)
-
- See :ref:`Laplace Distribution <rnd_fdist>`.
-
-.. function:: tdist_pdf(x, nu)
- tdist_P(x, nu)
- tdist_Q(x, nu)
- tdist_Pinv(x, nu)
- tdist_Qinv(x, nu)
-
- See :ref:`t- Distribution <rnd_tdist>`.
-
-.. function:: cauchy_pdf(x, a)
- cauchy_P(x, a)
- cauchy_Q(x, a)
- cauchy_Pinv(x, a)
- cauchy_Qinv(x, a)
-
- See :ref:`Cauchy Distribution <rnd_cauchy>`.
-
-.. function:: rayleigh_pdf(x, sigma)
- rayleigh_P(x, sigma)
- rayleigh_Q(x, sigma)
- rayleigh_Pinv(x, sigma)
- rayleigh_Qinv(x, sigma)
-
- See :ref:`Rayleigh Distribution <rnd_rayleigh>`.
-
-.. function:: fdist_pdf(x, nu1, nu2)
- fdist_P(x, nu1, nu2)
- fdist_Q(x, nu1, nu2)
- fdist_Pinv(x, nu1, nu2)
- fdist_Qinv(x, nu1, nu2)
-
- See :ref:`F- Distribution <rnd_fdist>`.
-
-.. function:: gamma_pdf(x, a, b)
- gamma_P(x, a, b)
- gamma_Q(x, a, b)
- gamma_Pinv(x, a, b)
- gamma_Qinv(x, a, b)
-
- See :ref:`Gamma Distribution <rnd_gamma>`.
-
-.. function:: beta_pdf(x, a, b)
- beta_P(x, a, b)
- beta_Q(x, a, b)
- beta_Pinv(x, a, b)
- beta_Qinv(x, a, b)
-
- See :ref:`Beta Distribution <rnd_beta>`.
-
-.. function:: gaussian_tail_pdf(x, a, sigma)
- gaussian_tail_P(x, a, sigma)
- gaussian_tail_Q(x, a, sigma)
- gaussian_tail_Pinv(x, a, sigma)
- gaussian_tail_Qinv(x, a, sigma)
-
- See :ref:`Gaussian tail Distribution <rnd_gaussian_tail>`.
-
-.. function:: exppow_pdf(x, a, b)
- exppow_P(x, a, b)
- exppow_Q(x, a, b)
- exppow_Pinv(x, a, b)
- exppow_Qinv(x, a, b)
-
- See :ref:`Exponential Power Distribution <rnd_exppow>`.
-
-.. function:: lognormal_pdf(x, zeta, sigma)
- lognormal_P(x, zeta, sigma)
- lognormal_Q(x, zeta, sigma)
- lognormal_Pinv(x, zeta, sigma)
- lognormal_Qinv(x, zeta, sigma)
-
- See :ref:`Lognormal Distribution <rnd_lognormal>`.
-
-.. function:: binomial_pdf(x, p, n)
- binomial_P(x, p, n)
- binomial_Q(x, p, n)
-
- See :ref:`Binomial Distribution <rnd_binomial>`.
-
-.. function:: poisson_pdf(x, mu)
- poisson_P(x, mu)
- poisson_Q(x, mu)
-
- See :ref:`Poisson Distribution <rnd_poisson>`.
+.. highlight:: lua + +.. include:: <isogrk1.txt> + +Probability Distribution Functions +================================== + +.. module:: randist + +The module :mod:`randist` offer a set of functions that mirrors those available from the module :mod:`rnd`. +Generally, for each kind of distribution a few functions are available to calculate the probability density, the cumulative probability and its inverse. + +Continuous random number distributions are defined by a probability density function, p(x), such that the probability of x occurring in the infinitesimal range x to x+dx is p dx. + +The cumulative distribution function for the lower tail P(x) is defined by the integral, + +.. math:: + P(x) = \int_{-\infty}^{x} dx' p(x') + +and gives the probability of a variate taking a value less than x. + +The cumulative distribution function for the upper tail Q(x) is defined by the integral, + +.. math:: + Q(x) = \int_{x}^{+\infty} dx' p(x') + +and gives the probability of a variate taking a value greater than x. + +The upper and lower cumulative distribution functions are related by :math:`P(x) + Q(x) = 1` and satisfy :math:`0 \le P(x) \le 1`, :math:`0 \le Q(x) \le 1`. + +The inverse cumulative distributions, :math:`x = P^{-1}(p)` and :math:`x = Q^{-1}(q)` give the values of x which correspond to a specific value of p or q. +They can be used to find confidence limits from probability values. + +For discrete distributions the probability of sampling the integer value k is given by :math:`p(k)`, where :math:`\sum_k p(k) = 1`. The cumulative distribution for the lower tail P(k) of a discrete distribution is defined as, + +.. math:: + P(k) = \sum_{i \le k} p(i) + +where the sum is over the allowed range of the distribution less than or equal to k. + +The cumulative distribution for the upper tail of a discrete distribution Q(k) is defined as + +.. math:: + Q(k) = \sum_{i > k} p(i) + +giving the sum of probabilities for all values greater than k. +These two definitions satisfy the identity :math:`P(k) + Q(k) = 1`. + +If the range of the distribution is 1 to n inclusive then :math:`P(n)=1`, +:math:`Q(n)=0` while :math:`P(1) = p(1)`, :math:`Q(1)=1-p(1)`. + +Naming Conventions +~~~~~~~~~~~~~~~~~~ + +The probability functions are named following an uniform naming convention. +The probability density function end with the suffix ``_pdf``. +The cumulative functions :math:`P(x)` and :math:`Q(x)` ends with the suffix ``_P`` and ``_Q`` respectively. +The inverse cumulative functions :math:`P^{-1}(x)` and :math:`Q^{-1}(x)` ends with the suffix ``_Pinv`` and ``_Qinv`` respectively. + +Functions Index +~~~~~~~~~~~~~~~ + + We present here the list of the available probability functions. + + .. note:: + Actually GSL Shell implements all the functions provided by the GSL library but some of them are not listed here. + Please consult the GSL reference manual if you need a complete list of all the distributions available. + +.. function:: gaussian_pdf(x, sigma) + gaussian_P(x, sigma) + gaussian_Q(x, sigma) + gaussian_Pinv(x, sigma) + gaussian_Qinv(x, sigma) + + See :ref:`Gaussian distribution <rnd_gaussian>`. + +.. function:: exponential_pdf(x, mu) + exponential_P(x, mu) + exponential_Q(x, mu) + exponential_Pinv(x, mu) + exponential_Qinv(x, mu) + + See :ref:`Exponential Distribution <rnd_exponential>`. + +.. function:: chisq_pdf(x, nu) + chisq_P(x, nu) + chisq_Q(x, nu) + chisq_Pinv(x, nu) + chisq_Qinv(x, nu) + + See :ref:`Chi square Distribution <rnd_chisq>`. + +.. function:: laplace_pdf(x, a) + laplace_P(x, a) + laplace_Q(x, a) + laplace_Pinv(x, a) + laplace_Qinv(x, a) + + See :ref:`Laplace Distribution <rnd_fdist>`. + +.. function:: tdist_pdf(x, nu) + tdist_P(x, nu) + tdist_Q(x, nu) + tdist_Pinv(x, nu) + tdist_Qinv(x, nu) + + See :ref:`t- Distribution <rnd_tdist>`. + +.. function:: cauchy_pdf(x, a) + cauchy_P(x, a) + cauchy_Q(x, a) + cauchy_Pinv(x, a) + cauchy_Qinv(x, a) + + See :ref:`Cauchy Distribution <rnd_cauchy>`. + +.. function:: rayleigh_pdf(x, sigma) + rayleigh_P(x, sigma) + rayleigh_Q(x, sigma) + rayleigh_Pinv(x, sigma) + rayleigh_Qinv(x, sigma) + + See :ref:`Rayleigh Distribution <rnd_rayleigh>`. + +.. function:: fdist_pdf(x, nu1, nu2) + fdist_P(x, nu1, nu2) + fdist_Q(x, nu1, nu2) + fdist_Pinv(x, nu1, nu2) + fdist_Qinv(x, nu1, nu2) + + See :ref:`F- Distribution <rnd_fdist>`. + +.. function:: gamma_pdf(x, a, b) + gamma_P(x, a, b) + gamma_Q(x, a, b) + gamma_Pinv(x, a, b) + gamma_Qinv(x, a, b) + + See :ref:`Gamma Distribution <rnd_gamma>`. + +.. function:: beta_pdf(x, a, b) + beta_P(x, a, b) + beta_Q(x, a, b) + beta_Pinv(x, a, b) + beta_Qinv(x, a, b) + + See :ref:`Beta Distribution <rnd_beta>`. + +.. function:: gaussian_tail_pdf(x, a, sigma) + gaussian_tail_P(x, a, sigma) + gaussian_tail_Q(x, a, sigma) + gaussian_tail_Pinv(x, a, sigma) + gaussian_tail_Qinv(x, a, sigma) + + See :ref:`Gaussian tail Distribution <rnd_gaussian_tail>`. + +.. function:: exppow_pdf(x, a, b) + exppow_P(x, a, b) + exppow_Q(x, a, b) + exppow_Pinv(x, a, b) + exppow_Qinv(x, a, b) + + See :ref:`Exponential Power Distribution <rnd_exppow>`. + +.. function:: lognormal_pdf(x, zeta, sigma) + lognormal_P(x, zeta, sigma) + lognormal_Q(x, zeta, sigma) + lognormal_Pinv(x, zeta, sigma) + lognormal_Qinv(x, zeta, sigma) + + See :ref:`Lognormal Distribution <rnd_lognormal>`. + +.. function:: binomial_pdf(x, p, n) + binomial_P(x, p, n) + binomial_Q(x, p, n) + + See :ref:`Binomial Distribution <rnd_binomial>`. + +.. function:: poisson_pdf(x, mu) + poisson_P(x, mu) + poisson_Q(x, mu) + + See :ref:`Poisson Distribution <rnd_poisson>`. diff --git a/doc/user-manual/randist.rst b/doc/user-manual/randist.rst index d88cced2..395ea86f 100644 --- a/doc/user-manual/randist.rst +++ b/doc/user-manual/randist.rst @@ -1,273 +1,273 @@ -.. highlight:: lua
-
-.. include:: <isogrk1.txt>
-.. include:: <isotech.txt>
-
-Random Number Distributions
-===========================
-
-.. module:: rnd
-
-This chapter describes functions for generating random variates and
-computing their probability distributions. Samples from the
-distributions described in this chapter can be obtained using any of
-the random number generators in the library as an underlying source of
-randomness.
-
-In the simplest cases a non-uniform distribution can be obtained
-analytically from the uniform distribution of a random number
-generator by applying an appropriate transformation. This method uses
-one call to the random number generator. More complicated
-distributions are created by the "acceptance-rejection" method, which
-compares the desired distribution against a distribution which is
-similar and known analytically. This usually requires several samples
-from the generator.
-
-The library also provides cumulative distribution functions and
-inverse cumulative distribution functions, sometimes referred to as
-quantile functions. The cumulative distribution functions and their
-inverses are computed separately for the upper and lower tails of the
-distribution, allowing full accuracy to be retained for small results.
-
-.. _rnd_gaussian:
-
-.. function:: gaussian(r, sigma)
-
- This function returns a Gaussian random variate, with mean zero and
- standard deviation 'sigma'. The probability distribution for
- Gaussian random variates is,
-
- .. math::
- p(x) dx = {1 \over \sqrt{2 \pi \sigma^2}} \exp (-x^2 / 2\sigma^2) dx
-
- for x in the range -\ |infin| to +\ |infin| . Use the transformation z =
- |mgr| + x on the numbers returned by 'gsl_ran_gaussian' to obtain a
- Gaussian distribution with mean |mgr|. This function uses the
- Box-Mueller algorithm which requires two calls to the random
- number generator R.
-
-.. _rnd_exponential:
-
-.. function:: exponential(r, mu)
-
- This function returns a random variate from the exponential
- distribution with mean 'mu'. The distribution is,
-
- .. math::
- p(x) dx = {1 \over \mu} \exp(-x/\mu) dx
-
- for x >= 0.
-
-.. _rnd_chisq:
-
-.. function:: chisq(r, nu)
-
- The chi-squared distribution arises in statistics. If Y\ :sub:`i` are n
- independent gaussian random variates with unit variance then the
- sum-of-squares,
-
- .. math::
- X_i = \sum_i Y_i^2
-
- has a chi-squared distribution with n degrees of freedom.
-
- This function returns a random variate from the chi-squared
- distribution with |ngr| degrees of freedom. The distribution function
- is,
-
- .. math::
- p(x) dx = {1 \over 2 \Gamma(\nu/2) } (x/2)^{\nu/2 - 1} \exp(-x/2) dx
-
- for x >= 0.
-
-.. _rnd_laplace:
-
-.. function:: laplace(r, a)
-
- This function returns a random variate from the Laplace
- distribution with width ``a``. The distribution is,
-
- .. math::
- p(x) dx = {1 \over 2 a} \exp(-|x/a|) dx
-
- for -\ |infin| < x < +\ |infin|.
-
-.. _rnd_tdist:
-
-.. function:: tdist(r, nu)
-
- The t-distribution arises in statistics. If Y\ :sub:`1` has a
- normal distribution and Y\ :sub:`2` has a chi-squared distribution
- with \nu degrees of freedom then the ratio,
-
- .. math::
- X = { Y_1 \over \sqrt{Y_2 / \nu} }
-
- has a t-distribution t(x; |ngr|) with |ngr| degrees of freedom.
-
- This function returns a random variate from the t-distribution.
- The distribution function is,
-
- .. math::
- p(x) dx = {\Gamma((\nu + 1)/2) \over \sqrt{\pi \nu} \Gamma(\nu/2)}
- (1 + x^2/\nu)^{-(\nu + 1)/2} dx
-
- for -\ |infin| < x < +\ |infin|.
-
-.. _rnd_cauchy:
-
-.. function:: cauchy(r, a)
-
- This function returns a random variate from the Cauchy
- distribution with scale parameter A. The probability distribution
- for Cauchy random variates is,
-
- .. math::
- p(x) dx = {1 \over a\pi (1 + (x/a)^2) } dx
-
- for x in the range - |infin| to + |infin| . The Cauchy distribution is
- also known as the Lorentz distribution.
-
-.. _rnd_rayleigh:
-
-.. function:: rayleigh(r, sigma)
-
- This function returns a random variate from the Rayleigh
- distribution with scale parameter |sgr|. The distribution is,
-
- .. math::
- p(x) dx = {x \over \sigma^2} \exp(- x^2/(2 \sigma^2)) dx
-
- for x > 0.
-
-.. _rnd_fdist:
-
-.. function:: fdist(r, nu1, nu2)
-
- The F-distribution arises in statistics. If Y\ :sub:`1` and Y\
- :sub:`2` are chi-squared deviates with |ngr| :sub:`1` and
- |ngr|\ :sub:`2` degrees of freedom then the ratio,
-
- .. math::
- X = { (Y_1 / \nu_1) \over (Y_2 / \nu_2) }
-
- has an F-distribution F(x; |ngr|\ :sub:`1`, |ngr|\ :sub:`2`).
-
- This function returns a random variate from the F-distribution with
- degrees of freedom |ngr|\ :sub:`1` and |ngr|\ :sub:`2`. The
- distribution function is,
-
- .. math::
- p(x) dx =
- { \Gamma((\nu_1 + \nu_2)/2)
- \over \Gamma(\nu_1/2) \Gamma(\nu_2/2) }
- \nu_1^{\nu_1/2} \nu_2^{\nu_2/2}
- x^{\nu_1/2 - 1} (\nu_2 + \nu_1 x)^{-\nu_1/2 -\nu_2/2}
-
- for x >= 0.
-
-.. _rnd_gamma:
-
-.. function:: gamma(r, a, b)
-
- This function returns a random variate from the gamma
- distribution. The distribution function is,
-
- .. math::
- p(x) dx = {1 \over \Gamma(a) b^a} x^{a-1} e^{-x/b} dx
-
- for x > 0.
-
- The gamma distribution with an integer parameter a is known as the
- Erlang distribution. The variates are computed using the
- Marsaglia-Tsang fast gamma method.
-
-.. _rnd_beta:
-
-.. function:: beta(r, a, b)
-
- This function returns a random variate from the beta
- distribution. The distribution function is,
-
- .. math::
- p(x) dx = {\Gamma(a+b) \over \Gamma(a) \Gamma(b)} x^{a-1} (1-x)^{b-1} dx
-
- for 0 <= x <= 1.
-
-.. _rnd_gaussian_tail:
-
-.. function:: gaussian_tail(r, a, sigma)
-
- This function provides random variates from the upper tail of a
- Gaussian distribution with standard deviation sigma. The values
- returned are larger than the lower limit a, which must be
- positive. The method is based on Marsaglia's famous
- rectangle-wedge-tail algorithm (Ann. Math. Stat. 32, 894窶?899
- (1961)), with this aspect explained in Knuth, v2, 3rd ed, p139,586
- (exercise 11).
-
- The probability distribution for Gaussian tail random variates is,
-
- .. math::
- p(x) dx = {1 \over N(a;\sigma) \sqrt{2 \pi \sigma^2}}
- \exp \left(- \frac{x^2}{2 \sigma^2}\right) dx
-
- for x > a where N(a; |sgr|) is the normalization constant,
-
- .. math::
- N(a; \sigma) = (1/2) \textrm{erfc}(a / \sqrt{2 \sigma^2}).
-
-.. _rnd_exppow:
-
-.. function:: exppow(r, a, b)
-
- This function returns a random variate from the exponential power
- distribution with scale parameter a and exponent b. The
- distribution is,
-
- .. math::
- p(x) dx = {1 \over 2 a \Gamma(1+1/b)} \exp(-|x/a|^b) dx
-
- for x >= 0. For b = 1 this reduces to the Laplace distribution. For
- b = 2 it has the same form as a gaussian distribution, but with
- :math:`a = \sqrt{2} \sigma`.
-
-.. _rnd_lognormal:
-
-.. function:: lognormal(r, zeta, sigma)
-
- This function returns a random variate from the lognormal
- distribution. The distribution function is,
-
- .. math::
- p(x) dx = {1 \over x \sqrt{2 \pi \sigma^2} }
- \exp(-(\ln(x) - \zeta)^2/2 \sigma^2) dx
-
- for x > 0.
-
-.. _rnd_binomial:
-
-.. function:: binomial(r, p, n)
-
- This function returns a random integer from the binomial
- distribution, the number of successes in n independent trials with
- probability p. The probability distribution for binomial variates
- is,
-
- .. math::
- p(k) = {n! \over k! (n-k)! } p^k (1-p)^{n-k}
-
- for 0 <= k <= n.
-
-.. _rnd_poisson:
-
-.. function:: poisson(r, mu)
-
- This function returns a random integer from the Poisson
- distribution with mean mu. The probability distribution for Poisson
- variates is,
-
- .. math::
- p(k) = {\mu^k \over k!} \exp(-\mu)
-
- for k >= 0.
+.. highlight:: lua + +.. include:: <isogrk1.txt> +.. include:: <isotech.txt> + +Random Number Distributions +=========================== + +.. module:: rnd + +This chapter describes functions for generating random variates and +computing their probability distributions. Samples from the +distributions described in this chapter can be obtained using any of +the random number generators in the library as an underlying source of +randomness. + +In the simplest cases a non-uniform distribution can be obtained +analytically from the uniform distribution of a random number +generator by applying an appropriate transformation. This method uses +one call to the random number generator. More complicated +distributions are created by the "acceptance-rejection" method, which +compares the desired distribution against a distribution which is +similar and known analytically. This usually requires several samples +from the generator. + +The library also provides cumulative distribution functions and +inverse cumulative distribution functions, sometimes referred to as +quantile functions. The cumulative distribution functions and their +inverses are computed separately for the upper and lower tails of the +distribution, allowing full accuracy to be retained for small results. + +.. _rnd_gaussian: + +.. function:: gaussian(r, sigma) + + This function returns a Gaussian random variate, with mean zero and + standard deviation 'sigma'. The probability distribution for + Gaussian random variates is, + + .. math:: + p(x) dx = {1 \over \sqrt{2 \pi \sigma^2}} \exp (-x^2 / 2\sigma^2) dx + + for x in the range -\ |infin| to +\ |infin| . Use the transformation z = + |mgr| + x on the numbers returned by 'gsl_ran_gaussian' to obtain a + Gaussian distribution with mean |mgr|. This function uses the + Box-Mueller algorithm which requires two calls to the random + number generator R. + +.. _rnd_exponential: + +.. function:: exponential(r, mu) + + This function returns a random variate from the exponential + distribution with mean 'mu'. The distribution is, + + .. math:: + p(x) dx = {1 \over \mu} \exp(-x/\mu) dx + + for x >= 0. + +.. _rnd_chisq: + +.. function:: chisq(r, nu) + + The chi-squared distribution arises in statistics. If Y\ :sub:`i` are n + independent gaussian random variates with unit variance then the + sum-of-squares, + + .. math:: + X_i = \sum_i Y_i^2 + + has a chi-squared distribution with n degrees of freedom. + + This function returns a random variate from the chi-squared + distribution with |ngr| degrees of freedom. The distribution function + is, + + .. math:: + p(x) dx = {1 \over 2 \Gamma(\nu/2) } (x/2)^{\nu/2 - 1} \exp(-x/2) dx + + for x >= 0. + +.. _rnd_laplace: + +.. function:: laplace(r, a) + + This function returns a random variate from the Laplace + distribution with width ``a``. The distribution is, + + .. math:: + p(x) dx = {1 \over 2 a} \exp(-|x/a|) dx + + for -\ |infin| < x < +\ |infin|. + +.. _rnd_tdist: + +.. function:: tdist(r, nu) + + The t-distribution arises in statistics. If Y\ :sub:`1` has a + normal distribution and Y\ :sub:`2` has a chi-squared distribution + with \nu degrees of freedom then the ratio, + + .. math:: + X = { Y_1 \over \sqrt{Y_2 / \nu} } + + has a t-distribution t(x; |ngr|) with |ngr| degrees of freedom. + + This function returns a random variate from the t-distribution. + The distribution function is, + + .. math:: + p(x) dx = {\Gamma((\nu + 1)/2) \over \sqrt{\pi \nu} \Gamma(\nu/2)} + (1 + x^2/\nu)^{-(\nu + 1)/2} dx + + for -\ |infin| < x < +\ |infin|. + +.. _rnd_cauchy: + +.. function:: cauchy(r, a) + + This function returns a random variate from the Cauchy + distribution with scale parameter A. The probability distribution + for Cauchy random variates is, + + .. math:: + p(x) dx = {1 \over a\pi (1 + (x/a)^2) } dx + + for x in the range - |infin| to + |infin| . The Cauchy distribution is + also known as the Lorentz distribution. + +.. _rnd_rayleigh: + +.. function:: rayleigh(r, sigma) + + This function returns a random variate from the Rayleigh + distribution with scale parameter |sgr|. The distribution is, + + .. math:: + p(x) dx = {x \over \sigma^2} \exp(- x^2/(2 \sigma^2)) dx + + for x > 0. + +.. _rnd_fdist: + +.. function:: fdist(r, nu1, nu2) + + The F-distribution arises in statistics. If Y\ :sub:`1` and Y\ + :sub:`2` are chi-squared deviates with |ngr| :sub:`1` and + |ngr|\ :sub:`2` degrees of freedom then the ratio, + + .. math:: + X = { (Y_1 / \nu_1) \over (Y_2 / \nu_2) } + + has an F-distribution F(x; |ngr|\ :sub:`1`, |ngr|\ :sub:`2`). + + This function returns a random variate from the F-distribution with + degrees of freedom |ngr|\ :sub:`1` and |ngr|\ :sub:`2`. The + distribution function is, + + .. math:: + p(x) dx = + { \Gamma((\nu_1 + \nu_2)/2) + \over \Gamma(\nu_1/2) \Gamma(\nu_2/2) } + \nu_1^{\nu_1/2} \nu_2^{\nu_2/2} + x^{\nu_1/2 - 1} (\nu_2 + \nu_1 x)^{-\nu_1/2 -\nu_2/2} + + for x >= 0. + +.. _rnd_gamma: + +.. function:: gamma(r, a, b) + + This function returns a random variate from the gamma + distribution. The distribution function is, + + .. math:: + p(x) dx = {1 \over \Gamma(a) b^a} x^{a-1} e^{-x/b} dx + + for x > 0. + + The gamma distribution with an integer parameter a is known as the + Erlang distribution. The variates are computed using the + Marsaglia-Tsang fast gamma method. + +.. _rnd_beta: + +.. function:: beta(r, a, b) + + This function returns a random variate from the beta + distribution. The distribution function is, + + .. math:: + p(x) dx = {\Gamma(a+b) \over \Gamma(a) \Gamma(b)} x^{a-1} (1-x)^{b-1} dx + + for 0 <= x <= 1. + +.. _rnd_gaussian_tail: + +.. function:: gaussian_tail(r, a, sigma) + + This function provides random variates from the upper tail of a + Gaussian distribution with standard deviation sigma. The values + returned are larger than the lower limit a, which must be + positive. The method is based on Marsaglia's famous + rectangle-wedge-tail algorithm (Ann. Math. Stat. 32, 894窶?899 + (1961)), with this aspect explained in Knuth, v2, 3rd ed, p139,586 + (exercise 11). + + The probability distribution for Gaussian tail random variates is, + + .. math:: + p(x) dx = {1 \over N(a;\sigma) \sqrt{2 \pi \sigma^2}} + \exp \left(- \frac{x^2}{2 \sigma^2}\right) dx + + for x > a where N(a; |sgr|) is the normalization constant, + + .. math:: + N(a; \sigma) = (1/2) \textrm{erfc}(a / \sqrt{2 \sigma^2}). + +.. _rnd_exppow: + +.. function:: exppow(r, a, b) + + This function returns a random variate from the exponential power + distribution with scale parameter a and exponent b. The + distribution is, + + .. math:: + p(x) dx = {1 \over 2 a \Gamma(1+1/b)} \exp(-|x/a|^b) dx + + for x >= 0. For b = 1 this reduces to the Laplace distribution. For + b = 2 it has the same form as a gaussian distribution, but with + :math:`a = \sqrt{2} \sigma`. + +.. _rnd_lognormal: + +.. function:: lognormal(r, zeta, sigma) + + This function returns a random variate from the lognormal + distribution. The distribution function is, + + .. math:: + p(x) dx = {1 \over x \sqrt{2 \pi \sigma^2} } + \exp(-(\ln(x) - \zeta)^2/2 \sigma^2) dx + + for x > 0. + +.. _rnd_binomial: + +.. function:: binomial(r, p, n) + + This function returns a random integer from the binomial + distribution, the number of successes in n independent trials with + probability p. The probability distribution for binomial variates + is, + + .. math:: + p(k) = {n! \over k! (n-k)! } p^k (1-p)^{n-k} + + for 0 <= k <= n. + +.. _rnd_poisson: + +.. function:: poisson(r, mu) + + This function returns a random integer from the Poisson + distribution with mean mu. The probability distribution for Poisson + variates is, + + .. math:: + p(k) = {\mu^k \over k!} \exp(-\mu) + + for k >= 0. @@ -1,342 +1,342 @@ -
-/* str.c -- A C library for string manipulation
- *
- * Copyright (C) 2009-2011 Francesco Abbate
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or (at
- * your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-#define _GNU_SOURCE 1
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <assert.h>
-#include "str.h"
-
-static inline size_t
-str_sz_round (size_t sz)
-{
-#define MINSIZE (1 << 4)
- size_t res = MINSIZE;
-
- while (res < sz)
- {
- res *= 2;
- /* integer overflow ? */
- assert (res > 0);
- }
-
- return res;
-}
-
-static inline void
-str_init_raw (str_ptr s, size_t len)
-{
- s->size = str_sz_round (len+1);
- s->heap = malloc (s->size);
-}
-
-void
-str_init (str_ptr s, int len)
-{
- if (len < 0)
- len = 0;
-
- str_init_raw (s, (size_t)len);
- s->heap[0] = 0;
- s->length = 0;
-}
-
-str_ptr
-str_new (void)
-{
- str_ptr s = malloc (sizeof(str_t));
- str_init_raw (s, 16);
- s->heap[0] = 0;
- s->length = 0;
- return s;
-}
-
-void
-str_free (str_ptr s)
-{
- if (s->size > 0)
- free (s->heap);
- s->heap = 0;
-}
-
-static void
-str_init_from_c_raw (str_ptr s, const char *sf, size_t len)
-{
- str_init_raw (s, len);
- memcpy (s->heap, sf, len+1);
- s->length = len;
-}
-
-void
-str_init_from_c (str_ptr s, const char *sf)
-{
- size_t len = strlen (sf);
- str_init_from_c_raw (s, sf, len);
-}
-
-void
-str_init_from_str (str_ptr s, const str_t sf)
-{
- str_init_from_c_raw (s, CSTR(sf), STR_LENGTH(sf));
-}
-
-void
-str_size_check (str_t s, size_t reqlen)
-{
- char *old_heap;
-
- if (reqlen+1 < s->size)
- return;
-
- old_heap = (s->size > 0 ? s->heap : NULL);
- str_init_raw (s, reqlen);
-
- if (old_heap)
- {
- memcpy (s->heap, old_heap, s->length+1);
- free (old_heap);
- }
-}
-
-void
-str_copy (str_t d, const str_t s)
-{
- const size_t len = s->length;
- str_size_check (d, len);
- memcpy (d->heap, s->heap, len+1);
- d->length = len;
-}
-
-void
-str_copy_c (str_t d, const char *s)
-{
- const size_t len = strlen (s);
- str_size_check (d, len);
- memcpy (d->heap, s, len+1);
- d->length = len;
-}
-
-void
-str_copy_c_substr (str_t d, const char *s, int len)
-{
- len = (len >= 0 ? len : 0);
- str_size_check (d, (size_t)len);
- memcpy (d->heap, s, (size_t)len);
- d->heap[len] = 0;
- d->length = len;
-}
-
-void
-str_append_c (str_t to, const char *from, int sep)
-{
- size_t flen = strlen (from);
- int use_sep = (sep != 0 && to->length > 0);
- size_t newlen = STR_LENGTH(to) + flen + (use_sep ? 1 : 0);
- int idx = STR_LENGTH(to);
-
- str_size_check (to, newlen);
-
- if (use_sep)
- to->heap[idx++] = sep;
-
- memcpy (to->heap + idx, from, flen+1);
- to->length = newlen;
-}
-
-void
-str_append (str_t to, const str_t from, int sep)
-{
- str_append_c (to, CSTR(from), sep);
-}
-
-void
-str_trunc (str_t s, int len)
-{
- if (len < 0 || (size_t)len >= STR_LENGTH(s))
- return;
-
- s->heap[len] = 0;
- s->length = len;
-}
-
-void
-str_get_basename (str_t to, const str_t from, int dirsep)
-{
- const char * ptr = strrchr (CSTR(from), dirsep);
-
- if (ptr == NULL)
- str_copy_c (to, CSTR(from));
- else
- str_copy_c (to, ptr + 1);
-}
-
-void
-str_dirname (str_t to, const str_t from, int dirsep)
-{
- const char * ptr = strrchr (CSTR(from), dirsep);
-
- if (ptr == NULL)
- str_copy_c (to, CSTR(from));
- else
- str_copy_c_substr (to, CSTR(from), ptr - CSTR(from));
-}
-
-int
-str_getline (str_t d, FILE *f)
-{
- char *res, *ptr;
- int szres, pending = 0;
-
- str_size_check (d, 0);
- str_trunc (d, 0);
-
- for (;;)
- {
- int j;
-
- ptr = d->heap + d->length;
- szres = d->size - d->length;
-
- res = fgets (ptr, szres, f);
-
- if (res == NULL)
- {
- if (feof (f) && pending)
- return 0;
- return (-1);
- }
-
- for (j = 0; j < szres; j++)
- {
- if (ptr[j] == '\n' || ptr[j] == 0)
- {
- ptr[j] = 0;
- d->length += j;
- break;
- }
- }
-
- if (j < szres - 1)
- break;
-
- pending = 1;
-
- str_size_check (d, 2 * d->length);
- }
-
- if (d->length > 0)
- {
- if (d->heap[d->length - 1] == '015円')
- {
- d->heap[d->length - 1] = 0;
- d->length --;
- }
- }
-
- return 0;
-}
-
-void
-str_vprintf (str_t d, const char *fmt, int append, va_list ap)
-{
-#define STR_BUFSIZE 64
- char buffer[STR_BUFSIZE];
- char *xbuf;
- int xbuf_size;
- int ns;
- va_list aq;
-
- va_copy (aq, ap);
-
- ns = vsnprintf (buffer, STR_BUFSIZE, fmt, ap);
-
- if (ns >= STR_BUFSIZE)
- {
- xbuf_size = ns+1;
- xbuf = malloc (xbuf_size);
- vsnprintf (xbuf, xbuf_size, fmt, aq);
- }
- else
- {
- xbuf = buffer;
- xbuf_size = 0;
- }
-
- va_end (aq);
-
- if (append)
- {
- str_append_c (d, xbuf, 0);
- if (xbuf_size > 0)
- free (xbuf);
- }
- else
- {
- if (xbuf_size > 0)
- {
- free (d->heap);
- d->heap = xbuf;
- d->size = xbuf_size;
- d->length = ns;
- }
- else
- {
- str_copy_c_substr (d, xbuf, ns);
- }
- }
-#undef STR_BUFSIZE
-}
-
-void
-str_printf (str_t d, const char *fmt, ...)
-{
- va_list ap;
- va_start (ap, fmt);
- str_vprintf (d, fmt, 0, ap);
- va_end (ap);
-}
-
-void
-str_printf_add (str_t d, const char *fmt, ...)
-{
- va_list ap;
- va_start (ap, fmt);
- str_vprintf (d, fmt, 1, ap);
- va_end (ap);
-}
-
-void
-str_pad (str_t s, int len, char sep)
-{
- int diff = len - s->length;
-
- if (diff <= 0)
- return;
-
- str_size_check (s, (size_t)len-1);
-
- memmove (s->heap + diff, s->heap, (s->length + 1) * sizeof(char));
- memset (s->heap, sep, diff * sizeof(char));
-
- s->length += diff;
-}
-
+ +/* str.c -- A C library for string manipulation + * + * Copyright (C) 2009-2011 Francesco Abbate + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#define _GNU_SOURCE 1 + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <assert.h> +#include "str.h" + +static inline size_t +str_sz_round (size_t sz) +{ +#define MINSIZE (1 << 4) + size_t res = MINSIZE; + + while (res < sz) + { + res *= 2; + /* integer overflow ? */ + assert (res > 0); + } + + return res; +} + +static inline void +str_init_raw (str_ptr s, size_t len) +{ + s->size = str_sz_round (len+1); + s->heap = malloc (s->size); +} + +void +str_init (str_ptr s, int len) +{ + if (len < 0) + len = 0; + + str_init_raw (s, (size_t)len); + s->heap[0] = 0; + s->length = 0; +} + +str_ptr +str_new (void) +{ + str_ptr s = malloc (sizeof(str_t)); + str_init_raw (s, 16); + s->heap[0] = 0; + s->length = 0; + return s; +} + +void +str_free (str_ptr s) +{ + if (s->size > 0) + free (s->heap); + s->heap = 0; +} + +static void +str_init_from_c_raw (str_ptr s, const char *sf, size_t len) +{ + str_init_raw (s, len); + memcpy (s->heap, sf, len+1); + s->length = len; +} + +void +str_init_from_c (str_ptr s, const char *sf) +{ + size_t len = strlen (sf); + str_init_from_c_raw (s, sf, len); +} + +void +str_init_from_str (str_ptr s, const str_t sf) +{ + str_init_from_c_raw (s, CSTR(sf), STR_LENGTH(sf)); +} + +void +str_size_check (str_t s, size_t reqlen) +{ + char *old_heap; + + if (reqlen+1 < s->size) + return; + + old_heap = (s->size > 0 ? s->heap : NULL); + str_init_raw (s, reqlen); + + if (old_heap) + { + memcpy (s->heap, old_heap, s->length+1); + free (old_heap); + } +} + +void +str_copy (str_t d, const str_t s) +{ + const size_t len = s->length; + str_size_check (d, len); + memcpy (d->heap, s->heap, len+1); + d->length = len; +} + +void +str_copy_c (str_t d, const char *s) +{ + const size_t len = strlen (s); + str_size_check (d, len); + memcpy (d->heap, s, len+1); + d->length = len; +} + +void +str_copy_c_substr (str_t d, const char *s, int len) +{ + len = (len >= 0 ? len : 0); + str_size_check (d, (size_t)len); + memcpy (d->heap, s, (size_t)len); + d->heap[len] = 0; + d->length = len; +} + +void +str_append_c (str_t to, const char *from, int sep) +{ + size_t flen = strlen (from); + int use_sep = (sep != 0 && to->length > 0); + size_t newlen = STR_LENGTH(to) + flen + (use_sep ? 1 : 0); + int idx = STR_LENGTH(to); + + str_size_check (to, newlen); + + if (use_sep) + to->heap[idx++] = sep; + + memcpy (to->heap + idx, from, flen+1); + to->length = newlen; +} + +void +str_append (str_t to, const str_t from, int sep) +{ + str_append_c (to, CSTR(from), sep); +} + +void +str_trunc (str_t s, int len) +{ + if (len < 0 || (size_t)len >= STR_LENGTH(s)) + return; + + s->heap[len] = 0; + s->length = len; +} + +void +str_get_basename (str_t to, const str_t from, int dirsep) +{ + const char * ptr = strrchr (CSTR(from), dirsep); + + if (ptr == NULL) + str_copy_c (to, CSTR(from)); + else + str_copy_c (to, ptr + 1); +} + +void +str_dirname (str_t to, const str_t from, int dirsep) +{ + const char * ptr = strrchr (CSTR(from), dirsep); + + if (ptr == NULL) + str_copy_c (to, CSTR(from)); + else + str_copy_c_substr (to, CSTR(from), ptr - CSTR(from)); +} + +int +str_getline (str_t d, FILE *f) +{ + char *res, *ptr; + int szres, pending = 0; + + str_size_check (d, 0); + str_trunc (d, 0); + + for (;;) + { + int j; + + ptr = d->heap + d->length; + szres = d->size - d->length; + + res = fgets (ptr, szres, f); + + if (res == NULL) + { + if (feof (f) && pending) + return 0; + return (-1); + } + + for (j = 0; j < szres; j++) + { + if (ptr[j] == '\n' || ptr[j] == 0) + { + ptr[j] = 0; + d->length += j; + break; + } + } + + if (j < szres - 1) + break; + + pending = 1; + + str_size_check (d, 2 * d->length); + } + + if (d->length > 0) + { + if (d->heap[d->length - 1] == '015円') + { + d->heap[d->length - 1] = 0; + d->length --; + } + } + + return 0; +} + +void +str_vprintf (str_t d, const char *fmt, int append, va_list ap) +{ +#define STR_BUFSIZE 64 + char buffer[STR_BUFSIZE]; + char *xbuf; + int xbuf_size; + int ns; + va_list aq; + + va_copy (aq, ap); + + ns = vsnprintf (buffer, STR_BUFSIZE, fmt, ap); + + if (ns >= STR_BUFSIZE) + { + xbuf_size = ns+1; + xbuf = malloc (xbuf_size); + vsnprintf (xbuf, xbuf_size, fmt, aq); + } + else + { + xbuf = buffer; + xbuf_size = 0; + } + + va_end (aq); + + if (append) + { + str_append_c (d, xbuf, 0); + if (xbuf_size > 0) + free (xbuf); + } + else + { + if (xbuf_size > 0) + { + free (d->heap); + d->heap = xbuf; + d->size = xbuf_size; + d->length = ns; + } + else + { + str_copy_c_substr (d, xbuf, ns); + } + } +#undef STR_BUFSIZE +} + +void +str_printf (str_t d, const char *fmt, ...) +{ + va_list ap; + va_start (ap, fmt); + str_vprintf (d, fmt, 0, ap); + va_end (ap); +} + +void +str_printf_add (str_t d, const char *fmt, ...) +{ + va_list ap; + va_start (ap, fmt); + str_vprintf (d, fmt, 1, ap); + va_end (ap); +} + +void +str_pad (str_t s, int len, char sep) +{ + int diff = len - s->length; + + if (diff <= 0) + return; + + str_size_check (s, (size_t)len-1); + + memmove (s->heap + diff, s->heap, (s->length + 1) * sizeof(char)); + memset (s->heap, sep, diff * sizeof(char)); + + s->length += diff; +} + @@ -1,76 +1,76 @@ -
-/* str.h -- A C library for string manipulation
- *
- * Copyright (C) 2009-2011 Francesco Abbate
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or (at
- * your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-#ifndef STR_H
-#define STR_H
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdarg.h>
-
-#include "defs.h"
-
-__BEGIN_DECLS
-
-struct _str {
- char *heap;
- size_t size;
- size_t length;
-};
-
-typedef struct _str str_t[1];
-typedef struct _str *str_ptr;
-typedef struct _str const *const_str_ptr;
-
-#define STR_PTR_FREE(s) { \
- str_free (s); \
- free (s); \
- (s) = NULL; \
- }
-#define STR_LENGTH(s) ((s)->length)
-#define CSTR(s) ((const char *) (s)->heap)
-#define str_set_null(s) str_trunc(s, 0);
-#define str_is_null(s) ((s)->length == 0)
-
-
-extern str_ptr str_new (void);
-extern void str_init (str_ptr s, int len);
-extern void str_free (str_ptr s);
-extern void str_size_check (str_t s, size_t reqlen);
-extern void str_init_from_c (str_ptr s, const char *sf);
-extern void str_init_from_str (str_ptr s, const str_t sf);
-extern void str_copy (str_t d, const str_t s);
-extern void str_copy_c (str_t d, const char *s);
-extern void str_copy_c_substr (str_t d, const char *s, int len);
-extern void str_append_c (str_t to, const char *from, int sep);
-extern void str_append (str_t to, const str_t from, int sep);
-extern void str_trunc (str_t s, int len);
-extern void str_get_basename (str_t to, const str_t from, int dirsep);
-extern void str_dirname (str_t to, const str_t from, int dirsep);
-extern int str_getline (str_t d, FILE *f);
-extern void str_printf (str_t d, const char *fmt, ...);
-extern void str_printf_add (str_t d, const char *fmt, ...);
-extern void str_vprintf (str_t d, const char *fmt, int append,
- va_list ap);
-extern void str_pad (str_t d, int len, char sep);
-
-__END_DECLS
-
-#endif
+ +/* str.h -- A C library for string manipulation + * + * Copyright (C) 2009-2011 Francesco Abbate + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef STR_H +#define STR_H + +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> + +#include "defs.h" + +__BEGIN_DECLS + +struct _str { + char *heap; + size_t size; + size_t length; +}; + +typedef struct _str str_t[1]; +typedef struct _str *str_ptr; +typedef struct _str const *const_str_ptr; + +#define STR_PTR_FREE(s) { \ + str_free (s); \ + free (s); \ + (s) = NULL; \ + } +#define STR_LENGTH(s) ((s)->length) +#define CSTR(s) ((const char *) (s)->heap) +#define str_set_null(s) str_trunc(s, 0); +#define str_is_null(s) ((s)->length == 0) + + +extern str_ptr str_new (void); +extern void str_init (str_ptr s, int len); +extern void str_free (str_ptr s); +extern void str_size_check (str_t s, size_t reqlen); +extern void str_init_from_c (str_ptr s, const char *sf); +extern void str_init_from_str (str_ptr s, const str_t sf); +extern void str_copy (str_t d, const str_t s); +extern void str_copy_c (str_t d, const char *s); +extern void str_copy_c_substr (str_t d, const char *s, int len); +extern void str_append_c (str_t to, const char *from, int sep); +extern void str_append (str_t to, const str_t from, int sep); +extern void str_trunc (str_t s, int len); +extern void str_get_basename (str_t to, const str_t from, int dirsep); +extern void str_dirname (str_t to, const str_t from, int dirsep); +extern int str_getline (str_t d, FILE *f); +extern void str_printf (str_t d, const char *fmt, ...); +extern void str_printf_add (str_t d, const char *fmt, ...); +extern void str_vprintf (str_t d, const char *fmt, int append, + va_list ap); +extern void str_pad (str_t d, int len, char sep); + +__END_DECLS + +#endif |