Forgot password?
 Create new account
View 99|Reply 3

怎么随机生成正交矩阵$M\in O(n)$

[Copy link]

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

hbghlyj Posted at 2025-1-18 23:46:30 |Read mode
怎么随机生成正交矩阵$M\in O(n)$


web.archive.org/web/20220121021352/https://st … ubgroup-rand-var.pdf
We suggest a simple algorithm for Monte Carlo generation of uniformly distributed variables on a compact group. Examples include random permutations, Rubik's cube positions, orthogonal, unitary, and symplectic matrices, and elements of $G L_n$ over a finite field. The algorithm reduces to the "standard" fast algorithm when there is one, but many new examples are included.

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

 Author| hbghlyj Posted at 2025-1-18 23:50:14
概述了一种均匀随机 3D 旋转矩阵的方法,步骤如下:
  • 绕 z 轴随机旋转(z 单位向量未移动)
  • 将z 单位向量旋转到单位球面上的随机位置

其中第一步是具有随机角度的简单 2D 旋转矩阵,参数为$x_1 \sim U(0, 1)$
$$R=\left[\begin{array}{ccc}
\cos \left(2 \pi x_1\right) & \sin \left(2 \pi x_1\right) & 0 \\
-\sin \left(2 \pi x_1\right) & \cos \left(2 \pi x_1\right) & 0 \\
0 & 0 & 1
\end{array}\right]$$
第二步比较棘手:它使用某些 Householder 矩阵,如下所示:
$$H=I-2 v v^{\mathrm{\top}}$$
$$v=\left[\begin{array}{c}
\cos \left(2 \pi x_2\right) \sqrt{x_3} \\
\sin \left(2 \pi x_2\right) \sqrt{x_3} \\
\sqrt{1-x_3}
\end{array}\right]$$
其中 $x_2$ 和 $x_3$ 也取自 $U(0, 1)$。最终完成的随机 3D 旋转矩阵为(包括通过原点的反射,将 Householder 矩阵变换从反射转换为旋转):$$M=-H R$$

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

 Author| hbghlyj Posted at 2025-1-18 23:57:35
Python
  1. import numpy as np
  2. def uniform_random_rotation(x):
  3.     """Apply a random rotation in 3D, with a distribution uniform over the
  4.     sphere.
  5.     Arguments:
  6.         x: vector or set of vectors with dimension (n, 3), where n is the
  7.             number of vectors
  8.     Returns:
  9.         Array of shape (n, 3) containing the randomly rotated vectors of x,
  10.         about the mean coordinate of x.
  11.     Algorithm taken from "Fast Random Rotation Matrices" (James Avro, 1992):
  12.     https://doi.org/10.1016/B978-0-08-050755-2.50034-8
  13.     """
  14.     def generate_random_z_axis_rotation():
  15.         """Generate random rotation matrix about the z axis."""
  16.         R = np.eye(3)
  17.         x1 = np.random.rand()
  18.         R[0, 0] = R[1, 1] = np.cos(2 * np.pi * x1)
  19.         R[0, 1] = -np.sin(2 * np.pi * x1)
  20.         R[1, 0] = np.sin(2 * np.pi * x1)
  21.         return R
  22.     # There are two random variables in [0, 1) here (naming is same as paper)
  23.     x2 = 2 * np.pi * np.random.rand()
  24.     x3 = np.random.rand()
  25.     # Rotation of all points around x axis using matrix
  26.     R = generate_random_z_axis_rotation()
  27.     v = np.array([
  28.         np.cos(x2) * np.sqrt(x3),
  29.         np.sin(x2) * np.sqrt(x3),
  30.         np.sqrt(1 - x3)
  31.     ])
  32.     H = np.eye(3) - (2 * np.outer(v, v))
  33.     M = -(H @ R)
  34.     x = x.reshape((-1, 3))
  35.     mean_coord = np.mean(x, axis=0)
  36.     return ((x - mean_coord) @ M) + mean_coord @ M
Copy the Code
Mathematica
  1. uniformRandomRotation[x_] := Module[
  2.   {generateRandomZAxisRotation, x2, x3, R, v, H, M, meanCoord},
  3.   
  4.   generateRandomZAxisRotation[] := Module[{m, r},
  5.     r = RandomReal[];
  6.     m = IdentityMatrix[3];
  7.     m[[1,1]] = m[[2,2]] = Cos[2 Pi r];
  8.     m[[1,2]] = -Sin[2 Pi r];
  9.     m[[2,1]] = Sin[2 Pi r];
  10.     m
  11.   ];
  12.   
  13.   x2 = 2 Pi RandomReal[];
  14.   x3 = RandomReal[];
  15.   R = generateRandomZAxisRotation[];
  16.   v = {Cos[x2] Sqrt[x3], Sin[x2] Sqrt[x3], Sqrt[1 - x3]};
  17.   H = IdentityMatrix[3] - 2 Outer[Times, v, v];
  18.   M = - (H . R);
  19.   meanCoord = Mean[x];
  20.   
  21.   ((x - meanCoord) M) + meanCoord M
  22. ]
Copy the Code

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

 Author| hbghlyj Posted at 2025-1-19 00:01:34
Sampling a Uniformly Random Rotation
这个是什么原理呢
  1. GenUniformRandRotation[dp_,dd_,xn_]:=Module[{x,\[Theta],\[Phi],z,r,Vx,Vy,Vz,st,ct,Sx,Sy},
  2. x=xn;(*RandomReal[1,3];*)
  3. \[Theta]=(x[[1]]-1/2)dp 2\[Pi];(*Rotation about the pole (Z). This is improved from Arvo's implementation by subtracting 1/2 so that the rotation is unbiased. *)
  4. \[Phi]=x[[2]]2\[Pi];(*For direction of pole deflection. *)
  5. z=x[[3]]dd 2;(*For magnitude of pole deflection. *)(*Compute a vector V used for distributing points over the sphere via the reflection I-V Transpose(V).This formulation of V will guarantee that if x[1] and x[2] are uniformly distributed, the reflected points will be uniform on the sphere.Note that V has length Sqrt[2] to eliminate the 2 in the Householder matrix. *)
  6. r=Sqrt[z];
  7. Vx=Sin[\[Phi]]r;
  8. Vy=Cos[\[Phi]]r;
  9. Vz=Sqrt[2-z];
  10. (*Compute the row vector S=Transpose(V)*R, where R is a simple rotation by theta about the z-axis. No need to compute Sz since it's just Vz. *)
  11. st=Sin[\[Theta]];
  12. ct=Cos[\[Theta]];
  13. Sx=Vx ct-Vy st;
  14. Sy=Vx st+Vy ct;
  15. (*Construct the rotation matrix (V Transpose(V)-I) R *R_z[\[Pi]], which is equivalent to V S-R.
  16. Note that Arvo's code is missing the multiplication by R_z[\[Pi]], which is unnoticeable for random rotations, but causes problems for random perturbations.*)
  17. M=(-Vx Sx+ct        -Vx Sy+st        Vx Vz
  18. -Vy Sx-st        -Vy Sy+ct        Vy Vz
  19. -Vz Sx        -Vz Sy        1-z
  20. )]
  21. sc = 0.02;
  22. maxNumSamples = 1000;
  23. xns = RandomReal[1,{3,maxNumSamples}]; (*precompute random numbers so that the Manipulate does not 'bounce'*)
  24. (*Test code:
  25. MatrixForm[GenUniformRandRotation[0,0,RandomReal[1,{3,1}]]]
  26. Rz[\[Theta]_] := (\[NoBreak]Cos[\[Theta]]        Sin[\[Theta]]        0
  27. -Sin[\[Theta]]        Cos[\[Theta]]        0
  28. 0        0        1
  29. \[NoBreak])
  30. A = (\[NoBreak]a        b        c
  31. d        e        f
  32. g        h        j
  33. \[NoBreak]);
  34. A.Rz[\[Pi]]
  35. Rz[\[Pi]].A*)
Copy the Code

手机版Mobile version|Leisure Math Forum

2025-4-20 12:25 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list