# How to save a function in sage

 0 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? asked Mar 12 '12 Anonymous

 2 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) sage: tmp = tmp_filename() sage: save(f,tmp) sage: load(tmp) x^2  Similarly, a polynomial is easy to pickle: sage: P. = QQ[] sage: p = x^2 sage: type(p) 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/ in () /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 : 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. posted Mar 12 '12 Simon King 376 ● 2 ● 11 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 (Mar 12 '12)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 (Mar 12 '12)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-filesniles (Mar 14 '12)
 0 Spam please delete posted Mar 14 '12 Edition 1 Shashank 1720 ● 8 ● 30 ● 62

[hide preview]

## Stats:

Asked: Mar 12 '12

Seen: 326 times

Last updated: Mar 14 '12