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 burcin)

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)

trac_7537-iterator.patch (2.0 KB) - added by burcin 10 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 11 years ago by burcin

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, and pow objects, which correspond to the obvious mathematical operations.

AFAICT, this is also the case in MMA:

Mathematica 7.0 for Linux x86 (32-bit)
Copyright 1988-2008 Wolfram Research, Inc.

In[1]:= T=x+1
Out[1]= 1 + x
In[2]:= T
Out[2]= 1 + x
In[3]:= T[[1]]
Out[3]= 1
In[4]:= X
Out[4]= X
In[5]:= X[[1]]
Part::partd: Part specification X[[1]] is longer than depth of object.
Out[5]= X[[1]]

We can raise a ValueError if the expression corresponds to a symbol, constant or numeric 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?

comment:2 Changed 11 years ago by malb

I see your point but why not return self in that case?

comment:3 Changed 11 years ago by burcin

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 malb

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 malb

So let's close this bug then?

comment:6 Changed 10 years ago by burcin

  • Authors set to Burcin Erocal
  • 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 burcin

comment:7 Changed 10 years ago by burcin

  • Description modified (diff)

I updated the patch with a new error message recommended in #9989.

comment:8 Changed 10 years ago by malb

  • 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 jdemeyer

  • Merged in set to sage-4.7.alpha3
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.