Ask Your Question
1

Sage ranges in IP-Notebooks on CSM

asked 2014-06-28 05:57:11 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

On cloud.sagemath

%load_ext sage

def f(n):
    for k in (1..n):
        print n
f(5)

gives

AttributeError                         
----> 1 f(Integer(5))
in f(n)
  2 
  3 def f(n):
----> 4     for k in (1..n):
  5         print n
AttributeError: 'float' object has no attribute 'n'

Does "%load_ext sage" not invoke the Sage preparser?

edit retag flag offensive close merge delete

Comments

Could you give more details on what exactly to do on cloud.sagemath.org to reproduce this?

slelievre gravatar imageslelievre ( 2014-06-28 11:49:57 +0100 )edit

Create a new IPython notebook, copy the five lines given. That's all.

petropolis gravatar imagepetropolis ( 2014-06-28 13:51:46 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2014-06-28 11:47:51 +0100

slelievre gravatar image

updated 2014-06-28 16:24:12 +0100

EDIT (knowing that this is happening in an IPython notebook):

If you put the line

%load_ext sage

in a cell by itself and evaluate it, and then put the rest in a second cell,

def f(n):
    for k in (1..n):
        print n

f(5)

then your example will work.

Note that, with everything in the same cell, as you had, if after getting the AttributeError you re-evaluate the cell, then you get this warning:

The sage extension is already loaded. To reload it, use:
  %reload_ext sage

but your function works and you get the expected output

5
5
5
5
5

The explanation is that %load_ext sage needs to be evaluated first before the preparser is activated letting you use any Sage syntax that violates standard Python syntax rules.

I would even put the definition of f and the command f(5) in different cells, so that you can work on the function definition separately from applying it to different values:

  • while working on the definition of f (getting rid of any syntax errors), no use in trying to apply it.

  • once f is defined, to try it with different arguments, no need to define it again each time.


Original answer:

Are you writing these lines in a Python file?

The error message seems to indicate that (1..n) was parsed as trying to apply the method .n to the float 1., which is similar to other errors one can get when using certain Sage syntax shorthands involving a dot (.) in a Python file.

Besides (1..n) and [1..n], I am thinking of syntax goodies to choose names for generators of an object as you define this object, such as in these examples:

R.<x,y,z> = PolynomialRing(QQ)
K.<a> = NumberField(x^2 - x - 1)
F.<a,b> = FreeGroup(2)

If used in a Python file, these will result in an error, because Python doesn't like these uses of the dot. In a Python file the examples above would become:

R = PolynomialRing(QQ,['x','y','z'])
x,y,z = R.gens()

P = PolynomialRing(QQ,'x')
x = P.gen()
K = NumberField(x**2 - x - 1)
a = K.gen()

F = FreeGroup(2)
a,b = F.gens()

(with appropriate import statements at the start of the file).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-06-28 05:57:11 +0100

Seen: 420 times

Last updated: Jun 28 '14