Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fortran 95 source code to compute inner-product of vetors.
!****************************************************
! ベクトルの内積を計算
!
! date name version
! 2018.08.14 mk-mode.com 1.00 新規作成
!
! Copyright(C) 2018 mk-mode.com All Rights Reserved.
!****************************************************
!
program inner_product
implicit none
integer :: i, dim
real(kind=8), allocatable :: v1(:), v2(:)
real(kind=8) :: tmp
write (*,*) 'Input dimension:'
read (*,*) dim
allocate(v1(dim), v2(dim)) ! メモリ確保
write (*,*) 'Input vector 1:'
read (*,*) v1(1:dim)
write (*,*) 'Input vector 2:'
read (*,*) v2(1:dim)
! 組み込み関数を使用しない場合
! tmp = 0
! do i = 1, dim
! tmp = tmp + v1(i) * v2(i)
! end do
!
! 組み込み関数を使用する場合
tmp = dot_product(v1, v2)
write (*,*) 'Inner product of v1 and v2 = ', tmp
deallocate(v1, v2) ! メモリ解放
stop
end program inner_product
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.