Karim BELABAS on Tue, 13 Jun 2000 11:20:08 +0200 (MET DST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: turn matrix to polynomial


> [Hui Zhu:]
>> Hi - does anyone know a fastest way in gp to convert a matrix c_{i,j} into
>> a polynomial sum c_{i,j}.x^i.y^j of two variables? vice versa ?
[Michael Somos:]
> How about
> gp> m2p(c)=local(rc);rc=matsize(c);sum(i=1,rc[1],sum(j=1,rc[2],c[i,j]*x^i*y^j))
> gp> m2p([1,2;3,4])
> %1 = (4*y^2 + 3*y)*x^2 + (2*y^2 + y)*x

This one is more cryptic but should be faster (no computation); it also
normalizes the coordinates (i,j) to start at (0,0):

? m2p2(c) = Polrev(vector(matsize(c)[1],i, Polrev(c[i,], y)), x)
? m2p2([1,2;3,4])
%1 = (4*y + 3)*x + (2*y + 1)

? m2p2(mathilbert(100));
time = 90 ms.
? m2p(mathilbert(100));
  ***   collecting garbage in sum.
  ***   collecting garbage in sum.
  ***   collecting garbage in sum.
  ***   collecting garbage in sum.
time = 2,590 ms.

The other way round is more complicated since the x coefficients (say) need
not have the same degree in y.

? p2v(p, n) = vectorv(n, i, polcoeff(p, i-1))
? p2m(p) =
{
  local(n,l); p = Vec(p); l = length(p);
  n = 1+ vecmax(vector(l,i,poldegree(p[i])));
  p = vector(l, i, p2v(p[l-i+1], n));
  p[1] = Mat(p[1]); concat(p)~;
}
? p2m(%1)
%4 = 
[1 2]

[3 4]

Both scripts assume that x has higher priority than y (the first would create
an invalid object, the second would return the transpose of the expected
matrix).

Cheers,

  Karim.
__
Karim Belabas                    email: Karim.Belabas@math.u-psud.fr
Dep. de Mathematiques, Bat. 425
Universite Paris-Sud             Tel: (00 33) 1 69 15 57 48
F-91405 Orsay (France)           Fax: (00 33) 1 69 15 60 19
--
PARI/GP Home Page: http://www.parigp-home.de/