"matmul" function problem

"matmul" function problem

[sectionbodytext]the last line  "mtpl(b,a)"  means matrix b * matrix a[/sectionbodytext]
[sectionbodytext]absolutely,it is illegal b is 3*2,and a is 1*3.  my question is [/sectionbodytext]
[sectionbodytext]why can it be Compiled Successfully[/sectionbodytext]

here is my simplified codes:

module mat
contains
    function mtpl(x,y)
    implicit none
        real:: x(:,:),y(:,:)
        real,allocatable:: mtpl(:,:)
        integer:: row_x,col_x,row_y,col_y
        !-----------------------------------
        row_x = ubound(x,1)
        col_x = ubound(x,2)
        row_y = ubound(y,1)
        col_y = ubound(y,2)       !--------(1)------
        allocate(mtpl(row_x,col_y))
        mtpl = matmul(x,y)
    end function mtpl
end module mat
!----------------------------------------------------
program problem
use mat
implicit none
    real:: a(1,3)=(/1,2,3/)
    real:: b(3,2)=reshape((/1,2,3,4,5,6/),(/3,2/))
    write(*,*) mtpl(b,a)          !--------(2)-------
end program problem
3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

There are many things that are mathematical nonsense (or "illegal", as you say) that can be programmed in Fortran (or C, PL/I, etc.). The Fortran compiler does not know more than rudimentary mathematics. In particular, it does not know that mtpl is a matrix multiplication routine, that the two arguments have to conform, what "conform" means, etc. It is up to you to program checks for conformability, if you desire.

More generally, it is possible to write a syntactically correct Fortran program that happens to implement an incorrect calculation or even a faulty algorithm. That is why one has test problems and other checks to examine whether the program is performing correctly.

Thank you for your answer!I know there's something wrong in my codes, and it exists some differences between compiler and me in understanding the codes. However, the Detailsof the problem is what I 'm mostconcerned about, if you want , please give me someProposal

Leave a Comment

Please sign in to add a comment. Not a member? Join today