在sympy中求解这个方程式

发布于 2025-02-12 04:44:05 字数 650 浏览 4 评论 0原文

我正在使用sympy玩旋转矩阵和类似概念。我正在尝试从以下矩阵中提取alpha,beta,gamma:

⎡       α - γ⋅sin(θ)        ⎤
⎢                           ⎥
⎢β⋅cos(φ) + γ⋅sin(φ)⋅cos(θ) ⎥
⎢                           ⎥
⎣-β⋅sin(φ) + γ⋅cos(φ)⋅cos(θ)⎦

这样我就可以获得此矩阵

⎡1     0        -sin(θ)   ⎤
⎢                         ⎥
⎢0  cos(φ)   sin(φ)⋅cos(θ)⎥
⎢                         ⎥
⎣0  -sin(φ)  cos(φ)⋅cos(θ)⎦

基本上求解方程f(x)= ax,但对于矩阵而不是x。其中f(x)是第一个矩阵,第二个矩阵和x是alpha,beta,伽马向量。 我天真地用sympy.solve进行了测试,但它输出矩阵A和X不对准。从我通常看到的是因为矩阵的尺寸不正确,但在我的情况下,我认为这是因为使用solve不是正确的方法。

我该如何解决这个问题?

I'm playing with rotational matrices and similar concepts using sympy. I'm trying to extract alpha, beta, gamma from the following matrix:

⎡       α - γ⋅sin(θ)        ⎤
⎢                           ⎥
⎢β⋅cos(φ) + γ⋅sin(φ)⋅cos(θ) ⎥
⎢                           ⎥
⎣-β⋅sin(φ) + γ⋅cos(φ)⋅cos(θ)⎦

so that I get this matrix

⎡1     0        -sin(θ)   ⎤
⎢                         ⎥
⎢0  cos(φ)   sin(φ)⋅cos(θ)⎥
⎢                         ⎥
⎣0  -sin(φ)  cos(φ)⋅cos(θ)⎦

Basically solve the equation F(x) = Ax, but for A matrix instead of x. Where F(x) is the first matrix, A the second matrix and x is the alpha, beta, gamma vector.
I naively tested with sympy.Solve but it outputs Matrices A and x are not aligned. From what I've seen usually is because the dimensions of the matrices are not correct but in my case I think it's because using solve is not the correct way.

How could I solve this with simpy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的奇迹 2025-02-19 04:44:05

您可以使用sympy的linear_eq_to_matrix函数:

In [6]: a, b, g = symbols("α β γ", positive=True)
   ...: t, p    = symbols("θ φ",   positive=True, nonzero=True)

In [7]: bres = Matrix([[a - g*sin(t)],
   ...:                [b*cos(p) + g*sin(p)*cos(t)],
   ...:                [-b*sin(p) + g*cos(p)*cos(t)]])

In [8]: bres
Out[8]: 
⎡       α - γ⋅sin(θ)        ⎤
⎢                           ⎥
⎢β⋅cos(φ) + γ⋅sin(φ)⋅cos(θ) ⎥
⎢                           ⎥
⎣-β⋅sin(φ) + γ⋅cos(θ)⋅cos(φ)⎦

In [9]: linear_eq_to_matrix(bres, [a,b,g])
Out[9]: 
⎛⎡1     0        -sin(θ)   ⎤  ⎡0⎤⎞
⎜⎢                         ⎥  ⎢ ⎥⎟
⎜⎢0  cos(φ)   sin(φ)⋅cos(θ)⎥, ⎢0⎥⎟
⎜⎢                         ⎥  ⎢ ⎥⎟
⎝⎣0  -sin(φ)  cos(θ)⋅cos(φ)⎦  ⎣0⎦⎠

https://docs.sympy.org/latest/modules/solvers/solveset.html#linelear-eq-to-matrix

You can use SymPy's linear_eq_to_matrix function:

In [6]: a, b, g = symbols("α β γ", positive=True)
   ...: t, p    = symbols("θ φ",   positive=True, nonzero=True)

In [7]: bres = Matrix([[a - g*sin(t)],
   ...:                [b*cos(p) + g*sin(p)*cos(t)],
   ...:                [-b*sin(p) + g*cos(p)*cos(t)]])

In [8]: bres
Out[8]: 
⎡       α - γ⋅sin(θ)        ⎤
⎢                           ⎥
⎢β⋅cos(φ) + γ⋅sin(φ)⋅cos(θ) ⎥
⎢                           ⎥
⎣-β⋅sin(φ) + γ⋅cos(θ)⋅cos(φ)⎦

In [9]: linear_eq_to_matrix(bres, [a,b,g])
Out[9]: 
⎛⎡1     0        -sin(θ)   ⎤  ⎡0⎤⎞
⎜⎢                         ⎥  ⎢ ⎥⎟
⎜⎢0  cos(φ)   sin(φ)⋅cos(θ)⎥, ⎢0⎥⎟
⎜⎢                         ⎥  ⎢ ⎥⎟
⎝⎣0  -sin(φ)  cos(θ)⋅cos(φ)⎦  ⎣0⎦⎠

https://docs.sympy.org/latest/modules/solvers/solveset.html#linear-eq-to-matrix

野稚 2025-02-19 04:44:05

这是我最接近的。如您所知f(x)(在下面称为bres)和x,您正在寻找a矩阵是9个变量(a11a12等)。乘以X乘以您的3个方程式具有自变量。我们知道ax -b = 0,但是要找到每个系数,您需要添加约束。例如,要查找a11,您需要求解a11的第一个方程添加βγ的约束是0(subs函数)。
请参阅我对您的答案的评论,因为这可能不是解决该问题的最佳方法。

from sympy import *

a, b, g = symbols("α β γ", positive=True)
t, p    = symbols("θ φ",   positive=True, nonzero=True)

x = Matrix([[a],
            [b],
            [g]])

bres = Matrix([[a - g*sin(t)],
               [b*cos(p) + g*sin(p)*cos(t)],
               [-b*sin(p) + g*cos(p)*cos(t)]])

a11, a12, a13, a21, a22, a23, a31, a32, a33 = symbols("a11, a12, a13, a21, a22, a23, a31, a32, a33", real=True)
A = Matrix([[a11, a12, a13],
            [a21, a22, a23],
            [a31, a32, a33]])

v = A*x-bres

Ares = Matrix([[solve(v[0].subs(b, 0).subs(g, 0), a11), solve(v[0].subs(a, 0).subs(g, 0), a12), solve(v[0].subs(a, 0).subs(b, 0), a13)]
              ,[solve(v[1].subs(b, 0).subs(g, 0), a21), solve(v[1].subs(a, 0).subs(g, 0), a22), solve(v[1].subs(a, 0).subs(b, 0), a23)]
              ,[solve(v[2].subs(b, 0).subs(g, 0), a31), solve(v[2].subs(a, 0).subs(g, 0), a32), solve(v[2].subs(a, 0).subs(b, 0), a33)]])

print(Ares)

结果:

Matrix([[[1], [0],       [-sin(θ)]],
        [[0], [cos(φ)],  [sin(φ)*cos(θ)]],
        [[0], [-sin(φ)], [cos(θ)*cos(φ)]]])

This is the closest I could come up with. As you know F(x) (called bres below) and x, you're looking for A matrix which are 9 variables (a11, a12, etc.). Multiplying A by x gives you 3 equations with independent variables. We know Ax - b = 0, but to find each coefficient you need to add constrains. For example to find a11, you need to solve the first equation for a11 but as you're looking for the α coefficient, you need to add the constraint that β and γ are 0 (subs function).
Please also see my comment to your answer, as this is probably not the best way to solve it.

from sympy import *

a, b, g = symbols("α β γ", positive=True)
t, p    = symbols("θ φ",   positive=True, nonzero=True)

x = Matrix([[a],
            [b],
            [g]])

bres = Matrix([[a - g*sin(t)],
               [b*cos(p) + g*sin(p)*cos(t)],
               [-b*sin(p) + g*cos(p)*cos(t)]])

a11, a12, a13, a21, a22, a23, a31, a32, a33 = symbols("a11, a12, a13, a21, a22, a23, a31, a32, a33", real=True)
A = Matrix([[a11, a12, a13],
            [a21, a22, a23],
            [a31, a32, a33]])

v = A*x-bres

Ares = Matrix([[solve(v[0].subs(b, 0).subs(g, 0), a11), solve(v[0].subs(a, 0).subs(g, 0), a12), solve(v[0].subs(a, 0).subs(b, 0), a13)]
              ,[solve(v[1].subs(b, 0).subs(g, 0), a21), solve(v[1].subs(a, 0).subs(g, 0), a22), solve(v[1].subs(a, 0).subs(b, 0), a23)]
              ,[solve(v[2].subs(b, 0).subs(g, 0), a31), solve(v[2].subs(a, 0).subs(g, 0), a32), solve(v[2].subs(a, 0).subs(b, 0), a33)]])

print(Ares)

Result:

Matrix([[[1], [0],       [-sin(θ)]],
        [[0], [cos(φ)],  [sin(φ)*cos(θ)]],
        [[0], [-sin(φ)], [cos(θ)*cos(φ)]]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文