=head1 NAME

PDL::Rmath::Raw - Rmath routines from statistics package, R,  for PDL

=head1 SYNOPSIS

 use PDL::Rmath:RAW;
 pnorm(.1,0.0,1.0);

 $a = sequence(10)
   [0 1 2 3 4 5 6 7 8 9]
 $a /=10.0
  [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
 pnorm($a,0.0,1.0)
    [0.5 0.53982784 0.57925971 0.61791142 0.65542174
     0.69146246 0.72574688 0.75803635  0.7881446 0.81593987]

=head1 DESCRIPTION

   These are special functions and distributions from the math library
   of the statistics package, R.  Threading is supported, as in the
   example above in which the normal distribution function with mean
   0, and standard deviation 1, is evaluated for ten values.

   Note that all the functions of the form 'rdist' return random
   numbers derived from a single random number generator.  Details on
   algorithms can be found in the source files.  The algorithm for the
   random number generator is in the source file sunif.c .  It would
   be a good idea to add the ability to choose a different generator.
   The functions are described below with their C prototypes.

   Special Functions of Mathematics

 double	gammafn(double x);  //disconnect with gamma 
 double	lgammafn(double x);  //disconnect with lgamma 
 double	digamma(double x);
 double	trigamma(double x);
 double	tetragamma(double x);
 double	pentagamma(double x);
 double	beta(double a, double b);
 double	lbeta(double a, double b);
 double	choose(double n, double k);
 double	lchoose(double n, double k);

 //double gamma(double);   //disconnect with gammafn
 //double lgamma(double);   //disconnect with lgammafn
 //void dpsifn(double, int, int, int, double*, int*, int*);


   Description:

        The functions `beta' and `lbeta' return the beta func-
        tion and the natural logarithm of the beta function.

        The functions `gamma' and `lgamma' return the gamma
        function and the natural logarithm of the gamma func-
        tion.

        The functions `digamma', `trigamma', `tetragamma' and
        `pentagamma' return the first, second, third and fourth
        derivatives of the logarithm of the gamma function.

        The functions `choose' and `lchoose' return binomial
        coefficients and their logarithms.


   The Beta Distribution

 double	dbeta(double, double, double, int);
 double	pbeta(double, double, double, int, int);
 double	qbeta(double, double, double, int, int);
 double	rbeta(double, double);
 double	pbeta_raw(double, double, double, int);

 double dbeta(double x, double a, double b);
 double pbeta(double x, double a, double);
 double pbeta_b(double x, double a, double b, double logbeta);
     (looks like pbeta_b should be removed)
 double qbeta(double x, double a, double b);
 double rbeta(double a, double b);

        These functions provide information about the Beta dis-
        tribution with parameters `a' and `b'.  `dbeta' gives
        the density, `pbeta' the distribution function, `qbeta'
        the quantile function and `rbeta' generates random
        deviates.

        The Beta distribution has density

            Gamma(a+b)/(Gamma(a)Gamma(b))x^(a-1)(1-x)^(b-1)

        for a > 0, b > 0 and 0 < x < 1.

   See Also:

        `beta' for the beta function, and `dgamma' for the
        Gamma distribution.


   The Binomial Distribution

 double dbinom(double x, double n, double p);
 double pbinom(double x, double n, double p);
 double qbinom(double x, double n, double p);
 double rbinom(double x, double n);

        These functions provide information about the binomial
        distribution with parameters `n' and `p'.  `dbinom'
        gives the density, `pbinom' gives the distribution
        function `qbinom' gives the quantile function and
        `rbinom' generates random deviates.

        The binomial distribution has density

                  p(x) = Choose(n,x) p^x (1-p)^(n-x)

        for x = 0, ..., n.


The Cauchy Distribution

 double dcauchy(double x, double scale, double location);
 double pcauchy(double x, double scale, double location);
 double qcauchy(double x, double scale, double location);
 double rcauchy(double scale, double location);

        These functions provide information about the Cauchy
        distribution with location parameter `location' and
        scale parameter `scale'.

        `dcauchy', `pcauchy', and `qcauchy' are respectively
        the density, distribution function and quantile func-
        tion of the Cauchy distribution.  `rcauchy' generates
        random deviates from the Cauchy.

        The Cauchy distribution with location l and scale s has
        density

                  f(x) = 1 / (pi s (1 + ((x-l)/s)^2))

        for all x.

   See Also:

        `dt' for the t distribution which generalizes
        `dcauchy(*, l = 0, s = 1)'.


   The Chi-Square Distribution

 double dchisq(double x, double df);
 double pchisq(double x, double df);
 double qchisq(double x, double df);
 double rchisq(double df);

        These functions provide information about the chi-
        square distribution with `df' degrees of freedom.
        `dchisq' gives the density, `pchisq' gives the distri-
        bution function `qchisq' gives the quantile function
        and `rchisq' generates random deviates.

        The chi-square distribution with `df' = n degrees of
        freedom has density

          f(x) = 1 / (2^(n/2) Gamma(n/2))  x^(n/2-1) e^(-x/2)

        for x > 0.

   See Also:

        `dnchisq', `dgamma', `dexp'.


   The Exponential Distribution

 double dexp(double x, double rate);
 double pexp(double x, double rate);
 double qexp(double x, double rate);
 double rexp(double rate);

        These functions provide information about the exponen-
        tial distribution with rate `rate' (i.e., mean
        `1/rate').  `dexp' gives the density, `pexp' gives the
        distribution function, `qexp' gives the quantile func-
        tion and `rexp' generates random deviates.

        The exponential distribution with rate lambda has den-
        sity

                     f(x) = lambda e^(- lambda x)

        for x >= 0.

   See Also:

        `exp', `dchisq' for the chisquare and `dweibull' for
        the Weibull distribution which both generalize the
        exponential.

   The F Distribution

 double df(double x, double n1, double n2);
 double pf(double x, double n1, double n2);
 double qf(double x, double n1, double n2);
 double rf(double n1, double);

        These functions provide information about the F distri-
        bution with `n1' and `n2' degrees of freedom.  `df'
        gives the density, `pf' gives the distribution function
        `qf' gives the quantile function and `rf' generates
        random deviates.

        The F distribution with n1 and n2 degrees of freedom
        has density

            f(x) =
            Gamma((n1 + n2)/2) / (Gamma(n1/2) Gamma(n2/2))
            (n1/n2)^(n1/2) x^(n1/2 - 1)
            (1 + (n1/n2) x)^-(n1 + n2)/2

        for x > 0.

   See Also:

        `dt' for Student's t distribution which is (almost)
        equivalent to `*f(..., n2 = 1)'.

   The Gamma Distribution

 double dgamma(double x, double shape, double scale);
 double pgamma(double x, double shape, double scale);
 double qgamma(double x, double shape, double scale);
 double rgamma(double x, double shape);

        These functions provide information about the gamma
        distribution with parameters `shape' and `scale'.
        `dgamma' gives the density, `pgamma' gives the distri-
        bution function `qgamma' gives the quantile function
        and `rgamma' generates random deviates.

        The gamma distribution with parameters `shape' = a and
        `scale' = b has density

               f(x) = 1/(b^a Gamma(a)) x^(a-1) e^-(x/b)

        for x > 0, a > 0 and b > 0.

   See Also:

        `gamma', `dbeta'.


   The Geometric Distribution

 double dgeom(double x, double p);
 double pgeom(double x, double p);
 double qgeom(double x, double p);
 double rgeom(double p);


        These functions provide information about the geometric
        distribution with parameter `p'.  `dgeom' gives the
        density, `pgeom' gives the distribution function,
        `qgeom' gives the quantile function, and `rgeom' gener-
        ates random deviates.

        The geometric distribution has density

                           p(x) = p (1-p)^x

        for x = 0, 1, 2, ...


   The Hypergeometric Distribution

 double dhyper(double x, double N1, double N2, double n);
 double phyper(double x, double N1, double N2, double n);
 double qhyper(double x, double N1, double N2, double n);
 double rhyper(double N1, double N2, double n);

        These functions provide information about the hypergeo-
        metric distribution with parameters `N1', `N2' and `n'.
        `dhyper' gives the density, `phyper' gives the distri-
        bution function `qhyper' gives the quantile function
        and `rhyper' generates random deviates.

        The hypergeometric distribution is used for sampling
        without replacement.  It has density

           p(x) =
           Choose(N1, x) Choose(N2, n-x) / Choose(N1+N2, n)

        for x = 0, ..., n.


   The Log Normal Distribution

 double dlnorm(double x, double meanlog, double sdlog);
 double plnorm(double x, double meanlog, double sdlog);
 double qlnorm(double x, double meanlog, double sdlog);
 double rlnorm(double meanlog, double sdlog);

        These functions provide information about the log nor-
        mal distribution whose logarithm has mean equal to
        `meanlog' and standard deviation equal to `sdlog'.
        `dlnorm' gives the density, `plnorm' gives the distri-
        bution function `qlnorm' gives the quantile function
        and `rlnorm' generates random deviates.

        If `meanlog' or `sdlog' are not specified they assume
        the default values of `0' and `1' respectively.

        The log normal distribution has density

        f(x) = 1/(sqrt(2 pi) sigma x) e^-((log x - mu)^2 / (2 sigma^2))

        where mu and sigma are the mean and standard deviation
        of the logarithm.

   See Also:

        `dnorm', etc.


   The Logistic Distribution


 double dlogis(double x, double location, double scale);
 double plogis(double x, double location, double scale);
 double qlogis(double x, double location, double scale);
 double rlogis(double location, double scale);

        These functions provide information about the logistic
        distribution with parameters `location' and `scale'.
        `dlogis' gives the density, `plogis' gives the distri-
        bution function `qlogis' gives the quantile function
        and `rlogis' generates random deviates.

        The Logistic distribution with `location' a and `scale'
        b has density

                f(x) =
                1/b exp((x-a)/b) (1 + exp((x-a)/b))^-2.


   The Negative Binomial Distribution

 double dnbinom(double x, double n, double p);
 double pnbinom(double x, double n, double p);
 double qnbinom(double x, double n, double p);
 double rnbinom(double n, double p);

        These functions provide information about the negative
        binomial distribution with parameters `n' and `p'.
        `dnbinom' gives the density, `pnbinom' gives the dis-
        tribution function, `qnbinom' gives the quantile func-
        tion and `rnbinom' generates random deviates.

        The negative binomial distribution has density

                  p(x) = Choose(x+n-1,x) p^n (1-p)^x

        for x = 0, 1, 2, ...

   See Also:

        `dbinom' for the binomial and `dpois' for the Poisson
        distribution.

   The Non-Central Chi-Square Distribution

 double dnchisq(double x, double df, double lambda);
 double pnchisq(double x, double df, double lambda);
 double qnchisq(double x, double df, double lambda);
 double rnchisq(double df, double lambda);

        These functions provide information about the non-cen-
        tral chi-square distribution with `df' degrees of free-
        dom and non-centrality parameter `lambda'.  `dnchisq'
        gives the density, `pnchisq' gives the distribution
        function, `qnchisq' gives the quantile function and
        `rnchisq' generates random deviates.

        The non-central chi-square distribution with df degrees
        of freedom and non-centrality parameter lambda has den-
        sity

        f(x) = exp(-lambda/2) SUM_{r=0}^infty (lambda^r / 2^r r!)
        pchisq(x, df + 2r)

        for x >= 0.

   See Also:

        `dchisq', `dgamma'.

   The Normal Distribution

 double dnorm(double x, double mean, double sd, int flag);
 double pnorm(double x, double mean, double sd);
 double qnorm(double x, double mean, double sd);
 double rnorm(double mean, double sd);
 
        These functions provide information about the normal
        distribution with mean equal to `mean' and standard
        deviation equal to `sd'.  `dnorm' gives the density,
        `pnorm' gives the distribution function `qnorm' gives
        the quantile function and `rnorm' generates random
        deviates.

        If `mean' or `sd' are not specified they assume the
        default values of `0' and `1', respectively.

        The normal distribution has density

        f(x) = 1/(sqrt(2 pi) sigma) e^-((x - mu)^2/(2 sigma^2))

        where mu is the mean of the distribution and sigma the
        standard deviation.

   See Also:

        `dlnorm' for the Lognormal distribution.

   The Poisson Distribution

 double dpois(double x, double lambda);
 double ppois(double x, double lambda);
 double qpois(double x, double lambda);
 double rpois(double lambda);

        These functions provide information about the Poisson
        distribution with parameter `lambda'.  `dpois' gives
        the density, `ppois' gives the distribution function
        `qpois' gives the quantile function and `rpois' gener-
        ates random deviates.

        The Poisson distribution has density

                    p(x) = lambda^x exp(-lambda)/x!

        for x = 0, 1, 2, ....

   The Student t Distribution


 double dt(double x, double df);
 double pt(double x, double df);
 double qt(double x, double df);
 double rt(double df);

        These functions provide information about the t distri-
        bution with `df' degrees of freedom.  `dt' gives the
        density, `pt' gives the distribution function, `qt'
        gives the quantile function and `rt' generates random
        deviates.

        The t distribution with n degrees of freedom has den-
        sity

        f(x) = Gamma((n+1)/2) / (sqrt(n pi) Gamma(n/2)) (1 + x^2/n)^-((n+1)/2)

        for all real x.

   See Also:

        `df' for the F distribution which generalizes the t
        one.


   The Uniform Distribution

 double dunif(double x, double min, double max);
 double punif(double x, double min, double max);
 double qunif(double x, double min, double max);
 double runif(double min, double max);

        These functions provide information about the uniform
        distribution on the interval from `min' to `max'.
        `dunif' gives the density, `punif' gives the distribu-
        tion function `qunif' gives the quantile function and
        `runif' generates random deviates.

        If `min' or `max' are not specified they assume the
        default values of `0' and `1' respectively.

        The uniform distribution has density

                          f(x) = 1/(max-min)

        for min <= x <= max.

   The Weibull Distribution

 double dweibull(double x, double shape, double scale);
 double pweibull(double x, double shape, double scale);
 double qweibull(double x, double shape, double scale);
 double rweibull(double shape, double scale);

        These functions provide information about the Weibull
        distribution with parameters `shape' and `scale'.
        `dweibull' gives the density, `pweibull' gives the dis-
        tribution function, `qweibull' gives the quantile func-
        tion and `rweibull' generates random deviates.

        The Weibull distribution with `shape' parameter a and
        `scale' parameter b has density given by

                f(x) = (a/b) (x/b)^(a-1) exp(- (x/b)^a)

        for x > 0.

   See Also:

        `dexp' for the Exponential which is a special case of a
        Weibull distribution.


=cut

=head1 BUGS

PDL::R is almost completley untested. 
Arguments may not be omitted as stated in the
documentation.

=head1 AUTHOR

This document and and the PDL::R interface are copyrighted:
Copyright (C) 1998 John Lapeyre under the terms in the file
COPYING in the PDL distribution.

This document is a modified version of the documentation found in the
R distribution. It is modified only to provide a description of the
PDL calling sytax.

The R language including all sources and documentation 
is copyrighted under the terms of the Gnu Public License:
R : A Computer Langage for Statistical Data Analysis
Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka

The PDL language is copyrighted:
Copyright (C) R.J.R. Williams 1997 (rjrw@ast.leeds.ac.uk), Karl Glazebrook
(kgb@aaoepp.aao.gov.au) and Tuomas J. Lukka (Tuomas.Lukka@helsinki.fi). 

All rights reserved. There is no warranty. You are allowed
to redistribute this software / documentation under certain
conditions. For details, see the file COPYING in the PDL 
distribution. If this file is separated from the PDL distribution, 
the copyright notice should be included in the file.

=cut
