#15321: Function changes behaviour, related to polynomial rings
---------------------------+----------------------------------------------
Reporter: gagern | Owner:
Type: defect | Status: needs_review
Priority: major | Milestone: sage-duplicate/invalid/wontfix
Component: algebra | Resolution:
Keywords: | Merged in:
Authors: | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
Dependencies: | Stopgaps:
---------------------------+----------------------------------------------
Changes (by nbruin):
* status: new => needs_review
* milestone: sage-5.13 => sage-duplicate/invalid/wontfix
Comment:
This is not a problem in Sage, but a documented "feature" of Python2. It
was realized to be a wart and fixed in Python3. The problem is indeed the
line you mark:
{{{
sage: p
(p0, p1, p2, p3, p4)
sage: [c for c, p in myc] # THIS LINE APPEARS TO BREAK THINGS!
[(t4 - t5)/t5, (t4 - t6)/t6]
sage: p
p2
}}}
List comprehensions leak their variables, so the value of your global
variable p (which gets used in your function `D`) gets overwritten. This
problem does not with happen if you write
{{{
sage: list(c for c, p in myc)
}}}
because iterators do not leak their variables. In Python3, list
comprehensions share the same semantics as iterators.
It's a wart, but as long as we stick with Python2 we'll have to live with
it. Ways to mitigate the problem:
- Don't rely on a global variable such as p inside a function. Derive
parents and their generators from your input variables (meaning: D should
probably have `PR2` as one of its parameters).
- Use `list(...)` rather than `[...]` if you don't want to leak variables
into scope.
Polynomials are indexable and iterable (for better or for worse), but
fraction field elements are not. Thus, you have:
{{{
sage: parent(D(4))
Multivariate Polynomial Ring in p0, p1, p2, p3, p4 over Fraction Field of
Multivariate Polynomial Ring in t0, t1, t2, t3, t4, t5, t6, t7, t8, t9
over Rational Field
sage: p=PR2.2 #THIS LINE BREAKS THINGS
sage: parent(D(4))
Fraction Field of Multivariate Polynomial Ring in t0, t1, t2, t3, t4, t5,
t6, t7, t8, t9 over Rational Field
}}}
--
Ticket URL: <http://trac.sagemath.org/ticket/15321#comment:2>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.