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.Sun, 24 Jul 2016 23:45:01 +0200Changing order of factors affects sympy expansionhttps://ask.sagemath.org/question/34204/changing-order-of-factors-affects-sympy-expansion/there is a strange behaviour when using sympy inside sage:
from sympy import Symbol, exp, I
import sympy
x = Symbol("x", real=True)
then, asking for ```exp(I*8*x).expand(complex=True)``` I get
I*sin(8*x) + cos(8*x)
that's ok. however, asking for ```exp(8*I*x).expand(complex=True)```, I get
I*exp(-8*im(x))*sin(8*re(x)) + exp(-8*im(x))*cos(8*re(x))
Why in the second case it forgets that $x$ is real? Is this behaviour expected?
this was causing me real trouble in another context, since I was calling functions from a package that itself uses sympy, and they didn't work unless some special ordering of the arguments was given.
I'm on SMC with sagemath kernel 6.10. I've checked in [Live SymPy](http://live.sympy.org/), the 1st result is obtained in both cases.
Sun, 24 Jul 2016 16:06:51 +0200https://ask.sagemath.org/question/34204/changing-order-of-factors-affects-sympy-expansion/Comment by mforets for <p>there is a strange behaviour when using sympy inside sage:</p>
<pre><code>from sympy import Symbol, exp, I
import sympy
x = Symbol("x", real=True)
</code></pre>
<p>then, asking for <code>exp(I*8*x).expand(complex=True)</code> I get </p>
<pre><code>I*sin(8*x) + cos(8*x)
</code></pre>
<p>that's ok. however, asking for <code>exp(8*I*x).expand(complex=True)</code>, I get</p>
<pre><code>I*exp(-8*im(x))*sin(8*re(x)) + exp(-8*im(x))*cos(8*re(x))
</code></pre>
<p>Why in the second case it forgets that $x$ is real? Is this behaviour expected? </p>
<p>this was causing me real trouble in another context, since I was calling functions from a package that itself uses sympy, and they didn't work unless some special ordering of the arguments was given. </p>
<p>I'm on SMC with sagemath kernel 6.10. I've checked in <a href="http://live.sympy.org/">Live SymPy</a>, the 1st result is obtained in both cases.</p>
https://ask.sagemath.org/question/34204/changing-order-of-factors-affects-sympy-expansion/?comment=34205#post-id-34205```(1*I).parent()``` gives SR, while ```(I*1).parent()``` gives 'ImaginaryUnit' object has no attribute 'parent'. My guess is that given a product it is searching for the parent of each operand sequentially, and it fails since I has no parent. Is this kind of shortcut useful in another context?Sun, 24 Jul 2016 18:47:34 +0200https://ask.sagemath.org/question/34204/changing-order-of-factors-affects-sympy-expansion/?comment=34205#post-id-34205Answer by tmonteil for <p>there is a strange behaviour when using sympy inside sage:</p>
<pre><code>from sympy import Symbol, exp, I
import sympy
x = Symbol("x", real=True)
</code></pre>
<p>then, asking for <code>exp(I*8*x).expand(complex=True)</code> I get </p>
<pre><code>I*sin(8*x) + cos(8*x)
</code></pre>
<p>that's ok. however, asking for <code>exp(8*I*x).expand(complex=True)</code>, I get</p>
<pre><code>I*exp(-8*im(x))*sin(8*re(x)) + exp(-8*im(x))*cos(8*re(x))
</code></pre>
<p>Why in the second case it forgets that $x$ is real? Is this behaviour expected? </p>
<p>this was causing me real trouble in another context, since I was calling functions from a package that itself uses sympy, and they didn't work unless some special ordering of the arguments was given. </p>
<p>I'm on SMC with sagemath kernel 6.10. I've checked in <a href="http://live.sympy.org/">Live SymPy</a>, the 1st result is obtained in both cases.</p>
https://ask.sagemath.org/question/34204/changing-order-of-factors-affects-sympy-expansion/?answer=34208#post-id-34208The main issue seems indeed that coercion is not well defined with sympy objects. Symbolics is handled by Sage with `giacpy`, and not `sympy`, this latter is not even imported at startup, which might explain the lack of integration of sympy within Sage. There have been some discussions to make `sympy` (or even `csympy`) the handler of symbolics in Sage, but it is not the case.
The coercion enters the game because `8` is a Sage integer, not a Python `int`. If you want to avoid that you can turn off preparsing, by doing:
sage: preparser(False)
Alternatively, you can tell Sage that `8` is a python `int`, in one of the following ways:
sage: 8r*I*x
8*I*x
sage: int(8)*I*x
8*I*x
Sun, 24 Jul 2016 23:45:01 +0200https://ask.sagemath.org/question/34204/changing-order-of-factors-affects-sympy-expansion/?answer=34208#post-id-34208