# How can I collapse a list of nearly unique vectors?

Posted 9 years ago
7042 Views
|
6 Replies
|
5 Total Likes
|
 The result of a computation I am doing returns a list of vectors some of which are either the reverse or the negative or the negative reverse of others. For my purposes, these reversed, negative, or negative reversed vectors are equivalent to the original and I would like to delete them. An example of my result is Out[26]= {{-0.23944, -0.557541, -0.777604, -0.164769}, {0.23944, 0.557541, 0.777604, 0.164769}, {-0.164769, -0.777604, -0.557541, -0.23944}, {0.164769, 0.777604, 0.557541, 0.23944}, {0.0605099, 0.365047, 0.6618, 0.651997}, {-0.0605099, -0.365047, -0.6618, -0.651997}, {-0.651997, -0.6618, -0.365047, -0.0605099}, {0.651997, 0.6618, 0.365047, 0.0605099}} I would like to collapse this list to yield only Out[26]= {{-0.23944, -0.557541, -0.777604, -0.164769}, {0.0605099, 0.365047, 0.6618, 0.651997}} where negatives, reversals, and negative reversals are eliminated. Does anyone have any idea how I can do this?
6 Replies
Sort By:
Posted 9 years ago
 By "canonicalizing" you mean converting to some sort of standard form?
Posted 9 years ago
 Yeah, like making the first element positive. Handling reversals is tricky if values can be duplicated (up to sign) in a given vector. If not, take forward or reverse depending on which has larger magnitude first element. If equal magnitudes can happen then one might need to keep checking subsequent elements at each end to see which direction is "canonical".
Posted 9 years ago
 If the list is not huge then the Union[...,SameTest->...] approach is good. If it is huge then this will be hindered by the fact that it is O(n^2) complexity. In which case I'd recommend some form of canonicalizing elements (linear time) and doing ordinary Union.
Posted 9 years ago
 I would not do that. I have been using Mathematica for over 20 years, and i am still "picking up shells by the sea while the entire ocean lies unexplored before me."
Posted 9 years ago
 Thanks David. I completely missed the SameTest option when I looked at the documentation for Union. Thanks for not flaming me.
Posted 9 years ago
 In[56]:= data = {{-0.23944, -0.557541, -0.777604, -0.164769}, \ {0.23944, 0.557541, 0.777604, 0.164769}, {-0.164769, -0.777604, -0.557541, -0.23944}, {0.164769, 0.777604, 0.557541, 0.23944}, {0.0605099, 0.365047, 0.6618, 0.651997}, {-0.0605099, -0.365047, -0.6618, -0.651997}, \ {-0.651997, -0.6618, -0.365047, -0.0605099}, {0.651997, 0.6618, 0.365047, 0.0605099}}; In[57]:= sameTest[x_, y_] := x == y || x == Reverse[y] || x == -y || x == -Reverse[y] In[58]:= Union[data, SameTest -> sameTest] Out[58]= {{-0.651997, -0.6618, -0.365047, -0.0605099}, {-0.23944, \ -0.557541, -0.777604, -0.164769}} This is not the same result, but equivalent by your rule.