Fortran - I don't understand variable declarations

I need to parse 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

      

Elsewhere I call this method

call ilocz (a(n11),y(2),a(n12),n,1)

      

I have to refer to ilocz 5 variables - a, b, c, n, m. Things are good. But the first line in ilocz has an array declaration. They have the same names as the method arguments.

When I call ilocz, I am referencing 5 real numbers (not arrays) per method. How is this possible? How it works?

Perhaps this number is assigned to each element of the array (a (11) - a (n, n), y (2) - b (n, m), a (n12) - c (n, m)) or something?

Can anyone explain this to me? Thank you in advance.

+3


source to share


1 answer


Here's the same code, just modernized. As you can see, it expects an array of reals for a

, b

and c

, but is FORTRAN

great for handling 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

call ilocz(a(1,1),b,a(2,1),1,1)

      

which takes the first element a

, the first element b

and writes to the second element 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 optimizations for performance comparisons of different ways to do it

+4


source







All Articles