Algebraic characterization of orthogonality in the space of polynomials