Message Boards Message Boards

0
|
7494 Views
|
6 Replies
|
5 Total Likes
View groups...
Share
Share this post:

How can I collapse a list of nearly unique vectors?

Posted 10 years ago

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?

POSTED BY: Mike Luntz
6 Replies

By "canonicalizing" you mean converting to some sort of standard form?

POSTED BY: Frank Kampas

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 BY: Daniel Lichtblau

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 BY: Daniel Lichtblau
Posted 10 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 BY: David Keith
Posted 10 years ago

Thanks David. I completely missed the SameTest option when I looked at the documentation for Union. Thanks for not flaming me.

POSTED BY: Mike Luntz
Posted 10 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.

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract