hypersphere.frink

Download or view hypersphere.frink in plain text format


/** This performs calculations on an n-dimensional hypersphere or hyperball. */

use ArbitraryPrecision.frink
use pi2.frink

/** Calculate the volume of a hypersphere in n dimensions and a radius of 1.

    See:
    https://en.wikipedia.org/wiki/Volume_of_an_n-ball

    The following could also be calculated with a recurrence relation and just
    multiplication and division and no need for the gamma function (see the
    above article.)

    This function theoretically works for non-integer n, although what it means
    to have a fractional dimension is left as an exercise for someone much
    smarter than me.
*/

hypersphereVolume[n is dimensionless, r=1, digits=getPrecision[]] :=
{
   oldPrec = getPrecision[]
   try
   {
      setPrecision[digits]
      // TODO:  Add a few digits to make last digits come out right
      return arbitraryPow[Pi.getPi[digits], (n/2)] / gamma[n/2 + 1, digits] arbitraryPow[r, n]
   }
   finally
      setPrecision[oldPrec]
}


Download or view hypersphere.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen, eliasen@mindspring.com