Ask Your Question

Revision history [back]

I don't know for the symbolic range, but note that sage provides srange which takes floats as input:

sage: srange(-3.0,3.0,0.02)
[-3.00000000000000,
 -2.98000000000000,
 -2.96000000000000,
 -2.94000000000000,
 ...
 2.96000000000000,
 2.98000000000000]

I don't know for the symbolic range, but note that sage provides srange which takes floats as input:

sage: srange(-3.0,3.0,0.02)
[-3.00000000000000,
 -2.98000000000000,
 -2.96000000000000,
 -2.94000000000000,
 ...
 2.96000000000000,
 2.98000000000000]

EDIT:

Above, the ellipse "..." replacing the long output was done by hand. To have the ellipse to appear automatically, the best is to create your own class. For example:

sage: class MyRange(SageObject):
....:     def __init__(self, start, stop, step):
....:         self._start = start
....:         self._stop = stop
....:         self._step = step
....:     def to_srange(self):
....:         return srange(self._start, self._stop, self._step)
....:     def __iter__(self):
....:         return iter(self.to_srange())
....:     def __repr__(self):
....:         L = self.to_srange()
....:         if len(L) < 10:
....:             return repr(L)
....:         else:
....:             Lellipse = L[:3] + ["..."] + L[-3:]
....:             return '['+', '.join(str(a) for a in Lellipse)+']'
....:         
....:         
sage: MyRange(-3,3,0.02)
[-3.00000000000000, -2.98000000000000, -2.96000000000000, ..., 2.94000000000000, 2.96000000000000, 2.98000000000000]
sage: sum(a^2 for a in MyRange(-3,3,0.02))
900.020000000001
sage: sum(a^2 for a in srange(-3,3,0.02))   # confirms that MyRange considers the whole list
900.020000000001

I don't know for the symbolic range, but note that sage provides srange which takes floats as input:

sage: srange(-3.0,3.0,0.02)
[-3.00000000000000,
 -2.98000000000000,
 -2.96000000000000,
 -2.94000000000000,
 ...
 2.96000000000000,
 2.98000000000000]

EDIT:

Above, the ellipse "..." replacing the long output was done by hand. To have the ellipse to appear automatically, the best is to create your own class. For example:

sage: class MyRange(SageObject):
....:     def __init__(self, start, stop, step):
....:         self._start = start
....:         self._stop = stop
....:         self._step = step
....:     def to_srange(self):
....:         return srange(self._start, self._stop, self._step)
....:     def __iter__(self):
....:         return iter(self.to_srange())
....:     def __repr__(self):
....:         L = self.to_srange()
....:         if len(L) < 10:
....:             return repr(L)
....:         else:
....:             Lellipse = L[:3] + ["..."] + L[-3:]
....:             return '['+', '.join(str(a) for a in Lellipse)+']'
....:         
....:         
sage: MyRange(-3,3,0.02)
MyRange(-3,3,1)      # short list no ellipse
[-3, -2, -1, 0, 1, 2]
sage: MyRange(-3,3,0.02)    # long list with ellipse
[-3.00000000000000, -2.98000000000000, -2.96000000000000, ..., 2.94000000000000, 2.96000000000000, 2.98000000000000]
sage: sum(a^2 for a in MyRange(-3,3,0.02))
900.020000000001
sage: sum(a^2 for a in srange(-3,3,0.02))   # confirms that MyRange considers the whole list
900.020000000001

To learn how to use classes in Python/Sage, you may also look at this fruit.py example available in my optional package.