  Particle-in-cell merging algorithms aim to resample dynamically the
six-dimensional phase space occupied by particles without distorting
substantially the physical description of the system. Whereas various
approaches have been proposed in previous works, none of them seemed to be able
to conserve fully charge, momentum, energy and their associated distributions.
We describe here an alternative algorithm based on the coalescence of N massive
or massless particles, considered to be close enough in phase space, into two
new macro-particles. The local conservation of charge, momentum and energy are
ensured by the resolution of a system of scalar equations. Various simulation
comparisons have been carried out with and without the merging algorithm, from
classical plasma physics problems to extreme scenarios where quantum
electrodynamics is taken into account, showing in addition to the conservation
of local quantities, the good reproducibility of the particle distributions. In
case where the number of particles ought to increase exponentially in the
simulation box, the dynamical merging permits a considerable speedup, and
significant memory savings that otherwise would make the simulations impossible
to perform.
