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


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

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[4](x) =  1/4*x^2-2/3*x+1/6
> >>  pp[5](x) = -1/8*x^3+31/48*x^2-7/12*x+1/24
> >>  ...
> > 
> > Why not write
> > 
> > pp[4] =  1/4*x^2-2/3*x+1/6
> > pp[5] = -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[128] 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[4] = pp[4]*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[4] =  1/4*x^2-2/3*x+1/6
then you can define
my(f=p[4]);(pp[4]=x->eval(f));
my(f=p[4]*1.);(pr[4]=x->eval(f));

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

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

Cheers,
Bill.