Difference between revisions of "Perm sign"
Jump to navigation
Jump to search
| Line 34: | Line 34: | ||
===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> | <pre> | ||
>> perm_sign(randperm(1000000)) | >> perm_sign(randperm(1000000)) | ||
Revision as of 16:08, 22 November 2012
| perm_sign | |
| Computes the sign of a permutation | |
| Other toolboxes required | none |
|---|---|
| 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 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