Bill Allombert on Thu, 17 Nov 2022 20:20:00 +0100

 Re: Conversion of a function/closure with rational coefficients into one with real coefficients

```On Thu, Nov 17, 2022 at 07:30:18PM +0100, Gottfried Helms wrote:
> Am 17.11.2022 um 18:25 schrieb Bill Allombert:
> > On Thu, Nov 17, 2022 at 01:42:58PM +0100, Gottfried Helms wrote:
> >> I'm trying to optimize some bivariate function,
> >> and found, that I can create a vector of polynomials p_r(x).
> >> Let pp be a vector of closures, so for instance
> >>
> >>  ...
> >>  pp(x) =  1/4*x^2-2/3*x+1/6
> >>  pp(x) = -1/8*x^3+31/48*x^2-7/12*x+1/24
> >>  ...
> >
> > Why not write
> >
> > pp =  1/4*x^2-2/3*x+1/6
> > pp = -1/8*x^3+31/48*x^2-7/12*x+1/24
> >
> > and use subst to evaluate pp[i] when needed ?
> >
> > Cheers,
> > Bill
> >
> >
> The problem that I have is runtime-behave.
> pp has order 126 polynomial with rational coefficients
> with hundred or more digits in numerator and denominator.
> Experiments with some manually generated real valued
> coefficients show much better runtime behave.

Then do
pr = pp*1.;

> I think it is better to have polynomials statically
> defined in the most appropriate form, so that any
> overload in typeconversion at runtime occurs only
> minimally.

Well, if you define
p =  1/4*x^2-2/3*x+1/6
then you can define
my(f=p);(pp=x->eval(f));
my(f=p*1.);(pr=x->eval(f));

or
if you really want to use pp(x) =  1/4*x^2-2/3*x+1/6,
then you can do

my(f=pp(x)*1.);ppr=x->eval(f);

Cheers,
Bill.

```