Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.



vector union
Posted:
Apr 15, 2000 3:42 AM


Hi all,
David Park responded to my programming challengefind the union of a list of nvectors with realvalued machine componentsand 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 nvectors."; 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 3vectors:
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) 3892872



