\Header{simpleApply}{simpleApply}
\keyword{iteration}{simpleApply}
\begin{Description}\relax
Note: very slow.
\code{func} is applied to all subsets
of \code{arrayObject} defined by \code{dimensions}, i.e.
for every element i of \code{arrayObject}[\code{dimensions}]
the function \code{func} is applied to \code{arrayObject}[i].
\code{func} must be unary.
Due to the recursive definition the function might
not only be slow but also very memory intensive.
The function aims to give you more control on the dimensionality
of the return value in contrast to \code{\Link{apply}};
cf. the examples.
Attention: Be careful with \code{funcResultDimensionality} !
\end{Description}
\begin{Usage}
\begin{verbatim}simpleApply(arrayObject, dimensions, func, funcResultDimensionality, DEBUG=FALSE)\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{arrayObject}] object of type \code{array}; required; default: missing
\item[\code{dimensions}] increasing numeric vector; required; default: missing
\item[\code{func}] unary function; required; default: missing
\item[\code{funcResultDimensionality}] numeric (vector) specifying the
dimensionality of the result value of \code{func}
\item[\code{DEBUG}] logical; required; default: \code{FALSE};
to trace the recursive calling you may use \code{DEBUG=TRUE}
\end{ldescription}
\end{Arguments}
\begin{Value}
\code{array} of \code{dim=c(dim( arrayObject[dimensions] ),
                              funcResultDimensionality     )};
possibly use \code{\Link{aperm}} to rearrange the dimensions
\end{Value}
\begin{Author}\relax
Andreas Buness <a.buness@dkfz.de>
\end{Author}
\begin{Examples}
\begin{ExampleCode}

a <- array(c(1:30),dim=c(3,2,5))
r <- simpleApply(a, 1, function(x){return(x[2,5])}, 1)
stopifnot( all(r == matrix(data=c(28:30))))

r <- simpleApply(a, 2, function(x){return(x[,])}, c(3,5))
stopifnot( all( a == aperm(r,c(2,1,3)) ) )

vec <- 1:10; dim(vec) <- c(10,1)
mat <- matrix(data=rep(1:10,4),nrow=10,ncol=4,byrow=FALSE)
r <- simpleApply(mat,1,function(y){return(mean(y))},1)
stopifnot(all(r==vec))

r <- simpleApply(mat, 1:2, function(x) return(x), 1)
stopifnot( all(r[,,1] == mat) )

r <- simpleApply(a, c(1,3) , function(x) return(x), dim(a)[2])
stopifnot( all(aperm(r[,,],c(1,3,2)) == a) )

r <- simpleApply(a, 1:2, function(x) return(x[2]), 1)
stopifnot( all(r[,,] == a[,,2]) )

r <- simpleApply(a, 1, function(x) return(x), c(dim(a)[2],dim(a)[3]))
stopifnot( all( r== a ) )

 

        \end{ExampleCode}
\end{Examples}

