Silverfrost Forums

Welcome to our forums

Matlab to FTN95

12 Dec 2011 8:38 #9364

I have some Matlab routines which I would like to avoid rewriting in FORTRAN.

Is it possible to convert a Matlab routine to FORTRAN-FTN95?

12 Dec 2011 11:57 #9365

Although one aways hesitates to say No, it seems to me to be very unlikely that anyone has even thought of trying to write a Mathlab to Fortran converter.

You can access a Fortran library from a Mathlab script but that is all.

12 Dec 2011 8:03 #9367

What about this? Is it good enough you think?

http://sourceforge.net/projects/matlab2fmex/

Christy

13 Dec 2011 5:51 #9370

Why not give it a try and see what it does? Is a Fortran90 mex file anything like a standard Fortran source file? Is it sufficiently versatile to convert your Mathlab code?

14 Dec 2011 8:31 #9377

Thanks Paul.

Actually, I'm also in the porocess of developing new code which is finally intended for FTN95

But, in the initial stages I need to use some math libraies (equation solvers,etc) which I therefore start with Matlab because it has these inbuilt math functions.Finally,I will be codinmg my own math functions too.

So, enquired if I start with Matlab code would it be a quick one to convert to FTN95

13 Jan 2012 2:59 #9460

We often use this strategy as well, i.e. test in Matlab and then write the final application in Fortran. However, since you do not have the code for some of the Matlab function you really have to start from scratch. Moreover if you have matrix opperations it is important to know that fortran is columnwise and Matlab row wise.

I have found the numerical recipes in fortran to be a good basis when developing your own libraries.

15 Jan 2012 7:51 #9467

jjgermis, thanks.

Sorry, but I do not follow when you say;

Moreover if you have matrix opperations it is important to know that fortran is columnwise and Matlab row wise.

Can you please explain, what you mean by this?

15 Jan 2012 1:36 #9470

What 'routines' have you written in MATLAB? If you have just used high-level matrix functions, you can get the same functionality by using a library like LAPACK, EISPACK etc. Most of these Fortran libraries are the basis of MATLAB anyway (MATLAB is just an interface to Fortran libraries!).

If you're writing or translating low-level stuff, you need to think about how Fortran and MATLAB differ in the way they store 2D arrays in memory. Read up about Row-major order and Column-major order on Wikipedia.

16 Jan 2012 12:55 #9471

Thanks davidb.

Yes, I did read wikepedia for row major order and column major order as suggested by davidb.

But, jjgermis, I see that both Fortran and MAtlab follow column major order

http://en.wikipedia.org/wiki/Row-major_order

Anyways, row major order or column major denote how the arrays are contiguously stored in memory.

How will it affect coding?

In the code I wil be dealing with aij and i always denoted the row and j the column.

16 Jan 2012 7:25 #9472

If you have a M 3x3 matrix the indices are always (row,column) to reference an entry.

When the matrix is saved in memory:

Matlab: M(1,1), M(1,2), M(1,3), M(2,1), M(2,2),... , M(3,2), M(3,3) → saving a row means you have to loop over the column indices

Fortran: M(1,1), M(2,1), M(3,1), M(1,2), M(2,2),..., M(2,3), M(3,3) → saving a column means you have to loop over the row indices

I you use the FTN95 debugger you can see how the matrix entries are saved.

With modern computers I cannot judge how much 'time' you save by doing it correct for either languages. Moreover, if your application is not that complex you propably will not even notice anything. It is however, good practice to know how the language handles the data.

Please login to reply.