MATLAB To FORTRAN Troubles (eig.m equivalent?)

Click For Summary

Discussion Overview

The discussion revolves around the challenges of transitioning from MATLAB to FORTRAN for computing eigenvalues and eigenvectors of a 10x10 real matrix in the context of plasma physics research. Participants explore various libraries and methods available in FORTRAN, as well as the implications of using high-level languages versus lower-level programming.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant expresses difficulty in finding a straightforward way to compute eigenvalues and eigenvectors in FORTRAN, noting that MATLAB's eig.m function is much simpler.
  • Another participant suggests that using BLAS/LAPACK or NAG libraries is standard practice in plasma physics for such computations.
  • Some participants mention that LAPACK does not directly return eigenvalues and eigenvectors from a square matrix, indicating a more complex interface.
  • Alternatives to using FORTRAN are proposed, including interfacing MATLAB with C/C++ or FORTRAN, and using tools like f2py or SWIG to generate necessary code.
  • A participant reflects on the general preference for FORTRAN in plasma physics due to performance considerations, especially when solving partial differential equations (PDEs).
  • Concerns are raised about the complexity of writing custom eigenvalue routines, with references to the challenges highlighted in the book Numerical Recipes.
  • Suggestions are made to create wrapper functions for LAPACK to simplify its interface for specific needs.
  • One participant mentions the possibility of extending their graduate studies to delve into numerical linear algebra for a deeper understanding, though it is acknowledged as a challenging endeavor.

Areas of Agreement / Disagreement

Participants generally agree on the utility of LAPACK and NAG libraries for eigenvalue computations in FORTRAN, but there is no consensus on the best approach to take, as various alternatives and personal preferences are discussed.

Contextual Notes

Some participants note the complexity of LAPACK's interface and the potential need for custom solutions, highlighting the limitations of existing libraries in terms of user-friendliness.

Gib007
Messages
3
Reaction score
0
Hello all, first time I post here. I'm doing a PhD in Plasma Physics at the moment and have always been using MATLAB. It seems the research industry however does not look kindly on MATLAB and develops solely on FORTRAN (!)..

Anyway, I don't need to go into a lot of detail. I have a big 10x10 matrix that is real. There is no particular symmetry, just real numbers (positive and negative). I need a piece of code to get me the eigenvalues and the eigenvectors for this matrix. I only deal with square matrices in my work. In MATLAB I could use the trusty eig.m function, which was perfect. Now that I have to code in FORTRAN, I'm stumped..

Thanks for any help you can provide.
 
Physics news on Phys.org
If you're working with Fortran in plasma physics, you'll almost certainly be working with people who use BLAS/LAPACK or NAG to solve such things. These are industry-standard libraries and you'll need to become familiar with them.
 
shoehorn said:
If you're working with Fortran in plasma physics, you'll almost certainly be working with people who use BLAS/LAPACK or NAG to solve such things. These are industry-standard libraries and you'll need to become familiar with them.

I have just been looking through LAPACK actually, yeah. My biggest issue with this is that it doesn't simply take in a square matrix and return eigenvalues and eigenvectors. It's a messier than this..
 
Gib007 said:
I have just been looking through LAPACK actually, yeah. My biggest issue with this is that it doesn't simply take in a square matrix and return eigenvalues and eigenvectors. It's a messier than this..

Well, that's one of the things you're going to have to deal with if you work in Fortran. :)

The reason I suggested LAPACK and NAG is that these really are standard libraries that you will presumably be expected to know if you're working in plasma physics. You should probably therefore just bite the bullet and learn the one that your group uses.

On the other hand, if you're really opposed to abandoning the convenience of a high-level language like Matlab, there are several other approaches you might try, including the following:

  • Matlab can interface with languages like C/C++ and Fortran using Matlab's engine library. The Matlab documentation contains all you need to know about this under "C/C++ and Fortran API reference".
  • You could choose to generate the necessary Fortran code automatically using something like f2py or SWIG, calling the resulting code from something like Python. This is actually quite common for rapidly testing out an idea without having to commit to developing the Fortran code first yourself. Again, there's lots of information out there on how to do things like this. Petter Langtangen's https://www.amazon.com/dp/3642093159/?tag=pfamazon01-20 gives a great introduction to this and your group may very well already have a copy you can read.
 
Last edited by a moderator:
shoehorn said:
Well, that's one of the things you're going to have to deal with if you work in Fortran. :)

The reason I suggested LAPACK and NAG is that these really are standard libraries that you will presumably be expected to know if you're working in plasma physics. You should probably therefore just bite the bullet and learn the one that your group uses.

On the other hand, if you're really opposed to abandoning the convenience of a high-level language like Matlab, there are several other approaches you might try, including the following:

  • Matlab can interface with languages like C/C++ and Fortran using Matlab's engine library. The Matlab documentation contains all you need to know about this under "C/C++ and Fortran API reference".
  • You could choose to generate the necessary Fortran code automatically using something like f2py or SWIG, calling the resulting code from something like Python. This is actually quite common for rapidly testing out an idea without having to commit to developing the Fortran code first yourself. Again, there's lots of information out there on how to do things like this. Petter Langtangen's https://www.amazon.com/dp/3642093159/?tag=pfamazon01-20 gives a great introduction to this and your group may very well already have a copy you can read.

Thanks for your replies! :)
I guess that's just what I'll have to do, unless there's a good and easy method for calculating such eigenvalues and eigenvectors, in which case it might be less cluttered if I just write my own routine for that particular purpose.
 
Last edited by a moderator:
Gib007 said:
Thanks for your replies! :)
I guess that's just what I'll have to do, unless there's a good and easy method for calculating such eigenvalues and eigenvectors, in which case it might be less cluttered if I just write my own routine for that particular purpose.

I'm guessing most plasma physics folks don't want to use MATLAB because in general they are solving pdes, and MATLAB will typically be much slower. I was in plasma physics too, although in the 1990s, and back then it was mostly fortran as well. However there were some folks who used C for their work.

Note that Matlab is built on top of lapack. There is a good reason why even after about 20 years or so Mathworks didn't spend the resources to home-brew their own numerics for this. Writing your own eig routine for general matrices is not easy. If I recall correctly, the book Numerical Recipes advises readers to write their own code for everything except eigen routines, because they can be a beast.

I think you should bite the bullet and learn lapack, unless you can find another such standard library that will work for you. If you think the inputs/outputs of lapack are too messy to deal with, simply write you own wrapper functions that provide inputs/outputs to your liking. Then you just have to deal with the lapack interface once for each lapack routine.

Of course, if you don't mind extending your graduate carreer a bit, you can delve into the world of numerical linear algebra and build your own. This might be fun but be warned that it is a non-trivial exercise and is certainly harder than learning lapack!

best of luck.

jason
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
64
Views
10K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K