Ask Your Question
1

Can declare symbolic variable with specific type?

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

ttdsmt gravatar image

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

FrédéricC gravatar image

I know syntax to declare symbolic variable is

var('x')

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
4

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

eric_g gravatar image

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

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)
abs(x)
sage: x = var('x', domain='complex')  # back to the default
sage: sqrt(x^2)
sqrt(x^2)

Other options are:

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

sage: x = var('x', domain='integer')
sage: cos(x*pi).simplify_full()
(-1)^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-12-17 11:57:15 +0100

Seen: 385 times

Last updated: Dec 17 '21