Difference between revisions of "Perm sign"

From QETLAB
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
|name=perm_sign
 
|name=perm_sign
 
|desc=Computes the sign of a permutation
 
|desc=Computes the sign of a permutation
 +
|rel=[[perm_inv]]
 +
|cat=[[List of functions#Helper_functions|Helper functions]]
 
|upd=November 21, 2012
 
|upd=November 21, 2012
|v=1.00
+
|v=0.50
 
|helper=1}}
 
|helper=1}}
<tt>'''perm_sign'''</tt> is a [[List of functions|function]] that computes the [http://en.wikipedia.org/wiki/Parity_of_a_permutation sign a permutation]. The output of the function is either 1, indicating the permutation is even, or -1, indicating the permutation is odd.
+
<tt>'''perm_sign'''</tt> is a [[List of functions|function]] that computes the [http://en.wikipedia.org/wiki/Parity_of_a_permutation sign of a permutation]. The output of the function is either 1, indicating the permutation is even, or -1, indicating the permutation is odd.
  
 
==Syntax==
 
==Syntax==
Line 16: Line 18:
 
===Small examples===
 
===Small examples===
 
The identity permutation is even:
 
The identity permutation is even:
<pre>
+
<syntaxhighlight>
 
>> perm_sign(1:4)
 
>> perm_sign(1:4)
  
Line 22: Line 24:
  
 
     1
 
     1
</pre>
+
</syntaxhighlight>
  
 
The permutation that transposes 3 and 4 is odd:
 
The permutation that transposes 3 and 4 is odd:
<pre>
+
<syntaxhighlight>
 
>> perm_sign([1,2,4,3,5])
 
>> perm_sign([1,2,4,3,5])
  
Line 31: Line 33:
  
 
     -1
 
     -1
</pre>
+
</syntaxhighlight>
  
 
===A large example===
 
===A large example===
This function has no trouble with large permutations. The following code determines the sign of a random permutation of 1:1000000 in under 1/2 of a second on a standard desktop computer.
+
This function has no trouble with large permutations. The following code determines the sign of a random permutation of 1:1000000 in under 1/2 of a second on a standard desktop computer:
<pre>
+
<syntaxhighlight>
 
>> perm_sign(randperm(1000000))
 
>> perm_sign(randperm(1000000))
  
Line 41: Line 43:
  
 
     1
 
     1
</pre>
+
</syntaxhighlight>
 +
 
 +
{{SourceCode|name=perm_sign|helper=1}}

Latest revision as of 16:26, 29 September 2014

perm_sign
Computes the sign of a permutation

Other toolboxes required none
Related functions perm_inv
Function category Helper functions
This is a helper function that only exists to aid other functions in QETLAB. If you are an end-user of QETLAB, you likely will never have a reason to use this function.

perm_sign is a function that computes the sign of a permutation. The output of the function is either 1, indicating the permutation is even, or -1, indicating the permutation is odd.

Syntax

  • SGN = perm_sign(PERM)

Argument descriptions

  • PERM: A vector containing a permutation of the integers 1, 2, ..., n.

Examples

Small examples

The identity permutation is even:

>> perm_sign(1:4)

ans =

     1

The permutation that transposes 3 and 4 is odd:

>> perm_sign([1,2,4,3,5])

ans =

    -1

A large example

This function has no trouble with large permutations. The following code determines the sign of a random permutation of 1:1000000 in under 1/2 of a second on a standard desktop computer:

>> perm_sign(randperm(1000000))

ans =

     1

Source code

Click here to view this function's source code on github.