Manual Reference Pages  - pack (3fortran)

NAME

PACK(3) - [ARRAY:CONSTRUCTION] Pack an array into an array of rank one

SYNOPSIS

result = pack( array, mask [,vector] )

         TYPE(kind=KIND) function pack(array,mask,vector)

TYPE(kind=KIND),option(in) :: array(..) logical :: mask(..) TYPE(kind=KIND),option(in),optional :: vector(*)

CHARACTERISTICS

o ARRAY is an array of any type
o MASK a logical scalar as well as an array conformable with ARRAY.
o VECTOR is of the same kind and type as ARRAY and of rank one
o the returned value is of the same kind and type as ARRAY

DESCRIPTION

PACK(3) stores the elements of ARRAY in an array of rank one.

The beginning of the resulting array is made up of elements whose MASK equals .true.. Afterwards, remaining positions are filled with elements taken from VECTOR

OPTIONS

o ARRAY : The data from this array is used to fill the resulting vector
o MASK : the logical mask must be the same size as ARRAY or, alternatively, it may be a logical scalar.
o VECTOR : an array of the same type as ARRAY and of rank one. If present, the number of elements in VECTOR shall be equal to or greater than the number of true elements in MASK. If MASK is scalar, the number of elements in VECTOR shall be equal to or greater than the number of elements in ARRAY.
VECTOR shall have at least as many elements as there are in ARRAY.

RESULT

The result is an array of rank one and the same type as that of ARRAY. If VECTOR is present, the result size is that of VECTOR, the number of [char46]true. values in MASK otherwise.

If MASK is scalar with the value .true., in which case the result size is the size of ARRAY.

EXAMPLES

Sample program:

        program demo_pack
        implicit none
        integer, allocatable :: m(:)
        character(len=10) :: c(4)

! gathering nonzero elements from an array: m = [ 1, 0, 0, 0, 5, 0 ] write(*, fmt="(*(i0, ’ ’))") pack(m, m /= 0)

! Gathering nonzero elements from an array and appending elements ! from VECTOR till the size of the mask array (or array size if the ! mask is scalar): m = [ 1, 0, 0, 2 ] write(*, fmt="(*(i0, ’ ’))") pack(m, m /= 0, [ 0, 0, 3, 4 ]) write(*, fmt="(*(i0, ’ ’))") pack(m, m /= 0 )

! select strings whose second character is "a" c = [ character(len=10) :: ’ape’, ’bat’, ’cat’, ’dog’] write(*, fmt="(*(g0, ’ ’))") pack(c, c(:)(2:2) == ’a’ )

! creating a quicksort using PACK(3f) block intrinsic random_seed, random_number real :: x(10) call random_seed() call random_number(x) write (*,"(a10,*(1x,f0.3))") "initial",x write (*,"(a10,*(1x,f0.3))") "sorted",qsort(x) endblock contains ! ! concise quicksort from @arjen and @beliavsky shows recursion, ! array sections, and vectorized comparisons. ! pure recursive function qsort(values) result(sorted) intrinsic pack, size real, intent(in) :: values(:) real :: sorted(size(values)) if (size(values) > 1) then sorted = & & [qsort(pack(values(2:),values(2:)<values(1))), values(1), & & qsort(pack(values(2:),values(2:)>=values(1)))] else sorted = values endif end function qsort end program demo_pack

Result:

        > 1 5
        > 1 2 3 4
        > 1 2
        > bat        cat       
        >    initial .833 .367 .958 .454 .122 .602 .418 .942 .566 .400
        >     sorted .122 .367 .400 .418 .454 .566 .602 .833 .942 .958

STANDARD

Fortran 95

SEE ALSO

MERGE(3), SPREAD(3), UNPACK(3)

Fortran intrinsic descriptions (license: MIT) @urbanjost


Nemo Release 3.1 pack (3fortran) November 02, 2024
Generated by manServer 1.08 from de55072b-9212-4ea7-b979-c004c17592fb using man macros.