polymat(P,A) := block ( [n,S,i,PP], PP:expand(P), n:hipow(PP,X), S:coeff(PP,X,n)*A^^n, for i:n-1 step -1 thru 0 do S:S+coeff(PP,X,i)*A^^i, S );