Formulas for powers and functions of matrices