Fast code based on symbolic manipulations

51 views
Skip to first unread message

Nathan Woods

unread,
Feb 24, 2020, 1:22:30 PM2/24/20
to symengine
Up front, my question is whether symengine is a good tool for what I want to do, or if I would be better served by something like sympy or mathematica.

I need to evaluate functions based on symbolic manipulations in a reasonably performant manner (comparable to basic, serial fortran code, for instance). I've done this before in sympy, and the workflow looked like this:
 - Provide an input symbolic expression
 - Perform a predetermined set of manipulations, including algebra and calculus
 - Simplify the result
 - Generate code (either python or C) and compile
 - Use this generated code in my numerical PDE solver (inside the tightest loop)

I wasn't super happy with how fiddly this workflow was. In particular, I'd like to consolidate this whole process into the initial setup of the PDE solver, rather than have multiple compile and link steps. Since I'm writing a new PDE solver, I thought I'd try to incorporate this all into the base code, hopefully to simplify the process for the end-user.

Is symengine a good choice for something like this? I see a code printer. I'd like to compile and link to the code at runtime; I suppose I'd have to handle that myself? Any other ideas, based on my description of the problem?

Thanks for your help,

Nathan Woods

Ondřej Čertík

unread,
Feb 24, 2020, 1:31:11 PM2/24/20
to syme...@googlegroups.com
Hi Nathan,
SymEngine can do a subset of what SymPy can (some of the calculus features might not be implemented yet, if so, just open up an issue for those please).

When should the symbolic expression be generated & compiled for your use case? I don't quite understand what you currently have and what you want to do. Both SymPy and SymEngine can generate LLVM code and compile, or just generate a C code and you compile.

Ondrej

Nathan Woods

unread,
Feb 24, 2020, 1:52:11 PM2/24/20
to symengine
Thanks for your response.

I'm not overly worried about the difficulty of the symbolic manipulations. It's mainly differentiation and as much simplification as I can get.

I'd like this whole process to happen as part of running the overall PDE code. You can imagine it as being something that's done while parsing the input deck, setting up the mesh, and so on. I don't want it to require an additional preprocessing step (i.e. manal compile, for instance), or otherwise feel very different from just running the code normally. Mathematica Compile works sort of like this, as does sympy's lambdify.

Ondřej Čertík

unread,
Feb 24, 2020, 1:57:25 PM2/24/20
to syme...@googlegroups.com


On Mon, Feb 24, 2020, at 11:52 AM, Nathan Woods wrote:
> Thanks for your response.
>
> I'm not overly worried about the difficulty of the symbolic
> manipulations. It's mainly differentiation and as much simplification
> as I can get.
>
> I'd like this whole process to happen as part of running the overall
> PDE code. You can imagine it as being something that's done while
> parsing the input deck, setting up the mesh, and so on. I don't want it
> to require an additional preprocessing step (i.e. manal compile, for
> instance), or otherwise feel very different from just running the code
> normally. Mathematica Compile works sort of like this, as does sympy's
> lambdify.

SymPy's lambify and SymEngine's lambdify can use LLVM to compile it for you. If you want performance of the manipulation or in general, then SymEngine is a great choice, that's why we wrote it.

I can discuss more as I understand your requirements more.

Ondrej
Reply all
Reply to author
Forward
0 new messages