Karim Belabas on Sun, 16 Jun 2013 23:35:48 +0200

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

 Re: Enumerating and eliminating

```* zak seidov [2013-06-16 18:49]:
> Charles,
> thanks for listput!
> I also found listpop - to remove element by index.
> And how eliminate element by his value?

The best is to use lists to incrementally build a vector, then convert
it to a set. Then, you can eliminate by value: setminus(v, ).
? v=List(); for(k=1,10,x=k+3;if(x<10,listput(v, x))); v = Set(v)
%1 = [4, 5, 6, 7, 8, 9]

? setminus(v, [3,5,9])
%2 = [4, 6, 7, 8]

Both arguments to setminus must be sets (i.e. sorted), use
setminus(Set(A), Set(B))
in case of doubt.

Cheers,

K.B.

P.S. Using only sets as in

v=[]; for(k=1,10,x=k+3;if(x<10, v = setunion(v,[x])));

is inefficient since the complexity is quadratic in the final set size.

In pari-2.6.0, you can use directly the "set notation":

? v = [x | x<-vector(10, k, k+3), x < 10]
%1 = [4, 5, 6, 7, 8, 9]

(See ??select and ??apply). Of course one can convert the resulting vector to
a true set: v = Set(v)  [ a no-op in this case ]

--
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/~kbelabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux1.fr/  [PARI/GP]
`

```