Converting Python code to Fortran code

Can we convert Python code to Fortran code. If yes how?

Please guide me I will be very thankful

It depends.

You have some options:

  • rewrite everything line by line (very time-consuming, especially if your Python code uses many libraries)
  • identify the bottlenecks of your code and only convert them to Fortran
    • one way to do this is to compile a Fortran module to be usable by Python (f2py3)
    • or you can use libraries, which already use C or Fortran, for example SciPy or NumPy

If your code uses NumPy arrays, you should be able to translate it easily to Fortran, because the NumPy syntax is heavily based on Fortran array operations.

2 Likes

As said already, if your code uses array operations, try changing that part to Fortran.
Also, If you want to change the whole python code (if it is not time-consuming and worth the effort), try to develop a similarity between the python and Fortran statements. To start with try this website to have a comparison between Fortran and python (Python Fortran Rosetta Stone — Fortran90 1.0 documentation). I personally find this comparison very useful.

bests of luck!!

1 Like

My list of Fortran tools has a section on Translation to Fortran, where pyccel is mentioned. The Readme says

Pyccel stands for Python extension language using accelerators.

The aim of Pyccel is to provide a simple way to generate automatically, parallel low level code. The main uses would be:

  1. Convert a Python code (or project) into a Fortran or C code.
  2. Accelerate Python functions by converting them to Fortran or C functions.

Pyccel can be viewed as:

  • Python-to-Fortran/C converter
  • a compiler for a Domain Specific Language with Python syntax

Pyccel comes with a selection of extensions allowing you to convert calls to some specific python packages to Fortran/C. The following packages will be covered (partially):

  • numpy
  • scipy
  • mpi4py (not available yet)
  • h5py (not available yet)

Pyccel’s acceleration capabilities lead to much faster code. A small speed comparison of python vs pyccel or other tools can be found in the performance file.

I wrote about it in a December 2020 thread Translating Python to Fortran with pyccel – it has become more capable since then. Pyccel is an active project with numerous contributors. It would be great if translation to Fortran became a standard way of speeding up Python code, especially scientific code. If many Fortranners tried it and gave feedback to the Pyccel developers, that would benefit both the scientific Python and Fortran communities. They were responsive to a few issues I raised.

5 Likes

Last year, a minibook Fortran Best Practice was imported from fortran90.org (@certik) to fortran-lang.org

I remember we also considered importing the Python Fortran Rosetta Stone but were stopped by technical problems to keep the layout of the tables. Does the Sphinx new framework changes something? (@awvwgk @henilp105)

See: Transfer fortran90.org content · Issue #112 · fortran-lang/webpage · GitHub

1 Like

With the new framework you should be able to just copy over the fortran90.org files without requiring any change as we can also generate pages from rst directly. This should allow to get a quick overview on how the pages will look like on fortran-lang.org.

Since we want a consistent source format, the pages should still be converted to markdown at some point.

3 Likes

For markdown, @certik suggested on GitHub we use MyST markdown.

Indeed, we are already using MyST markdown for the new page. So everything that is possible in reStructuredText, is also possible in markdown there.

3 Likes

@vmagnin I tried to use the rst directly and it looks great , extremely similar to the original site, but for using myst markdown ( pandoc produces unuseful output) we would have to manually rewrite the entire document to include in the learn section.

CC @awvwgk

2 Likes

Did you try with the command I used for Fortran Best Practice?

$ pandoc --standalone --toc --resource-path=../images --to markdown_strict+pipe_tables+backtick_code_blocks best-practices.rst -o best-practices.md

It did a good job. But for Rosetta, the problem was the tables for code comparison.

2 Likes

@vmagnin I had tried with only markdown , I would have to retry with the markdown strict and flags.
Thanks

2 Likes

Thanks Sir, Resolved it and Opened an PR for the same.

Thanks and Regards,
Henil

2 Likes

Thanks for resurrecting this. Yes, it would be cool to port this.

1 Like