\HeaderA{nnzero}{The Number of Non-Zero Values of a Matrix}{nnzero}
\keyword{attribute}{nnzero}
\begin{Description}\relax
Returns the number of non-zero values of a numeric-like \R{} object, and
in particular an object \code{x} inheriting from class
\code{\LinkA{Matrix}{Matrix.Rdash.class}}.
\end{Description}
\begin{Usage}
\begin{verbatim}
nnzero(x, na.counted = NA)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] an \R{} object, typically inheriting from class
\code{\LinkA{Matrix}{Matrix.Rdash.class}} or \code{\LinkA{numeric}{numeric}}.
\item[\code{na.counted}] a \code{\LinkA{logical}{logical}} describing how
\code{\LinkA{NA}{NA}}s should be counted.  There are three possible
settings for \code{na.counted}:
\describe{
\item[TRUE] \code{NA}s \emph{are} counted as non-zero (since
\dQuote{they are not zero}).
\item[NA] (default)the result will be \code{NA} if there are \code{NA}'s in
\code{x} (since \dQuote{NA's are not known, i.e., \emph{may be} zero}).
\item[FALSE] \code{NA}s are \emph{omitted} from \code{x} before
the non-zero entries are counted.
}
For sparse matrices, you may often want to use \code{na.counted = TRUE}.

\end{ldescription}
\end{Arguments}
\begin{Value}
the number of non zero entries in \code{x} (typically
\code{\LinkA{integer}{integer}}).

Note that for a \emph{symmetric} sparse matrix \code{S} (i.e., inheriting from
class \code{\LinkA{symmetricMatrix}{symmetricMatrix.Rdash.class}}), \code{nnzero(S)} is
typically \emph{twice} the \code{length(S@x)}.
\end{Value}
\begin{SeeAlso}\relax
The \code{\LinkA{Matrix}{Matrix.Rdash.class}} class also has a
\code{\LinkA{length}{length}} method; typically, \code{length(M)} is much
larger than \code{nnzero(M)} for a sparse matrix M, and the latter is
a better indication of the \emph{size} of \code{M}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
m <- Matrix(0+1:28, nrow = 4)
m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0
(mT <- as(m, "dgTMatrix"))
nnzero(mT)
(S <- crossprod(mT))
nnzero(S)
str(S) # slots are smaller than nnzero()
stopifnot(nnzero(S) == sum(as.matrix(S) != 0))# failed earlier

data(KNex)
M <- KNex$mm
class(M)
dim(M)
length(M); stopifnot(length(M) == prod(dim(M)))
nnzero(M) # more relevant than length
## the above are also visible from
str(M)
\end{ExampleCode}
\end{Examples}

