A generalized Graeffe's iteration for evaluating polynomials and rational functions