Array indices

I just stumbled upon the fact that the compiler allows me to use whole arrays as indices for other arrays. For instance:

  implicit none

  real*8 :: a(3), b(2)
  integer :: idx(2)

  a=1.d0
  idx=(/1,2/)

  b = a(idx)
  print*,shape(b) 
  print*,b
  print*

  end

Given the fact that this seems to work with both gfortan and the PGI compiler, I wonder if this can use the language, not the compiler. I would appreciate if someone more knowledgeable than me could comment on if this is really a language feature.

And if this is so, then I would appreciate if someone sets out the exact language rules for how such constructions are interpreted in the multidimensional case, for example:

  implicit none
  real*8  :: aa(3,3), bb(2,2)
  integer :: idx(2)

  do i=1,3 ; do j=1,3
    aa(i,j) = 1.d0*(i+j)
  enddo; enddo

  bb=aa(idx,idx)
  print*,shape(bb)
  print*,bb

  end
+3
source share
1 answer

Yes it is.

Fortran 2008, ISO/IEC JTC 1/SC 22/WG 5/N1830, ftp://ftp.nag.co.uk/sc22wg5/N1801-N1850/N1830.pdf . 84

4.8

...

6 ac , (6.5.3.2) .

real, dimension(20) :: b
...

k = (/3, 1, 4/)
b(k) = 0.0      ! section b(k) is a rank-one array with shape (3) and
                !  size 3. (0.0 is assigned to b(1), b(3), and b(4).)

,

implicit none
  real*8  :: aa(3,3), bb(2,2)
  integer :: idx(2),i,j,k
  idx=(/3, 2/)
  k=0
  do i=1,3 ; do j=1,3
    k=k+1
    aa(i,j) = aa(i,j)+1.d0*k
  enddo; enddo
  write(*,*),shape(aa)
  write(*,'(3G24.6,2X)') aa
  bb=aa(idx,idx)
  print*,shape(bb)
  write(*,'(2G24.6,2X)'),bb
  end

:

       3           3
         1.00000                 4.00000                 7.00000    
         2.00000                 5.00000                 8.00000    
         3.00000                 6.00000                 9.00000    
       2           2
         9.00000                 6.00000    
         8.00000                 5.00000
+3

Source: https://habr.com/ru/post/1785938/


All Articles