Fortran - Construction Functions



The following table describes the construction functions:

Function Description
merge(tsource, fsource, mask) This function joins two arrays. It gives the elements in tsource if the condition in mask is .true. and fsource if the condition in mask is .false. The two fields tsource and fsource have to be of the same type and the same shape. The result also is of this type and shape. Also mask must have the same shape.
pack(array, mask, vector) It packs an array to a vector with the control of mask. The shape of the logical array mask, has to agree with the one for array, or else mask must be a scalar. If vector is included, it has to be an array of rank 1 (i.e. a vector) with at least as many elements as those that are true in mask, and have the same type as array. If mask is a scalar with the value .true. then vector instead must have the same number of elements as array.
spread(source, dim, ncopies) It returns an array of the same type as the argument source with the rank increased by one. The parameters dim and ncopies are integer. if ncopies is negative the value zero is used instead. If source is a scalar, then spread becomes a vector with ncopies elements that all have the same value as source. The parameter dim indicates which index is to be extended. it has to be within the range 1 and 1+(rank of source), if source is a scalar then dim has to be one. The parameter ncopies is the number of elements in the new dimensions.
unpack(vector, mask, array)

It scatters a vector to an array under control of mask. The shape of the logical array mask has to agree with the one for array. The array vector has to have the rank 1 (i.e. it is a vector) with at least as many elements as those that are true in mask, and also has to have the same type as array. If array is given as a scalar then it is considered to be an array with the same shape as mask and the same scalar elements everywhere.

The result will be an array with the same shape as mask and the same type as vector. The values will be those from vector that are accepted, while in the remaining positions in array the old values are kept.

Example

The following example demonstrates the concept:

program arrayConstruction
implicit none
   interface
      subroutine write_array (a)
         real :: a(:,:)
      end subroutine write_array
      
      subroutine write_l_array (a)
         logical :: a(:,:)
      end subroutine write_l_array
   end interface

   real, dimension(2,3) :: tsource, fsource, result
   logical, dimension(2,3) :: mask
   
   tsource = reshape( (/ 35, 23, 18, 28, 26, 39 /), &
                    (/ 2, 3 /) )
   fsource = reshape( (/ -35, -23, -18, -28, -26, -39 /), &
                    (/ 2,3 /) )
   mask = reshape( (/ .true., .false., .false., .true., &
                 .false., .false. /), (/ 2,3 /) )

   result = merge(tsource, fsource, mask)
   call write_array(tsource)
   call write_array(fsource)
   call write_l_array(mask)
   call write_array(result)
   
end program arrayConstruction



subroutine write_array (a)

   real :: a(:,:)
   do i = lbound(a,1), ubound(a,1)
      write(*,*) (a(i, j), j = lbound(a,2), ubound(a,2) )
   end do
   return
   
end subroutine write_array


subroutine write_l_array (a)

   logical :: a(:,:)
   do i = lbound(a,1), ubound(a,1)
      write(*,*) (a(i, j), j= lbound(a,2), ubound(a,2))
   end do
   return
   
end subroutine write_l_array

When the above code is compiled and executed, it produces the following result:

35.0000000   18.0000000   26.0000000    
23.0000000   28.0000000   39.0000000    
-35.0000000  -18.0000000  -26.0000000    
-23.0000000  -28.0000000  -39.0000000    
T F F
F T F
35.0000000   -18.0000000  -26.0000000    
-23.0000000  28.0000000   -39.0000000    
fortran_arrays.htm
Advertisements