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, 26 Oct 2018 13:24:48 -0500How to read and run commands from a text file?http://ask.sagemath.org/question/44086/how-to-read-and-run-commands-from-a-text-file/I have all my commands in a text file, and I would just like to run them in Sage. What is the easiest way to do this?
For example, in sage.txt, I have written:
d0=matrix(ZZ,4,4,{(0,0):1,(2,0):1,(1,1):1,(3,1):1,(1,2):1,(3,2):1});
d1=matrix(ZZ,4,4,{(1,0):-1,(2,0):-1,(3,1):-1,(1,2):1,(2,2):1,(3,3):1});
The command `with open('/home/sage.txt','r') as ll: bdrs=[sage_eval(l.strip()) for l in ll];` returns
File "<string>", line 1
d0=matrix(ZZ,Integer(4),Integer(4),{(Integer(0),Integer(0)):Integer(1),(Integer(2),Integer(0)):Integer(1),(Integer(1),Integer(1)):Integer(1),(Integer(3),Integer(1)):Integer(1),(Integer(1),Integer(2)):Integer(1),(Integer(3),Integer(2)):Integer(1)});
^
SyntaxError: invalid syntax
Also, does Sage have a limit on how much RAM it can use? If yes, how can I increase the available memory?LeonFri, 26 Oct 2018 13:24:48 -0500http://ask.sagemath.org/question/44086/Sagecell with code hosted in GitHubhttp://ask.sagemath.org/question/42115/sagecell-with-code-hosted-in-github/I would like to embed a Sage Cell in a webpage whose content comes from a GitHub `.py` file, so that changes in this file are immediately shown in the webpage. I have tried to use `javascript` / `jquery` like this (with the standard scripts in the head) and with some variations:
<div class="sage"><script type="text/x-sage" id="sage1"></script></div>
<script>$('#sage1').load("https://rawgit.com/path_to_the_file.py");</script>
and it sometimes work, but not always. I guess it has to do with the order of all the scripts involved in the process. Which would be the most appropiate and clean way to inject such code in a Sage Cell?
**EDIT:** I would like to show the input cell together with the input code, for educational purposes. So the content of the input cell has to be replaced with the content of the `.py` file.jepstraSat, 21 Apr 2018 10:51:40 -0500http://ask.sagemath.org/question/42115/Where is the sage save or load dir and how do I change it?http://ask.sagemath.org/question/37832/where-is-the-sage-save-or-load-dir-and-how-do-i-change-it/ How in the heck does sage notebook (for Linux) manage files? I can't figure where it's saving them or how to load one. I tried os.getcwd() and got a temp file '/tmp/tmpJcEfeK' so I tried changing to the download directory, where it looked like an sws file was saved, with os.chdir("/home/jim/Downloads") That appeared to work since there was no error message. But then I did getcwd again and was still in the temp file. I just want to load a python script "diffusion.py" that's in the downloads folder but that fails. Finding where Sage actually is or changing it is a nightmare.cybervigilanteWed, 07 Jun 2017 18:21:35 -0500http://ask.sagemath.org/question/37832/How to make a function of 'fname' that produce a file named 'fname'http://ask.sagemath.org/question/36655/how-to-make-a-function-of-fname-that-produce-a-file-named-fname/ Hi all,
Typing a correction on some recurrent sequences $u_{n+1}=f(u_n)$ I use sage to build a nice plot illustrating the behavior of the sequence depending on the initial data.
I took a nice function from this awesome [book.](http://sagebook.gforge.inria.fr/)
def escargot(f,x,u0,n,xmin,xmax,file):
u = u0
P = plot(x, x, xmin, xmax, color='gray')
for i in range(n):
P+= line([[u,u],[u,f(u)],[f(u),f(u)]], color ='red')
u = f(u)
P += f.plot(x, xmin, xmax, color='blue')
P.save('/home/myfolder/f1a.png')
So everyhting work perfectly but, since there is a lot of examples to produce, I would like to make the function to have an extra argument say filename so that now the file so that now the plot is saved in /home/myfolder/filename.png,
but I have no idea how sage manipulate string.
Help would be welcome !
Laurent BSat, 18 Feb 2017 12:38:48 -0600http://ask.sagemath.org/question/36655/Where should I save my sage files?http://ask.sagemath.org/question/34825/where-should-i-save-my-sage-files/ I am new to using sage from the terminal. Is there an organized way to store .sage files to make it easy to load the file into the terminal. I would like to avoid retyping the whole path into the terminal. dowewas2Thu, 15 Sep 2016 06:25:44 -0500http://ask.sagemath.org/question/34825/how to run a sage filehttp://ask.sagemath.org/question/34239/how-to-run-a-sage-file/ I am new to sage. Can someone please tell me with an example how to make a sage file and run it ?
I use ubuntu.nebuckandazzerTue, 26 Jul 2016 12:37:43 -0500http://ask.sagemath.org/question/34239/Can I create a sage file, and import it as a Python module?http://ask.sagemath.org/question/33954/can-i-create-a-sage-file-and-import-it-as-a-python-module/ I am trying to create a file that produces some data using sage specific modules (eg graphs. ).
Is there a way that I can save this file and then import it in a Python shell and / or in a Sage shell?
The problems I have encountered so far are:
- if I try to save the file as filename.py and import it as a module in a Python or Sage shell, Python/ Sage won't recognize the sage specific functions (which makes sense)
- if I try to save the file as filename.sage and import it as a module in a Sage shell, I get the error "No module named filename"
I am working from an Ubuntu terminal.
Thank you!silvia_n_netsTue, 28 Jun 2016 11:14:23 -0500http://ask.sagemath.org/question/33954/How to import the file "function.py" to worksheet sage? sagemath loaded by virtual machine (VirtualBox)http://ask.sagemath.org/question/32474/how-to-import-the-file-functionpy-to-worksheet-sage-sagemath-loaded-by-virtual-machine-virtualbox/i want to import a file function called "function.py" to my Worksheet. How to do?jmarcellopereiraSat, 06 Feb 2016 14:12:15 -0600http://ask.sagemath.org/question/32474/write output of for loop in pari to filehttp://ask.sagemath.org/question/32491/write-output-of-for-loop-in-pari-to-file/Hi all,
I would like to write the output of a for loop in Pari to a file named 'foo', However the output of a for loop has no numbering contrast with other output. If I look at the numbers labeling the outputs before and after the for loop and use that number in the following command
\w n foo
Then what I get in foo is 0. What happened and how can this be fixed?
Dianbin BaoSun, 07 Feb 2016 20:36:16 -0600http://ask.sagemath.org/question/32491/load a graph from a dot filehttp://ask.sagemath.org/question/29477/load-a-graph-from-a-dot-file/ Sage allows me to get the representation of a Graph, G, in the [dot language](https://en.wikipedia.org/wiki/DOT_(graph_description_language)) by using the method
> [G.graphviz_string()](http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_graph.html#sage.graphs.generic_graph.GenericGraph.graphviz_string)
I can even have this string written directly to a file by using:
> [G.graphviz_to_file_named('my_graph_file.dot')](http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_graph.html#sage.graphs.generic_graph.GenericGraph.graphviz_to_file_named)
Is there a way of reloading G from such a file? I tried doing
> G = Graph('my_graph_file.dot')
> G = Graph(open('my_graph_file.dot'))
> G = Graph(open('my_graph_file.dot').read())
Where the contents of 'my_graph_file.dot' are:
> graph MyGraph{
> foo -- {bar}
> bar -- {foo; bas}
> bas -- {bar}
> }
But none of these worked.
Mark BellWed, 16 Sep 2015 14:47:01 -0500http://ask.sagemath.org/question/29477/Running .sagetex.sage fails to return the 'out' filehttp://ask.sagemath.org/question/26769/running-sagetexsage-fails-to-return-the-out-file/ Have compiled Latex on the doc st_example. Returns the expected result, including the file st_example.sagetex.sage
I then run this file via the terminal using the command $ sage (path to ).st_example.sagetex.sage
This seems to be OK until it gets to the last line of the file when the following errors are shown
Traceback (most recent call last):
File "/home/ian/disks/Tex_docs/Alltrials/alltests/st_example.sagetex.sage.py", line 29, in <module>
_st_.endofdoc()
File "/home/ian/sage-6.6/local/lib/python2.7/site-packages/sagetex.py", line 282, in endofdoc
sagef = open(self.filename + '.sagetex.sage', 'r')
IOError: [Errno 2] No such file or directory: 'st_example.sagetex.sage'
The problem seems to be in the .py file where the path to st_example is not picked up and hence the 'out' file is not produced. I'm running Python 2.7 and the install of sage is recent ie one day ago. Everything else is working perfectly. is the generated .py file still expecting python 2.6 (as in the documentation) or is the final line of the python code incorrect?
ianjmatthewsFri, 08 May 2015 17:49:21 -0500http://ask.sagemath.org/question/26769/how to use file to save a huge list data...and use the list elements from the file in sage?http://ask.sagemath.org/question/26451/how-to-use-file-to-save-a-huge-list-dataand-use-the-list-elements-from-the-file-in-sage/ In computing the algebraic immunity of 8 var bent boolean function i need to use files....store the bent function in the file...and then use each one to compute algebraic immunity .....I am new to sage...Need help...subrotosterTue, 07 Apr 2015 08:11:49 -0500http://ask.sagemath.org/question/26451/How to input from file?http://ask.sagemath.org/question/26221/how-to-input-from-file/ I have a file with set of matrices in a file.
How to put them into array of matrices in sage?
Example of file:
[1,0,0]
[0,1,0]
[1,1,1]
[0,1,0]
[1,0,0]
[0,0,1]
FeckousTue, 17 Mar 2015 11:17:22 -0500http://ask.sagemath.org/question/26221/load data file to draw surfacehttp://ask.sagemath.org/question/24589/load-data-file-to-draw-surface/ I have a matrix consisting of 512 rows and 512 columns. I have written this matrix down into a text file. Each cell of the matrix contains a positive value. I want to plot these values in 3 dimensional system to form a surface by uploading the text file. How to do it in sage ?
Thank youAvijitThu, 23 Oct 2014 03:56:20 -0500http://ask.sagemath.org/question/24589/Copy file to a different projecthttp://ask.sagemath.org/question/11151/copy-file-to-a-different-project/What is the best way to copy a file from one project to another? I want to do this because I want to share/collaborate this file with others - but I don't want to share the other files in the project.dart2163Sun, 18 May 2014 18:09:37 -0500http://ask.sagemath.org/question/11151/Read bitmap into 2D array?http://ask.sagemath.org/question/11069/read-bitmap-into-2d-array/How can I read the bitmap image file into a two-dimensional list of numbers?v_2eWed, 11 Jun 2014 20:48:23 -0500http://ask.sagemath.org/question/11069/Adding Code To My SageMath Notebookhttp://ask.sagemath.org/question/11052/adding-code-to-my-sagemath-notebook/I have written a class, LRC, using the Sage Notebook which works just fine. Now I would like to put it into my sage environment without including it on each worksheet.
Before I go on I should describe my environment a bit. I am running a windows 7 machine with Virtual Box. I imported a Sage Notebook virtual machine and run it. The virtual box shares some of the drive space with the windows machine, in particular a spot called D:\unixshare
So I extracted the code from the cell to a file and placed it at D:\unixshare\forLRC\LRC.py which at least in theory available to the virtual box machine.
In a new worksheet I put this in a cell.
import sys
sys.path.append('/media/sf_D_DRIVE//unixshare/forLRC') # move your python files here
import LRC
lrc = LRC()
.-.-.-.-.-.-.-.-.-.-
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_4.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aW1wb3J0IHN5cwpzeXMucGF0aC5hcHBlbmQoJy9tZWRpYS9zZl9EX0RSSVZFLy91bml4c2hhcmUvZm9yTFJDJykgIyBtb3ZlIHlvdXIgcHl0aG9uIGZpbGVzIGhlcmUKCmltcG9ydCBMUkMKCmxyYyAgICA9IExSQygp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpMF8IdA/___code___.py", line 5, in <module>
import LRC
File "/media/sf_D_DRIVE//unixshare/forLRC/LRC.py", line 22, in <module>
class LRC( object ):
File "/media/sf_D_DRIVE//unixshare/forLRC/LRC.py", line 31, in LRC
var( "myOmega" ) # sometimes used as the angular frequency
NameError: name 'var' is not defined
I did get a new file: D:\unixshare\forLRC\LRC.pyc
So I have a problem that does not let me go any farther. I am not a particularly experienced python programmer, but have programmed in various languages for some time. Some help? Thanks.
russ_henselMon, 17 Feb 2014 14:11:05 -0600http://ask.sagemath.org/question/11052/Save and retrieving my datahttp://ask.sagemath.org/question/11039/save-and-retrieving-my-data/I often compute things that take a lot of time. When I stop working, I have to turn off my laptop. When I resume my work later, I'd like to be able to retrieve the previous data, computations, etc. For instance, I have computed a big list and stored it to the variable `x`. I would like to be able to save my data including the contents of this variable, then when I resume my work, I'd like to be able to have `x` back.
I tried to save my data to a file `mydata.dat` via the *Data -->Upload or create file* menu. Then, I quit sage, ran it again, and tried to link the data file back via the same menu. I got the following:
> The data filename already exists in other worksheet Delete the file in the other worksheet before creating a link.
I don't really understand what that means. Then, I tried all of the following:
DATA+'mydata.dat'
open(DATA+'mydata.dat')
DATA=DATA+'mydata.dat'
but in any case, if I type x, I get
NameError: name 'x' is not defined
Could somebody tell me how I should achieve what I'm trying?
geo909Sat, 15 Feb 2014 10:36:12 -0600http://ask.sagemath.org/question/11039/How to delete output file for rewriting?http://ask.sagemath.org/question/10892/how-to-delete-output-file-for-rewriting/I am using the Python's wave library to create some simple .wav files. It is impossible to overwrite these files, so I need a way to delete them before I run the code to create them. Right now, I am having to rename the file every time. There must be a simple way to delete the files in Terminal but I can't seem to find them. I am creating the files via the Notebook GUI. Any clues where the files could be saved or know of any other way to delete them?corleymjSun, 12 Jan 2014 16:52:09 -0600http://ask.sagemath.org/question/10892/Sometimes there is no any output from text file.http://ask.sagemath.org/question/10601/sometimes-there-is-no-any-output-from-text-file/Hello! <br>
Please explain me this code in Sage Notebook:<br>
Cell 1:<br>
f = open('/home/andrei/Docs/1.html')
Cell 2:
for s in f: print(s)
I type the code in cell 1 then in cell 2. First I press shift+enter in cell 1 then in cell 2. My code works fine. Then I press shift+enter in cell 2 - there is no any output. If after that I press shift+enter in cell 1 and then in cell 2 -- my code works again. Why?<br>
Thanks a lot!AndreWinWed, 09 Oct 2013 01:35:58 -0500http://ask.sagemath.org/question/10601/Save file from online notebookhttp://ask.sagemath.org/question/10353/save-file-from-online-notebook/Is it possible to save plot to svg file from online Sage notebook? bakantTue, 16 Jul 2013 03:07:26 -0500http://ask.sagemath.org/question/10353/How to output data to a filehttp://ask.sagemath.org/question/9703/how-to-output-data-to-a-file/Here is my code that graphs a function:
z1=2*pi*650*10^6
p1=2*pi*1.95*10^9
p2=2*pi*5*10^9
adc=.667
deltaF=.1*10^9
N=(adc*p1*p2)/z1
M(freq)=(2*i*pi*freq+z1)/((2*i*pi*freq+p1)*(2*i*pi*freq+p2))
Z=abs(N*M(freq))
g(freq)=20*log(Z,10)
pts=[(freq,g(freq).n()) for freq in srange(10^8,10^11,deltaF)]
list_plot(pts, plotjoined=True, scale='semilogx', gridlines="minor", ymin=-25, ymax=5)
How do I output this data to a file so I can compare it to data generated by another piece of software?
gjmFri, 11 Jan 2013 04:52:02 -0600http://ask.sagemath.org/question/9703/File size limitations for uploading into DATA folderhttp://ask.sagemath.org/question/9452/file-size-limitations-for-uploading-into-data-folder/I wanted to upload a file into DATA folder in Sage, but got the following error:
Bad Request
Maximum length of 10485760 bytes exceeded.
I guess there is a limitation 10MB on the size of a file.
How can I change this limitation?azerbajdzanSun, 21 Oct 2012 04:43:31 -0500http://ask.sagemath.org/question/9452/Opening .sage files within other fileshttp://ask.sagemath.org/question/9345/opening-sage-files-within-other-files/Hi,
I'm trying to create a project in Sage and ultimately want to run all my .sage files to get things going. So I want to have one run.sage file which I can load from the Sage command line:
load /SAGE_ROOT/Project/run.sage.
This works beautifully if I have all my functions and classes defined in this one file. Ideally I want to have this one file open all my other files so that I can be more modular in my coding.
But this load command doesn't work when it's within a file. Why? I have tried sage_eval and eval.... but it's not working. It tells me it does not recognize the path or something....
Any tips?
Thanks.
SLOtoSFSun, 23 Sep 2012 19:34:52 -0500http://ask.sagemath.org/question/9345/reading, computing, writinghttp://ask.sagemath.org/question/9228/reading-computing-writing/OK, I just installed Sage for the first time and now I would like to figure out how to read and write a file as well as run 'functions' like primepi (count of prime numbers).
Axxxxxx is a sequence in the OEIS at oeis.org.
The data file has two columns "n, A104272(n)" with n > 3*10^6:
1 2
2 11
3 17
4 29
5 41
... ...
After reading the data I need it take the difference of the A182873(n) = A104272(n+1) - A104272(n), find primepi(A104272(n+1)) and primepi(A104272(n)) and the difference of those two A190874(n) = primepi(A104272(n+1)) - primepi(A104272(n)). After that I would like to write the data back to a file.
I have been looking at the tutorial, but I have not found the section on reading data or anything like it.reddwarf2956Sun, 12 Aug 2012 18:52:32 -0500http://ask.sagemath.org/question/9228/Loading and analyzing data, Datamininghttp://ask.sagemath.org/question/8077/loading-and-analyzing-data-datamining/I am considering using Sage as a data-mining type tool. Is this way off base? If so where should I head? I do not see much in the reference docs on loading and interfacing with large-ish datafiles? As well as cleaning, standardizing and faceting the data.
In particular I have a 18mg compressed CSV file that expands to at least 512 mg.. It has about 20 fields per record and 400,000 or so records (it is Residential Real Estate listing data) I need to summarize, average and count by area, subdivision and dates (and more) I had been using An Access database, but am looking to move to Python, and avoid an SQL structure if possible, since now it seems we have enough memory to hold the data in memory..
My brief search has not turned up any similar examples??
At the least any pointers to loading the compressed CSV? and guidance on the right Python/Sage Data structures to start off with?dartdogFri, 15 Apr 2011 07:11:22 -0500http://ask.sagemath.org/question/8077/Loading a predefined functionhttp://ask.sagemath.org/question/8637/loading-a-predefined-function/One thread showed how to [load functions at startup](http://ask.sagemath.org/question/1072/preload-defined-functions-for-later-use)
But how to load some predefined function (or a bunch of functions from some .py file, say) only as needed?Green diodSat, 11 Feb 2012 03:26:47 -0600http://ask.sagemath.org/question/8637/How to share files across worksheets ?http://ask.sagemath.org/question/8147/how-to-share-files-across-worksheets/I want that code in several worksheets, from the same user, work over the same files. The DATA variable works only for the worksheet that uses it in a cell.
More, inside a new package: how to produce a path to that common directory over all worksheets of an user ?PedroThu, 02 Jun 2011 00:27:23 -0500http://ask.sagemath.org/question/8147/Writing re-usable sage scriptshttp://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/I'm looking to write some reusable code to do things like parse a particular file format outputted by some of my tools, but I don't see a particularly obvious "right way" to do this. I can define a python module, add it to the python path using the init.sage file, and then import it, but then I don't get some of the nice features I would get if I just loaded a .sage file from my current directory (namely tab-complete...).
Is there a preferred directory where I can save .sage files for use with the "load" commmand? If so, is there a way to specify a directory that is not in the SAGE_ROOT area, so I can source-control my specific .sage files independent of the sage installation?
ThanksNevilleSThu, 17 Mar 2011 05:00:10 -0500http://ask.sagemath.org/question/8005/how to organize multifile projecthttp://ask.sagemath.org/question/7980/how-to-organize-multifile-project/I am working on a project to do some computations for a specialized theory. Currently, we have several files that define different objects that have various dependencies on each other. Currently, I have a file named "attachall.sage" that looks like
attach "poly.sage"
attach "util2.sage"
attach "quasihomogeneous.sage"
attach "FJRW.sage"
attach "algebra.sage"
etc. Then when I want to do some computations, I do
sage: load attachall.sage
sage: #do stuff with the objects
It works fine, I guess, but doesn't seem particularly clean-- it seems like there should be a preferred way to do this. Should I use python's import? But then I would have to re-preparse every time I changed the source, and have potentially annoying bugs if someone forgot to re-preparse after changing a file. Or should I look into making a spkg? I don't really know a lot about that and I'd like to keep it simple.
Eventually we'd like to be able to share this code with other groups.
Does anyone have any thoughts?paragonWed, 02 Mar 2011 08:40:50 -0600http://ask.sagemath.org/question/7980/