sympy 可以求解这些多项式平方根方程吗? 方程解决尝试等价方程

如何解决sympy 可以求解这些多项式平方根方程吗? 方程解决尝试等价方程

我尝试在 SymPy 中以 2 种不同形式求解方程,但存在错误和无限循环。这是在 Python 3.8.8 和 SymPy 1.7.1 上,在两个设备上的结果相同。相比之下,Mathematica 在一秒内解决了这两个问题。

方程

这里是相同方程的等价形式

enter image description here

from sympy import *
   
x,xh,xhh,p,q,r = symbols('x xh xhh p q r')

eq1 = ((2*q**2-2*r**2)/(-q - r + xh) - 2*q - 2*r + x + xhh 
       + sqrt(4*P*x + 4*P*xh + 4*q**2 + x**2 + 2*x*xh + xh**2) 
       + sqrt(4*P*xh + 4*q**2 + xh**2 + xhh**2 + 2*xhh*(2*p + xh))
      )

eq2 = (4*q**2 + 4*q*r - q*x - 2*q*xh - r*x - 2*r*xh + x*xh + xhh*(-q - r + xh)
       + (-q - r + xh)*sqrt(4*P*xh + 4*q**2 + xh**2 + xhh**2 + 2*xhh*(2*p + xh)) 
       + (-q - r + xh)*sqrt(4*P*x + 4*P*xh + 4*q**2 + x**2 + 2*x*xh + xh**2)
      )

A,C,D,F,G,H = symbols('A C D F G H')

eq3 = A + xhh + C/D + sqrt(F) + sqrt(G + H*xhh + xhh**2)

解决尝试

求解第一个形式 solve(eq1,xhh) 返回错误

Traceback (most recent call last):

  File "<ipython-input-24-9923c6f02abd>",line 1,in <module>
    solve(eq1,xhh)

  File "/Users/jacobrichardson/anaconda/envs/env_sympy/lib/python3.8/site-packages/sympy/solvers/solvers.py",line 1097,in solve
    solution = _solve(f[0],*symbols,**flags)

  File "/Users/jacobrichardson/anaconda/envs/env_sympy/lib/python3.8/site-packages/sympy/solvers/solvers.py",line 1460,in _solve
    gen = f_num.match(D.xreplace({d: w}))[w]

TypeError: 'nonetype' object is not subscriptable

解决第二个 solve(eq2,xhh) 运行了一个多小时而没有在我的两台设备上完成!

确实在我的手动帮助下,第三种形式解决solve(eq3,xhh) [(-A**2*D**2 - 2*A*C*D - 2*A*D**2*sqrt(F) - C**2 - 2*C*D*sqrt(F) - D**2*F + D**2*G)/(D*(2*A*D + 2*C + 2*D*sqrt(F) - D*H))]

有没有什么方法可以让 SymPy 像 Mathematica eq1eq2 一样轻松方便地求解 Solve[eq1,xhh]Solve[eq2,xhh] 等方程?


等价方程

print(simplify(eq1 * (-q - r + xh) - eq2)) # "0" equations 1 and 2 equivalent up to one singularity
print(simplify(eq1 -
               eq3.subs([(A,-2*q - 2*r + x),(C,2*q**2 - 2*r**2),(D,-q - r + xh),(F,4*P*x + 4*P*xh + 4*q**2 + x**2 + 2*x*xh + xh**2),(G,4*P*xh + 4*q**2 + xh**2),(H,2*(2*p + xh))]))) # "0" equations 1 and 3 equivalent

解决方法

我尝试了 solve(eq1,xhh) 并且我看到了您在使用 sympy 1.7.1(最新版本)进行测试时显示的错误。我认为这是一个已经修复的错误,因为我没有看到最新的“主”版本的 sympy。事实上,这是修复它的拉取请求:

https://github.com/sympy/sympy/pull/20842

相反,我发现当你报告 eq2 时,它只是很慢。中断它我可以看到它在可以禁用的检查代码中很慢。 check=False eq1 需要 4 秒,eq2 需要 5 秒:

In [2]: %time solve(eq1,xhh,check=False)
CPU times: user 4.21 s,sys: 45.3 ms,total: 4.25 s
Wall time: 4.33 s
Out[2]: 
⎡                                                                                                                      ______________________
⎢       2                                   2                          2      4       3        3        3         3   ╱                     2
⎢- 2⋅p⋅q ⋅x - 4⋅p⋅q⋅r⋅x + 4⋅p⋅q⋅x⋅xh - 2⋅p⋅r ⋅x + 4⋅p⋅r⋅x⋅xh - 2⋅p⋅x⋅xh  - 8⋅q  - 16⋅q ⋅r + 4⋅q ⋅x + 8⋅q ⋅xh + 4⋅q ⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q 
⎢────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
⎢                                                                                                                                            
⎢                                                                                                                                            
⎣                                                                                                                                            

_____________________                                               ___________________________________________                              
    2              2       2  2      2           2           2     ╱                     2    2              2     2  2      2         2     
 + x  + 2⋅x⋅xh + xh   - 8⋅q ⋅r  + 8⋅q ⋅r⋅x + 16⋅q ⋅r⋅xh + 8⋅q ⋅r⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   - q ⋅x  - 7⋅q ⋅x⋅xh - q ⋅x⋅╲╱
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                             
                                                                                                                                             
                                                                                                                                             

 ___________________________________________                         ___________________________________________                             
╱                     2    2              2       2   2      2      ╱                     2    2              2         2          2         
  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   - 2⋅q ⋅xh  - 6⋅q ⋅xh⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   + 4⋅q⋅r ⋅x + 8⋅q⋅r ⋅xh + 4⋅q
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                 ____________
                                   2                             2                    2      3      2      2        2       2   ╱            
                            - 2⋅p⋅q  - 4⋅p⋅q⋅r + 4⋅p⋅q⋅xh - 2⋅p⋅r  + 4⋅p⋅r⋅xh - 2⋅p⋅xh  - 4⋅q  - 8⋅q ⋅r + q ⋅x + 5⋅q ⋅xh + q ⋅╲╱  4⋅p⋅x + 4⋅p

       ___________________________________________                                       ___________________________________________         
  2   ╱                     2    2              2           2                           ╱                     2    2              2          
⋅r ⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   - 2⋅q⋅r⋅x  - 10⋅q⋅r⋅x⋅xh - 2⋅q⋅r⋅x⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   - 4⋅q⋅r⋅
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
_______________________________                                          ___________________________________________                        _
         2    2              2         2                                ╱                     2    2              2                        ╱ 
⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   - 4⋅q⋅r  + 2⋅q⋅r⋅x + 6⋅q⋅r⋅xh + 2⋅q⋅r⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   - 2⋅q⋅x⋅xh - 2⋅q⋅xh⋅╲╱  

                  ___________________________________________                                       _________________________________________
  2              ╱                     2    2              2         2              2              ╱                     2    2              
xh  - 8⋅q⋅r⋅xh⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   + 2⋅q⋅x ⋅xh + 4⋅q⋅x⋅xh  + 2⋅q⋅x⋅xh⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
__________________________________________                        ___________________________________________                        ________
                    2    2              2     2      2       2   ╱                     2    2              2                        ╱        
4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   + r ⋅x + r ⋅xh + r ⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   - 2⋅r⋅x⋅xh - 2⋅r⋅xh⋅╲╱  4⋅p⋅x +

__              ___________________________________________                               ___________________________________________        
2          2   ╱                     2    2              2     2  2      2         2     ╱                     2    2              2       2 
   + 2⋅q⋅xh ⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   - r ⋅x  - 3⋅r ⋅x⋅xh - r ⋅x⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   - 2⋅r ⋅
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
___________________________________                        ___________________________________________                                       
             2    2              2        2     3     2   ╱                     2    2              2                                        
 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   + x⋅xh  - xh  + xh ⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh                                         

                 ___________________________________________                                       __________________________________________
  2      2      ╱                     2    2              2         2              2              ╱                     2    2              2
xh  - 2⋅r ⋅xh⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   + 2⋅r⋅x ⋅xh + 4⋅r⋅x⋅xh  + 2⋅r⋅x⋅xh⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh 
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                             
                                                                                                                                             
                                                                                                                                             

_              ___________________________________________                             ___________________________________________⎤
          2   ╱                     2    2              2     2   2       3       2   ╱                     2    2              2 ⎥
  + 2⋅r⋅xh ⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh   - x ⋅xh  - x⋅xh  - x⋅xh ⋅╲╱  4⋅p⋅x + 4⋅p⋅xh + 4⋅q  + x  + 2⋅x⋅xh + xh  ⎥
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────⎥
                                                                                                                                  ⎥
                                                                                                                                  ⎥
                                                                                                                                  ⎦

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?