Bill Allombert on Sun, 26 Mar 2023 17:13:17 +0200


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

Re: from t_POL to t_CLOSURE (and back)


On Sun, Mar 26, 2023 at 10:51:35AM -0400, Max Alekseyev wrote:
> Perhaps, I should clarify my question. The definition
> f = z -> subst(p,variable(p),z)
> looks inefficient (and ugly) to me. I'd like to have a direct way to get
> the corresponding t_CLOSURE from a given polynomial, like I'd do for a
> fixed polynomial:
> f(x) = x^3 + x + 1
> 
> My best attempt so far is
> 
> ? p = x^3 + x + 1
> %1 = x^3 + x + 1
> ? eval(strprintf("f(%s) = %s", variable(p), p))
> %2 = (x)->x^3+x+1
> ? type(f)
> %3 = "t_CLOSURE"

How is it more efficient ? It is much slower since it computes all the powers
separately.

p=(x^1000-1)/(x-1);
eval(strprintf("f(%s) = %s", variable(p), p));
a=ffgen([17,100]);
subst(p,x,a);
  ***   last result computed in 2 ms.
f(a);
  ***   last result computed in 34 ms.
subst(p,x,3^1000);
  ***   last result computed in 210 ms.
f(3^1000);
  ***   last result computed in 1,565 ms.
subst(p,x,matid(100));
  ***   last result computed in 464 ms.
f(matid(100));
  ***   last result computed in 1min, 978 ms.

For the ugliness, define this 
poltoclosure(p)=my(v=variable(p));z->subst(p,v,z)
and then you can do
f = poltoclosure(p)

Cheers,
Bill.