hypersphere, a MATLAB code which carries out various operations for an M-dimensional hypersphere, including converting between Cartesian and spherical coordinates, stereographic projection, sampling the surface of the sphere, and computing the surface area and volume.


The computer code and data files described and made available on this web page are distributed under the MIT license


hypersphere is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version.

Related Data and Programs:


geometry, a MATLAB code which performs geometric calculations in 2, 3 and M dimensional space.

hypersphere_angle, a MATLAB code which considers the problem of describing the typical value of the angle between a pair of points randomly selected on the unit hypersphere in M dimensions. Since by symmetry, this will be zero, we instead look at the average of the absolute value of the dot product, and the corresponding angle. This starts out at 1 for dimension 1, and rapidly decreases as the spatial dimension increases.

hypersphere_distance, a MATLAB code which computes the expected value of the distance between a pair of points randomly selected on the unit hypersphere in M dimensions.

hypersphere_integrals, a MATLAB code which returns the exact value of the integral of any monomial over the surface of the unit hypersphere in M dimensions.

hypersphere_monte_carlo, a MATLAB code which applies a Monte Carlo method to estimate the integral of a function on the surface of the unit sphere in M dimensions;

hypersphere_surface, a MATLAB code which illustrates a procedure for estimating the location of a hypersurface defined by a characteristic function or a signed function.

random_data, a MATLAB code which generates sample points for various probability distributions, spatial dimensions, and geometries, including the M-dimensional cube, ellipsoid, simplex and sphere.

sphere_stereograph, a MATLAB code which computes the stereographic mapping between points on the unit sphere and points on the plane Z = 1; a generalized mapping is also available

sphere_stereograph_display, a MATLAB code which computes and displays the results of several stereographic projections between a sphere and a plane.


    George Marsaglia,
    Choosing a point from the surface of a sphere,
    Annals of Mathematical Statistics,
    Volume 43, Number 2, April 1972, pages 645-646.

Source Code:

Last revised on 01 February 2019.