On Sat, Jun 14, 2014 at 3:53 PM, <jeanbigbo...@gmail.com> wrote: >> >> > > 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.
That seems like a bug in the _sympy_ conversion, in that it is discarding that the variables are assumed real. Could you just directly use sympy for what you're doing? Note that you'll have to turn off the Sage preparser, since with it on, you'll run into (surprising) trouble like this: sage: A,B,C,D,u,v = sympy.var('A B C D u v', real=True) sage: type(u) <class 'sympy.core.symbol.Symbol'> sage: type(1/u) <type 'sage.symbolic.expression.Expression'> where you've just left the world of sympy behind. Ugh. > > 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. I couldn't figure out how to set it. By the way, Dima claimed above that Sage uses Maxima for expand, which is NOT correct (It was true 5 years ago, but not today). The vast majority of the symbolic computation in Sage is implemented in the C++ library Ginac http://www.ginac.de/. It's easy to convert back and forth between Ginac and Maxima though. z = var('A B C D u v') qi = 1/(u - I*v) qf = (A + B/qi)/(C + D/qi) s = 1/qf s._maxima_() I don't know what problem you're trying to solve, but maybe this code is relevant: z = var('A B C D u v', domain='real') qi = 1/(u - I*v) qf = (A + B/qi)/(C + D/qi) s = 1/qf s.real() which outputs: B*D*u^2/(B^2*u^2 + B^2*v^2 + 2*A*B*u + A^2) + B*D*v^2/(B^2*u^2 + B^2*v^2 + 2*A*B*u + A^2) + B*C*u/(B^2*u^2 + B^2*v^2 + 2*A*B*u + A^2) + A*D*u/(B^2*u^2 + B^2*v^2 + 2*A*B*u + A^2) + A*C/(B^2*u^2 + B^2*v^2 + 2*A*B*u + A^2) WARNING! If you first do this in a session: z = var('A B C D u v') then the above will *NOT* work. As far as I can tell, you cannot redeclare a variable to be real if you ever made it without making it real before in the same session. Similarly, you can't change things with assume, e.g., this works fine: X, Y = var("X Y", domain = 'real') (X+I*Y).real() but this (surprisingly) fails: X, Y = var("X Y") assume("X", 'real') assume("Y", 'real') (X+I*Y).real() It appears that the assume command with second argument a "domain" is dangerously broken in Sage. So the main upshot for you is to start a new session and declare your variables to be real from the get go. William -- 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.