factrix := proc(M) local Mt, t, v, f, i, j, m, n, q, S, Si, res; options `Maple Advisor Database 1.00 for Maple V Release 4 and 5`, `Copyright (c) 1998 by Robert B. Israel. All rights reserved`; Mt := M; Mt := evalm(Mt); if hastype(eval(Mt),float) then readlib(`tools/gensym`); S:=map(t -> (t=`tools/gensym`(f)), indets([entries(Mt)],float)); Mt:= map2(subs,S,Mt); fi; if type(Mt, matrix) then m := linalg[rowdim](Mt); n := linalg[coldim](Mt); v := factor(q*add(add(f[i, j]*Mt[i, j], i = 1 .. m), j = 1 .. n)); t := select(hastype, v, f[anything, anything]); Mt := subs(q=1,[seq([seq(coeff(t, f[i, j]), j = 1 .. n)], i = 1 .. m)]); if hastype(M, matrix) then Mt := matrix(Mt) fi elif type(Mt, vector) then m := linalg[vectdim](Mt); v := factor(q*add(f[i]*Mt[i], i = 1 .. m)); t := select(hastype, v, f[anything]); Mt := subs(q=1,[seq(coeff(t, f[i]), i = 1 .. m)]); if hastype(M, vector) then Mt := vector(Mt) fi else ERROR(M, `does not evaluate to a matrix or vector`) fi; res := subs(q=1,v/t)*eval(Mt); if S = 'S' then res else Si:= map(e -> (rhs(e)=lhs(e)), S); map2(subs, Si, res) fi; end;