Ask Your Question
1

Use expression.find with SR.wild in python scripts

asked 2021-10-27 16:21:11 +0100

Spirit gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-10-27 19:26:37 +0100

rburing gravatar image

updated 2021-10-27 19:30:58 +0100

The problem is that the x in polynomial is not the x = var('x'):

>>> x is polynomial.variables()[0]
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})
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-10-27 16:21:11 +0100

Seen: 205 times

Last updated: Oct 27 '21