Hi Jason, thanks for your suggestion and your detailed answer, but actually I did not start this thread for performance reasons, I did start it to ask why "i^2" is not treated like an exact symbolic expression in sage:
---------------------------------------------------------------------- | Sage Version 3.2, Release Date: 2008-11-20 | | Type notebook() for the GUI, and license() for information. | ---------------------------------------------------------------------- sage: i^2 -1 sage: CDF(_) -1.0 + 1.22460635382e-16*I and I was wondering if this is a bug, obviously it is not, but I still don't understand it :-) Georg On 3 Dez., 04:06, Jason Grout <[EMAIL PROTECTED]> wrote: > ggrafendorfer wrote: > > Hi Robert, > > this was not a misunderstanding, there is an "n" missing :-), I > > corrected it: > > I wanted to write > > > then i^2 should also be of type CDF, but > > > rather then > > > the i^2 should also .... > > > as an answer to your statement, namely that i^2 is getting turned into > > CDF(i)^CDF(2) > > > I hope this is clear now > > >> Generally in Sage we prefer exact arithmetic to numeric > >> approximations, because once you start doing numeric things one has > >> to deal with rounding errors, etc. and there's no going back. > > > and that's exactly the reason why I don't understand why "i^2" is not > > be treated like an exact symbolic expression > > See the bottom of the message for my suggestion, which basically is to > use the python complex i if you really want speed (i.e., at the top of > your program, do "i = complex(I)" > > I'll take a try at explaining this. This may be more for the interested > reader than for you specifically, so sorry if it's rehashing ground that > has already been covered. > > In Sage, as you know, we have numerous ways of representing i. The > default way is to represent it using a symbolic expression: > sage: type(i) > <class 'sage.functions.constants.I_class'> > sage: type(i^2) > <class 'sage.calculus.calculus.SymbolicArithmetic'> > > Of course, we can also treat i as a numeric quantity (basically, we > treat complex numbers as double precision floating point numbers; one > double for the real part, one for the imaginary part). That's using CDF: > > sage: cdf_i=CDF.0 > sage: cdf_i > 1.0*I > sage: cdf_i^2 > -1.0 + 1.22460635382e-16*I > sage: cdf_i*cdf_i > -1.0 > > Unfortunately, as you see above, this representation is prone to > roundoff error and gives slightly inaccurate results when squaring. > That's what happens when you work with a numerical i (that's why the > default "i" in Sage is symbolic). > > You could also declare i as the root of the polynomial x^2+1; that's > what Robert did earlier, and is still fast, and what's more, it is exact. > > sage: K.<ii> = QuadraticField(-1) > sage: ii > ii > sage: ii^2 > -1 > > In fact, using the quadratic field is the fastest Sage solution from > what we have above: > > sage: #Symbolic I > sage: timeit('i^2') > 625 loops, best of 3: 86.7 µs per loop > sage: # CDF I > sage: timeit('cdf_i^2') > 625 loops, best of 3: 41.3 µs per loop > sage: # Exact I as a root of a polynomial > sage: timeit('ii^2') > 625 loops, best of 3: 5.65 µs per loop > > However, for your purposes, maybe using the builtin python complex type > is best; it is by far the fastest and it has the property that you want > (namely, i^2=-1) > > sage: python_i = complex(I) > sage: python_i > 1j > sage: python_i^2 > (-1+0j) > sage: timeit('python_i^2') > 625 loops, best of 3: 2.72 µs per loop > > Jason --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---