ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 05 Jun 2019 22:58:34 -0500Can I convert a Sage symbolic expression into a callable Python function?https://ask.sagemath.org/question/46813/can-i-convert-a-sage-symbolic-expression-into-a-callable-python-function/Hi, and thanks ahead of time for the help!
I haven't had any luck finding an answer to this so far, and I'm still fairly new to Sage (and honestly, code-work in general).
I've been working on a (probably for most) small project of determining coefficient functions by use of a taylor expansion and solving a linear system. Basically, I have one polynomial with unknown coefficient functions f_2, f_3, f_4, ..., expand it with a taylor series and then match the expansion to another, known polynomial function. Then, I am solving this system for f_2, f_3, f_4, etc.
I've got this all figured out for the most part since, but my research adviser wants these functions (once solved) to be output as a callable python function. Basically, once the Sage script finds the functions, the goal is to have a defined python function that can either be copy/pasted into another script for use (at the least), or for them to be output into their own type of package file that can then be imported into a python script for use.
Like, as an example, "f_2=2*n_b+L**K" gets converted into a python function f_2(n_b, L, K) so the end-user doesn't have to rebuild f_2 from scratch in their python script.
I was thinking I might be able to spend a day or two grinding them out in a manual sense with print() commands, but was wondering if there is a better/good/any way to actually do this. Wed, 05 Jun 2019 12:46:30 -0500https://ask.sagemath.org/question/46813/can-i-convert-a-sage-symbolic-expression-into-a-callable-python-function/Comment by John Palmieri for <p>Hi, and thanks ahead of time for the help!
I haven't had any luck finding an answer to this so far, and I'm still fairly new to Sage (and honestly, code-work in general).</p>
<p>I've been working on a (probably for most) small project of determining coefficient functions by use of a taylor expansion and solving a linear system. Basically, I have one polynomial with unknown coefficient functions f_2, f_3, f_4, ..., expand it with a taylor series and then match the expansion to another, known polynomial function. Then, I am solving this system for f_2, f_3, f_4, etc.</p>
<p>I've got this all figured out for the most part since, but my research adviser wants these functions (once solved) to be output as a callable python function. Basically, once the Sage script finds the functions, the goal is to have a defined python function that can either be copy/pasted into another script for use (at the least), or for them to be output into their own type of package file that can then be imported into a python script for use.</p>
<p>Like, as an example, "f_2=2<em>n_b+L</em>*K" gets converted into a python function f_2(n_b, L, K) so the end-user doesn't have to rebuild f_2 from scratch in their python script.</p>
<p>I was thinking I might be able to spend a day or two grinding them out in a manual sense with print() commands, but was wondering if there is a better/good/any way to actually do this. </p>
https://ask.sagemath.org/question/46813/can-i-convert-a-sage-symbolic-expression-into-a-callable-python-function/?comment=46818#post-id-46818You can save a function and then load it in a new Sage session: `f_2.save(FILENAME)` to save it, , and then `g = load(FILENAME)`.Wed, 05 Jun 2019 22:58:34 -0500https://ask.sagemath.org/question/46813/can-i-convert-a-sage-symbolic-expression-into-a-callable-python-function/?comment=46818#post-id-46818