2017-08-01 5 views
0

私は多項式関数を書いたPythonプログラムを持っており、Sympyのsolve()関数を使ってその逆を見つける。私は次のエラーを取得するプログラムの実行時にSympifyError解析多項式

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)) 

しかし、:Sympyについては

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固有の構文が本当に分かりません。

答えて

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] 
+0

は、私は2よりも高い指数でそれを行うことができます任意の方法はありますか? – Matt

+0

はい、この部分の2をより大きな整数に変更してください: 'random_poly(x、2、-1000,1000)' – Brian