Bill Allombert on Wed, 21 Oct 2015 01:03:19 +0200


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

Re: Vector of args


On Tue, Oct 20, 2015 at 08:58:16AM +0200, Loïc Grenié wrote:
> On 2015-10-19 at 23:24 GMT+02:00 Bill wrote:
> 
> > So, what do you think about this proposal ?
> >
> 
>        Il looks perfect for me. Excatly what I would have needed (not
>   tested though).

Thanks, I have applied the patch.
The documentation explains how to do generic memoization.

call(f, A):

   A = [a_1,..., a_n] being a vector and f being a function, return the 
evaluation of f(a_1,...,a_n). f can also be the name of a built-in GP function.
If f is variadic,  the variadic arguments must grouped in a vector in the
last component of A.

   This function is useful

* when writing a variadic function, to call another:

   fprintf(file,format,args[..]) = write(file,call(Strprintf,[format,args]))

*  when  dealing  with function arguments with unspecified arity 

The function below implements a global memoization interface:

   memo=Map();
   memoize(f,A[..])=
   {
     my(res);
     if(!mapisdefined(memo, [f,A], &res),
       res = call(f,A);
       mapput(memo,[f,A],res));
    res;
   }
   for example:

   ? memoize(factor,2^128+1)
   %3 = [59649589127497217,1;5704689200685129054721,1]
   ? ##
     ***   last result computed in 76 ms.
   ? memoize(factor,2^128+1)
   %4 = [59649589127497217,1;5704689200685129054721,1]
   ? ##
     ***   last result computed in 0 ms.
   ? memoize(ffinit,3,3)
   %5 = Mod(1,3)*x^3+Mod(1,3)*x^2+Mod(1,3)*x+Mod(2,3)
   ? fibo(n)=if(n==0,0,n==1,1,memoize(fibo,n-2)+memoize(fibo,n-1));
   ? fibo(100)
   %7 = 354224848179261915075

* to call operators through their internal names without using alias

   matnbelts(M) = call("_*_",matsize(M))

   The library syntax is GEN call0(GEN f, GEN A).

Cheers,
Bill