4
$\begingroup$

Say if I have two sets of vectors, for example:

$$v_{1}=((0,0,0),(0,1,0),(0,0,1),(1,1,1))$$

$$v_{2}=((0,1,0),(1,1,1),(0,0,0),(0,0,1))$$

I want to find a way of verifying that both $v_{1}$ and $v_{2}$ contain the same vectors, regardless of order.

I'm not sure how to do it, below are attempts that did not work:

v1={{0,0,0},{0,1,0},{0,0,1},{1,1,1}}
v2={{0,1,0},{1,1,1},{0,0,0},{0,0,1}}
TrueQ[OrderlessPatternSequence[v1]==OrderlessPatternSequence[v2]]
Equal[Permutations[v1],Permutations[v2]]
Intersection[v1,v2]
$\endgroup$
6
  • 1
    $\begingroup$ Should the multiplicity of the vectors match as well? If it doesn't matter then Union[v1] === Union[v2] should be okay. $\endgroup$
    – Syed
    Commented Aug 31, 2023 at 10:32
  • $\begingroup$ As in can there be duplicates of the same vector or am I misinterpreting "multiplicity of the vectors"? $\endgroup$
    – am567
    Commented Aug 31, 2023 at 10:57
  • 2
    $\begingroup$ From the examples at OrderlessPatternSequence you can use MatchQ[v1, {OrderlessPatternSequence[Sequence@@v2]}] $\endgroup$
    – creidhne
    Commented Aug 31, 2023 at 10:57
  • 1
    $\begingroup$ Union will sort as well as delete the duplicates. Yes, you are interpreting it correctly. I guess the ambiguity exists as you used the word set instead of list. $\endgroup$
    – Syed
    Commented Aug 31, 2023 at 10:59
  • 1
    $\begingroup$ My understanding is that Mathematica does not have a Set construct but only a List construct. If, however, you use functions intended for sets, such as Union, Intersection, Complement, then these functions will sort (since order does not matter in a set) and will delete duplicates (since a set contains unique elements only). $\endgroup$
    – Syed
    Commented Aug 31, 2023 at 11:25

4 Answers 4

10
$\begingroup$

One way:

Sort@v1==Sort@v2
(* True *)
$\endgroup$
4
$\begingroup$

You might want ContainsExactly:

ContainsExactly[v1, v2]

although it will ignore multiplicities.

$\endgroup$
3
$\begingroup$

1.

A slight variation of creidhne's suggestion in comments:

sameOrderlessPatternQ = MatchQ[{OrderlessPatternSequence @@ #}]@#2 &;

sameOrderlessPatternQ[v1, v2]
True

2.

Internal`ComparePatterns @@ (OrderlessPatternSequence @@@ {v1, v2})
"Identical"

3.

GeneralUtilities`EquivalentPatternQ @@ (OrderlessPatternSequence @@@ {v1, v2})
True

4.

GeneralUtilities`AllSameBy[Sort] @ {v1, v2}
True
$\endgroup$
2
$\begingroup$

Using SymmetricDifference which came with V 13.1

v1 = {{0, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 1}};
v2 = {{0, 1, 0}, {1, 1, 1}, {0, 0, 0}, {0, 0, 1}};

SymmetricDifference[v1, v2] === {}

(* True *)

From the documentation:

"SymmetricDifference[list1, list2] gives the sorted list of elements that are in list1 or list2 but not in both."

If we change the third element of v2 to

v2 = {{0, 1, 0}, {1, 1, 1}, {0, 2, 0}, {0, 0, 1}};

we get

SymmetricDifference[v1, v2]

{{0, 0, 0}, {0, 2, 0}}

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.