Hướng dẫn python random rotation matrix
Generate uniformly distributed rotations. Parameters numint or None, optionalNumber of random rotations to generate. If None (default), then a single rotation is generated. random_state{None, int,numpy.random.Generator ,If seed is None (or np.random), the
Rotation instanceContains a single rotation if num is None. Otherwise contains a stack of num rotations. Notes This function is optimized for efficiently sampling random rotation matrices in three dimensions. For generating random rotation
matrices in higher dimensions, see Examples >>> from scipy.spatial.transform import Rotation as R Sample a single rotation: >>> R.random().as_euler('zxy', degrees=True) array([-110.5976185 , 55.32758512, 76.3289269 ]) # random Sample a stack of rotations: >>> R.random(5).as_euler('zxy', degrees=True) array([[-110.5976185 , 55.32758512, 76.3289269 ], # random [ -91.59132005, -14.3629884 , -93.91933182], [ 25.23835501, 45.02035145, -121.67867086], [ -51.51414184, -15.29022692, -172.46870023], [ -81.63376847, -27.39521579, 2.60408416]]) https://math.stackexchange.com/questions/442418/random-generation-of-rotation-matrices This may answer your question, at least as far as the strategy goes. As far as I know, there is no standard or library function from numpy that will do precisely what you need. A myriad of computational strategies are presented there, but I would prefer an extension of the quaternion approach since it can preserve a uniform distribution, which is what I'm assuming you're after. EDIT: My original answer stated "roughly uniform," whereas after a bit more research I found that the quaternion approach, when sampled from a uniform distribution, will in fact preserve a uniform distribution in the rotations. May 12, 2015 Making a random rotation matrix is somewhat hard. You can’t just use “random elements”; that’s not a random matrix. First attempt: Rotate around a random vectorMy first thought was the following:
That’s accomplished with the following code:
Unfortunately, this does not give you a uniformly rotated vector: you end up where you started too often. Second attempt: Uniform Random Rotation MatrixIf we want a uniformly distributed rotation, that’s a little trickier than the above. There’s a Wikipedia article section on it, but its not too helpful. Instead, I found some C code from a book called “Graphics Gems III”. Then, I started with a direct translation of the C code:
Then I did a more Pythonic version, using
This method does a much better job of creating a uniform distribution. In other words, for any vector \(\vec v\), after multiplying \(\vec v^\prime = M \vec v\) (or |