{"slug": "transpiler-from-python-to-fortran", "title": "Transpiler from Python to Fortran", "summary": "A developer built a transpiler that converts numerical Python, NumPy, pandas, and partial SciPy code into Fortran using Codex and Claude Code, with some translations running 10-100 times faster and a geometric mean speedup of 2x across John Burkardt's Python programs. The transpiler infers scalar, array, string, and pandas DataFrame cases, emits Fortran source, optionally compiles it with gfortran, and compares Python and Fortran output for regression testing; a prime-counting example translated to Fortran ran about 20 times faster. The tool also handles the NumPy examples from the Python Fortran Rosetta Stone, and xp2f_fpm.py can package several drivers with a shared stateless Python module into an FPM project.", "body_md": "Using Codex and Claude Code I have created a Python, NumPy, pandas, and (partial) SciPy to Fortran transpiler. It is intended for numerical Python programs that use a Fortran-friendly subset of Python. It can infer many scalar, array, string, and pandas DataFrame cases, emit Fortran source, optionally compile it with `gfortran`, and compare Python and Fortran output for regression testing. I have tested it on the Python programs of John Burkardt, and some of the Fortran translations are [10-100 times faster](https://github.com/Beliavsky/Python-to-Fortran/blob/main/TIMING_RESULTS.md), with a geometric mean speed up of 2x. The translation of\n\n``` python\n\"\"\"Count primes up to n\"\"\"\n\ndef is_prime(n):\n    if n < 2:\n        return False\n    if n == 2:\n        return True\n    if n % 2 == 0:\n        return False\n    d = 3\n    while d * d <= n:\n        if n % d == 0:\n            return False\n        d += 2\n    return True\n\nlimit = 10**6\nnprime = 0\nmax_prime = None\n\nfor n in range(2, limit + 1):\n    if is_prime(n):\n        nprime += 1\n        max_prime = n\n\nprint(\"number of primes =\", nprime)\nprint(\"largest prime    =\", max_prime)\n```\n\nto Fortran\n\n```\nmodule xprime_proc_mod\n   use, intrinsic :: iso_fortran_env, only: real64\n   implicit none\n   private\n   integer, parameter :: dp = real64\n   public :: dp, is_prime\ncontains\n\npure elemental function is_prime(n) result(func_res)\n   integer, intent(in) :: n\n   logical :: func_res\n   integer :: d\n   \n   if (n < 2) then\n      func_res = .false.\n      return\n   end if\n   if (n == 2) then\n      func_res = .true.\n      return\n   end if\n   if (modulo(n, 2) == 0) then\n      func_res = .false.\n      return\n   end if\n   d = 3\n   do while (d * d <= n)\n      if (modulo(n, d) == 0) then\n         func_res = .false.\n         return\n      end if\n      d = d + 2\n   end do\n   func_res = .true.\nend function is_prime\n\nend module xprime_proc_mod\n\nprogram xprime\n   use xprime_proc_mod, only: is_prime\n   implicit none\n   integer, parameter :: limit = 10 ** 6 ! constant from python source\n   integer :: max_prime, n, nprime\n   \n   nprime = 0\n   max_prime = -1\n   do n = 2, limit\n      if (is_prime(n)) then\n         nprime = nprime + 1\n         max_prime = n\n      end if\n   end do\n   print *, \"number of primes =\", nprime\n   print *, \"largest prime    =\", max_prime\nend program xprime\n```\n\nis about 20 times faster. It translates the NumPy examples from [Python Fortran Rosetta Stone — Fortran Programming Language](https://fortran-lang.org/learn/rosetta_stone/). `xp2f_fpm.py` can package several drivers with a shared stateless Python module into an [FPM project](https://fpm.fortran-lang.org/spec/manifest.html).", "url": "https://wpnews.pro/news/transpiler-from-python-to-fortran", "canonical_source": "https://fortran-lang.discourse.group/t/transpiler-from-python-to-fortran/11141#post_1", "published_at": "2026-09-30 19:17:59+00:00", "updated_at": "2026-09-30 19:46:25.657594+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools", "ai-agents"], "entities": ["Codex", "Claude Code", "NumPy", "pandas", "SciPy", "Fortran", "gfortran", "John Burkardt"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/transpiler-from-python-to-fortran", "markdown": "https://wpnews.pro/news/transpiler-from-python-to-fortran.md", "text": "https://wpnews.pro/news/transpiler-from-python-to-fortran.txt", "jsonld": "https://wpnews.pro/news/transpiler-from-python-to-fortran.jsonld"}}