vector union

• To: mathgroup at smc.vnet.net
• Subject: [mg23078] vector union
• From: Russell Towle <rustybel at foothill.net>
• Date: Sat, 15 Apr 2000 03:00:35 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Hi all,

David Park responded to my programming challenge--find the union of a list
of n-vectors with real-valued machine components--and contrived an
excellent solution. He appealed to a function written by Carl Woll. David's
methods are about 15 times faster!

Now my good old 'vector union' function looks like this:
vunion::usage = "vunion[v] returns the union of a list of n-vectors.";
vunion[v_] :=
Union[Chop[v, .000001],
SameTest -> ((Sign[#1] == Sign[#2]) &&
(Chop[#1 - #2, .000001] == Table[0, {Length[v[[1]] ]}] ) &)]

And David's looks like this:
(*Carl Woll's function, slightly modified*)
OrderedUnion[li_] :=
Block[{i},
i[n_] := (i[n] = Null; n);
i /@ li]

vunion2[v_]:=
Block[{accuracylist,v2},
accuracylist = OrderedUnion[SetAccuracy[v2=Sort[Chop[v,.000001]], 6]];
Do[If[accuracylist[[i]] === Null, v2[[i]] = Null], {i, Length[v2]}];
DeleteCases[v2, Null]
]

A comparison, using 1280 3-vectors:

Length[testlist]
Length[testlist[[1]]]
Length[v1=vunion[testlist]]//Timing
Length[v2=vunion2[testlist]]//Timing
v1==v2

1280
3
{5.71667 Second, 322}
{0.366667 Second, 322}
True

Russell Towle
Box 141
Dutch Flat, CA 95714
(530) 389-2872

```

• Prev by Date: Re: Needs[] and <<
• Next by Date: Re: Product of the first 1000th Prime