0

I need to use a math package which is entirely written in Fortran. The package is not in a single file but is compiled into an archive file (.a file).

I have the full source code. I don't know Fortran.

I did a quick read on f2py document here:

http://docs.scipy.org/doc/numpy-dev/f2py/

It just doesn't work for me. Are there any better documentation or guide for such problem?

EDIT

As requested in comments. I am not sure if I get it correctly (I don't know Fortran), there is some function that probably useful for me:

  SUBROUTINE QP_initialize( data, control, inform )
  TYPE ( QP_data_type ), INTENT( INOUT ) :: data
  TYPE ( QP_control_type ), INTENT( OUT ) :: control
  TYPE ( QP_inform_type ), INTENT( OUT ) :: inform

  SUBROUTINE QP_solve( prob, data, control, inform, C_stat, B_stat )
  TYPE ( QPT_problem_type ), INTENT( INOUT ) :: prob
  TYPE ( QP_data_type ), INTENT( INOUT ) :: data
  TYPE ( QP_control_type ), INTENT( IN ) :: control
  TYPE ( QP_inform_type ), INTENT( OUT ) :: inform
  INTEGER, INTENT( OUT ), OPTIONAL, DIMENSION( prob%m ) :: C_stat
  INTEGER, INTENT( OUT ), OPTIONAL, DIMENSION( prob%n ) :: B_stat
wh0
  • 510
  • 1
  • 6
  • 19
  • How many Fortran functions do you need to call, and can you give us one or two examples including their argument lists? – John Zwinck Dec 14 '15 at 08:07
  • @JohnZwinck I don't know. Possibly around 3-5 in production. I need to try different algorithms to see which one works for me. But the code that I can freely find on the Internet are all written in fortran. – wh0 Dec 14 '15 at 08:28
  • I think in practice, there should be some way to automatically extract the prototype (signature?) from the fortran code and then I can simply link those object file into a shared library. Finally, I call the shared library using the prototype in python? – wh0 Dec 14 '15 at 08:31
  • Yes, that's not too far off. However, can you give us one or two examples including their argument lists? There is a big difference between calling functions that expect an int vs a string vs an array.... – John Zwinck Dec 14 '15 at 08:43
  • I am not sure if I get it correctly. I get one that is probably useful for me: SUBROUTINE QP_initialize( data, control, inform ) TYPE ( QP_data_type ), INTENT( INOUT ) :: data TYPE ( QP_control_type ), INTENT( OUT ) :: control TYPE ( QP_inform_type ), INTENT( OUT ) :: inform – wh0 Dec 14 '15 at 08:57
  • Please add that additional information to the question instead of putting it in the comments. – Warren Weckesser Dec 14 '15 at 08:59
  • some kind of data structure like in C and make the problem more difficult? – wh0 Dec 14 '15 at 08:59
  • OK. I added the information to the question – wh0 Dec 14 '15 at 09:02
  • So you're using the Galahad library, right? http://www.galahad.rl.ac.uk/doc/qp.pdf See this question for another user trying to do QP in Python: http://stackoverflow.com/questions/17009774/quadratic-program-qp-solver-that-only-depends-on-numpy-scipy – John Zwinck Dec 14 '15 at 09:43
  • Yes. I also know the existence of our library, but I want to test if Galahad performance is better or not. – wh0 Dec 14 '15 at 12:36

1 Answers1

0

Since you say you only need to call 3-5 different functions, one easy way is to use ctypes. It's included with Python, and while at first glance it appears to be made for calling C functions, on many systems Fortran is compatible with C for the most part.

See here: https://docs.python.org/2/library/ctypes.html

Alternatively, you may be able to use SciPy to do your quadratic program optimization: http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.minimize.html

John Zwinck
  • 239,568
  • 38
  • 324
  • 436