I’ve looked at SymEngine a few times but lack the time to get my hands dirty. Probably the low-level C interface could be lifted back up in Fortan to an OO-style interface so users could just write their symbolic expressions normally using operator overloading. As a next step you could use the delayed evaluation technique described by @Arjen Markus in “Modern Fortran in Practice” (page 31). It was also shown in his presentation at FortranCon 2020 - “Experimental Programming in Fortran”.
With LFortran run dynamically from a Jupyter Notebook, I guess it would be interesting to have equivalents of
sympifyandlambdifyto convert between textual representation, symbolic expressions, and Fortran code.
Great ideas!
For static compilation however, with existing tools, I don’t really see how I could get a symbolic block replaced with Fortran. I guess it is possible to write a fypp macro to read a Fortran expression into a symbolic representation, perform symbolic differentation or simplification, and then get replaced with Fortran code. But I am not sure what is the advantage compared to doing it separately in a Jupyter notebook (apart from having the code in a single file).
This has to be implemented, but you would call it exactly as you do it now (whether from a Jupyter notebook, or a standalone .py file), except that you will use Fortran instead of Python. So you either write a .f90 program (a “script”) that gets compiled (with any Fortran compiler) and executed; or you call it from a Jupyter notebook via LFortran. This “script” will then call SymEngine to do whatever manipulation you want and generate Fortran code.
The overall idea is that in Matlab you don’t need to switch to another language (Python or Mathematica) to do symbolics, you simply use Matlab for everything: numerics, plotting, symbolics and code generation. In the same way, we could use Fortran down the road for all these tasks also. Until we do it, I can’t of course guarantee that it will be easy to use and natural, but if Matlab can do it, then I think Fortran (as a language) can do it too, as easily. (If Matlab couldn’t do it, then that would be different.)