Python Uniform distribution of points on a 4-dimensional sphere

I need a uniform distribution of points on a 4-dimensional sphere. I know that this is not so trivial as picking up 3 angles and using polar coordinates.

In 3 dimensions I use

from random import random

u=random()
costheta = 2*u -1 #for distribution between -1 and 1
theta = acos(costheta)
phi = 2*pi*random

x=costheta
y=sin(theta)*cos(phi)
x=sin(theta)*sin(phi)

This gives a uniform distribution of x, y and z.

How can I get a similar distribution for 4 dimensions?

+5
source share
3 answers

The standard way , although perhaps not the fastest , is to use the Mueller method to create evenly distributed points on the N-sphere:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

N = 600
dim = 3

norm = np.random.normal
normal_deviates = norm(size=(dim, N))

radius = np.sqrt((normal_deviates**2).sum(axis=0))
points = normal_deviates/radius

fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
ax.scatter(*points)
ax.set_aspect('equal')
plt.show()

enter image description here

Just change dim = 3to dim = 4to create points on a 4-ball.

+5
source

4D . 4- .

from random import random
import math
x=random.normalvariate(0,1)
y=random.normalvariate(0,1)
z=random.normalvariate(0,1)
w=random.normalvariate(0,1)
r=math.sqrt(x*x + y*y + z*z + w*w)
x/=r
y/=r
z/=r
w/=r
print (x,y,z,w)
+1

@unutbu, ( ), , : ( ).

  • .
  • ( ).
  • :
    • , 1 ( , 1).
    • , , , .
  • For each saved sample point, divide the selected point by a norm to renormalize it with a unit radius.
  • Rinse and repeat for more dots due to discarded samples.

This obviously works in n-dimensional space, since the radius is always the L2-norm in higher dimensions.

This is fast, since avoiding the square root and sampling by the Gaussian distribution, but this is not a vector algorithm.

0
source

All Articles