Charles Greathouse on Tue, 25 Nov 2014 21:09:13 +0100


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

Re: New gp function fold()


We might consider a function (or flag) that folds by binary splitting. For example, this could be used to keep subproducts small:

foldA((x,y)->print(x*y);x*y,primes(10))
6
35
385
2310
221
437
12673
2800733
%1 = 6469693230

fold((x,y)->print(x*y);x*y,primes(10))
6
30
210
2310
30030
510510
9699690
223092870
6469693230
%2 = 6469693230

Of course this would be valid only for associative functions.

Charles Greathouse
Analyst/Programmer
Case Western Reserve University

On Tue, Nov 25, 2014 at 2:27 PM, Bill Allombert <Bill.Allombert@math.u-bordeaux.fr> wrote:
Dear PARI developers,

As discuted last year,

I have added a function 'fold' to GP

fold(f,A) Apply the function f of arity 2 to the entries of A
 to return f(..., f(f(A[1],A[2]),A[3])... ,A[\#A]).

Some examples:

? fold((x,y)->x*y, [1,2,3,4])
%1 = 24
? fold((x,y)->[x,y], [1,2,3,4])
%2 = [[[1, 2], 3], 4]
? fold((x,f)->f(x), [2,sqr,sqr,sqr])
%3 = 256
? fold((x,y)->(x+y)/(1-x*y),[1..5])
%4 = -9/19
? bestappr(tan(sum(i=1,5,atan(i))))
%5 = -9/19

Cheers,
Bill