On 7/27/07, chowy <[EMAIL PROTECTED]> wrote:
> I've discovered Sage two days ago... I try to use it to compute
> integral and to play with polynomials... but my program seems to be
> very slow compared with swiginac library... Is that normal ?

General symbolic integration with SAGE uses Maxima via a pipes-based interface,
so it is slow, at least if done repeatedly, though it can handle
probably a much wider
range of integrals than Sympy or (?) swiginac.    The same is true for
arithmetic with symbolic polynomials.

Arithmetic with polynomials in a polynomial ring over a specific base ring
is extremely fast in SAGE, in many cases.  Here's a comparison:

sage: R. = QQ[]
sage: f = 3*x1*x2 + x1 - 2*x2 + 4
sage: time g = f^10  # does the "expand" part by default...
CPU time: 0.00 s,  Wall time: 0.00 s
sage: time g = f^100
CPU time: 0.67 s,  Wall time: 0.69 s

sage: import sympy   # included with SAGE-2.7.1
sage: x1 = sympy.Symbol("x1")
sage: x2 = sympy.Symbol("x2")
sage: f = int(3)*x1*x2 + x1 - int(2)*x2 + int(4)
sage: time g=(f^int(10)).expand()       # This already takes
"forever"... I gave up waiting.
... a long long time ...

Unfortunately, SAGE multivariate polynomials don't have a symbolic
integration method (though they do have a diff method for symbolic
differentiation).   It shouldn't be hard to add an integrate method for
polynomials and work with SAGE multivariate polynomials, if polynomials
are all you need.  Nobody has requested this before.

QUESTIONS:
   (1) Are you just working with polynomials?
   (2) Are you posting to point out shortcomings with SAGE (thanks!) or
because you specifically want to do SAGE?
   (3) I would greatly appreciate if you could create some more
similar timings, perhaps in a table (with code though), comparing
swiginac, SAGE, Sympy, (Maple, Mathematica?),
for a range of calculations.

The longterm plan with symbolics in SAGE was to implement something that
is very powerful to nail down the interface (and have the implementation be
correct and get done quickly, by building on what Maxima already does), then
redo some of the symbolic functionality using probably swiginac to make things
much faster.

> Here is times to compute 10 double integrals (I repeat tests 3 times
> and I keep the min time) :
>
> * with swiginac : 0.002506
> * with sympy : 0.554952
> * with sage : 1.101324
>
>
>
> ###################################
> ##
> ## Tests with sympy and swigianc
> ##
> ###################################
>
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
>
> import sys
> import sympy
> import swiginac
>
> def integSympy():
>     x1 = sympy.Symbol("x1")
>     x2 = sympy.Symbol("x2")
>
>     f = 3*x1*x2 + x1 - 2*x2 + 4
>
>     for _ in range(10):
>         sympy.integrate(f, (x2, -1, 1), (x1, -1, 1))
>
>     return 0
>
>
>
> def integSwiginac():
>     x1 = swiginac.symbol("x1")
>     x2 = swiginac.symbol("x2")
>
>     f = 3*x1*x2 + x1 - 2*x2 + 4
>
>     for _ in range(10):
>         swiginac.integral(x2, -1, 1, swiginac.integral(x1, -1, 1,
> f)).eval_integ()
>
>     return 0
>
>
> if __name__ == "__main__":
>
>     from timeit import Timer
>
>     t = Timer('integSympy()', 'from __main__ import integSympy')
>     print "execution with sympy : %f" % min(t.repeat(3, 1))
>
>     t = Timer('integSwiginac()', 'from __main__ import integSwiginac')
>     print "execution with swiginac : %f" % min(t.repeat(3, 1))
>
>     sys.exit(0)
>
>
>
>
> ###################################
> ##
> ## Tests with sage
> ##
> ###################################
>
>
> #!/usr/bin/env sage-python
> # -*- coding: utf-8 -*-
>
> import sys
> from sage.all import *
>
> def integ():
>     variables = var('x1,x2')
>
>     f = 3*x1*x2 + x1 - 2*x2 + 4
>
>     for _ in range(10):
>         integrate(integrate(f, x2, -1, 1), x1, -1, 1)
>
>     return 0
>
>
>
>
> if __name__ == "__main__":
>
>     from timeit import Timer
>
>     t = Timer('integ()', 'from __main__ import integ')
>     print "execution with sage : %f" % min(t.repeat(3, 1))
>
>
>     sys.exit(0)
>
>
>
> Thanks
>
>
> >
>


-- 
William Stein
Associate Professor of Mathematics
University of Washington
http://www.williamstein.org

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-forum
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to