ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 17 Dec 2021 14:11:48 +0100Can declare symbolic variable with specific type?https://ask.sagemath.org/question/60323/can-declare-symbolic-variable-with-specific-type/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 SagemathFri, 17 Dec 2021 11:57:15 +0100https://ask.sagemath.org/question/60323/can-declare-symbolic-variable-with-specific-type/Answer by eric_g for <p>I know syntax to declare symbolic variable is</p>
<pre><code>var('x')
</code></pre>
<p>What I want is associating type to this variable, say like <code>var('x', type=float)</code>. Can we do it in Sagemath</p>
https://ask.sagemath.org/question/60323/can-declare-symbolic-variable-with-specific-type/?answer=60327#post-id-60327You 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)^xFri, 17 Dec 2021 14:11:48 +0100https://ask.sagemath.org/question/60323/can-declare-symbolic-variable-with-specific-type/?answer=60327#post-id-60327