Opened 11 years ago
Closed 10 years ago
#7537 closed defect (fixed)
list(SR('c').iterator()) is empty
Reported by: | malb | Owned by: | burcin |
---|---|---|---|
Priority: | critical | Milestone: | sage-4.7 |
Component: | symbolics | Keywords: | |
Cc: | burcin | Merged in: | sage-4.7.alpha3 |
Authors: | Burcin Erocal | Reviewers: | Martin Albrecht |
Report Upstream: | N/A | Work issues: | |
Branch: | Commit: | ||
Dependencies: | Stopgaps: |
Description (last modified by )
This looks like a bug to me:
sage: list(SR('c+1').iterator()) [c, 1] sage: list(SR('c').iterator()) []
Apply trac_7537-iterator.patch
Depends on #9989
Attachments (1)
Change History (10)
comment:1 Changed 11 years ago by
comment:2 Changed 11 years ago by
I see your point but why not return self
in that case?
comment:3 Changed 11 years ago by
Do you mean return an iterable which returns self
when .next()
is called?
If you're traversing a symbolic expression, and working with iterators, I can't think of any case where symbols, constants and numeric coefficients don't form a special case which will be treated separately. The usual way to write code to traverse the expression tree should check if .operator()
returns None
.
I admit that I don't use this interface often enough to decide on the design though. The recent thread on sage-devel about indexing parts of expressions also shows that this needs work.
Do you have a use case we can work from? How did you run into this?
comment:4 Changed 11 years ago by
I am converting boolean polynomials to Integer Programming problems, for this I convert them to symbolic expressions first and then to MIP. Probably not the most natural application.
comment:5 Changed 11 years ago by
So let's close this bug then?
comment:6 Changed 10 years ago by
- Status changed from new to needs_review
With attachment:trac_7537-iterator.patch we get an error when trying to iterate over symbols, constants or numeric objects:
sage: x.iterator() Traceback (most recent call last): ... ValueError: cannot iterate over numeric, constant or symbol sage: pi.iterator() Traceback (most recent call last): ... ValueError: cannot iterate over numeric, constant or symbol sage: SR(5).iterator() Traceback (most recent call last): ... ValueError: cannot iterate over numeric, constant or symbol
This patch depends on #9989 (not in terms of functionality, but it will fail to apply since the patch there touches the same part of sage/symbolic/expression.pyx
).
Changed 10 years ago by
comment:7 Changed 10 years ago by
- Description modified (diff)
I updated the patch with a new error message recommended in #9989.
comment:8 Changed 10 years ago by
- Reviewers set to Martin Albrecht
- Status changed from needs_review to positive_review
Patch looks good, applies cleanly against 4.7.alpha2 and doctests pass.
comment:9 Changed 10 years ago by
- Merged in set to sage-4.7.alpha3
- Resolution set to fixed
- Status changed from positive_review to closed
I don't think this is a bug. :)
It doesn't make sense to iterate over variables, constants or numeric coefficients. Iterators are only available over
add
,mul
, andpow
objects, which correspond to the obvious mathematical operations.AFAICT, this is also the case in MMA:
We can raise a
ValueError
if the expression corresponds to asymbol
,constant
ornumeric
object. The docstring should also be enhanced to point to the data structure for symbolic expressions here:http://www.ginac.de/tutorial/Internal-representation-of-products-and-sums.html
Comments?