mgaitan / fortran_magic

An extension for IPython/Jupyter that helps to use Fortran in your interactive session.
BSD 3-Clause "New" or "Revised" License
117 stars 32 forks source link

Retrieve fortran source code #28

Closed jbcaillau closed 1 year ago

jbcaillau commented 4 years ago

@mgaitan Hi, very nice piece of work.

One question: is there any easy way to retrieve the original fortran code, e.g. as a mere string, from the generated function ? Something like

In[2]: %load_ext fortranmagic

In[3]: %%fortran

       subroutine f1(x, y, z)
            real, intent(in) :: x,y
            real, intent(out) :: z

            z = sin(x+y)

       end subroutine f1

In[4]: print(f1.__source__)

       subroutine f1(x, y, z)
            real, intent(in) :: x,y
            real, intent(out) :: z

            z = sin(x+y)

       end subroutine f1

The motivation can be to reuse the source code for further transformations, such as automatic differentiation (e.g. with Tapenade online tool).

mgaitan commented 4 years ago

@jbcaillau hi, I've just pushed that little request, thanks for the idea.

However, it's very limited what could be done without a fortran parser. For instance, if your source compile to several object, all of them will share all the source from the cell.

jbcaillau commented 4 years ago

Thanks for the quick reply.

Yes, sharing all the source from the cell for every compiled can be misleading. Still, one can work piece by piece. All in all, I think it makes sense to keep track of the original code as it allows to iterate static operations on it (automatic differentiation, e.g.)

jbcaillau commented 4 years ago

PS. A more general question: it is a bit strange to me that no one has made for C/C++ a magic as easy to use as the one you did for fortran. (Of course there are cython and other stuff...)

mgaitan commented 4 years ago

PS. A more general question: it is a bit strange to me that no one has made for C/C++ a magic as easy to use as the one you did for fortran. (Of course there are cython and other stuff...)

this attempt exists https://github.com/D3f0/ipython_gcc