All published worksheets from http://sagenb.org
Image: ubuntu2004
Suppose we'd like to randomly generate some points that are uniformly distributed throughout the unit disk. Since the disk is known to be parametrized by
$$
\begin{array}{lll}
x(r,\theta) & = & r \cos(\theta) \\
y(r,\theta) & = & r \sin(\theta),
\end{array}
$$
one natural guess might be to pick uniformly from the unit interval and uniformly from . We could then determine and from the above equations. We can code this as follows.
Let's try it.
Uh-oh - the points are clearly not uniformly distributed; they appear to cluster too tightly near the center. In retrospect, the problem is obvious: The circle with radius 1/2 contains just about half the points but only a quarter of the area.
At MathWorld (http://mathworld.wolfram.com/DiskPointPicking.html) we see that we should pick uniformly from the unit interval and uniformly from and then determine and according the equations
$$
\begin{array}{lll}
x(r,\theta) & = & \sqrt{r} \cos(\theta) \\
y(r,\theta) & = & \sqrt{r} \sin(\theta).
\end{array}
$$
Let's try that.
Much better. Ultimately, this works because the determinant of the Jacobian of the transformation is constant.
We can use this criterion on other transformations. For example, this parametrization of an ellipse has a Jacobian with constant determinant.
Thus, we can use that transformation to uniformly generate points in an ellipse.
Looks good!
Finding area preserving transformations
Of course, the big question is, how might we find such transformations? One technique is to start with a known parametrization and scale the variables by some non-linear function. Typically, that function will need to solve some differential equation to work. Suppose, for example, we start with the well-known parametrization of a disk.
$$
\begin{array}{lll}
x(r,\theta) & = & r \cos(\theta) \\
y(r,\theta) & = & r \sin(\theta),
\end{array}
$$
We suspect that some radially re-scaled transformation might have constant Jacobian determinant. Let's write that down in terms of some, as yet, unknown function.
$$
\begin{array}{lll}
x(r,\theta) & = & f(r) \cos(\theta) \\
y(r,\theta) & = & f(r) \sin(\theta),
\end{array}
$$
We now compute the determinant of the Jacobian of this transformation.
If we want this to be constant, then we must solve the differential equation . Any constant will do, as long as the differential equation can be solved, but note that solves the equation with .
Uniform points in the sphere
Let's now consider the uniform distribution of points throughout a sphere, which is quite a bit harder. The basic transformation is given by the spherical coordinate formulae. We can generalize to the case of an ellipsoid by simply multiplying the , , and equations by , , and .
The should be familiar to anyone who's worked with cylindrical coordinates. Let's now try scaling both the and variables with unknown functions and .
Looking at this, I would say that we need to solve the differential equations and . Furthermore, we'll need . The first equation is easy.
The second equation has a couple of complications. First, experimentation led me to realize that the choice of the constant is a bit important down the line. I ended up choosing .
So I guess we'll take . Note that this is never negative; presumably, we lost some information applying the arccosine function. We can still try it and run a numerical check at the end. First, let's check the determinant of the Jacobian to see if it is, indeed, constant.
OK, let's try it on the sphere. Note the (2*randint(0,1)-1)
construct,which slaps a in front of the -coordinate.
The following computations should yield something close to , since approximately of the points should be inside a sub-sphere of radius .
You can try to visualize, too, but it might look silly.