MATLAB To FORTRAN Troubles (eig.m equivalent?)

In summary: 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++, and I remember one guy who used LAPACK. I don't remember if he was successful or not. I'm guessing that if you want to be successful in plasma physics these days you need to be proficient in at least one of the big three languages.
  • #1
Gib007
3
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
  • #2
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.
 
  • #3
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..
 
  • #4
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:
  • #5
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:
  • #6
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
 

FAQ: MATLAB To FORTRAN Troubles (eig.m equivalent?)

1. What is the difference between MATLAB and FORTRAN?

MATLAB and FORTRAN are both programming languages used for scientific and numerical computing. However, MATLAB is primarily used for high-level mathematical operations and data analysis, while FORTRAN is a general-purpose language that is often used for complex scientific and engineering calculations.

2. What is the equivalent of the eig.m function in FORTRAN?

The eig.m function in MATLAB is equivalent to the eig function in FORTRAN. Both functions are used to calculate the eigenvalues and eigenvectors of a matrix.

3. Can I directly convert MATLAB code to FORTRAN?

No, you cannot directly convert MATLAB code to FORTRAN. While both languages have similar syntax and functions, they are not completely interchangeable. Some manual adjustments and modifications may be necessary to successfully convert code from one language to the other.

4. Why am I having trouble with my MATLAB to FORTRAN conversion?

There can be several reasons for having trouble with a MATLAB to FORTRAN conversion. It could be due to differences in syntax, data types, or missing functions in FORTRAN. It is important to carefully check and adjust the code to ensure it functions properly in FORTRAN.

5. Are there any tools or resources available to help with MATLAB to FORTRAN conversion?

Yes, there are some tools and resources available to assist with MATLAB to FORTRAN conversion. These include online converters, software packages, and tutorials. However, it is important to thoroughly check and test the converted code to ensure accuracy and functionality.

Similar threads

Replies
1
Views
1K
Replies
2
Views
3K
Replies
6
Views
1K
Replies
2
Views
3K
Replies
2
Views
1K
Replies
2
Views
4K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
1
Views
4K
Back
Top