problem with Imaginary in sympy

23 views
Skip to first unread message

Xuemei Gu

unread,
Apr 1, 2019, 3:02:27 PM4/1/19
to sympy

hey,

I wander whether one has occurred such problem: 

I define a initial state: 

state=a[1]*b[0]*c[0]*d[0]*1j+a[1]*b[0]*c[0]*d[0] + a[2]*b[1]*c[1]*d[1]     # for some reason I have to do such decomposition

datalist=state.as_coefficients_dict()   


I can get: 
            { FF2[0]*FF3[0]*FF4[0]: 1,  FF2[1]*FF3[1]*FF4[1]: 1,    I*FF2[0]*FF3[0]*FF4[0]: 1 } 

Now the problem is :

Because I is a Imaginary number, it's not a symbol. How can I get something such as  { FF2[0]*FF3[0]*FF4[0]: 1,  FF2[1]*FF3[1]*FF4[1]: 1,    FF2[0]*FF3[0]*FF4[0]: 1j} or { FF2[0]*FF3[0]*FF4[0]: 1+1j,  FF2[1]*FF3[1]*FF4[1]: 1 } 

Thank you very much!


 

Aaron Meurer

unread,
Apr 1, 2019, 3:31:35 PM4/1/19
to sympy
I was hoping something like collect(x*y + x, [x, y], evaluate=False)
would do what you want, but it doesn't quite.

You could create a Poly with the coefficients as variables and use
Poly.as_coefficients_dict.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/d4505824-6110-4541-8199-09b6768315d9%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Xuemei Gu

unread,
Apr 4, 2019, 10:41:26 AM4/4/19
to sy...@googlegroups.com
Thank you very much!

I have another question: 

I get a string list such as  setupstr='LI(OAMHolo(OAMHolo(XX,e,6),f,2),a,f)'

however in the string, LI OAMHolo are own defined functions, I don't know how to replace them as functions. 
I want to know how to do in sympy like I use ToExpression in Mathematica. 

Thank you very much!
best regards
Mei

You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/GKRuQ1iYgEI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.

To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.

For more options, visit https://groups.google.com/d/optout.


--
Xuemei Gu
PhD Student

State Key Laboratory for Novel Software Technology, Nanjing University
163 Xianlin Avenue, Qixia District, 210023, Nanjing City, China  

Institute for Quantum Optics and Quantum Information, Vienna
Boltzmanngasse 3, A-1090 Vienna, Austria

http://iqoqi-vienna.at/
Office: +43 (0) 1 4277 29568

Oscar Benjamin

unread,
Apr 4, 2019, 12:00:33 PM4/4/19
to sympy
You can do it like this:

In [7]: setupstr='LI(OAMHolo(OAMHolo(XX,e,6),f,2),a,f)'

In [8]: from sympy import S

In [9]: S(setupstr)
Out[9]: LI(OAMHolo(OAMHolo(XX, e, 6), f, 2), a, f)
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2BDAqr9yd3BkQnNYQ%3Dp6cXKccc8yZrFgirqxB3DmxEq%3Djy1grg%40mail.gmail.com.

Xuemei Gu

unread,
Apr 4, 2019, 12:47:35 PM4/4/19
to sy...@googlegroups.com
Thank you very much, I tried this, but it doesn't work. It only give the output but the the functions (LI,  OAMHolo) are not executed.

For simple example
stringA='3+4'
S(stringA)

It will give you the results 7.  It's correct but in my case these functions don't work.


Did I misunderstand?  Thank you very much! 



For more options, visit https://groups.google.com/d/optout.

Aaron Meurer

unread,
Apr 4, 2019, 1:53:58 PM4/4/19
to sympy
I answered on StackOverflow. You can pass in custom functions as the
second argument to sympify().

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2BDAqr_OEqGBrFEBHre7VRifeDGCrAEyzeuXqxNYriaiLDHgCw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages