Karim Belabas on Wed, 15 Feb 2012 21:16:31 +0100


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

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


* Jack Brennen [2012-02-15 19:14]:
> 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)))

Using 'stable' (2.5.*), you may try
  N = 3;
  V = [1/(x-1), 1/(x-2), 1/(x-3)];
  apply(v->trap(,0,subst(v,x,N)),  V);

or indirectly

  SUBST(v) = trap(,0,subst(v,x,N));
  apply(SUBST,  V);

Test:

  V = vector(10^5,i, 1/(x-3));
  ? N=3; apply(SUBST,  V); \\ spring the trap
  time = 36 ms.
  ? N=1; apply(SUBST,  V); \\ don't spring the trap
  time = 52 ms.
  ? subst(V,x,1); \\ direct subst
  time = 28 ms.

Comparable...

Cheers,

    K.B.
-- 
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite Bordeaux 1          Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation    http://www.math.u-bordeaux1.fr/~belabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux1.fr/  [PARI/GP]
`