The reason why the symbolic stuff in Sage is slow is that it uses a
psuedo-tty interface to talk to Maxima. There is a lot of overhead
with this due to waiting, synchronization, parsing the string output,
etc. One way to get the symbolic stuff to be faster is to make using
Sympy since it won't have that overhead (even though it is slower than
native maxima at the moment).
--Mike
Those times above are really weird. On my laptop (OSX 10.5.1):
sage: var(x)
x
sage: time sum(((x+sin(i))/x+(x-sin(i))/x).rational_simplify() for i
in xrange(100))
200
Time: CPU 0.97 s, Wall: 3.20 s
sage: time maxima('sum(ratsimp((x+sin(i))/x+(x-sin(i))/x),i,1,100)')
200
CPU time: 0.01 s, Wall time: 0.34 s
Thus it takes 3.2 seconds wall time instead of 39.10 seconds for me.
If you use more specialized algebraic structures in Sage, then the
time directly in Sage comes down a lot (to beat Maxima):
sage: x = RDF['x'].gen()
sage: time sum(((x+math.sin(i))/x+(x-math.sin(i))/x) for i in xrange(100))
200.0*x^200/(1.0*x^200)
CPU time: 0.20 s, Wall time: 0.21 s
Above we make x the indeterminate of the RDF[x], and use the floating
point sin function from Python. But it's not symbolic.
But sympy is still way faster and is symbolic:
sage: from sympy import Symbol, sin
sage: x = Symbol('x')
sage: time sum(((x+sin(i))/x+(x-sin(i))/x).expand() for i in xrange(100))
200
Time: CPU 0.09 s, Wall: 0.09 s
which is why it's a good thing that sympy is the future of symbolic
computation in Sage :-).
And since Sympy comes with Sage, maybe you can use it for
your intended application right now?!
-- William
> sage: time mathematica('Sum[Simplify[(x+Sin[i])/x+(x-Sin[i])/x],{i,
> 1,100}]')
> 200
> CPU time: 0.00 s, Wall time: 0.09 s
> sage: time maxima('sum(ratsimp((x+sin(i))/x+(x-sin(i))/x),i,1,10000)')
> 20000
> CPU time: 0.01 s, Wall time: 30.21 s
> sage: time mathematica('Sum[Simplify[(x+Sin[i])/x+(x-Sin[i])/x],{i,
> 1,10000}]')
> 20000
> CPU time: 0.01 s, Wall time: 2.20 s
> sage: time mathematica('Sum[Simplify[(x+Sin[i])/x+(x-Sin[i])/x],{i,
> 1,100000}]')
> 200000
> CPU time: 0.00 s, Wall time: 70.05 s
> sage: time mathematica('Sum[Evaluate[Simplify[(x+Sin[i])/x+(x-Sin[i])/
> x]],{i,1,100000}]')
> 200000
> CPU time: 0.00 s, Wall time: 0.03 s
> >
>
--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org
Sympy provides it's own matrices. As mentioned before, there needs to
be more work done with sympy in Sage so that what you tried does work.
In the meantime, look at the following example:
sage: import sympy
sage: x = sympy.Symbol('x')
sage: m = sympy.Matrix([[1,x],[x,1]])
sage: m
1 x
x 1
sage: m^int(2)
1 + x**2 2*x
2*x 1 + x**2
--Mike
BTW, we just fixed this ugly output of matrices in sympy, but I didn't
yet update the spkg.
> But sympy is still way faster and is symbolic:
>
> sage: from sympy import Symbol, sin
> sage: x = Symbol('x')
> sage: time sum(((x+sin(i))/x+(x-sin(i))/x).expand() for i in xrange(100))
> 200
> Time: CPU 0.09 s, Wall: 0.09 s
>
> which is why it's a good thing that sympy is the future of symbolic
> computation in Sage :-).
I think those timings are wrong, because sympy uses caching:
sage: from sympy import Symbol, sin
sage: x = Symbol('x')
sage: time sum(((x+sin(i))/x+(x-sin(i))/x).expand() for i in xrange(100))
CPU times: user 0.57 s, sys: 0.00 s, total: 0.57 s
Wall time: 0.57
200
sage: time sum(((x+sin(i))/x+(x-sin(i))/x).expand() for i in xrange(100))
CPU times: user 0.10 s, sys: 0.00 s, total: 0.10 s
Wall time: 0.09
200
See my previous email in this thread for more accurate timings.
Anyway, if you find any bugs in SymPy, please report them to sympy, so
that we can fix them.
Ondrej