First time here? Check out the FAQ!

Ask Your Question
1

check if symbolic expression contains a variable

asked 12 years ago

leonardo gravatar image

updated 12 years ago

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

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
4

answered 12 years ago

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

example on aleph.sagemath.org

Preview: (hide)
link

Comments

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.

jupsal gravatar imagejupsal ( 8 years ago )
2

answered 12 years ago

Jason Grout gravatar image

updated 12 years ago

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

Preview: (hide)
link

Comments

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

leonardo gravatar imageleonardo ( 12 years ago )

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 12 years ago

Seen: 1,760 times

Last updated: May 04 '12