Ask Your Question

Revision history [back]

This has nothing to do with SMC, it is a Sage vs Python issue.

The reason is that (0..n) does not exists in Python. Python understands 0..n as follows: 0. is a float corresponding to 0.0, hence 0..n is the method n of the float object 0.0.

Sage understands this because it has a prepaser that allows such syntaxic sugar, in the same way that 2^2 is preparsed (translated on the fly) into the pythonic 2**2.

Now, when you copy the function binomial_trans() into the trans.py file, this is raw Python without preparsing.

So, you need to transform (0..n) into something equivalent in Python, say range(n+1).

But then, if you try again, you will get a new error NameError: global name 'binomial' is not defined. Indeed, you use binomial in a python file without importing it. To know what to do, you can use the Sage function import_statements() which helps Sage devs a lot (note that Sage source code is made of raw unpreparsed Python files where stuff like that needs to be imported):

sage: import_statements("binomial")
# **Warning**: distinct objects with name 'binomial' in:
#   - sage.functions.other
#   - sage.rings.arith
from sage.rings.arith import binomial

So all what you have to do is to add the line from sage.rings.arith import binomial at the beginning of your file trans.py.

This has nothing to do with SMC, it is a Sage vs Python issue.

The reason is that (0..n) does not exists in Python. Python understands 0..n as follows: 0. is a float corresponding to 0.0, hence 0..n is the method n of the float object 0.0.

Sage understands this because it has a prepaser that allows such syntaxic sugar, in the same way that 2^2 is preparsed (translated on the fly) into the pythonic 2**2.

Now, when you copy the function binomial_trans() into the trans.py file, this is raw Python without preparsing.

So, you need to transform (0..n) into something equivalent in Python, say range(n+1).

But then, if you try again, you will get a new error NameError: global name 'binomial' is not defined. Indeed, you use binomial the binomial function in a python Python file without importing it. To know what to do, you can use the Sage function import_statements() which helps Sage devs developers a lot (note that Sage source code is made of raw unpreparsed Python files where stuff like that needs to be imported):imported, if you look at the source code you will see a lot of import statements):

sage: import_statements("binomial")
# **Warning**: distinct objects with name 'binomial' in:
#   - sage.functions.other
#   - sage.rings.arith
from sage.rings.arith import binomial

So all what you have to do is to add the line from sage.rings.arith import binomial at the beginning of your file trans.py.

Now it should work.

This has nothing to do with SMC, it is a Sage vs Python issue.

The reason is that (0..n) does not exists exist in Python. Python understands 0..n as follows: 0. is a float corresponding to 0.0, hence 0..n is the method n of the float object 0.0.

Sage understands this the way you want because it has a prepaser that allows such syntaxic sugar, in the same way that 2^2 is preparsed (translated on the fly) into the pythonic 2**2.

Now, when you copy the function binomial_trans() into the trans.py file, this is raw Python without preparsing.

So, you need to transform (0..n) into something equivalent in Python, say range(n+1).

But then, if you try again, you will get a new error NameError: global name 'binomial' is not defined. Indeed, you use the binomial function in a Python file without importing it. To know what to do, you can use the Sage function import_statements() which helps Sage developers a lot (note that Sage source code is made of raw unpreparsed Python files where stuff like that needs to be imported, if you look at the source code you will see a lot of import statements):

sage: import_statements("binomial")
# **Warning**: distinct objects with name 'binomial' in:
#   - sage.functions.other
#   - sage.rings.arith
from sage.rings.arith import binomial

So all what you have to do is to add the line from sage.rings.arith import binomial at the beginning of your file trans.py.

Now it should work.