Python | SymPy Permutation.commutes_with() method

Python | SymPy Permutation.commutes_with() method

The commutes_with() method in the Permutation class of the SymPy library determines whether two given permutations commute. Two permutations p and q commute if their product is the same regardless of the order, i.e., p⋅q=q⋅p.

Let's see how to use this function:

  1. Import the necessary components.
  2. Create two Permutation objects.
  3. Check if they commute using the commutes_with() method.

Here's a simple example:

from sympy.combinatorics import Permutation # Define two permutations p = Permutation(1, 2, 0) q = Permutation(2, 0, 1) # Check if they commute print(p.commutes_with(q)) # True or False 

In this example, the permutations p and q are given. The method commutes_with() is used to check if they commute with each other.

Here's a more explanatory example:

from sympy.combinatorics import Permutation # Define two permutations p = Permutation([2, 0, 1, 3]) q = Permutation([3, 1, 0, 2]) # Multiply p with q and q with p pq = p * q qp = q * p print("p * q:", pq.array_form) print("q * p:", qp.array_form) # Check if they commute print(p.commutes_with(q)) # This will return False since p * q != q * p 

In the second example, the results of p⋅q and q⋅p are printed out, showing explicitly that they're different. The commutes_with() method then confirms this by returning False.


More Tags

unique-values partial apache-spark-1.5 selenium-grid semantic-ui-react polling javapns model minecraft jakarta-mail

More Programming Guides

Other Guides

More Programming Examples