Reputation: 1008
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?
Upvotes: 0
Views: 179
Reputation: 14530
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
Upvotes: 2
Reputation: 4553
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(φ)]]])
Upvotes: 1