# [Maxima] select all x in L

Richard Fateman fateman at cs.berkeley.edu
Wed May 23 23:06:37 CDT 2007

```I don't know what the elements of the list V_ look like.  If they are all
symbols, like x y z then
you could try
ans:0\$
for i in V do ans:ans+i\$

Then the list looks like 3*z+4*y+x    etc.  And you can make it into a list
[3*z,4*y,x]
By using substpart(ans,0,"["). Or you can pick out the coefficients by using
coeff.

Or if the elements are numbers, try
For i in V do ans:ans+histogram[i].

The algorithm you propose is quite inefficient, but maybe that doesn't
matter to you.

You could also sort the list, and then count the runs.

Or you could do    for i in V do histogram[i]:histogram[i]+1,   and then
look at arrayinfo(histogram).

RJF

> -----Original Message-----
> From: maxima-bounces at math.utexas.edu
> [mailto:maxima-bounces at math.utexas.edu] On Behalf Of Wolfgang Lindner
> Sent: Wednesday, May 23, 2007 2:32 PM
> To: maxima-help
> Cc: Sebastian Scigala
> Subject: [Maxima] select all x in L
>
> dear group,
>
> currently we (me and my students) are studying a book on
> elementary probability theory
> (with use CAS DERIVE as assistent) and are porting some of
> the code to (wx)Maxima 5.12.
> Some of the basic concepts we will define ab ovo, before we
> will switch to Mario
> Riotorto's massive application package 'descriptive.mac' in
> order to draw histograms etc.
>
> One early problem is to define the 'frequency of a in list v_'.
> The Derive code is:
>                      H(a, v_) := DIM(SELECT(x = a, x, v_))
>
> I defined the helper function
> (%i1)  select(a,L) := block([LL:[]], for x in L do if x=a
> then LL:cons(x,LL),LL)\$
> (%i2)  select(2,[3,2,4,2]);
> (%i3)  H(a,L):= length(select(a,L));
> (%i4)  H(2,[3,2,4,2,5]);
> (%o4)  2
> which seems ok.
>
> Q1: is there a predefined function 'select' (with another
> name?) in Maxima
>     I have overlooked?
> Q2: I would like to have a 'more functional and general'
> version of select.
>     Does a member of the Maxima community has some hints?
>
> --
> HTH  Wolfgang
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
```