大学院情報処理演習 第 4 回 (行列の直接解法) 「形状明示仮配列型の受け渡し」 講義ノート目次

module 内の subroutine や function に配列を渡すときは、 Fortran は C 言語と同じように、配列名とその大きさをもって、 配列の受け渡しを行うことも可能である。これを 形状明示仮配列explict shape dummy array という。

	
      module subpart
      implicit none
      contains
      subroutine dummy_print(e,k)
      integer, intent(in) :: k 
      real(8), intent(in) :: e(0:k-1,0:k-1)
      integer i, j
      do i = 0, k - 1
         write(*,'(100e12.4)') (e(i,j), j = 0, k-1)
      end do
      end subroutine dummy_print
      end module subpart
      program explicit
      use subpart
      implicit none
      real(8), allocatable :: e(:,:)
      integer :: n = 5
      integer i, j
      allocate (e(0:n-1,0:n-1))      
      do i = 0, n - 1
         do j = 0, n - 1
            if (j == i) then 
               e(i,j) = 1.0d0
            else 
               e(i,j) = 0.0d0
            end if
         end do
      end do
      call dummy_print(e,size(e(1,:)))
      end program explicit

大きさがいくつかどうかを調べたくない場合は、size(配列(次元,:)) も使える。