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, 14 Mar 2012 08:14:18 +0100How to save a function in sagehttps://ask.sagemath.org/question/8786/how-to-save-a-function-in-sage/I have a function in sage I want to save, but I cannot figure out how. I'm running sage on a mac through the terminal. I'm finding stuff online about save_session, or saving to a document.sage file or something but nothing really seems to be working, is there a straightforward way to do this? Thanks.
Edit: Also while I have your attention, say you have a function f, how do you display the contents of f without actually running it?Mon, 12 Mar 2012 14:54:04 +0100https://ask.sagemath.org/question/8786/how-to-save-a-function-in-sage/Answer by Simon King for <p>I have a function in sage I want to save, but I cannot figure out how. I'm running sage on a mac through the terminal. I'm finding stuff online about save_session, or saving to a document.sage file or something but nothing really seems to be working, is there a straightforward way to do this? Thanks.</p>
<p>Edit: Also while I have your attention, say you have a function f, how do you display the contents of f without actually running it?</p>
https://ask.sagemath.org/question/8786/how-to-save-a-function-in-sage/?answer=13356#post-id-13356I think the answer crucially depends on what you mean by the word "function".
It could be a Python or Cython function, either defined in an interactive session or in a module. It could be a symbolic expression, or perhaps could be a polynomial, which are sometimes mistaken for a function.
Symbolic function is easy to pickle:
sage: f(x) = x^2
sage: type(f)
<type 'sage.symbolic.expression.Expression'>
sage: tmp = tmp_filename()
sage: save(f,tmp)
sage: load(tmp)
x^2
Similarly, a polynomial is easy to pickle:
sage: P.<x> = QQ[]
sage: p = x^2
sage: type(p)
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>
sage: save(p,tmp)
sage: load(tmp)
x^2
However, if you are really talking about a Python function (e.g., a lambda function), then there is a general problem: You can't easily pickle Python functions defined in an interactive session. And Python is Sage's programming language.
sage: def f(x):
....: return x
....:
sage: save(f,tmp)
---------------------------------------------------------------------------
PicklingError Traceback (most recent call last)
/home/simon/SAGE/sage-5.0.beta7/devel/sage-main/<ipython console> in <module>()
/home/simon/SAGE/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/structure/sage_object.so in sage.structure.sage_object.save (sage/structure/sage_object.c:8647)()
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
However, if you define the same function in some Python module, then pickling would work.
And what do you mean by the "contents" of a function?
Do you mean, you have a Python function and want to see its code?
Then again, if it is defined in a module, then it works easily: If you have any object `X` (function or anything else) in Sage, then you can try to see its code by appending two question marks and hitting return (or shift-return in the Sage notebook). And with `edit(X,'vim')` (inserting another editor name if you don't like vim), then you could actually edit the code.
But if the function is only defined in an interactive session, then Python can not find the code. Mon, 12 Mar 2012 16:11:17 +0100https://ask.sagemath.org/question/8786/how-to-save-a-function-in-sage/?answer=13356#post-id-13356Comment by niles for <p>I think the answer crucially depends on what you mean by the word "function".</p>
<p>It could be a Python or Cython function, either defined in an interactive session or in a module. It could be a symbolic expression, or perhaps could be a polynomial, which are sometimes mistaken for a function.</p>
<p>Symbolic function is easy to pickle:</p>
<pre><code>sage: f(x) = x^2
sage: type(f)
<type 'sage.symbolic.expression.Expression'>
sage: tmp = tmp_filename()
sage: save(f,tmp)
sage: load(tmp)
x^2
</code></pre>
<p>Similarly, a polynomial is easy to pickle:</p>
<pre><code>sage: P.<x> = QQ[]
sage: p = x^2
sage: type(p)
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>
sage: save(p,tmp)
sage: load(tmp)
x^2
</code></pre>
<p>However, if you are really talking about a Python function (e.g., a lambda function), then there is a general problem: You can't easily pickle Python functions defined in an interactive session. And Python is Sage's programming language.</p>
<pre><code>sage: def f(x):
....: return x
....:
sage: save(f,tmp)
---------------------------------------------------------------------------
PicklingError Traceback (most recent call last)
/home/simon/SAGE/sage-5.0.beta7/devel/sage-main/<ipython console> in <module>()
/home/simon/SAGE/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/structure/sage_object.so in sage.structure.sage_object.save (sage/structure/sage_object.c:8647)()
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
</code></pre>
<p>However, if you define the same function in some Python module, then pickling would work.</p>
<p>And what do you mean by the "contents" of a function?</p>
<p>Do you mean, you have a Python function and want to see its code?</p>
<p>Then again, if it is defined in a module, then it works easily: If you have any object <code>X</code> (function or anything else) in Sage, then you can try to see its code by appending two question marks and hitting return (or shift-return in the Sage notebook). And with <code>edit(X,'vim')</code> (inserting another editor name if you don't like vim), then you could actually edit the code.</p>
<p>But if the function is only defined in an interactive session, then Python can not find the code. </p>
https://ask.sagemath.org/question/8786/how-to-save-a-function-in-sage/?comment=20134#post-id-20134you just have to define the function in a file, and then load it into your sage session: http://www.sagemath.org/doc/tutorial/programming.html#loading-and-attaching-sage-filesWed, 14 Mar 2012 08:14:18 +0100https://ask.sagemath.org/question/8786/how-to-save-a-function-in-sage/?comment=20134#post-id-20134Comment by Jason Grout for <p>I think the answer crucially depends on what you mean by the word "function".</p>
<p>It could be a Python or Cython function, either defined in an interactive session or in a module. It could be a symbolic expression, or perhaps could be a polynomial, which are sometimes mistaken for a function.</p>
<p>Symbolic function is easy to pickle:</p>
<pre><code>sage: f(x) = x^2
sage: type(f)
<type 'sage.symbolic.expression.Expression'>
sage: tmp = tmp_filename()
sage: save(f,tmp)
sage: load(tmp)
x^2
</code></pre>
<p>Similarly, a polynomial is easy to pickle:</p>
<pre><code>sage: P.<x> = QQ[]
sage: p = x^2
sage: type(p)
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>
sage: save(p,tmp)
sage: load(tmp)
x^2
</code></pre>
<p>However, if you are really talking about a Python function (e.g., a lambda function), then there is a general problem: You can't easily pickle Python functions defined in an interactive session. And Python is Sage's programming language.</p>
<pre><code>sage: def f(x):
....: return x
....:
sage: save(f,tmp)
---------------------------------------------------------------------------
PicklingError Traceback (most recent call last)
/home/simon/SAGE/sage-5.0.beta7/devel/sage-main/<ipython console> in <module>()
/home/simon/SAGE/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/structure/sage_object.so in sage.structure.sage_object.save (sage/structure/sage_object.c:8647)()
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
</code></pre>
<p>However, if you define the same function in some Python module, then pickling would work.</p>
<p>And what do you mean by the "contents" of a function?</p>
<p>Do you mean, you have a Python function and want to see its code?</p>
<p>Then again, if it is defined in a module, then it works easily: If you have any object <code>X</code> (function or anything else) in Sage, then you can try to see its code by appending two question marks and hitting return (or shift-return in the Sage notebook). And with <code>edit(X,'vim')</code> (inserting another editor name if you don't like vim), then you could actually edit the code.</p>
<p>But if the function is only defined in an interactive session, then Python can not find the code. </p>
https://ask.sagemath.org/question/8786/how-to-save-a-function-in-sage/?comment=20137#post-id-20137I suppose you can use sage.misc.fpickle.pickle_function to pickle some functions. It works for your f above, for example. This uses code copied from twisted, IIRC.Mon, 12 Mar 2012 17:54:27 +0100https://ask.sagemath.org/question/8786/how-to-save-a-function-in-sage/?comment=20137#post-id-20137Comment by Zaubertrank for <p>I think the answer crucially depends on what you mean by the word "function".</p>
<p>It could be a Python or Cython function, either defined in an interactive session or in a module. It could be a symbolic expression, or perhaps could be a polynomial, which are sometimes mistaken for a function.</p>
<p>Symbolic function is easy to pickle:</p>
<pre><code>sage: f(x) = x^2
sage: type(f)
<type 'sage.symbolic.expression.Expression'>
sage: tmp = tmp_filename()
sage: save(f,tmp)
sage: load(tmp)
x^2
</code></pre>
<p>Similarly, a polynomial is easy to pickle:</p>
<pre><code>sage: P.<x> = QQ[]
sage: p = x^2
sage: type(p)
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>
sage: save(p,tmp)
sage: load(tmp)
x^2
</code></pre>
<p>However, if you are really talking about a Python function (e.g., a lambda function), then there is a general problem: You can't easily pickle Python functions defined in an interactive session. And Python is Sage's programming language.</p>
<pre><code>sage: def f(x):
....: return x
....:
sage: save(f,tmp)
---------------------------------------------------------------------------
PicklingError Traceback (most recent call last)
/home/simon/SAGE/sage-5.0.beta7/devel/sage-main/<ipython console> in <module>()
/home/simon/SAGE/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/structure/sage_object.so in sage.structure.sage_object.save (sage/structure/sage_object.c:8647)()
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
</code></pre>
<p>However, if you define the same function in some Python module, then pickling would work.</p>
<p>And what do you mean by the "contents" of a function?</p>
<p>Do you mean, you have a Python function and want to see its code?</p>
<p>Then again, if it is defined in a module, then it works easily: If you have any object <code>X</code> (function or anything else) in Sage, then you can try to see its code by appending two question marks and hitting return (or shift-return in the Sage notebook). And with <code>edit(X,'vim')</code> (inserting another editor name if you don't like vim), then you could actually edit the code.</p>
<p>But if the function is only defined in an interactive session, then Python can not find the code. </p>
https://ask.sagemath.org/question/8786/how-to-save-a-function-in-sage/?comment=20136#post-id-20136Yes a lambda function was what I had in mind. Could you extrapolate on how to create these python modules and load them into your sage session, I've been unsuccessful so far using the info I've found online.Mon, 12 Mar 2012 20:22:32 +0100https://ask.sagemath.org/question/8786/how-to-save-a-function-in-sage/?comment=20136#post-id-20136