
forums.silverfrost.com Welcome to the Silverfrost forums

View previous topic :: View next topic 
Author 
Message 
AntonioGJerez
Joined: 01 Jun 2015 Posts: 9 Location: Spain

Posted: Tue Jun 02, 2015 7:17 pm Post subject: errors detection for matmul 


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
complex*8 c(2,2)
real*8 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:',c*b
write(*,*)'same types:',c*cmplx(b)
write(*,*)
write(*,*)'mixed type matrix multiplication works awful:'
write(*,*)'mixed types:',matmul(c,b)
write(*,*)'same types:',matmul(c,cmplx(b))
end program 

Back to top 


mecej4
Joined: 31 Oct 2006 Posts: 653

Posted: Wed Jun 03, 2015 12:29 am Post subject: 


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? 

Back to top 


AntonioGJerez
Joined: 01 Jun 2015 Posts: 9 Location: Spain

Posted: Wed Jun 03, 2015 10:13 am Post subject: 


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 

Back to top 


mecej4
Joined: 31 Oct 2006 Posts: 653

Posted: Wed Jun 03, 2015 2:24 pm Post subject: 


After running your program, I agree that FTN95 has a bug that occurs when MATMUL is used to multiply a singleprecision complex matrix with a doubleprecision 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 singleprecision 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. Mixedmode expression are so frequently used in Fortran programs that many compilers do not issue corresponding warnings when no explicit warnings request is made. 

Back to top 




You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group
