distinct elements in a list



Two observations:

(1) unique_elements misbehaves when use_fast_arrays is true:

  (%i3) unique_elements(p), use_fast_arrays : false;
  (%o3) [1,2,3,4,5,8,9]

  (%i4) unique_elements(p), use_fast_arrays : true;
  (%o4) [true,9,8,5,4,3,2,1]

(2) A compiled unique_elements doesn't work correctly:

  (%i5) compile(unique_elements)$
  (%i6) unique_elements(p);
  Maxima encountered a Lisp error:
  Error in MACSYMA-TOP-LEVEL [or a callee]: The function $KILL is
  undefined.

Barton

-----maxima-bounces at math.utexas.edu wrote: -----

>To: "sen1 at math.msu.edu"
>From: "Stavros Macrakis"
>Sent by: maxima-bounces at math.utexas.edu
>Date: 06/17/2007 10:26PM
>cc: maxima at math.utexas.edu
>Subject: Re: [Maxima] distinct elements in a list
>
>On 6/17/07, sen1 at math.msu.edu  wrote:
>Hello,
>  Given a list 'list_1', one way to make a list of the distinct
>  elements of 'list_1' is;
>
>listify(setify(list_1))$
>
>Is there a better (i.e., faster) way ?
>
>
>This is a good and fast way.  Setify uses sorting (using orderlessp as the
>order) to put equivalent elements next to each other. You could also use
>hasharrays, something like this:
>
>
>unique_elements(l) :=
>    if l=[] then []
>    else ( kill(unique_elements_hash),
>           for i in l do unique_elements_hash[i]:i,
>           listarray(unique_elements_hash))$
>
>Setify does not use the hash technique because it wants the elements in
>standard order. (Some quick experiments seem to show that listarray
>returns the elements in standard order, too..., though.)
>
>
>In principle, you'd expect the hasharray approach to be faster; I am not
>sure what the practicalities look like.  If you perform some interesting
>benchmarks (e.g. elements of l are all numbers; elements of l are complex
>expressions; elements of l are very similar objects; sizeof(l) approx = to
>sizeof(unique(l)); sizeof(l) >> sizeof(unique(l)) etc. etc.), let us know.
>
>
>             -s
>
>
>_______________________________________________
>Maxima mailing list
>Maxima at math.utexas.edu
>http://www.math.utexas.edu/mailman/listinfo/maxima