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.Fri, 02 Dec 2016 15:46:54 -0600Use sage files as python moduleshttp://ask.sagemath.org/question/35883/use-sage-files-as-python-modules/ I've got a project where on the one hand I'd like to use the syntactic sugar the sage preparser adds. So I want my source files to be `*.sage` not `*.py` at least in my editor. On the other hand, I would like to make use of Python's module system: each sage file should have its own module scope, and names put there should not affect other modules using the same names. I'd also like for modules to reference on another via relative imports, to facilitate code sharing.
Does Sage offer any help for this kind of use case? Any documentation somewhere?
I guess I could manually run the preparser, then rename all the files since `Foo.sage.py` can't contain a module `Foo.sage` as dots are not valid parts of a module name there. But perhaps there is something more clever. Some hook to the import system, so it can handle these `*.sage.py` files or automatically crate them via preparsing if needed. Or some way to select a different extension for the `sage -preparse` output. Or some way to have `sage.repl.load.load` do whole-file preparsing, adding relevant imports in particular. Something along these lines.MvGFri, 02 Dec 2016 15:46:54 -0600http://ask.sagemath.org/question/35883/preparse(False) (still) not workinghttp://ask.sagemath.org/question/24696/preparsefalse-still-not-working/ I write .sage files and then attach them in the interpreter (rather than using the notebook). Sometimes I want to have a section in .sage file consisting of pure python code (using for example the python module cvxopt), and of course this sometimes leads to type errors. I've tried including preparse(False) at the beginning of my code (as suggested here: http://www.sagemath.org/doc/faq/faq-usage.html#i-have-type-issues-using-scipy-cvxopt-or-numpy-from-sage) but in at least one case it returns the error AttributeError: 'bool' object has no attribute 'lstrip'.
I would very much appreciate a natural solution to this problem that doesn't involve changing anything in python (by decorating it with
int,r, and so on).
Here is my code (which works just fine as pure python or works fine if I type preparser(False) in the sage interpreter and then cut and paste the code into the interpreter instead of typing attach('code.sage')).
import cvxopt as cvx
from cvxopt import solvers
preparse(False)
c=cvx.matrix([1.,-1.,1.])
G=[cvx.matrix([[-7., -11., -11., 3.],[ 7., -18., -18., 8.],[-2., -8., -8., 1.]]) ]
G+=[cvx.matrix([[-21., -11., 0., -11., 10., 8., 0., 8., 5.],[ 0., 10., 16., 10., -10., -10., 16., -10., 3.],[ -5., 2., -17., 2., -6., 8., -17., 8., 6.]]) ]
h =[cvx.matrix([[33., -9.], [-9., 26.]]) ]
h+=[cvx.matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]])]
sol = solvers.sdp(c, Gs=G, hs=h)
print(sol['x'])
The expected output is
pcost dcost gap pres dres k/t
0: -1.2037e+00 -1.8539e+02 2e+02 2e-16 8e+00 1e+00
1: -1.2937e+00 -6.8551e+00 5e+00 5e-16 3e-01 3e-02
2: -2.8964e+00 -3.7331e+00 7e-01 1e-15 4e-02 5e-02
3: -3.0150e+00 -3.2556e+00 2e-01 7e-16 1e-02 2e-02
4: -3.1389e+00 -3.1932e+00 5e-02 4e-16 3e-03 5e-03
5: -3.1533e+00 -3.1547e+00 1e-03 3e-16 7e-05 1e-04
6: -3.1535e+00 -3.1536e+00 5e-05 6e-16 3e-06 6e-06
7: -3.1535e+00 -3.1535e+00 1e-06 3e-16 7e-08 2e-07
Optimal solution found.
[-3.68e-01]
[ 1.90e+00]
[-8.88e-01]
EDIT: tmonteil suggested changing preparse(False) to preparser(False). This works if I cut and paste the code into the interpreter but doesn't work if I attach the .sage file in the interpreter. See tmonteil's answer and my comment.
EDIT2: It also works in sage worksheet if I replace preparser(False) with %python (but I very much don't want to use the sage worksheet). Is this a bug, or am I doing something wrong?PoloniusThu, 30 Oct 2014 03:15:08 -0500http://ask.sagemath.org/question/24696/python equivalent of T(x,y)=(x*y,x+y) ?http://ask.sagemath.org/question/7930/python-equivalent-of-txyxyxy/In sage command line, I can write T(x,y) = (x*y,x+y) and can apply T.diff() to compute Jacobian.
But when I write the same in python, it says you can not assign to a function.
Does anyone know what I should write in python for T(x,y)=(x*y,x+y)?
ShuTue, 08 Feb 2011 05:49:59 -0600http://ask.sagemath.org/question/7930/