Ask Your Question

Can declare symbolic variable with specific type?

asked 2021-12-17 11:57:15 +0200

ttdsmt gravatar image

updated 2024-07-18 20:43:17 +0200

FrédéricC gravatar image

I know syntax to declare symbolic variable is


What I want is associating type to this variable, say like var('x', type=float). Can we do it in Sagemath

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2021-12-17 14:11:48 +0200

eric_g gravatar image

updated 2021-12-17 14:19:15 +0200

You have to keep in mind that in SageMath, symbolic variables have their own type:

sage: type(x)
<class 'sage.symbolic.expression.Expression'>

What you can do is to specify some domain for a symbolic variable; by default the domain is assumed to be the set of complex numbers, but you can restrict to real numbers by declaring

sage: x = var('x', domain='real')

This is taken into account in simplifications:

sage: sqrt(x^2)
sage: x = var('x', domain='complex')  # back to the default
sage: sqrt(x^2)

Other options are:

sage: x = var('x', domain='positive')
sage: sqrt(x^2)

sage: x = var('x', domain='integer')
sage: cos(x*pi).simplify_full()
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


Asked: 2021-12-17 11:57:15 +0200

Seen: 289 times

Last updated: Dec 17 '21