Ask Your Question
0

How to save a function in sage

asked 13 years ago

anonymous user

Anonymous

updated 13 years ago

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?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 13 years ago

Simon King gravatar image

I 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.

Preview: (hide)
link

Comments

1

I 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.

Jason Grout gravatar imageJason Grout ( 13 years ago )

Yes 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.

Zaubertrank gravatar imageZaubertrank ( 13 years ago )
1

you 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-files

niles gravatar imageniles ( 13 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 13 years ago

Seen: 3,786 times

Last updated: Mar 12 '12