Max Alekseyev on Sun, 26 Mar 2023 20:15:21 +0200


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

Re: from t_POL to t_CLOSURE (and back)


Wow! I was worrying about overhead but it appears that subst() recognizes polynomials and makes efficient substitution in them, but t_CLOSURE without subst() seems to ignore the nature of the underlying function.
Lesson learned. Thank you!
Max


On Sun, Mar 26, 2023 at 11:12 AM Bill Allombert <Bill.Allombert@math.u-bordeaux.fr> wrote:
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.