square and multiply power for polynomials and matrices