Jack Brennen on Wed, 15 Feb 2012 19:14:17 +0100


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

Question on subst(), deferred evaluation and trap()


Using GP, I'm trying to set up a vector full of expressions on the variable x, and then evaluate the vector with each non-computable element replaced by the number 0. (Could be any value, 0 works for my purposes...)

Basically, I'd like to do something like this:

  V=[trap(,0,1/(x-1)),trap(,0,1/(x-2)),trap(,0,1/(x-3))];

  N=3;subst(V,x,N) -> [1/2, 1, 0]    ...  doesn't do the right thing.

However, that doesn't work because the trap() functions are evaluated immediately rather than stored in the vector.


So far, the best I've come up with is:

  V=[1/(x-1),1/(x-2),1/(x-3)];

And then instead of a simple subst() on V, I use:

  N=3;vector(#V,j,trap(,0,subst(V[j],x,N)))


However, for cases where the trap is not taken, my method is about 10 times slower than just doing subst(V,x,N). Doing it the hard way only if the easy way traps is an obvious and useful optimization:

  N=3;trap(,vector(#V,j,trap(,0,subst(V[j],x,N))),subst(V,x,N))

However, in many (most?) cases, I expect that at least some of the vector elements will be non-computable. (My expressions are more complicated than simple reciprocals -- the reciprocal expressions are just an easy way to demonstrate the problem...)

Is there any way to do this with better performance in GP? I think I could resort to using the PARI library and basically writing my own version of vector() with the desired behavior, but I'd really prefer to do something which is usable in a vanilla PARI/GP interpreter.