On Saturday, June 14, 2014 2:04:18 PM UTC-7, Michael Orlitzky wrote:
>
> On 06/14/2014 12:43 AM, jeanbi...@gmail.com <javascript:> wrote: 
> > I am coming up to speed on Python, Sympy, and Sage by doing some simple 
> > problems on all three.  Sympy has an option for its expand function, 
> > complex=True, that has made some of my expressions easier to read/use. 
> > I'm working with quotients of complex numbers.  This option allows Sympy 
> > to return expressions that look like a + bi broadly speaking with nice 
> > relations for a and b.  With Sage so far, I can't seem to get the 
> > equivalent level of expansion. 
> > 
> > I'd like to ask if there is a way to invoke that complex=True or its 
> > equivalent within Sage.  I've done a bit of searching but so far the 
> > answer has not jumped out at me.  I also tried the other support system 
> > but don't have enough points to post. 
> > 
>
> The sage expand() function doesn't use sympy. However, we have the 
> ability to send symbolic expressions back and forth between sage and 
> sympy, so it is possible. 
>
> First thing you should do is open a trac ticket with a feature request. 
> It should be possible to add the "complex" parameter to the sage 
> expand() function using sympy. 
>
> Second, for a workaround, you can do the following: 
>
>   sage: x = SR.symbol('x') 
>   sage: x._sympy_().expand() 
>   x 
>   sage: x._sympy_().expand(complex = True) 
>   re(x) + I*im(x) 
>
> This will give you the *visual* representation that you want, but it 
> isn't very convenient to work with. It looks like nobody's told sage how 
> to convert the sympy im() function back into a sage expression yet: 
>
>   sage: x._sympy_().expand(complex = True)._sage_() 
>   ... 
>   AttributeError: 'im' object has no attribute '_sage_' 
>
> This wouldn't be super difficult to do, though; the following already 
> works in sage: 
>
>   sage: x.real_part() + I*x.imag_part() 
>   I*imag_part(x) + real_part(x) 
>

This is very helpful, thanks. 

In Sympy, I did the following:
var('A B C D u v', real=True)
qi = 1/(u - I*v)
qf = (A + B/qi)/(C + D/qi)
expand(1/qf, complex=True)A2+2ABu+B2u2+B2v2+ADuA2+2ABu+B2u2+B2v2−iADvA2+2ABu
+B2u2+B2v2+BCuA2+2ABu+B2u2+B2v2+iBCvA2+2ABu+B2u2+B2v2+BDu2A2+2ABu+B2u2+B2v2+
BDv2A2+2ABu+B2u2+B2v2

I got a reasonable 7 term expansion of 1/qf so long as I defined real=True 
in the var statement (I learned that via the Sympy group).  Before I 
learned that step, the expansion was gigantic.


In Sage, I did
var('A B C D u v', domain = 'real') 
defined qi, qf, and 1/qf as above

When I tried
(1/qf)._sympy_().expand(complex = True) 
I got a huge expression similar to the one Sympy gave me before I learned 
about the real=True setting on the initial variables.

So, it looks like the method you suggested does work.  I'll dig deeper into 
Sage to see how I can get the results to be the same.  There may be 
something in addition to domain='real' that I need to set and/or send into 
Sympy.

Appreciate the help.



-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to