Constrained approximation and invariant subspaces