chap05/kadai5.f90

サンプルコードのダウンロード

 1program answer
 2  implicit none
 3
 4  real(8), allocatable :: vector1(:), vector2(:), vector3(:), matrix(:, :)
 5  integer :: i, j, n
 6
 7  read(*, *) n
 8
 9  allocate(vector1(n))
10  allocate(vector2(n))
11  allocate(vector3(n))
12  allocate(matrix(n, n))
13
14  read(*, *) vector1
15  read(*, *) matrix
16  matrix = transpose(matrix)
17
18  vector2 = 0.0_8
19  do j = 1, n
20    do i = 1, n
21      vector2(i) = vector2(i) + matrix(i, j) * vector1(j)
22    enddo
23  enddo
24
25  vector3 = matmul(matrix, vector1)
26
27  write(*, *) 'Matrix-vector product with do loop'
28  do i = 1, n
29    write(*, *) vector2(i)
30  enddo
31
32  write(*, *) 'Matrix-vector product with matmul'
33  do i = 1, n
34    write(*, *) vector3(i)
35  enddo
36
37  deallocate(vector1)
38  deallocate(vector2)
39  deallocate(vector3)
40  deallocate(matrix)
41
42  stop
43endprogram answer