  Using a generalization of the Madelung transformation, we derive the
hydrodynamic representation of the Klein-Gordon-Einstein equations in the weak
field limit. We consider a complex self-interacting scalar field with a
$\lambda|\varphi|^4$ potential. We study the evolution of the homogeneous
background in the fluid representation and derive the linearized equations
describing the evolution of small perturbations in a static and in an expanding
universe. We compare the results with simplified models in which the
gravitational potential is introduced by hand in the Klein-Gordon equation, and
assumed to satisfy a (generalized) Poisson equation. We study the evolution of
the perturbations in the matter era using the nonrelativistic limit of our
formalism. Perturbations whose wavelength is below the Jeans length oscillate
in time while pertubations whose wavelength is above the Jeans length grow
linearly with the scale factor as in the cold dark matter model. The growth of
perturbations in the scalar field model is substantially faster than in the
cold dark matter model. When the wavelength of the pertubations approaches the
cosmological horizon (Hubble length), a relativistic treatment is mandatory. In
that case, we find that relativistic effects attenuate or even prevent the
growth of pertubations. This paper exposes the general formalism and provides
illustrations in simple cases. Other applications of our formalism will be
considered in companion papers.
