ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 24 Apr 2019 12:14:49 -0500"from __future__ import print_function" fails on Sage scriptshttp://ask.sagemath.org/question/46342/from-__future__-import-print_function-fails-on-sage-scripts/Hello, Sage Community.
I am trying to use the "print" function in a .sage script, so I have added the line
from __future__ import print_function
as the first line to be included in my .sage file. Unfortunately, when running `sage test.sage`, Sage preparses the document and creates an auxiliary file "test.sage.py", which makes an import, then predefines some constants, and finally adds my preparsed code. As a consequence, the `from __future__ import print_function` is not the first line, and I get the following error message:
File "test.sage.py", line 6
from __future__ import print_function
SyntaxError: from __future__ imports must occur at the beginning of the file
Here is a MWE. The file "test.sage" containing:
from __future__ import print_function
print(1+1)
is preparsed to "test.sage.py" containing:
# This file was *autogenerated* from the file test.sage
from sage.all_cmdline import * # import sage library
_sage_const_1 = Integer(1)
from __future__ import print_function
print(_sage_const_1 +_sage_const_1 )
Of course, I could manually add this line to the .sage.py file and then execute it, but this could be tedious in my case for two reasons: 1. I have a lot of files which I have to modify and rerun every 15 minutes. 2. I also need this process to be automatic to be able to execute it with automatically generated script and even sageTeX.
Thanks in advance for your answers!dsejasWed, 24 Apr 2019 12:14:49 -0500http://ask.sagemath.org/question/46342/how to get hints on fixing silly syntax errorshttp://ask.sagemath.org/question/45243/how-to-get-hints-on-fixing-silly-syntax-errors/ Hi, I just spent half an hour on these two commands.
def mov():
P=matrix(QQ,[[1, 1],[0, 1]]);
show(P)
I retyped them, I moved them in different cells, nothing, I get syntax error.
"<ipython-input-76-049764d5c40b>", line 1
def mov() :
^
SyntaxError: invalid syntax
It works as long as I do not try to make a function
florinThu, 31 Jan 2019 05:05:30 -0600http://ask.sagemath.org/question/45243/File imports for polynomial rings in sagehttp://ask.sagemath.org/question/7584/file-imports-for-polynomial-rings-in-sage/If i type in the following
R.<z1,z2> = PolynomialRing(GF(5),2,"z")
direclty to Sage it accepts it as a polynomial ring.
But if i place this in a .py file and load it from this Sage
eg `sage my.py` it gives me a syntax error.
I guess i need a module loaded for it to be detected, but which module and hence which command do i need to make Sage accept this ring.aeThu, 19 Aug 2010 04:41:02 -0500http://ask.sagemath.org/question/7584/Traceback errorhttp://ask.sagemath.org/question/33456/traceback-error/ Hello,
I am new to Sage Math and new to this forum. Basically, I have written code to generate a specific graph on 10 vertices from its adjacency matrix. I am receiving an error (pasted below), but when my advisor runs the same exact code on his machine, he does not receive an error. I do not know how to resolve this situation. I've pasted my code below, and the error that I receive follows. Any insight would be much appreciated, and let me know if you need more details (e.g., information about the machine I'm using, operating system, etc.).
sage: M = Matrix([(0,0,1,0,1,1,1,0,1,1), (0,0,0,1,1,1,0,1,1,1), \
(1,0,0,1,1,1,1,0,0,0), (0,1,1,0,1,1,0,1,0,0), (1,1,1,1,0,1,0,0,1,0), \
(1,1,1,1,1,0,0,0,0,1), (1,0,1,0,0,0,0,1,1,1), (0,1,0,1,0,0,1,0,1,1), \
(1,1,0,0,1,0,1,1,0,1), (1,1,0,0,0,1,1,1,1,0)])
sage: M
[0 0 1 0 1 1 1 0 1 1]
[0 0 0 1 1 1 0 1 1 1]
[1 0 0 1 1 1 1 0 0 0]
[0 1 1 0 1 1 0 1 0 0]
[1 1 1 1 0 1 0 0 1 0]
[1 1 1 1 1 0 0 0 0 1]
[1 0 1 0 0 0 0 1 1 1]
[0 1 0 1 0 0 1 0 1 1]
[1 1 0 0 1 0 1 1 0 1]
[1 1 0 0 0 1 1 1 1 0]
sage: G = Graph(M); G
Graph on 10 vertices
sage: G.plot().show()
Here is the error.
Traceback (most recent call last): [0 0 0 1 1 1 0 1 1 1]
File "", line 1, in <module>
File "/private/var/folders/sw/0pqlf1452k58z_jbk3_yv5m97h2tyd/T/tmpabbyzH/___code___.py", line 5
G = Graph(M); G
^
SyntaxError: invalid syntax
JEAThu, 19 May 2016 14:56:50 -0500http://ask.sagemath.org/question/33456/Polynomials with coefficients in Zp - python SyntaxErrorhttp://ask.sagemath.org/question/31343/polynomials-with-coefficients-in-zp-python-syntaxerror/ Hi,
I am trying to create ℤₚ[x], the set of polynomials with coefficients in ℤₚ, with the following syntax :<br />
Zp = Integers(p)<br />
PRZp.<x> = Zp[]<br />
This works fine in the sage interpreter, but in a python file I get a syntax error at the "<" character. How can I solve this ? I've tried alternatives, like PRZp = PolynomialRing(Zp, 'x'), but this fails too with a "NameError: name 'x' is not defined" when I define a polynomial expression after that.OnoxSat, 05 Dec 2015 07:17:37 -0600http://ask.sagemath.org/question/31343/Can't find Cause of Traceback Errorhttp://ask.sagemath.org/question/29923/cant-find-cause-of-traceback-error/ I'm trying to convert this code that runs in Python 2.7 to Sage but can't even get a hint from Sage at what line the code is not accepted by Sage.
def Ramanujan(t):
cubes = [x**3 for x in range(1,t/10)]
crev = [] ''' Calculating Cube Roots'''
for x,x3 in enumerate(cubes): crev[x3] = x + 1
sums = sorted(x + y for x in cubes for y in cubes if y < x) ''' Organizing Data'''
for i in range(1, len(sums)-1):
if sums[i-1] != sums[i] and sums[i] == sums[i+1]: ''' Finding solutions'''
if sums[i]<=t: ''' Limiting how many solutions printed.'''
print "%10d"%(sums[i]) ''' Printing desired outputs '''
else:
break ''' Ending the function.'''
The only thing that Sage will report back is Traceback Error for SyntaxError: invalid syntax but no line information..
Can anyone tell where specifically the syntax is invalid??KristofferHSun, 11 Oct 2015 22:40:23 -0500http://ask.sagemath.org/question/29923/Syntax error on limithttp://ask.sagemath.org/question/23434/syntax-error-on-limit/ I would like to execute
limit(((n^(2n)-2n!+n*log(n,10))^((n^(2n))/(n!)))/(n^(((2n)^(2n))/((n-1)!))),n=infinity)
but I obtain the well-known big huge error message with painful infos inside. I cannot manage to fix the error, since I do not understand what is.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_19.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("bGltaXQoKChuXigybiktMm4hK24qbG9nKG4sMTApKV4oKG5eKDJuKSkvKG4hKSkpLyhuXigoKDJuKV4oMm4pKS8oKG4tMSkhKSkpLG49aW5maW5pdHkp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpe9jdup/___code___.py", line 3
limit(((n**(2n)-2n!+n*log(n,_sage_const_10 ))**((n**(2n))/(n!)))/(n**(((2n)**(2n))/((n-_sage_const_1 )!))),n=infinity)
^
SyntaxError: invalid syntax
'Sage Version 6.1.1, Release Date: 2014-02-04'CaterpillarWed, 16 Jul 2014 08:35:44 -0500http://ask.sagemath.org/question/23434/field extensions in pythonhttp://ask.sagemath.org/question/9998/field-extensions-in-python/You can define a field extenion at the sage prompt with
QC.<j>; = QQ.extension(x^2+1)
however this same line in a python script yields a syntax error
def mytest():
QC.<j> = QQ.extension(x^2+1);
return
Traceback (most recent call last):
...
def mytest(): QC.<j> = QQ.extension(x**_sage_const_2 +_sage_const_1 ); return
^
SyntaxError: invalid syntax
Note:syntax error point to < character
eval also fails
eval('QC.<j> = QQ.extension(x^2+1)')
Traceback (most recent call last):
....
QC.<j> = QQ.extension(x^2+1)
^
SyntaxError: invalid syntax
I also have similar problems with defining Quaternion Algebras in python
HQ.<q1,q2,q3> = QuaternionAlgebra(QC,-1,-1)
so why does QC.<j> = QQ.extension(x^2+1) work at the sage prompt and not in python or with eval(string)
Thanks,
StevestrangerlandSat, 06 Apr 2013 05:57:22 -0500http://ask.sagemath.org/question/9998/What's Wrong With This?http://ask.sagemath.org/question/9273/whats-wrong-with-this/ #Pneumatic Air Gun Calculations
import matplotlib.pyplot as plt
#Variables Outlined
#Muzzle Velocity
Vm=var('Vm')
#Atmospheric Pressure
Patm=var('Patm')
#Gas Chamber Volume
Vc=var('Vc')
#Barrel Area[Internal]
Ab=var('Ab')
#Barrel Length
Lb=var('Lb')
#Specific Heat of Air
k=var('k')
#CB Ratio[see table]
cb=var('cb')
#formula variable
eng=var('eng')
#2nd Formula Variable
eng2=var('eng2')
#Mass of the Projectile
md=var('md')
#Operating Pressure in atm
Pc=var('Pc')
#Definitions
k = 1.4
Patm = 101235
cb = 0.261208
#m/s^2
Vm= 91.44
md=2
Pc=10
eng=0.618081
#Main Formula
solve(eng=(k-1)*md*Vm^2/(2*Patm*Vc*(Pc-Pc^(1/k)),Vc)
Can anyone tell me why this is giving a syntax error and pointing to my variable definition Vm? It's really bugging me!duke11235Tue, 28 Aug 2012 10:55:37 -0500http://ask.sagemath.org/question/9273/