Hi!

On 2 Dez., 17:47, Simon King <simon.k...@nuigalway.ie> wrote:
[...]
> IIRC, I tried various other methods (without strings), but they were
> all slower. However, I don't remember any concrete examples.
> So, it would help me if you commented 
> onhttp://trac.sagemath.org/sage_trac/ticket/7580
> what I should try instead. I am overhauling the code anyway.

Here is one, that is actually needed internally:

# Create a polynomial ring
# (the typical underlying finite polynomial ring of a densely
implemented InfinitePolynomialRing)
sage: Vars = ['x_'+str(n) for n in range(50)]+['y'+str(n) for n in
range(50)]
sage: R = PolynomialRing(QQ,Vars)
# Create a big random element
sage: p = R.random_element()
sage: p *= R.random_element()
sage: p *= R.random_element()
sage: p *= R.random_element()
sage: p *= R.random_element()
# Some RE to analyse the leading monomial
sage: import re
sage: RE = re.compile('([a-zA-Z0-9]+)_([0-9]+)\^?([0-9]*)')
sage: RE.findall(str(p.lm()))
[('x', '1', '2'),
 ('x', '13', '2'),
 ('x', '16', ''),
 ('x', '23', ''),
 ('x', '45', '')]
# Essentially the same information is contained in the exponent vector
# of the leading monomial. Say:
sage: zip(Vars,p.lm().exponents()[0])
[('x_0', 0),
 ('x_1', 2),
 ('x_2', 0),
 ('x_3', 0),
 ('x_4', 0),
 ('x_5', 0),
 ('x_6', 0),
...
# Now compare the timings:
sage: timeit('L = RE.findall(str(p.lm()))')
625 loops, best of 3: 23.8 µs per loop
sage: timeit('L = zip(Vars,p.lm().exponents()[0])')
625 loops, best of 3: 40.5 µs per loop

The obvious reason is that only few variables of R actually appear in
p.lm().
*But*: This is the typical situation. And this is why I chose to work
with regular expressions rather than "cleaner" methods.

Cheers,
Simon

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to