LF Fortran 95

SPREAD Function

Description

The SPREAD function adds a dimension to an array by adding copies of a data object along a given dimension.

Syntax

SPREAD (source, dim, ncopies)

Arguments

source is an INTENT(IN) scalar or array of any type. Its rank must be less than seven.

dim is an INTENT(IN) scalar of type INTEGER with a value in the range 1 ≤ xn, where n is the rank of source.

ncopies is an INTENT(IN) scalar of type INTEGER.

Result

The result is an array of the same type and kind as source and of rank n + 1, where n is the rank of source.

If source is scalar, the shape of the result is MAX(ncopies, 0) and each element of the result has a value equal to source.

If source is an array with shape (d1, d2, ..., dn), the shape of the result is (d1, d2, ..., ddim-1, MAX(ncopies, 0), ddim+1, ..., dn) and the element of the result with subscripts (r1, r2, ..., rn+1) has the value source(r1, r2, ..., rdim-1, rdim+1, ..., rn+1).

Example

integer :: b(2,2)=reshape((/1,2,3,4/),shape(b)) ! show how shape of array changes after spreading write(*,*) shape(b) ! writes 2 2 write(*,*) shape(spread(b,1,3)) ! writes 3 2 2 write(*,*) shape(spread(b,2,3)) ! writes 2 3 2 write(*,*) shape(spread(b,3,3)) ! writes 2 2 3 ! show element values after spreading write(*,*) b ! writes 1 2 3 4 write(*,*) spread(b,1,3) ! writes 1 1 1 2 2 2 3 3 3 4 4 4 write(*,*) spread(b,2,3) ! writes 1 2 1 2 1 2 3 4 3 4 3 4 write(*,*) spread(b,3,3) ! writes 1 2 3 4 1 2 3 4 1 2 3 4