Blame randist/sphere.c

Packit 67cb25
/* randist/sphere.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2007 James Theiler, Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <math.h>
Packit 67cb25
#include <gsl/gsl_rng.h>
Packit 67cb25
#include <gsl/gsl_randist.h>
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_ran_dir_2d (const gsl_rng * r, double *x, double *y)
Packit 67cb25
{
Packit 67cb25
  /* This method avoids trig, but it does take an average of 8/pi =
Packit 67cb25
   * 2.55 calls to the RNG, instead of one for the direct
Packit 67cb25
   * trigonometric method.  */
Packit 67cb25
Packit 67cb25
  double u, v, s;
Packit 67cb25
  do
Packit 67cb25
    {
Packit 67cb25
      u = -1 + 2 * gsl_rng_uniform (r);
Packit 67cb25
      v = -1 + 2 * gsl_rng_uniform (r);
Packit 67cb25
      s = u * u + v * v;
Packit 67cb25
    }
Packit 67cb25
  while (s > 1.0 || s == 0.0);
Packit 67cb25
Packit 67cb25
  /* This is the Von Neumann trick. See Knuth, v2, 3rd ed, p140
Packit 67cb25
   * (exercise 23).  Note, no sin, cos, or sqrt !  */
Packit 67cb25
Packit 67cb25
  *x = (u * u - v * v) / s;
Packit 67cb25
  *y = 2 * u * v / s;
Packit 67cb25
Packit 67cb25
  /* Here is the more straightforward approach, 
Packit 67cb25
   *     s = sqrt (s);  *x = u / s;  *y = v / s;
Packit 67cb25
   * It has fewer total operations, but one of them is a sqrt */
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_ran_dir_2d_trig_method (const gsl_rng * r, double *x, double *y)
Packit 67cb25
{
Packit 67cb25
  /* This is the obvious solution... */
Packit 67cb25
  /* It ain't clever, but since sin/cos are often hardware accelerated,
Packit 67cb25
   * it can be faster -- it is on my home Pentium -- than von Neumann's
Packit 67cb25
   * solution, or slower -- as it is on my Sun Sparc 20 at work
Packit 67cb25
   */
Packit 67cb25
  double t = 6.2831853071795864 * gsl_rng_uniform (r);          /* 2*PI */
Packit 67cb25
  *x = cos (t);
Packit 67cb25
  *y = sin (t);
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_ran_dir_3d (const gsl_rng * r, double *x, double *y, double *z)
Packit 67cb25
{
Packit 67cb25
  double s, a;
Packit 67cb25
Packit 67cb25
  /* This is a variant of the algorithm for computing a random point
Packit 67cb25
   * on the unit sphere; the algorithm is suggested in Knuth, v2,
Packit 67cb25
   * 3rd ed, p136; and attributed to Robert E Knop, CACM, 13 (1970),
Packit 67cb25
   * 326.
Packit 67cb25
   */
Packit 67cb25
Packit 67cb25
  /* Begin with the polar method for getting x,y inside a unit circle
Packit 67cb25
   */
Packit 67cb25
  do
Packit 67cb25
    {
Packit 67cb25
      *x = -1 + 2 * gsl_rng_uniform (r);
Packit 67cb25
      *y = -1 + 2 * gsl_rng_uniform (r);
Packit 67cb25
      s = (*x) * (*x) + (*y) * (*y);
Packit 67cb25
    }
Packit 67cb25
  while (s > 1.0);
Packit 67cb25
Packit 67cb25
  *z = -1 + 2 * s;              /* z uniformly distributed from -1 to 1 */
Packit 67cb25
  a = 2 * sqrt (1 - s);         /* factor to adjust x,y so that x^2+y^2
Packit 67cb25
                                 * is equal to 1-z^2 */
Packit 67cb25
  *x *= a;
Packit 67cb25
  *y *= a;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_ran_dir_nd (const gsl_rng * r, size_t n, double *x)
Packit 67cb25
{
Packit 67cb25
  double d;
Packit 67cb25
  size_t i;
Packit 67cb25
  /* See Knuth, v2, 3rd ed, p135-136.  The method is attributed to
Packit 67cb25
   * G. W. Brown, in Modern Mathematics for the Engineer (1956).
Packit 67cb25
   * The idea is that gaussians G(x) have the property that
Packit 67cb25
   * G(x)G(y)G(z)G(...) is radially symmetric, a function only
Packit 67cb25
   * r = sqrt(x^2+y^2+...)
Packit 67cb25
   */
Packit 67cb25
  d = 0;
Packit 67cb25
  do
Packit 67cb25
    {
Packit 67cb25
      for (i = 0; i < n; ++i)
Packit 67cb25
        {
Packit 67cb25
          x[i] = gsl_ran_gaussian (r, 1.0);
Packit 67cb25
          d += x[i] * x[i];
Packit 67cb25
        }
Packit 67cb25
    }
Packit 67cb25
  while (d == 0);
Packit 67cb25
  d = sqrt (d);
Packit 67cb25
  for (i = 0; i < n; ++i)
Packit 67cb25
    {
Packit 67cb25
      x[i] /= d;
Packit 67cb25
    }
Packit 67cb25
}