% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/S3_operations.R
\name{reduce}
\alias{reduce}
\title{Reduce a group of HDF5 datasets by accumulation (generic)}
\usage{
reduce(x, ...)
}
\arguments{
\item{x}{An \code{HDF5Matrix}.}

\item{...}{Additional arguments forwarded to the method.}
}
\value{
A new \code{HDF5Matrix} containing the accumulated result.
}
\description{
Generic S3 function for reducing (accumulating) all datasets in the same
HDF5 group as \code{x} into a single dataset using a binary operation
(\code{"+"} or \code{"-"}).

This function operates on the \strong{group} that contains \code{x}, not
on the matrix data of \code{x} itself. All datasets in the group must have
the same dimensions.
}
\details{
\strong{Two access patterns are available:}
\itemize{
\item \code{reduce(x, ...)} — use when you already have an
\code{HDF5Matrix} object open. Operates on all datasets in its group.
\item \code{\link{hdf5_reduce}(filename, group, ...)} — use when you only
have the file path and group name, without needing to open an object.
}
This separation mirrors the pattern used by packages such as
\pkg{DBI}/\pkg{RSQLite} (connection object vs. file path access).

\strong{Note:} this is \emph{not} a row- or column-wise reduction (use
\code{colSums()}, \code{rowMeans()}, etc. for those). It accumulates
entire datasets element-wise.
}
\examples{
\donttest{
fn <- tempfile(fileext = ".h5")

# Create three matrices in the same group
hdf5_create_matrix(fn, "partials/chunk_0", data = matrix(1:100, 10, 10))
hdf5_create_matrix(fn, "partials/chunk_1", data = matrix(1:100, 10, 10))
hdf5_create_matrix(fn, "partials/chunk_2", data = matrix(1:100, 10, 10))

# Open one as entry point — reduce() operates on its whole group
partial <- hdf5_matrix(fn, "partials/chunk_0")
total   <- reduce(partial, func = "+")
dim(total)

hdf5_close_all()
unlink(fn)
}

}
\seealso{
\code{\link{hdf5_reduce}} for the standalone group-level version.
}
