Ask Your Question

check if symbolic expression contains a variable

asked 2012-05-04 14:38:37 +0200

leonardo gravatar image

updated 2012-05-05 21:43:53 +0200

kcrisman gravatar image

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

2 Answers

Sort by ยป oldest newest most voted

answered 2012-05-04 19:55:00 +0200

Mike Hansen gravatar image

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)

example on

edit flag offensive delete link more


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

y(x) = -3/(x^3 + 3*c + 3*x) + f(c)

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

jupsal gravatar imagejupsal ( 2016-07-26 23:03:08 +0200 )edit

answered 2012-05-04 16:09:08 +0200

Jason Grout gravatar image

updated 2012-05-04 16:09:29 +0200

Try the .variables() method for an expression:

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

edit flag offensive delete link more


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

leonardo gravatar imageleonardo ( 2012-05-05 14:02:59 +0200 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2012-05-04 14:38:37 +0200

Seen: 1,562 times

Last updated: May 04 '12