Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » 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_] :=
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







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

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.