ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 06 Apr 2011 09:29:07 +0200notebook questionshttps://ask.sagemath.org/question/8054/notebook-questions/Hello!
Could you help me? I used notebook and in it I wrote a program in SAGE which generated a huge dictionary. I would like to save only this dictionary because I need to use this dictionary in another SAGE program. Unfortunately, I am not very familiar in using the notebook, so I don't know how to do this... Another (very simple) question when there is a bug in my program in the notebook, the computer wrotes me which line the bug is. But I don't how to find this line with the given number. Is there a line-numerator in the notebook? Mon, 04 Apr 2011 10:48:53 +0200https://ask.sagemath.org/question/8054/notebook-questions/Comment by Shashank for <p>Hello!</p>
<p>Could you help me? I used notebook and in it I wrote a program in SAGE which generated a huge dictionary. I would like to save only this dictionary because I need to use this dictionary in another SAGE program. Unfortunately, I am not very familiar in using the notebook, so I don't know how to do this... Another (very simple) question when there is a bug in my program in the notebook, the computer wrotes me which line the bug is. But I don't how to find this line with the given number. Is there a line-numerator in the notebook? </p>
https://ask.sagemath.org/question/8054/notebook-questions/?comment=21908#post-id-21908The question is not very clear. Can you write a sample code to explain what you mean by dictionary and how you use it in another sage program.Mon, 04 Apr 2011 11:00:40 +0200https://ask.sagemath.org/question/8054/notebook-questions/?comment=21908#post-id-21908Comment by Katika for <p>Hello!</p>
<p>Could you help me? I used notebook and in it I wrote a program in SAGE which generated a huge dictionary. I would like to save only this dictionary because I need to use this dictionary in another SAGE program. Unfortunately, I am not very familiar in using the notebook, so I don't know how to do this... Another (very simple) question when there is a bug in my program in the notebook, the computer wrotes me which line the bug is. But I don't how to find this line with the given number. Is there a line-numerator in the notebook? </p>
https://ask.sagemath.org/question/8054/notebook-questions/?comment=12254#post-id-12254Hello!
I show you an example. My dictionary is the following:
square_sum={}
for a in range(1000):
for b in range(1000):
square_sum[a,b]=a^2+b^2
Now in another notebook worksheet I would like to upload only this dictionary (or is it called vocabulary?) square_sum. So the question is how to save the dictionary and how to upload it in another worksheet?
Mon, 04 Apr 2011 11:15:23 +0200https://ask.sagemath.org/question/8054/notebook-questions/?comment=12254#post-id-12254Answer by kcrisman for <p>Hello!</p>
<p>Could you help me? I used notebook and in it I wrote a program in SAGE which generated a huge dictionary. I would like to save only this dictionary because I need to use this dictionary in another SAGE program. Unfortunately, I am not very familiar in using the notebook, so I don't know how to do this... Another (very simple) question when there is a bug in my program in the notebook, the computer wrotes me which line the bug is. But I don't how to find this line with the given number. Is there a line-numerator in the notebook? </p>
https://ask.sagemath.org/question/8054/notebook-questions/?answer=12259#post-id-12259> Another (very simple) question when
> there is a bug in my program in the
> notebook, the computer wrotes me which
> line the bug is. But I don't how to
> find this line with the given number.
> Is there a line-numerator in the
> notebook?
Hmm, that is harder. At least for simple examples, the line does seem to be the line I made the mistake in:
for i in range(10):
print i+1
print i^2
a=i[0]
print a+i
But multicell ones don't seem to be as nice. You may just have to look at the code that causes the problem and find it in your input. We don't have interactive markup (e.g. coloring) yet, because the javascript really slows things down.Mon, 04 Apr 2011 12:34:54 +0200https://ask.sagemath.org/question/8054/notebook-questions/?answer=12259#post-id-12259Comment by kcrisman for <blockquote>
<p>Another (very simple) question when
there is a bug in my program in the
notebook, the computer wrotes me which
line the bug is. But I don't how to
find this line with the given number.
Is there a line-numerator in the
notebook?</p>
</blockquote>
<p>Hmm, that is harder. At least for simple examples, the line does seem to be the line I made the mistake in:</p>
<pre><code>for i in range(10):
print i+1
print i^2
a=i[0]
print a+i
</code></pre>
<p>But multicell ones don't seem to be as nice. You may just have to look at the code that causes the problem and find it in your input. We don't have interactive markup (e.g. coloring) yet, because the javascript really slows things down.</p>
https://ask.sagemath.org/question/8054/notebook-questions/?comment=21890#post-id-21890Yes. There should be a large check mark that shows up on each answer, which can be clicked. I don't know if you can vote for your own, but you can vote for others. It also adds to your 'karma' a small amount.Wed, 06 Apr 2011 09:29:07 +0200https://ask.sagemath.org/question/8054/notebook-questions/?comment=21890#post-id-21890Comment by kcrisman for <blockquote>
<p>Another (very simple) question when
there is a bug in my program in the
notebook, the computer wrotes me which
line the bug is. But I don't how to
find this line with the given number.
Is there a line-numerator in the
notebook?</p>
</blockquote>
<p>Hmm, that is harder. At least for simple examples, the line does seem to be the line I made the mistake in:</p>
<pre><code>for i in range(10):
print i+1
print i^2
a=i[0]
print a+i
</code></pre>
<p>But multicell ones don't seem to be as nice. You may just have to look at the code that causes the problem and find it in your input. We don't have interactive markup (e.g. coloring) yet, because the javascript really slows things down.</p>
https://ask.sagemath.org/question/8054/notebook-questions/?comment=21897#post-id-21897If any one of the answers was best, be sure to help future users and mark it as such, so that it's easily searchable. Doesn't have to be my answer, either :-)Tue, 05 Apr 2011 13:25:35 +0200https://ask.sagemath.org/question/8054/notebook-questions/?comment=21897#post-id-21897Comment by Katika for <blockquote>
<p>Another (very simple) question when
there is a bug in my program in the
notebook, the computer wrotes me which
line the bug is. But I don't how to
find this line with the given number.
Is there a line-numerator in the
notebook?</p>
</blockquote>
<p>Hmm, that is harder. At least for simple examples, the line does seem to be the line I made the mistake in:</p>
<pre><code>for i in range(10):
print i+1
print i^2
a=i[0]
print a+i
</code></pre>
<p>But multicell ones don't seem to be as nice. You may just have to look at the code that causes the problem and find it in your input. We don't have interactive markup (e.g. coloring) yet, because the javascript really slows things down.</p>
https://ask.sagemath.org/question/8054/notebook-questions/?comment=21891#post-id-21891I secretly voted :-) (Or voting is different from marking... ?)Wed, 06 Apr 2011 05:15:55 +0200https://ask.sagemath.org/question/8054/notebook-questions/?comment=21891#post-id-21891Comment by Katika for <blockquote>
<p>Another (very simple) question when
there is a bug in my program in the
notebook, the computer wrotes me which
line the bug is. But I don't how to
find this line with the given number.
Is there a line-numerator in the
notebook?</p>
</blockquote>
<p>Hmm, that is harder. At least for simple examples, the line does seem to be the line I made the mistake in:</p>
<pre><code>for i in range(10):
print i+1
print i^2
a=i[0]
print a+i
</code></pre>
<p>But multicell ones don't seem to be as nice. You may just have to look at the code that causes the problem and find it in your input. We don't have interactive markup (e.g. coloring) yet, because the javascript really slows things down.</p>
https://ask.sagemath.org/question/8054/notebook-questions/?comment=12260#post-id-12260Thank you very much :-) Tue, 05 Apr 2011 06:07:11 +0200https://ask.sagemath.org/question/8054/notebook-questions/?comment=12260#post-id-12260Answer by Shashank for <p>Hello!</p>
<p>Could you help me? I used notebook and in it I wrote a program in SAGE which generated a huge dictionary. I would like to save only this dictionary because I need to use this dictionary in another SAGE program. Unfortunately, I am not very familiar in using the notebook, so I don't know how to do this... Another (very simple) question when there is a bug in my program in the notebook, the computer wrotes me which line the bug is. But I don't how to find this line with the given number. Is there a line-numerator in the notebook? </p>
https://ask.sagemath.org/question/8054/notebook-questions/?answer=12255#post-id-12255 def square_sum(a,b):
return(a*a+b*b)
fname="filename.dat"
fp=open(fname,'w')
for a in range(1000):
for b in range(1000):
s=str(a)+' '+str(b)+' '+str(square_sum(a,b))+'\n'
fp.write(s)
fp.close()
This will write the data to a filename named "filename.dat". You can also give a path if you wish.
To read it from another notebook use the following code
import numpy as np
array=np.genfromtxt('/path-to-file/filename.dat',delimiter='')
Of course you have to replace 'path-to-file' by whatever path it is. Now you can use array the way we usually use arrays.
I hope this helps.Mon, 04 Apr 2011 11:28:53 +0200https://ask.sagemath.org/question/8054/notebook-questions/?answer=12255#post-id-12255Answer by DSM for <p>Hello!</p>
<p>Could you help me? I used notebook and in it I wrote a program in SAGE which generated a huge dictionary. I would like to save only this dictionary because I need to use this dictionary in another SAGE program. Unfortunately, I am not very familiar in using the notebook, so I don't know how to do this... Another (very simple) question when there is a bug in my program in the notebook, the computer wrotes me which line the bug is. But I don't how to find this line with the given number. Is there a line-numerator in the notebook? </p>
https://ask.sagemath.org/question/8054/notebook-questions/?answer=12256#post-id-12256If Sage is the only program that needs to read the data, you can use the "save" and "load" commands:
sage: square_sum = dict(((a,b),a^2+b^2) for a,b in CartesianProduct([1..5],[1..5]))
sage: square_sum
{(1, 3): 10, (5, 4): 41, (2, 1): 5, (5, 1): 26, (2, 5): 29, (1, 2): 5, (3, 3): 18, (4, 4): 32, (1, 5): 26, (2, 2): 8, (5, 3): 34, (4, 1): 17, (1, 1): 2, (3, 2): 13, (4, 5): 41, (5, 5): 50, (1, 4): 17, (2, 3): 13, (4, 2): 20, (3, 5): 34, (3, 4): 25, (3, 1): 10, (4, 3): 25, (5, 2): 29, (2, 4): 20}
sage: save(square_sum, "ssum")
sage: s2 = load("ssum")
sage: s2
{(1, 3): 10, (3, 2): 13, (2, 1): 5, (5, 1): 26, (2, 5): 29, (1, 2): 5, (3, 3): 18, (4, 4): 32, (1, 5): 26, (2, 2): 8, (3, 5): 34, (4, 1): 17, (1, 1): 2, (5, 4): 41, (4, 5): 41, (1, 4): 17, (2, 3): 13, (4, 2): 20, (5, 5): 50, (5, 3): 34, (5, 2): 29, (3, 1): 10, (4, 3): 25, (3, 4): 25, (2, 4): 20}
sage: s2 == square_sum
True
(Remember that dictionaries don't have an order, so the fact that the keys show up differently in s2 than in the original square_sum doesn't matter.)Mon, 04 Apr 2011 11:47:53 +0200https://ask.sagemath.org/question/8054/notebook-questions/?answer=12256#post-id-12256Comment by Katika for <p>If Sage is the only program that needs to read the data, you can use the "save" and "load" commands:</p>
<pre><code>sage: square_sum = dict(((a,b),a^2+b^2) for a,b in CartesianProduct([1..5],[1..5]))
sage: square_sum
{(1, 3): 10, (5, 4): 41, (2, 1): 5, (5, 1): 26, (2, 5): 29, (1, 2): 5, (3, 3): 18, (4, 4): 32, (1, 5): 26, (2, 2): 8, (5, 3): 34, (4, 1): 17, (1, 1): 2, (3, 2): 13, (4, 5): 41, (5, 5): 50, (1, 4): 17, (2, 3): 13, (4, 2): 20, (3, 5): 34, (3, 4): 25, (3, 1): 10, (4, 3): 25, (5, 2): 29, (2, 4): 20}
sage: save(square_sum, "ssum")
sage: s2 = load("ssum")
sage: s2
{(1, 3): 10, (3, 2): 13, (2, 1): 5, (5, 1): 26, (2, 5): 29, (1, 2): 5, (3, 3): 18, (4, 4): 32, (1, 5): 26, (2, 2): 8, (3, 5): 34, (4, 1): 17, (1, 1): 2, (5, 4): 41, (4, 5): 41, (1, 4): 17, (2, 3): 13, (4, 2): 20, (5, 5): 50, (5, 3): 34, (5, 2): 29, (3, 1): 10, (4, 3): 25, (3, 4): 25, (2, 4): 20}
sage: s2 == square_sum
True
</code></pre>
<p>(Remember that dictionaries don't have an order, so the fact that the keys show up differently in s2 than in the original square_sum doesn't matter.)</p>
https://ask.sagemath.org/question/8054/notebook-questions/?comment=21902#post-id-21902I tried this, but it doesn't work if I would like to load "ssum" in another notebook cell... Is there a way to tell the path to the ssum.obj? I think something load('~/sage/ssum.sobj')?Tue, 05 Apr 2011 10:29:52 +0200https://ask.sagemath.org/question/8054/notebook-questions/?comment=21902#post-id-21902Comment by DSM for <p>If Sage is the only program that needs to read the data, you can use the "save" and "load" commands:</p>
<pre><code>sage: square_sum = dict(((a,b),a^2+b^2) for a,b in CartesianProduct([1..5],[1..5]))
sage: square_sum
{(1, 3): 10, (5, 4): 41, (2, 1): 5, (5, 1): 26, (2, 5): 29, (1, 2): 5, (3, 3): 18, (4, 4): 32, (1, 5): 26, (2, 2): 8, (5, 3): 34, (4, 1): 17, (1, 1): 2, (3, 2): 13, (4, 5): 41, (5, 5): 50, (1, 4): 17, (2, 3): 13, (4, 2): 20, (3, 5): 34, (3, 4): 25, (3, 1): 10, (4, 3): 25, (5, 2): 29, (2, 4): 20}
sage: save(square_sum, "ssum")
sage: s2 = load("ssum")
sage: s2
{(1, 3): 10, (3, 2): 13, (2, 1): 5, (5, 1): 26, (2, 5): 29, (1, 2): 5, (3, 3): 18, (4, 4): 32, (1, 5): 26, (2, 2): 8, (3, 5): 34, (4, 1): 17, (1, 1): 2, (5, 4): 41, (4, 5): 41, (1, 4): 17, (2, 3): 13, (4, 2): 20, (5, 5): 50, (5, 3): 34, (5, 2): 29, (3, 1): 10, (4, 3): 25, (3, 4): 25, (2, 4): 20}
sage: s2 == square_sum
True
</code></pre>
<p>(Remember that dictionaries don't have an order, so the fact that the keys show up differently in s2 than in the original square_sum doesn't matter.)</p>
https://ask.sagemath.org/question/8054/notebook-questions/?comment=21901#post-id-21901kcrisman's example shows how you can do this: save(A,"some-hardcoded-path"), or save(A,os.path.expanduser('~/')+'test') if you want to use a tilde. (Tilde expansion isn't automatic).Tue, 05 Apr 2011 11:44:43 +0200https://ask.sagemath.org/question/8054/notebook-questions/?comment=21901#post-id-21901Answer by kcrisman for <p>Hello!</p>
<p>Could you help me? I used notebook and in it I wrote a program in SAGE which generated a huge dictionary. I would like to save only this dictionary because I need to use this dictionary in another SAGE program. Unfortunately, I am not very familiar in using the notebook, so I don't know how to do this... Another (very simple) question when there is a bug in my program in the notebook, the computer wrotes me which line the bug is. But I don't how to find this line with the given number. Is there a line-numerator in the notebook? </p>
https://ask.sagemath.org/question/8054/notebook-questions/?answer=12234#post-id-12234Katika, you should be able to save any object in Sage, as well as a whole session. If you click "Help" on the notebook at the top, the page you get should have
Use "save obj1 obj2 ..." and "load obj1 obj2 ...". This allows for easy moving of objects from one worksheet to another, and saving of objects for later use.
toward the bottom. The syntax is wrong, though.This is now [Ticket 11124](http://trac.sagemath.org/sage_trac/ticket/11124).
This should work.
A = matrix([[2,3],[3,4]])
save(A,'test')
And then a link to the actual `.sobj` file should be created, and you could use that. The help for the DATA variable should help as well. If you want to use your home directory, then
save(A,os.path.expanduser('~/')+'test')
might help you; it will show up in your home directory, if you use a system which supports the tilde. Maybe that should be in the help as well...
Mon, 04 Apr 2011 12:21:29 +0200https://ask.sagemath.org/question/8054/notebook-questions/?answer=12234#post-id-12234