Can I convert a Sage symbolic expression into a callable Python function?

asked 2019-06-05 19:46:30 +0200

adeacondarkly gravatar image

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=2n_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.

edit retag flag offensive close merge delete



You can save a function and then load it in a new Sage session: to save it, , and then g = load(FILENAME).

John Palmieri gravatar imageJohn Palmieri ( 2019-06-06 05:58:34 +0200 )edit