# check if symbolic expression contains a variable

check if symbolic expression contains a given variable

Is there a function or method that tests a symbolic expression <type 'sage.symbolic.expression.expression'=""> to see if it contains a particular variable?

For example to test y(x) == -3/(x^3 + 3c + 3x) to see if it contains the variable c.

(I'm trying to write a a program to automatically plot the return value of the differential equation function desolve(), and it would help to be able to detect if the return expression contains the 'arbitrary constant' c.)

edit retag close merge delete

Sort by » oldest newest most voted You can also use the has() method for an expression:

sage: var('x,c')
sage: y(x)=-3/(x^3 + 3*c + 3*x)
sage: y.has(c)
True


example on aleph.sagemath.org

more

Is there an extension of this to functions? For example,

var('x,c')
function('f')
y(x) = -3/(x^3 + 3*c + 3*x) + f(c)
y.has(f(c))


? I am aware that this does not work as written, but I wonder if it is possible to extend it.

Try the .variables() method for an expression:

var('x,c')
y(x)=-3/(x^3 + 3*c + 3*x)
print y.variables()
c in y.variables()


http://aleph.sagemath.org/?q=aefb061f...

more

Thanks, Mike and Jason. Both methods work very nicely in my program.