\documentclass[nocolor,memo]{j3}
\renewcommand{\hdate}{22 March 2004}
\renewcommand{\vers}{J3/04-276}
\usepackage{amsmath}
\usepackage{lineno}
\usepackage{longtable}
\usepackage{xr}
\externaldocument{007}
\input pdftest
\begin{document}
\vspace{-10pt}
\begin{tabbing}
Subject: \hspace*{0.25in}\=Variation on PACK\\
From: \>Van Snyder\\
%Reference: \>03-258r1, section \\
\end{tabbing}
\pagewiselinenumbers
\leftlinenumbers
\linenumbers*
\section*{Number}
TBD
\section*{Title}
Variation on PACK.
\section*{Submitted By}
J3
\section*{Status}
For consideration.
\section*{Basic Functionality}
Provide a variation on PACK that packs in only one dimension, producing a
result with the same rank as the primary argument.
\section*{Rationale}
The PACK function doesn't always do the thing appropriate to a problem.
The obvious implementation of an alternative to the function proposed
here requires two extra anonymous array temps. A more efficient
implementation requires a named array temp or function result, and a loop
to fill it. If implemented by a function, a separate function is
required for each type, kind and rank of the first argument, and each
kind of the other two. Here's one for default REAL and rank 4, default
LOGICAL and default INTEGER arguments:
{\tt\begin{verbatim}
function PACK ( ARRAY, MASK, DIM ) RESULT ( R )
real, intent(in) :: ARRAY(:,:,:,:)
logical, intent(in) :: MASK(:)
integer, intent(in) :: DIM
real, allocatable :: R(:,:,:,:)
integer :: I, J, S(4)
! Checking of the value of DIM and the extent of MASK omitted
s = shape(array)
s(dim) = count(mask)
allocate ( r(s(1),s(2),s(3),s(4)) ) ! Status should be checked
j = 0
select case ( dim )
case ( 1 )
do i = 1, size(mask)
if ( mask(i) ) then
j = j + 1
r(j,:,:,:) = array(i,:,:,:)
end if
end do
case ( 2 )
! ... obvious implementation for dim == 2, 3, 4
end select
end function PACK
\end{verbatim}}
The loop for {\tt case ( 1 )} could be written
{\tt\begin{verbatim}
r = array(pack([(i,i=1,size(mask))],mask),:,:,:)
\end{verbatim}}
but skeptical users probably don't trust optimizers in all processors
that might be used to compile the program to do as good a job as writing
out the loop explicitly. In particular, to be prudent, one should assume
that evaluating the subscript for the first dimension in this example will
require two array temps.
\section*{Estimated Impact}
Minor both for standard and implementors: One intrinsic function.
\section*{Detailed Specification}
In addition to the description of the function, a summary is needed in
13.5.13.
{\sffamily\bfseries\large 13.7.89$\mathbf{\frac12}$ PACK(ARRAY, MASK, DIM)}
\desc{Pack an array in one dimension, under control of a mask, giving an
array of the same rank.}
\class{Transformational function.}
\arguments{}
\intrinarg{ARRAY}{may be of any type. It shall not be scalar.}
\intrinarg{MASK}{shall be of type logical. It shall be an array of rank
one with the same extent as the DIM dimension of ARRAY.}
\intrinarg{DIM}{shall be a scalar of type integer with a value in the
range $1 \leq \text{DIM} \leq n$, where $n$ is the rank of ARRAY.}
\reschar{The result is an array of the same type, kind and rank as ARRAY.
In every dimension except DIM its extent is the same as the extent in the
corresponding dimension of ARRAY. In the DIM dimension its extent is
COUNT(MASK).}
\resvalue{The result has the value ARRAY(:, :, \dots,
PACK([(I,I=1,SIZE(ARRAY,DIM))], MASK), \dots, :, :)}, where the dimension
subscripted by the result of PACK instead of a colon is DIM.
\examples{The value of PACK( $\left[ \begin{array}{cccc}
1 & 2 & 3 & 4\\
5 & 6 & 7 & 8 \end{array} \right]$, [.TRUE., .FALSE., .TRUE., .FALSE.], 2) is
$\left[ \begin{array}{cc}
1 & 3 \\
5 & 7 \end{array} \right]$. The value of PACK( $\left[ \begin{array}{cccc}
1 & 2 & 3 & 4\\
5 & 6 & 7 & 8 \end{array} \right]$, [.TRUE., .FALSE.], 1) is
RESHAPE([1, 2, 3, 4], [1,4]).}
If 04-275 proceeds the {\bf Result Value} clause can be simplified:
\resvalue{The result has the value ARRAY(:, :, \dots, WHERE(MASK), \dots,
:, :)}, where the dimension subscripted by the result of WHERE instead of
a colon is DIM.
If 04-275 proceeds, the PACK function proposed herein is not as urgently
needed. It would still be useful, because it would avoid the need to
form the array result of WHERE.
\section*{History}
\label{lastpage}
\end{document}