Reputation: 57
I need to analize some Fortran code
subroutine ilocz (a,b,c,n,m)
real a(n,n),b(n,m),c(n,m)
do 1 i=1,n
do 2 j=1,m
c(i,j)=0
do 3 k=1,n
3 c(i,j)=c(i,j)+a(i,k)*b(k,j)
2 continue
1 continue
return
end
In other place I'm calling this method
call ilocz (a(n11),y(2),a(n12),n,1)
I should refer to ilocz 5 variables - a, b, c, n, m . It is OK. But in first line in ilocz is declaration of arrays. They have te same name as my method arguments.
When i call ilocz i refer 5 real numbers (not arrays) to method. How it is possible? How it works?
Maybe this number is assigned to every array element ( a(11) to a(n,n) , y(2) to b(n,m) , a(n12) to c(n,m) ) or something?
Could someone explain this to me ? Thank you in advance.
Upvotes: 3
Views: 121
Reputation: 29274
Here is the same code, just modernized. As you can see it expects an array of reals for a
,b
, and c
, but FORTRAN
is great in that you can treat scalars like arrays
pure subroutine ilocz (a,b,c,n,m)
implicit none
! Arguments
integer, intent(in) :: n,m
real, intent(in) :: a(n,n),b(n,m)
real, intent(out) :: c(n,m)
! Local Vars
integer :: i,j,k
do i=1,n
do j=1,m
c(i,j)=0
do k=1,n
c(i,j)=c(i,j)+a(i,k)*b(k,j)
end do
end do
end do
return
end
This we can call as
call ilocz(a(1,1),b,a(2,1),1,1)
which takes the first element of a
, the first element of b
and writes into the 2nd element of a
.
Edit
You can also use the following code:
do i=1,n
do j=1,m
c(i,j)=DOT_PRODUCT(a(i,1:n),b(1:n,i)
end do
end do
or even
c = MATMUL(a,b)
see Fortran matrix multiplication performance in different optimization for performance comparisons the different ways to do this
Upvotes: 4