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, the 1st result is obtained in both cases.

edit retag close merge delete

(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?

Sort by » oldest newest most voted

The 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
more
{"ft_lang_label":"__label__en","ft_lang_prob":0.8799402,"math_prob":0.6866437,"size":1058,"snap":"2019-43-2019-47","text_gpt3_token_len":290,"char_repetition_ratio":0.08444023,"word_repetition_ratio":0.0,"special_character_ratio":0.26181474,"punctuation_ratio":0.11504425,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9661726,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-10-14T18:47:56Z\",\"WARC-Record-ID\":\"<urn:uuid:18b84eaa-4f3d-4b40-afff-b95be34fe3fd>\",\"Content-Length\":\"56846\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0a84dc52-fd21-4404-b047-da7c68f7070d>\",\"WARC-Concurrent-To\":\"<urn:uuid:4c382a78-a69a-414f-bb3f-3d5659c5261f>\",\"WARC-IP-Address\":\"140.254.118.68\",\"WARC-Target-URI\":\"https://ask.sagemath.org/question/34204/changing-order-of-factors-affects-sympy-expansion/?answer=34208\",\"WARC-Payload-Digest\":\"sha1:YT57JLMQT6446TKVMLA3ELQEBAU6QDRJ\",\"WARC-Block-Digest\":\"sha1:5ZU67KQPDMH6RJTXK6U3W36R7DYHLJKV\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-43/CC-MAIN-2019-43_segments_1570986654086.1_warc_CC-MAIN-20191014173924-20191014201424-00010.warc.gz\"}"}