The Math Forum

Search All of the Math Forum:

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

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Topic: vector union
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Russell Towle

Posts: 70
Registered: 12/3/04
vector union
Posted: Apr 15, 2000 3:42 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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_] :=
i[n_] := (i[n] = Null; n);
i /@ li]

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:


{5.71667 Second, 322}
{0.366667 Second, 322}

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

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2017. All Rights Reserved.