MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Vector Union

  • To: mathgroup at
  • Subject: [mg23141] Re: Vector Union
  • From: Jens-Peer Kuska <kuska at>
  • Date: Thu, 20 Apr 2000 03:20:59 -0400 (EDT)
  • Organization: Universitaet Leipzig
  • References: <8djl4o$>
  • Sender: owner-wri-mathgroup at

Hi Russel,

I overlooked you question, so my answer will be a bit late.
I can't give you a n-dimensional version but the extension is 
easy if you know the dimension. The fastest version to find the
unique $n$-dimensional points in a set is to use a tree of $2^n$

In three dimensions one can use

$SamePrecision = 0.1*^-6;

pointCount = 0;

getIndex[p_List, p1_List] :=
        index = 1;
       If[p[[1]] < p1[[1]], index += 1];
       If[p[[2]] < p1[[2]], index += 2];
       If[p[[3]] < p1[[3]], index += 4];

(* pointCount is fluid from MakeOctTree[] *)

OctTreeInsert[p_List, {}] := {p, pointCount++, Table[{}, {8}], {}}

OctTreeInsert[p_List, {p1_, i_Integer, l_, poly_List}] :=
  Module[{index, delta, l1},
       delta = p - p1;
       If[Abs[Dot[delta, delta]] < $SamePrecision, 
        Return[{p1, i, l, poly}]
    index = getIndex[p, p1];
    l1 = OctTreeInsert[p, l[[index]]];
    {p1, i, ReplacePart[l, l1, index], poly}

and for the points pnts

pointCount = 0;

uniquePoints=Cases[Fold[OctTreeInsert[#2, #1] &, {}, 
    pnts], {_?NumericQ, _?NumericQ, _?NumericQ}, Infinity]

The speed gain depends on the number of points. MathGL3d uses an octtree
to make a indexed point set form the vertex data. Typical for 10^4
points the
usual search  is to slow. A second advantage may be, that the octtree is 
incremental. You can add a k+1 point to your set and find quickly if it
already in the set. The Union/Sort[] versions need always the full set
points and can't be incremented without a new Sort[] call that consume
operations where the octtree need only Log[k+1] operations.

For small point sets the overhead of the tree creation is to huge and
the other versions maye work well. Also the memory for the tree is a
constrain, because for a surface a maximum of 4 links may be used and
four links are useless memory. In higher dimensions this becomes more

How ever for more than 10^4 points (with MathGL3d I have tryed 10^7)
the octtree works excelent.


Russell Towle wrote:
> The problem of finding a function to return the union of a list of
> n-vectors which is at once *fast* and yet *unfailing* has beguiled me for
> several years. I contrived a slow but unfailing 'vunion' function about
> five years ago; made a not-so-slow and unfailing version a year ago; and
> then found it to occasionally fail when Chop was applied to the vectors
> without a second argument.

  • Prev by Date: Re: Mathematica and 3D surface.
  • Next by Date: Re: how to rank a list of elements?
  • Previous by thread: Re: Vector Union
  • Next by thread: Re: Re: Vector Union