Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Apparently the imag() function is trying to do the imaginary part of everything right off the bat:

sage: f=(I*(sqrt(-cos(l) + 1)*cosh(sin(1/2*l)) - sqrt(2)*sinh(sqrt(sin(1/2*l)^2)))*sin(1/2*l)^3/((-cos(l) + 1)^(3/2)*e^(1/2*I*l)))
sage: fimag=imag(f)

Now, compare f and fimag. fimag is way bigger (we'll just show the lengths of the string representations here:

sage: len(str(fimag))
25541
sage: len(str(f))
121

Let's look at the fast callable versions

sage: ff=fast_callable(f,vars=[l],domain=CC)
sage: ff.python_calls()
[cos, cos, <function sqrt at 0x10abaa2a8>, sin, cosh, <function sqrt at 0x10abaa2a8>, sin, <function sqrt at 0x10abaa2a8>, sinh, exp, sin]
sage: ffimag=fast_callable(fimag,vars=[l],domain=CC)
sage: len(ffimag.python_calls())
2336
sage: timeit('ff(1.0)')
625 loops, best of 3: 173 µs per loop
sage: timeit('ffimag(1.0)')
25 loops, best of 3: 27.1 ms per loop

I think this is a problem with the imag function. Here's what I would do to work around it for now:

sage: ff=fast_callable(f,vars=[l],domain=CDF)
sage: time plot(lambda x: imag(ff(x)), (0,10))

Time: CPU 0.42 s, Wall: 0.42 s

(Note that I use domain=CDF; that will be faster, as it uses machine floating point numbers)

Apparently the imag() function is trying to do the imaginary part of everything right off the bat:

sage: f=(I*(sqrt(-cos(l) + 1)*cosh(sin(1/2*l)) - sqrt(2)*sinh(sqrt(sin(1/2*l)^2)))*sin(1/2*l)^3/((-cos(l) + 1)^(3/2)*e^(1/2*I*l)))
sage: fimag=imag(f)

Now, compare f and fimag. fimag is way bigger (we'll just show the lengths of the string representations here:

sage: len(str(fimag))
25541
sage: len(str(f))
121

Let's look at the fast callable versions

sage: ff=fast_callable(f,vars=[l],domain=CC)
sage: ff.python_calls()
[cos, cos, <function sqrt at 0x10abaa2a8>, sin, cosh, <function sqrt at 0x10abaa2a8>, sin, <function sqrt at 0x10abaa2a8>, sinh, exp, sin]
sage: ffimag=fast_callable(fimag,vars=[l],domain=CC)
sage: len(ffimag.python_calls())
2336
sage: timeit('ff(1.0)')
625 loops, best of 3: 173 µs per loop
sage: timeit('ffimag(1.0)')
25 loops, best of 3: 27.1 ms per loop

I think this is a problem with the imag function. Here's what I would do to work around it for now:

sage: ff=fast_callable(f,vars=[l],domain=CDF)
sage: time plot(lambda x: imag(ff(x)), (0,10))

Time: CPU 0.42 s, Wall: 0.42 s

(Note that I use domain=CDF; that will be faster, as it uses machine floating point numbers)

Or here's another way to do it that tells imag to not try to symbolically compute the imaginary part:

sage: var('l')
l
sage: f=(I*(sqrt(-cos(l) + 1)*cosh(sin(1/2*l)) - sqrt(2)*sinh(sqrt(sin(1/2*l)^2)))*sin(1/2*l)^3/((-cos(l) + 1)^(3/2)*e^(1/2*I*l)))
sage: fimag=imag(f,hold=True)
sage: fimag
imag_part((I*sqrt(-cos(l) + 1)*cosh(sin(1/2*l)) - I*sqrt(2)*sinh(sqrt(sin(1/2*l)^2)))*e^(-1/2*I*l)*sin(1/2*l)^3/(-cos(l) + 1)^(3/2))
sage: ffimag=fast_callable(fimag,vars=[l],domain=CDF)
sage: ffimag.python_calls()
[(^2), (^3), imag_part]
sage: time plot(ffimag, (0,10))

Time: CPU 0.39 s, Wall: 0.39 s