prima icon indicating copy to clipboard operation
prima copied to clipboard

Creating a Python reference implementation

Open nbelakovski opened this issue 1 year ago • 5 comments

Hi all,

I've been discussing with @zaikunzhang about creating a Python reference implementation and now I'm making it official by creating this issue!

I think a Python reference implementation would be a great asset. @zaikunzhang mentioned readability several times in his initial post on the Fortran discourse, and indeed many references to the idea that the code should be as similar as possible to the mathematical presentation. I'd argue that Python is an even better language for this than Fortran, but the goal here is not to replace one with the other, both are necessary for separate reasons.

There's some questions about how to go about this. I think that as a starting point COBYLA is the best place since the current version of SciPy contains the old F77 COBYLA implementation so we could use that for a few tests as a bootstrapping with (not for long term, just to get started. Long term it should probably be tested against the modern Fortran implementation).

@zaikunzhang suggested using tools to automatically translate the Fortran code into Python. The idea is that automatic translation might be easier to verify, but I'm not so sure about this for two reasons. First of all, I'm not aware of the existence of tools to translate Fortran to Python. I've done some Googling and not found anything compelling (i.e. something that didn't look like someone's toy project from a few years ago). I figured the most likely path for such a translation would be via LFortran and LLVM, but no luck there either. Secondly, regardless of how the code is created, the verification checks should be the same regardless.

One other point I'd like to bring up for discussion (and I will summarize these at the end) is the use of language features. I think @zaikunzhang wants to limit these, but we're entering into vague territory here and additional detail would be welcome. For example, Python has a number of features that improve readability, like lambda expressions, list comprehensions, numpy's @ symbol for matmul, etc., most of which I think are either readily understandable or quickly understandable with a little documentation. There are some more complicated ones, like the yield expression, which I would avoid unless it was both very intuitive AND the alternative would be cumbersome. If one chooses to use fancy language features when there are non-cumbersome alternatives, well that's just showing off 🤓.

So to summarize, here are 3 points for discussion:

  1. Are there tools for translating Fortran to Python that I'm not aware of that could help?
  2. What does verification look like? Should we just find a way to add the Python implementation to (some of) the existing Fortran tests?
  3. Re: language features, how should we frame our thinking on them? Perhaps @zaikunzhang has an example of a Fortran language feature he has explicitly chosen not to use for sake of readability?

Looking forward to working on this with you all!

nbelakovski avatar Jul 08 '23 19:07 nbelakovski