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, 04 Nov 2015 22:18:56 +0100Save boolean polynomial ring to file and read it againhttps://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/ I have BooleanPolynomialRing of 256 variables and I need many thick polynomials. Generate them is very time consuming so I want to save them to file and when needed read again. Is there some good way? Till now I have found out something like this (simplified)
variables=[]
for i in range(1,257):
variables.append('x'+str(i))
R = BooleanPolynomialRing(names=variables)
R.inject_variables()
fw = open('/home/pro/Desktop/polynomials.txt', 'w')
polynomial = R.random_element(degree=2,terms=+infinity)
fw.write(str(polynomial) + "\n")
fr = open('/home/pro/Desktop/polynomials.txt', 'r')
polynomial = fr.readline()
How can I convert string back to polynomial?
Thanks
f.close()
Tue, 03 Nov 2015 21:14:56 +0100https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/Answer by fidbc for <p>I have BooleanPolynomialRing of 256 variables and I need many thick polynomials. Generate them is very time consuming so I want to save them to file and when needed read again. Is there some good way? Till now I have found out something like this (simplified)</p>
<pre><code>variables=[]
for i in range(1,257):
variables.append('x'+str(i))
R = BooleanPolynomialRing(names=variables)
R.inject_variables()
fw = open('/home/pro/Desktop/polynomials.txt', 'w')
polynomial = R.random_element(degree=2,terms=+infinity)
fw.write(str(polynomial) + "\n")
fr = open('/home/pro/Desktop/polynomials.txt', 'r')
polynomial = fr.readline()
</code></pre>
<p>How can I convert string back to polynomial?</p>
<p>Thanks
f.close()</p>
https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?answer=30435#post-id-30435 This may not be the best way to recover a polynomial from its string representation but it appears to work.
Say `p = R.random_element(degree=2,terms=+infinity)`, then `s = str(p)` gives you its representation as a string. To recover `p` you can try `q = R( eval(s))`. Then `p==q` should evaluate to `True`.
Now, if you are reading a file `fr` with a polynomial per line, you can try the following to recover the polynomials.
vars = ['x'+str(i) for i in range(10)]
R= BooleanPolynomialRing(names=vars)
R.inject_variables()
polys = [ R(eval(l[:-1])) for l in fr]
----------
Update: Added iteration through file.Tue, 03 Nov 2015 21:44:44 +0100https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?answer=30435#post-id-30435Comment by fidbc for <p>This may not be the best way to recover a polynomial from its string representation but it appears to work.</p>
<p>Say <code>p = R.random_element(degree=2,terms=+infinity)</code>, then <code>s = str(p)</code> gives you its representation as a string. To recover <code>p</code> you can try <code>q = R( eval(s))</code>. Then <code>p==q</code> should evaluate to <code>True</code>.</p>
<p>Now, if you are reading a file <code>fr</code> with a polynomial per line, you can try the following to recover the polynomials.</p>
<pre><code>vars = ['x'+str(i) for i in range(10)]
R= BooleanPolynomialRing(names=vars)
R.inject_variables()
polys = [ R(eval(l[:-1])) for l in fr]
</code></pre>
<hr/>
<p>Update: Added iteration through file.</p>
https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?comment=30436#post-id-30436For some other polynomial rings it is probably a better approach to save the coefficients and then reconstruct from the list of coefficients.Tue, 03 Nov 2015 21:46:02 +0100https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?comment=30436#post-id-30436Answer by tmonteil for <p>I have BooleanPolynomialRing of 256 variables and I need many thick polynomials. Generate them is very time consuming so I want to save them to file and when needed read again. Is there some good way? Till now I have found out something like this (simplified)</p>
<pre><code>variables=[]
for i in range(1,257):
variables.append('x'+str(i))
R = BooleanPolynomialRing(names=variables)
R.inject_variables()
fw = open('/home/pro/Desktop/polynomials.txt', 'w')
polynomial = R.random_element(degree=2,terms=+infinity)
fw.write(str(polynomial) + "\n")
fr = open('/home/pro/Desktop/polynomials.txt', 'r')
polynomial = fr.readline()
</code></pre>
<p>How can I convert string back to polynomial?</p>
<p>Thanks
f.close()</p>
https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?answer=30437#post-id-30437Instead of using strings, you can directly save the Sage object:
sage: polynomial.save('/home/pro/Desktop/polynomials.sobj')
Then you can load it as follows:
sage: P = load('/home/pro/Desktop/polynomials.sobj')
**EDIT**
You can save several polynomials in the same file by saving a list of polynomials (but since lists have no `save` method, you have to use the `save` function). For example, if `P1`,`P2`,`P3` are polynomials, you can do:
sage: L = [P1, P2, P3]
sage: save(L, '/home/pro/Desktop/polynomials.sobj')
and then recover it as before:
sage: L = load('/home/pro/Desktop/polynomials.sobj')
and you can then get your polynomials back:
sage: P1, P2, P3 = L
Tue, 03 Nov 2015 21:52:43 +0100https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?answer=30437#post-id-30437Comment by Pro for <p>Instead of using strings, you can directly save the Sage object:</p>
<pre><code>sage: polynomial.save('/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p>Then you can load it as follows:</p>
<pre><code>sage: P = load('/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p><strong>EDIT</strong></p>
<p>You can save several polynomials in the same file by saving a list of polynomials (but since lists have no <code>save</code> method, you have to use the <code>save</code> function). For example, if <code>P1</code>,<code>P2</code>,<code>P3</code> are polynomials, you can do:</p>
<pre><code>sage: L = [P1, P2, P3]
sage: save(L, '/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p>and then recover it as before:</p>
<pre><code>sage: L = load('/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p>and you can then get your polynomials back:</p>
<pre><code>sage: P1, P2, P3 = L
</code></pre>
https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?comment=30439#post-id-30439So I need one file for every polynomial right? Is there a way to save multiple polynomials to one file?Tue, 03 Nov 2015 22:24:37 +0100https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?comment=30439#post-id-30439Comment by tmonteil for <p>Instead of using strings, you can directly save the Sage object:</p>
<pre><code>sage: polynomial.save('/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p>Then you can load it as follows:</p>
<pre><code>sage: P = load('/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p><strong>EDIT</strong></p>
<p>You can save several polynomials in the same file by saving a list of polynomials (but since lists have no <code>save</code> method, you have to use the <code>save</code> function). For example, if <code>P1</code>,<code>P2</code>,<code>P3</code> are polynomials, you can do:</p>
<pre><code>sage: L = [P1, P2, P3]
sage: save(L, '/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p>and then recover it as before:</p>
<pre><code>sage: L = load('/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p>and you can then get your polynomials back:</p>
<pre><code>sage: P1, P2, P3 = L
</code></pre>
https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?comment=30443#post-id-30443I updated my answer to ansver your question.Tue, 03 Nov 2015 23:43:09 +0100https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?comment=30443#post-id-30443Comment by Pro for <p>Instead of using strings, you can directly save the Sage object:</p>
<pre><code>sage: polynomial.save('/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p>Then you can load it as follows:</p>
<pre><code>sage: P = load('/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p><strong>EDIT</strong></p>
<p>You can save several polynomials in the same file by saving a list of polynomials (but since lists have no <code>save</code> method, you have to use the <code>save</code> function). For example, if <code>P1</code>,<code>P2</code>,<code>P3</code> are polynomials, you can do:</p>
<pre><code>sage: L = [P1, P2, P3]
sage: save(L, '/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p>and then recover it as before:</p>
<pre><code>sage: L = load('/home/pro/Desktop/polynomials.sobj')
</code></pre>
<p>and you can then get your polynomials back:</p>
<pre><code>sage: P1, P2, P3 = L
</code></pre>
https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?comment=30474#post-id-30474It works perfectly. Thank you very much.Wed, 04 Nov 2015 22:18:56 +0100https://ask.sagemath.org/question/30432/save-boolean-polynomial-ring-to-file-and-read-it-again/?comment=30474#post-id-30474