SympifyError解析多项式

时间:2017-08-01 02:00:24

标签: python sympy polynomials

我有一个编写多项式函数的python程序,然后使用Sympy的solve()函数来查找它们的反转。

x = Symbol('x')
y = Symbol('y')
for i in range(10):
    p = Poly.create_random()
    print("p orig " + str(p))
    solve(p, x)
    print("p in terms of x: " + str(p))
    solve(p, y)
    print("p in terms of y (inverse): " + str(p))

但是,当我运行程序时,我收到以下错误:

p orig y = 5*x**2 - 55*x**1 + 140
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/sympy/core/sympify.py", line 354, in sympify
    expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
  File "/usr/local/lib/python3.5/dist-packages/sympy/parsing/sympy_parser.py", line 894, in parse_expr
    return eval_expr(code, local_dict, global_dict)
  File "/usr/local/lib/python3.5/dist-packages/sympy/parsing/sympy_parser.py", line 807, in eval_expr
    code, global_dict, local_dict)  # take local objects in preference
  File "<string>", line 1
    Symbol ('y' )=Integer (5 )*Symbol ('x' )**Integer (2 )-Integer (55 )*Symbol ('x' )**Integer (1 )+Integer (140 )
                 ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "z.py", line 110, in <module>
    solve(p, x)
  File "/usr/local/lib/python3.5/dist-packages/sympy/solvers/solvers.py", line 833, in solve
    f, symbols = (_sympified_list(w) for w in [f, symbols])
  File "/usr/local/lib/python3.5/dist-packages/sympy/solvers/solvers.py", line 833, in <genexpr>
    f, symbols = (_sympified_list(w) for w in [f, symbols])
  File "/usr/local/lib/python3.5/dist-packages/sympy/solvers/solvers.py", line 824, in _sympified_list
    return list(map(sympify, w if iterable(w) else [w]))
  File "/usr/local/lib/python3.5/dist-packages/sympy/core/sympify.py", line 356, in sympify
    raise SympifyError('could not parse %r' % a, exc)
sympy.core.sympify.SympifyError: Sympify of expression 'could not parse 'y = 5*x**2 - 55*x**1 + 140'' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)

对于Sympy,您必须使用**编写指数,并将乘法符号编写为*。除此之外,我不太确定Sympy的具体语法。

1 个答案:

答案 0 :(得分:0)

我认为你需要用符号x和y来定义多项式。 solve假设多项式等于零,因此从中减去y,如下所示。在Python 3.5.2中:

from sympy import Symbol, polys, solve
x = Symbol('x')
y = Symbol('y')
for i in range(10):
    p = polys.specialpolys.random_poly(x,2,-1000,1000) - y # equals zero
    print("p orig " + str(p))

    p_solution_x = solve(p, x)
    print('p_solution_x:', p_solution_x)

    p_solution_y = solve(p, y)
    print('p_solution_y:', p_solution_y)

给出了这个结果:

p orig -292*x**2 - 550*x - y - 984
p_solution_x: [-sqrt(-292*y - 211703)/292 - 275/292, sqrt(-292*y - 211703)/292 - 275/292]
p_solution_y: [-292*x**2 - 550*x - 984]
p orig 969*x**2 + 809*x - y - 676
p_solution_x: [-sqrt(3876*y + 3274657)/1938 - 809/1938, sqrt(3876*y + 3274657)/1938 - 809/1938]
p_solution_y: [969*x**2 + 809*x - 676]
p orig -604*x**2 + 382*x - y - 705
p_solution_x: [-sqrt(-604*y - 389339)/604 + 191/604, sqrt(-604*y - 389339)/604 + 191/604]
p_solution_y: [-604*x**2 + 382*x - 705]
p orig -721*x**2 - 846*x - y - 908
p_solution_x: [-sqrt(-721*y - 475739)/721 - 423/721, sqrt(-721*y - 475739)/721 - 423/721]
p_solution_y: [-721*x**2 - 846*x - 908]
p orig 422*x**2 + 77*x - y + 914
p_solution_x: [-sqrt(1688*y - 1536903)/844 - 77/844, sqrt(1688*y - 1536903)/844 - 77/844]
p_solution_y: [422*x**2 + 77*x + 914]
p orig 847*x**2 - 273*x - y - 68
p_solution_x: [-sqrt(3388*y + 304913)/1694 + 39/242, sqrt(3388*y + 304913)/1694 + 39/242]
p_solution_y: [847*x**2 - 273*x - 68]
p orig -703*x**2 + 587*x - y + 589
p_solution_x: [-sqrt(-2812*y + 2000837)/1406 + 587/1406, sqrt(-2812*y + 2000837)/1406 + 587/1406]
p_solution_y: [-703*x**2 + 587*x + 589]
p orig -999*x**2 + 827*x - y + 699
p_solution_x: [-sqrt(-3996*y + 3477133)/1998 + 827/1998, sqrt(-3996*y + 3477133)/1998 + 827/1998]
p_solution_y: [-999*x**2 + 827*x + 699]
p orig 364*x**2 + 517*x - y - 552
p_solution_x: [-sqrt(1456*y + 1071001)/728 - 517/728, sqrt(1456*y + 1071001)/728 - 517/728]
p_solution_y: [364*x**2 + 517*x - 552]
p orig 456*x**2 - 852*x - y + 144
p_solution_x: [-sqrt(114*y + 28953)/228 + 71/76, sqrt(114*y + 28953)/228 + 71/76]
p_solution_y: [456*x**2 - 852*x + 144]