\HeaderA{compress.default}{Do "zero" run-length encoding compression of a vector of numbers.}{compress.default}
\keyword{manip}{compress.default}
\begin{Description}\relax
Efficiently compress a vector containing many zeroes.
\end{Description}
\begin{Usage}
\begin{verbatim}
compress.default(v, verbose=FALSE,...)

\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{v}] The vector that you wish to compress. This compression function is efficient at compressing vectors with many zeroes, but is not a \emph{general} compression routine.
\item[\code{verbose}] If\code{TRUE} then this routine prints out the degree of compression achieved. 
\item[\code{...}] any other arguments
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
Images are large objects. Thresholded 2d wavelet objects (\code{\LinkA{imwd}{imwd}}) are also large, but many of their elements are zero. compress.default takes a vector, decides whether compression is necessary and if it is makes an object of class \code{compressed} containing the nonzero elements and their position in the original vector. 

The decision whether to compress the vector or not depends on two things, first the number of non-zero elements in the vector (r, say), and second the length of the vector (n, say). Since the position and value of the non-zero elements is stored we will need to store 2r values for the non-zero elements. So compression takes place if \code{2r < n}. 

This function is the default method for the generic function \code{\LinkA{compress}{compress}}. It can be invoked by calling compress for an object of the appropriate class, or directly by calling compress.default regardless of the class of the object.
\end{Details}
\begin{Value}
An object of class compressed if \code{compression} took place, otherwise a an object of class \code{uncompressed}.
\end{Value}
\begin{Section}{RELEASE}
Version 3.5.3 Copyright Guy Nason 1994
\end{Section}
\begin{Note}\relax
Sometimes the compressed object can be larger than the original. This usually only happens for small objects, so it doesn't really matter.
\end{Note}
\begin{SeeAlso}\relax
\code{\LinkA{compress}{compress}},  \code{\LinkA{imwd}{imwd}}, \code{\LinkA{threshold.imwd}{threshold.imwd}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
#
# Compress a vector with lots of zeroes
#
#compress(c(rep(0,100),99))
#$position:
#[1] 101
#
#$values:
#[1] 99
#
#$original.length:
#[1] 101
#
#attr(, "class"):
#[1] "compressed"
#
# Try to compress a vector with not many zeroes
#
#compress(1:10)
#$vector:
#[1]  1 2  3  4  5  6  7  8  9 10
#
#attr(, "class"):
#[1] "uncompressed"
#
#
\end{ExampleCode}
\end{Examples}

