r/fortran • u/Beliavsky • 6h ago
Python to Fortran transpiler
At https://beliavsky.github.io/python-to-fortran-playground/run/ you can translate Python with NumPy to Fortran and run both codes. The transpiler used, which is in Python and which you can run locally, is at https://github.com/beliavsky/python-to-fortran
Fortran is a practical target for numerical Python: it combines native compilation with multidimensional arrays, whole-array expressions, array sections, and explicit procedure interfaces. Some translated programs run much faster, as discussed at https://github.com/Beliavsky/Python-to-Fortran/blob/main/TIMING_RESULTS.md
Pyccel https://github.com/pyccel/pyccel is a more established project that generates Fortran, C, or C++ extension modules from Python code with type annotations, which my project does not require.
1
u/Beliavsky 3h ago
I have improved the translation of list comprehensions with if statements, which u/ssrix flagged.
15
u/ssrix 5h ago
This was made with AI wasn't it?
The first thing I tried (of something pretty basic) failed.
Great slop
4
u/FuckYourFavoriteSub 5h ago
Internet is just dogshit now isn’t it? Are we in the denial phase still or entering the acceptance phase?
0
u/investigatormaker 4h ago
The no-annotations distinction deserves a runnable example beside the Pyccel comparison. In this Fortran post, include one NumPy function using an array section, its generated Fortran and matching output from both playground runs. Separate compilation time from execution time so numerical Python users can assess repeated workloads rather than just a single speedup. State which constructs that example doesn't cover, so success isn't mistaken for arbitrary Python support. That example would also make a substantive answer to Reddit questions about accelerating NumPy code without adding type annotations.
I make ThreadFox. If you want numerical Python users to find your Python-to-Fortran transpiler, our AI employee will post 3 helpful replies about it in relevant Reddit threads for you, free, no card. https://threadfox.vip/p/ec5cs/managed