# Let's get fortran-lang/minpack into SciPy

**URL:** <https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722>\
**Category:** Uncategorized\
**Created:** [February 3, 2022, 4:40pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722 "2022-02-03T16:40:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [February 3, 2022, 4:40pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/1 "2022-02-03T16:40:17Z")

</div>

We now maintain two libraries under fortran-lang:

- fftpack: [https://github.com/fortran-lang/fftpack/](https://github.com/fortran-lang/fftpack/)
- minpack: [https://github.com/fortran-lang/minpack/](https://github.com/fortran-lang/minpack/)

Let’s get them to be used by other projects. In particular SciPy, that is the most high profile “user”. Unfortunately they already replaced fftpack with a C alternative, we were too late. However they still use minpack. Let’s get our modernized version into SciPy:

- [https://github.com/fortran-lang/minpack/issues/14](https://github.com/fortran-lang/minpack/issues/14)

and as part of this, start contributing to SciPy in modern Fortran. If we can pull this off, and convince the SciPy community that this is a worthwhile goal and worth the extra hassle and complexity that this will inevitably bring, then we have turned the tide for Fortran.

What this will bring are possible issues on Windows, Conda possibly using too old of a Fortran compiler, [pyodide](https://pyodide.org/en/stable/) using an old compiler (that doesn’t support modern Fortran) to convert to WebAssembly, and so on. So the hard work that we need to do is help SciPy fix all these issues. I don’t know what the technical problems will be, but I know there will be some. We need to overcome them. The hardest problem might be getting the SciPy community on board, and again, we have to do our best, I think it can be done.

Is anybody here interested in making this happen? If we can have a few people willing to help out, I think we can pull this off.

Some relevant links:

- [https://twitter.com/OndrejCertik/status/1489274112425873413](https://twitter.com/OndrejCertik/status/1489274112425873413)
- [https://twitter.com/fortranlang/status/1489266531200602113](https://twitter.com/fortranlang/status/1489266531200602113)

---

<div class="post-metadata">

**Author:** ![rgoswami](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rgoswami/32/23_2.png) [@rgoswami](https://fortran-lang.discourse.group/u/rgoswami)\
**Post date:** [February 4, 2022, 4:16pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/2 "2022-02-04T16:16:10Z")

</div>

I’m up for helping with any F2PY issues for the wrapping 🙂

I’m not super familiar with the rest of the SciPy codebase other than the new `meson` build backend.

---

<div class="post-metadata">

**Author:** ![jacobwilliams](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jacobwilliams/32/10_2.png) [@jacobwilliams](https://fortran-lang.discourse.group/u/jacobwilliams)\
**Post date:** [February 4, 2022, 4:50pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/3 "2022-02-04T16:50:55Z")

</div>

My question is: if we provide a C-style interface with `iso_c_binding`, do we need `f2py`?

Disclaimer: I literally know nothing about how SciPy is built, but f2py always seemed to me something holding back their use of modern fortran…since I think it was mostly designed for f77? How do they interface with C code now? If we provide a C compatible interface, it should be no different right?

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [February 4, 2022, 5:41pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/4 "2022-02-04T17:41:00Z")

</div>

> [@jacobwilliams](#):
>
> My question is: if we provide a C-style interface with `iso_c_binding` , do we need `f2py` ?

Since @rgoswami is one of the maintainers of f2py now, he is the perfect person to answer.

From the Fortran side, we should provide C interface using `iso_c_binding` I think. It is needed for other languages too. How should f2py take it from there?

Well, we also have a new `pywrap` backend in LFortran that we are working on, that might help: it parses the C `iso_c_binding` modules and produces a C header file and Python bindings. We can adapt it / merge it with f2py, whatever makes the most sense.

---

<div class="post-metadata">

**Author:** ![rgoswami](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rgoswami/32/23_2.png) [@rgoswami](https://fortran-lang.discourse.group/u/rgoswami)\
**Post date:** [February 4, 2022, 5:55pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/5 "2022-02-04T17:55:58Z")

</div>

Yup, `f2py` is still needed. The issue is that even with a compatible C-style interface it still needs to be wrapped into Python. That is the right Python-C API needs to be generated and for this SciPy exclusively uses `f2py` (even `C` codes are provided with `f2py` signature files for wrapping purposes). So in particular, mapping modules to Python modules, compatibility with the NumPy-C API and other object representations are handled by `f2py`.

However, `f2py` does support `iso_c_binding` now (to some extent, actively improving) and it does make it easier… So by all means a C-style interface is a good thing for the library to provide.

---

<div class="post-metadata">

**Author:** ![rgoswami](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rgoswami/32/23_2.png) [@rgoswami](https://fortran-lang.discourse.group/u/rgoswami)\
**Post date:** [February 4, 2022, 5:57pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/6 "2022-02-04T17:57:39Z")

</div>

It would be good to iterate on this, but AFAIK the LFortran-Python support is via Cython which may not be feasible for SciPy (will need to check that though).

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [February 4, 2022, 6:26pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/7 "2022-02-04T18:26:09Z")

</div>

I had a look into how SciPy interfaces with minpack. The interface is mainly low-level plumbing via the Python and numpy header, _i.e._ defining the Python module directly in C, neither f2py nor Cython is involved at this point. This means for a minimum effort solution we only have to define a C API to make minpack usable by SciPy.

~~I will try to collect all the references from the SciPy source and post them in the tracking issue.~~

I collected some info on minpack in SciPy [here](https://github.com/fortran-lang/minpack/issues/14#issuecomment-1030259637).

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [February 4, 2022, 6:38pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/8 "2022-02-04T18:38:52Z")

</div>

> [@rgoswami](#):
>
> It would be good to iterate on this, but AFAIK the LFortran-Python support is via Cython which may not be feasible for SciPy (will need to check that though).

We can add other way to wrap this, possibly even just generating the `f2py signature files`, and let f2py do the wrapping.

---

<div class="post-metadata">

**Author:** ![KjellJorner](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/kjelljorner/32/590_2.png) [@KjellJorner](https://fortran-lang.discourse.group/u/KjellJorner)\
**Post date:** [February 7, 2022, 5:37pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/9 "2022-02-07T17:37:47Z")

</div>

I saw that the [recent version 1.8](https://github.com/scipy/scipy/releases) of SciPy includes a new interface to the PROPACK Fortran library for sparse SVD.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 1, 2022, 12:25pm UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/10 "2022-03-01T12:25:39Z")

</div>

There is a GitHub project [cbouilla](https://github.com/cbouilla)/ **[minpack-1.1](https://github.com/cbouilla/minpack-1.1)**

> C versions of the 1980 Fortran MINPACK

where the versions of MINPACK are discussed, including modernization efforts, and where the author finds speed improvements by using LAPACK:

> ## `lapackified` version
> 
> The code in the `lapackified` folder differs from the `base/` code as follows:
> 
> - It uses LAPACK to compute QR factorizations and related operations. This reduces the amount of code (completely removes the `qrfac` function) and makes it much faster on large instances.
> - It uses some BLAS functions when applicable, notably to reduce code size.
> - The `mldif` and `lmder` functions are very similar; they have been merged.
> - The `hybrid` and `hybrj` functions are very similar; they have been merged.
> - Some functions have been converted to zero-indexed arrays.
> 
> All-in-all, the `lapackified` code is about 25% smaller and much faster than the `base` code.

Edit: I see [Use LAPACK for the QR factorization · Issue #2 · fortran-lang/minpack · GitHub](https://github.com/fortran-lang/minpack/issues/2) where cbouilla has commented.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [April 11, 2022, 11:09am UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/11 "2022-04-11T11:09:59Z")

</div>

5 posts were split to a new topic: [Conda Fortran toolchain on Windows](https://fortran-lang.discourse.group/t/conda-fortran-toolchain-on-windows/3206)

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [July 1, 2022, 10:59am UTC](https://fortran-lang.discourse.group/t/lets-get-fortran-lang-minpack-into-scipy/2722/12 "2022-07-01T10:59:25Z")

</div>

A post was split to a new topic: [Modernizing Arpack](https://fortran-lang.discourse.group/t/modernizing-arpack/3886)
