在 SymPy 中格式化嵌套平方根

问题描述 投票:0回答:1

我正在与

sympy
合作以获得方程的符号解。这是我的代码:

import sympy as sp

# Define the symbolic variable
x = sp.symbols('x')

# Define f(x)
f = ((x**2 - 2)**2 - 2)**2 - 2

# Solve the equation f(x) = 0
solutions = sp.solve(f, x)

# Filter only the positive solutions
positive_solutions = [sol for sol in solutions if sol.is_real and sol > 0]

# Print the positive solutions
print("The positive solutions of the equation f(x) = 0 are:")
for sol in positive_solutions:
    print(sol)

我正在使用 SymPy 来求解方程

[
L_3(x) = \left(\left(x^2 - 2\right)^2 - 2\right)^2 - 2 = 0
]

我能够得到正解。但是,解会以嵌套平方根的方式返回,使内部根出现在左侧,如下所示:

sqrt(2 - sqrt(2 - sqrt(2)))
sqrt(2 - sqrt(sqrt(2) + 2))
sqrt(sqrt(2 - sqrt(2)) + 2)
sqrt(sqrt(sqrt(2) + 2) + 2)

我希望嵌套平方根的格式设置为全部显示在右侧,如下所示:

sqrt(2 - sqrt(2 - sqrt(2)))
sqrt(2 - sqrt(sqrt(2) + 2))
sqrt(2 + sqrt(2 - sqrt(2))) 
sqrt(2 + sqrt(sqrt(2) + 2))

有没有办法调整解决方案的输出格式,以便嵌套的平方根按需要显示?任何帮助将不胜感激!谢谢!

python sympy symbolic-math
1个回答
0
投票

如果

s
是表达式列表,则以下内容可以使用
order
关键字(指导打印机如何排序术语)根据需要打印它们:

>>> pprint(ok,order='rev-lex')
⎡   ________________     ________________     ________________     ___________
⎢  ╱       ________     ╱       ____________________
⎣╲╱  2 - ╲╱ 2 - √2  , ╲╱  2 - ╲╱ 2 + √2  , ╲╱  2 + ╲╱ 2 - √2  , ╲╱  2 + ╲╱ 2 +

_____⎤
____ ⎥
 √2  ⎦
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.