Silverfrost Forums

Welcome to our forums

errors detection for matmul

2 Jun 2015 6:17 #16403

Dear Friends

I am translating MATLAB code into Fortran and I can't understand some particularities of the latter which makes me waste too much time. For example, matmul(x,y) provides absurds results without any warning if x and y are of differen type (while element by element multiplication works fine with mixed types). I suggest including (at least) some warning or runtime error.

Regards

program bug2 complex8 c(2,2) real8 b(2,2)

b=dble(reshape((/1.,2.,3.,4./),(/2,2/))) c=cmplx(reshape((/1.,2.,3.,4./),(/2,2/)))

write(,)'mixed type element by element multiplication works well:' write(,)'mixed types:',cb write(,)'same types:',ccmplx(b) write(,)

write(,)'mixed type matrix multiplication works awful:' write(,)'mixed types:',matmul(c,b) write(,)'same types:',matmul(c,cmplx(b))

end program

2 Jun 2015 11:29 #16405

I'm afraid that this is not a practical suggestion, for the following simple reason. If, for you, a set of warnings are provided about how Fortran semantics differ from Matlab semantics, similar sets of warnings would be expected by Excel users, Basic users, C programmers, Mathematica users, etc. Every program would produce so many unwanted warnings that most users would feel insulted, and Fortran programming would no longer be a pleasurable activity.

Matlab does not warn Fortran programmers, does it?

3 Jun 2015 9:13 #16406

Well, I disagree with you. Maybe my introduction about Matlab was unnecesary. I only wanted to show an example where the behavior of ftn95 seems weak and Matlab performs reasonably. Forget about Matlab and go to this case.

Which is the fundamental reason for requiring the same types in A and B in matmul(A,B) whereas it is required neither in the 'developed calculation' out(i,j)=A(i,1)*B(1,j)+A(i,2)*B(2,j)+... nor in a element by element multiplication out(i,j)=A(i,j)*B(i,j)?

Is it unreasonable at least a compiler warnining? The ftn95 compiler does provide useful warning when procedures are called with unmatching variable types

3 Jun 2015 1:24 #16408

After running your program, I agree that FTN95 has a bug that occurs when MATMUL is used to multiply a single-precision complex matrix with a double-precision real matrix. I have reported this with a simplified version of your program at http://forums.silverfrost.com/viewtopic.php?p=18249#18249 .

This bug is a routine compiler bug, and need not be discussed as part of a comparison between Matlab and Fortran. If you try either example program with a compiler other than FTN95, you will find that the Fortran program behaves as specified in Fortran language rules. Note that CMPLX(A), where A is double precision real, gives only a single-precision complex matrix. In general, such usage can cause loss of precision, to avoid which you can specify the KIND=kind optional argument to CMPLX. I agree that when CMPLX is called in this way the compiler should give a warning unless warnings are turned off. Mixed-mode expression are so frequently used in Fortran programs that many compilers do not issue corresponding warnings when no explicit warnings request is made.

Please login to reply.