-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvector_subscript.f90
68 lines (68 loc) · 1.51 KB
/
vector_subscript.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module m
implicit none
contains
!
subroutine disp_vec(v,title)
integer, intent(in) :: v(:)
character (len=*), intent(in) :: title
write (*,"(a)") title // " ="
write (*,"(*(i4))") v
write (*,*)
end subroutine disp_vec
!
subroutine disp_mat(m,title)
integer, intent(in) :: m(:,:)
character (len=*), intent(in) :: title
integer :: i1
write (*,"(a)") title // " ="
do i1=1,size(m,1)
write (*,"(*(i4))") m(i1,:)
end do
write (*,*)
end subroutine disp_mat
!
pure subroutine twice(m)
integer, intent(in out) :: m(:,:)
m = 2*m
end subroutine twice
!
end module m
!
program vector_subscript ! analog to Numpy "fancy indexing"
use m, only: twice, disp_vec, disp_mat
implicit none
integer, parameter :: n1 = 3, n2 = 4
integer :: mat(n1,n2), vec(n2), i1, i2
forall (i1=1:n1,i2=1:n2) mat(i1,i2) = 10*i1 + i2
vec = mat(1,:)
call disp_vec(vec,"vec")
call disp_vec(vec([1,3]),"vec([1,3])")
call disp_mat(mat,"mat")
call disp_mat(mat([1,3],:),"mat([1,3],:)")
! Vector subscripts in multiple dimensions yield a
! tensor product.
call disp_mat(mat([1,3],[2,4]),"mat([1,3],[2,4])")
call twice(mat)
! line below invalid because intent(in out) dummy argument
! has actual argument with vector subscript
! call twice(mat([1],:))
end program vector_subscript
! output:
! vec =
! 11 12 13 14
!
! vec([1,3]) =
! 11 13
!
! mat =
! 11 12 13 14
! 21 22 23 24
! 31 32 33 34
!
! mat([1,3],:) =
! 11 12 13 14
! 31 32 33 34
!
! mat([1,3],[2,4]) =
! 12 14
! 32 34