r/fortran • • 19d ago

Python to Fortran transpiler

Post image

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.

8 Upvotes

19 comments sorted by

4

u/admadguy 18d ago edited 18d ago

Why would you need this? Also aren't a lot of libraries that Python calls still in C or Fortran?

4

u/Beliavsky 18d ago

There are cases where the translated Fortran is 100 times faster than the original Python, as described at https://github.com/Beliavsky/Python-to-Fortran/blob/main/TIMING_RESULTS.md . It is 10 times faster than a Python code that counts primes up to 1 million https://github.com/beliavsky/python-to-fortran#small-example . There will be cases where the speedup is minimal. One can experiment with it.

2

u/CharacterUse 13d ago

That's toy example. In any real world case you're using the python to as a wrapper to call compiled and optimised libraries like numpy etc rather than doing the calculation in python itself. Nobody in the real world would (or at least, should) be doing the heavy calculation in python.

1

u/Humble-Captain3418 12d ago

Imagine you have an application calculation that leverages partial solutions written in Fortran/C through the Python API and now you need to make that shit faster...

1

u/admadguy 18d ago edited 18d ago

I mean sure the wrapper commands are executed faster. But the wrapper consumes a pittance of the total computing time when fort libraries are called. I'm not a fan of python (more so as people's first language) but I get why it's popular. It's easy to start with it. If python is only handling the wrapper stuff, i don't see why that needs to be translated in the first place. More so when the translation is prone to mistakes. Everything doesn't need hyper local optimisation.

20

u/ssrix 19d ago

This was made with AI wasn't it?

The first thing I tried (of something pretty basic) failed.

Great slop

10

u/ssrix 19d ago

unbearably long (yet unhelpful) readme

Yep that's AI. It's specifically a junior developer using AI thinking they've made something good, very polished doesn't mean good, but hasn't had enough experience to know whether it's good or not.

-5

u/ButterflyMundane7187 19d ago

And the true know-it-all has spoken. He even replies to his own comments because nobody else can stand him.

-7

u/Beliavsky 19d ago

Yes, it was. It won't translate all Python programs, but for the ones it does it will be cheaper than using an LLM and reproducible. Can you post the Python code that failed?

4

u/DHermit 19d ago

But for such simple programs doing it by hand will be quicker than copy pasting.

2

u/ssrix 19d ago

A list comprehension with a if statement. Don't ask the LLM to fix it though, because there'll likely be a million more and it isn't worth wasting tokens for

8

u/FuckYourFavoriteSub 19d ago

Internet is just dogshit now isn’t it? Are we in the denial phase still or entering the acceptance phase?

3

u/Zorahgna 19d ago

I really don’t know what shouldn't be denied here

1

u/Beliavsky 19d ago

I have improved the translation of list comprehensions with if statements, which u/ssrix flagged.

3

u/ssrix 18d ago

It still doesn't work....

And the python output isn't even correct...

Transpile: FAIL (unsupported expr: GeneratorExp at line 2: num = (f for f in range(20) if f not in absent))

Translation settings: {"int_kind":"default","preserve_comments":true}

1

u/Barnowl93 12d ago

I don't understand why i wouldn't have a pytnon wrapper for Fortran?

1

u/keithstellyes 12d ago

Cython but slop?