Theory of inhomogeneous magnetic electron gas