如何告诉sympy一个变量是已知的?

huangapple go评论53阅读模式
英文:

how do I tell sympy that a variable is known?

问题

我正在尝试使用sympy解决一个包含32个未知变量和4个已知变量以及34个方程式的大型线性方程组。

已知变量:e, j, o, t
未知变量:a, b, c, d, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a_a, b_b, c_c, e_e, t_t, f_f, m_m, p_p, s_s, o_o

我正在寻找关于u, v, w, y, z, c_c的表达式,但只依赖于已知变量。

如何告诉sympy e, j, o, t是已知的?

我尝试这样解决我的问题:

import sympy as sp

a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = sp.symbols(
    'a b c d e f g h i j k l m n o p q r s t u v w x y z')
a_a, b_b, c_c, e_e, t_t, f_f, m_m, p_p, s_s, o_o = sp.symbols(
    'a_a b_b c_c e_e t_t f_f m_m p_p s_s o_o')

eq1 = sp.Eq(a+b+c+d, e)
eq2 = sp.Eq(f+g+h+i, j)
eq3 = sp.Eq(k+l+m+n, o)
eq4 = sp.Eq(p+q+r+s, t)
eq5 = sp.Eq(a+f+k+p, 1)
eq6 = sp.Eq(b+g+l+q, 1)
eq7 = sp.Eq(c+h+m+r, 1)
eq8 = sp.Eq(d+i+n+s, 1)
eq9 = sp.Eq(1/m_m, a)
eq10 = sp.Eq(x/m_m, f)
eq11 = sp.Eq(a_a/m_m, k)
eq12 = sp.Eq(e_e/m_m, p)
eq13 = sp.Eq(u/p_p, b)
eq14 = sp.Eq(1/p_p, g)
eq15 = sp.Eq(b_b/p_p, l)
eq16 = sp.Eq(t_t/p_p, q)
eq17 = sp.Eq(v/s_s, l)
eq18 = sp.Eq(y/s_s, h)
eq19 = sp.Eq(1/s_s, m)
eq20 = sp.Eq(f_f/s_s, r)
eq21 = sp.Eq(w/o_o, d)
eq22 = sp.Eq(z/o_o, i)
eq23 = sp.Eq(c_c/o_o, n)
eq24 = sp.Eq(1/o_o, s)
eq25 = sp.Eq(x, 1/u)
eq26 = sp.Eq(a_a, 1/v)
eq27 = sp.Eq(e_e, 1/w)
eq28 = sp.Eq(b_b, 1/y)
eq29 = sp.Eq(t_t, 1/z)
eq30 = sp.Eq(f_f, 1/c_c)
eq31 = sp.Eq(1+x+a_a+e_e, m_m)
eq32 = sp.Eq(u+1+b_b+t_t, p_p)
eq33 = sp.Eq(v+y+1+f_f, s_s)
eq34 = sp.Eq(w+z+c_c+1, o_o)

solution = sp.solve((eq1, eq2, eq3, eq4, eq5, eq6,
                    eq7, eq8, eq9, eq10, eq11, eq12,
                    eq13, eq14, eq15, eq16, eq17, eq18,
                    eq19, eq20, eq21, eq22, eq23, eq24,
                    eq25, eq26, eq27, eq28, eq29, eq30,
                    eq31, eq32, eq33, eq34
                     ), u, v, w, y, z, c_c)

print(solution)

打印函数给我一个空列表:

[]

我预期语法是正确的,因为我没有收到任何错误,但我没有找到一种方法来告诉sympy e, j, o, t 是已知的,我希望解决方案只依赖于已知变量 e, j, o, t

英文:

I am trying to solve a large system of linear equations with 32 unknown variables and 4 known variables und 34 equations using sympy.

known variables: e, j, o, t
unknown variables: a, b, c, d, f, g, h,
i, j, k, l, m, n, o,
p, q, r, s, t, u, v,
w, x, y, z, a_a, b_b,
c_c, e_e, t_t, f_f,
m_m, p_p, s_s, o_o

I am looking for expressions for u, v, w, y, z, c_c but only depending on the known variables.

How can I tell sypmy that e, j, o, t are known?

I tryed solving my problem like this:

import sympy as sp
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = sp.symbols(
'a b c d e f g h i j k l m n o p q r s t u v w x y z')
a_a, b_b, c_c, e_e, t_t, f_f, m_m, p_p, s_s, o_o = sp.symbols(
'a_a b_b c_c e_e t_t f_f m_m p_p s_s o_o')
eq1 = sp.Eq(a+b+c+d, e)
eq2 = sp.Eq(f+g+h+i, j)
eq3 = sp.Eq(k+l+m+n, o)
eq4 = sp.Eq(p+q+r+s, t)
eq5 = sp.Eq(a+f+k+p, 1)
eq6 = sp.Eq(b+g+l+q, 1)
eq7 = sp.Eq(c+h+m+r, 1)
eq8 = sp.Eq(d+i+n+s, 1)
eq9 = sp.Eq(1/m_m, a)
eq10 = sp.Eq(x/m_m, f)
eq11 = sp.Eq(a_a/m_m, k)
eq12 = sp.Eq(e_e/m_m, p)
eq13 = sp.Eq(u/p_p, b)
eq14 = sp.Eq(1/p_p, g)
eq15 = sp.Eq(b_b/p_p, l)
eq16 = sp.Eq(t_t/p_p, q)
eq17 = sp.Eq(v/s_s, l)
eq18 = sp.Eq(y/s_s, h)
eq19 = sp.Eq(1/s_s, m)
eq20 = sp.Eq(f_f/s_s, r)
eq21 = sp.Eq(w/o_o, d)
eq22 = sp.Eq(z/o_o, i)
eq23 = sp.Eq(c_c/o_o, n)
eq24 = sp.Eq(1/o_o, s)
eq25 = sp.Eq(x, 1/u)
eq26 = sp.Eq(a_a, 1/v)
eq27 = sp.Eq(e_e, 1/w)
eq28 = sp.Eq(b_b, 1/y)
eq29 = sp.Eq(t_t, 1/z)
eq30 = sp.Eq(f_f, 1/c_c)
eq31 = sp.Eq(1+x+a_a+e_e, m_m)
eq32 = sp.Eq(u+1+b_b+t_t, p_p)
eq33 = sp.Eq(v+y+1+f_f, s_s)
eq34 = sp.Eq(w+z+c_c+1, o_o)
solution = sp.solve((eq1, eq2, eq3, eq4, eq5, eq6,
eq7, eq8, eq9, eq10, eq11, eq12,
eq13, eq14, eq15, eq16, eq17, eq18,
eq19, eq20, eq21, eq22, eq23, eq24,
eq25, eq26, eq27, eq28, eq29, eq30,
eq31, eq32, eq33, eq34
), u, v, w, y, z, c_c)
print(solution)

The print function gives me an empty list:

[]

I expected the syntax to be correct, since I don't get any errors, but I did not find a way to tell sympy that e, j, o, t are known and that I want the solution to be dependend of the known variables e, j, o, t only.

答案1

得分: 1

一旦消除了所有单个字母变量(除了e、j、o、t),你将得到10个非线性方程和11个未知数,以及约束条件 e + j + o + t = 4 作为第11个方程。因此,似乎你需要额外的关系来解决这个方程组。我将a_a 替换为 A 等等,并只保留你的表达式的分子部分,这是我得到的:

[e + j + o + t - 4,
E*O*P*S + F*M*O*P - M*O*P*S*t + M*O*S*T + M*P*S,
O*P*S*(-B**2*M*O*S - B*C*M*P*S + B*E*O*P*S + B*M*O*P*S*o - B*M*O*P - B*M*O*S*T - B*M*O*S + B*O*P*S + C*M*P**2*S - C*M*P*S*T - C*M*P*S - E*O*P**2*S + E*O*P*S*T + E*O*P*S - M*O*P**2*S*o + M*O*P**2*S + M*O*P**2 + M*O*P*S*T*o - M*O*P*S*T + M*O*P*S*o - M*O*P*S - M*O*P*T - M*O*P - O*P**2*S + O*P*S*T),
S*(-B**2*M*O*S - B*C*M*P*S + B*M*O*P*S*o - B*M*O*P - O*P**2),
-B**2*E*M*O*S - B*E*F*M*O*P - B*E*M*O*P*S*e + 2*B*E*M*O*P*S - B*E*M*O*P - B*E*M*O*S*T - B*E*M*O*S + B*E*O*P*S + B*M*P*S - E*M*O*P, O*P*S*(-C*E*M*P*S*T + E**2*O*P*S*T + E*F*M*O*P*T + E*M*O*P*S*T*e + E*M*O*P*S*T*j + E*M*O*P*S*T*o - 3*E*M*O*P*S*T + E*M*O*P*S*T**2 - E*M*P*S - M*P*S*T),
C*F - 1,
-S*(E*M*O*P*e - E*M*O*P + E*M*O*T + E*M*O - E*O*P - M*P),
O*P*S*(E*O*P*S + F*M*O*P + M*O*P*S*e + M*O*P*S*j + M*O*P*S*o - 4*M*O*P*S + M*O*S*T + M*P*S)]
英文:

Once you eliminate all single-letter variables (except for e,j,o,t) you end up with 10 nonlinear equations in 11 unknowns and the constraint e + j + o + t = 4 as the 11 equation. So it seems you would need an additional relationship to solve this system of equations. I replaced a_a with A, etc..., and am keeping only the numerators of your expressions and this is what I get:

[e + j + o + t - 4, 
E*O*P*S + F*M*O*P - M*O*P*S*t + M*O*S*T + M*P*S, 
O*P*S*(-B**2*M*O*S - B*C*M*P*S + B*E*O*P*S + B*M*O*P*S*o - B*M*O*P - B*M*O*S*T - B*M*O*S + B*O*P*S + C*M*P**2*S - C*M*P*S*T - C*M*P*S - E*O*P**2*S + E*O*P*S*T + E*O*P*S - M*O*P**2*S*o + M*O*P**2*S + M*O*P**2 + M*O*P*S*T*o - M*O*P*S*T + M*O*P*S*o - M*O*P*S - M*O*P*T - M*O*P - O*P**2*S + O*P*S*T), 
S*(-B**2*M*O*S - B*C*M*P*S + B*M*O*P*S*o - B*M*O*P - O*P**2), 
-B**2*E*M*O*S - B*E*F*M*O*P - B*E*M*O*P*S*e + 2*B*E*M*O*P*S - B*E*M*O*P - B*E*M*O*S*T - B*E*M*O*S + B*E*O*P*S + B*M*P*S - E*M*O*P, O*P*S*(-C*E*M*P*S*T + E**2*O*P*S*T + E*F*M*O*P*T + E*M*O*P*S*T*e + E*M*O*P*S*T*j + E*M*O*P*S*T*o - 3*E*M*O*P*S*T + E*M*O*S*T**2 - E*M*P*S - M*P*S*T), 
C*F - 1, 
-S*(E*M*O*P*e - E*M*O*P + E*M*O*T + E*M*O - E*O*P - M*P), 
O*P*S*(E*O*P*S + F*M*O*P + M*O*P*S*e + M*O*P*S*j + M*O*P*S*o - 4*M*O*P*S + M*O*S*T + M*P*S)]

huangapple
  • 本文由 发表于 2023年1月9日 00:30:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定