Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: vector union
Replies: 0

 Russell Towle Posts: 70 Registered: 12/3/04
vector union
Posted: Apr 15, 2000 3:42 AM

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