Optimal quadrature formulas using generalized inverses. I. General theory and minimum variance formulas