# Use expression.find with SR.wild in python scripts

I want to detect polynomials of the form x^n + m in a python script.

Found this helpful piece of code that works perfectly in sage's jupyter notebook:

x = var('x')
w0 = SR.wild(0)
w1 = SR.wild(1)
(x**2-2).find(x**w0+w1)


However, when I throw this into a .py file and run it I get the error *** TypeError: unsupported operand parent(s) for ^: 'Symbolic Ring' and 'Symbolic Ring'

Minimal Failing Example:

from sage.all import *
import sage
from sage.calculus.var import var

from sage.symbolic.ring import SymbolicRing
SR = SymbolicRing()
polynomial = SR('x^2-2')

x = var('x')
w0 = SR.wild(0)
w1 = SR.wild(1)
polynomial.find(x**w0+w1)


What am I missing?

edit retag close merge delete

Sort by » oldest newest most voted The problem is that the x in polynomial is not the x = var('x'):

>>> x is polynomial.variables()
False


It is better to be explicit:

>>> x = SR.var('x')
>>> polynomial = x**2 - 2
>>> polynomial.find(x**w0+w1)
[x^2 - 2]
>>> polynomial.match(x**w0+w1)
{$1: -2,$0: 2}


Or, if you would like to use the preparser:

>>> x = SR.var('x')
>>> polynomial = sage_eval('x^2 - 2', locals={'x' : x})

more