8

Variants of this question have been crossposted to Stack Overflow and Computational Science Stack Exchange. Additional answers may be found at these other sites.

Math people:

In an attempt to solve a larger problem, I defined a function $\sigma$ as follows: if $(x_1, x_2, \ldots, x_n)$ is a finite sequence of distinct real numbers, then $\sigma(x_1, x_2, \ldots, x_n) = 1$ if $(x_1, x_2, \ldots, x_n)$ is an even permutation of an increasing sequence, and $\sigma(x_1, x_2, \ldots, x_n) = -1$ if $(x_1, x_2, \ldots, x_n)$ is an odd permutation of an increasing sequence. Does this function have a name? I Googled "parity of a finite sequence" and found nothing. I found plenty on the parity of a permutation, but $(x_1, x_2, \ldots, x_n)$ is not a permutation. Note that $n$ can be any positive integer. The function is defined, and I need to define it, only for finite sequences of distinct real numbers.

An example of an increasing sequence is $(1, 2, 4, 7, 10)$. The numbers $x_1, \ldots, x_n$ are distinct, so there is exactly one increasing sequence you can form using all of them exactly once.

Here is an example: $(2.3, 4.7, 9.9, 10, 13)$ is an increasing sequence of real numbers. $(4.7, 2.3, 10, 9.9, 13)$ is an even permutation of that sequence. So $\sigma(4.7, 2.3, 10, 9.9, 13) = 1$. Got it?

Regardless of whether this function has a standard name, does anyone know if there is a function built-in to Matlab or Maple to compute it?

UPDATE: I got some help at Stack Overflow. If I enter

A = [2 7 4 10]

then

[i a] =sort(A)

then

a

at the Matlab command prompt, the value of $a$ is $[1\ 3\ 2\ 4]$.

The sign of the permutation vector $a$ can be computed in Matlab in two lines using only two additional commands:

J=eye(length(a));

sign =det(J(:,a)))

Stefan (STack Exchange FAN)

Stefan Smith
  • 8,322
  • What do you mean by an increasing sequence? I don't know that your two options exhaust all possibilities, nor do I know if you wanted them to do so. – Ian Coley May 03 '13 at 00:44
  • An example of an increasing sequence is $(1, 2, 4, 7, 10)$. – Stefan Smith May 03 '13 at 01:41
  • Isn't it just the sign of a permutation? (No idea about matlab). Also, the talk about increasing seems to be unnecessary. Replace each $x_i$ with the rank in the increasing order, and talk about the sign of that... – Aryabhata May 03 '13 at 01:49
  • 1
    The closest I can think of is the [inversion number](http://en.wikipedia.org/wiki/Inversion_(discrete_mathematics). – Julien May 03 '13 at 01:52
  • @Aryabhata: No. For example, $(2, 1.3, 7, 4.5, 10)$ is not a permutation. It is a sequence of real numbers. It is an "even permutation" of the increasing sequence $(1.3, 2, 4.5, 7, 10)$, so by my definition, $\sigma(2, 1.3, 7., 4.5, 10) = 1$. The word "increasing" is crucial. If you compared $(2, 1.3, 7, 4.5, 10)$ to a different ordering of the same five numbers, the sign of the permutation would be different. – Stefan Smith May 03 '13 at 01:54
  • 1
    But now that I think about it, $(-1)^\mbox{inversion number}$ is your $\sigma$. So I doubt it has a more specfic name than that. That's even how the signature of a permutation is defined in the first place here. – Julien May 03 '13 at 01:58
  • @StefanSmith: When talking about the parity of a permutation, you obviously need to have the identity permutation in mind. All I am saying is that talking about the increasing is just a distraction. If Matlab can sort, and compute the sign of a permutation, it can do what you want. – Aryabhata May 03 '13 at 01:59
  • @julien : Thank you for your intelligent, helpful, and apparently correct answer. If you give your last comment as an answer, and include the link, I will accept it. I suppose I can associate or identify a sequence of real numbers, which is not technically a permutation, with a permutation (the permutation that converts an increasing sequence to it) . Any idea on the Matlab? – Stefan Smith May 03 '13 at 02:12
  • @StefanSmith Yes, you can define the inversion number for every injective (finite) sequence in $\mathbb{R}$. The inversions of your tuple correspond to the inversions of the underlying permutation. So all is well. And you even have a neat product formula which allows to compute the signature explcitly (see the last formula in my answer). This formula also justifies, essentially, why the two nost common definitions of signature coincide. – Julien May 03 '13 at 02:54

3 Answers3

2

Assume your numbers are in order: $x_1<x_2<\ldots<x_n$. Shuffling the t-uple $(x_1,x_2,\ldots,x_n)$ amounts to permuting the indices by a uniquely determined $\sigma\in S_n$: $(x_{\sigma(1)},x_{\sigma(2)},\ldots,x_{\sigma(n)})$.

We call inversion of $(x_{\sigma(1)},x_{\sigma(2)},\ldots,x_{\sigma(n)})$ the number of pairs $(i,j)$ such that $i<j$ and $x_{\sigma(i)}>x_{\sigma(j)}$. Note that the latter is equivalent to $\sigma(i)>\sigma(j)$. The number of inversions in such a t-uple is called its inversion number. And we have $$ \mbox{inversion number}(x_{\sigma(1)},x_{\sigma(2)},\ldots,x_{\sigma(n)})=\mbox{inversion number}(\sigma(1),\sigma(2),\ldots,\sigma(n)). $$

Fact: for every permutation $\sigma$, the inversion number of $(\sigma(1),\sigma(2),\ldots,\sigma(n))$ has the same parity has every set of transpositions $S$ such that $\sigma=\prod_{\tau\in S}\tau$. The signature of $\sigma$ is defined to be $-1$ if the latter is odd, $+1$ if it is even.

Conclusion: your function $\sigma(x_{\sigma(1)},x_{\sigma(2)},\ldots,x_{\sigma(n)})$ corresponds to the signature $\epsilon (\sigma)$ of the permutation $\sigma$. Therefore $$ \sigma(x_{\sigma(1)},x_{\sigma(2)},\ldots,x_{\sigma(n)})=\epsilon(\sigma)=(-1)^{\mbox{inversion number}(x_{\sigma(1)},x_{\sigma(2)},\ldots,x_{\sigma(n)})}. $$

Proof of the fact: you can find it here. See in particular proof 2.

Matlab/Maple: I don't know if this is already implemented. But if your sequences are not insanely long, I think both will compute easily $$ \sigma(x_{\sigma(1)},x_{\sigma(2)},\ldots,x_{\sigma(n)})=\frac{\prod_{i<j}x_{\sigma(i)}-x_{\sigma(j)}}{\prod_{i<j} x_i-x_j}. $$ Note that it is not even necessary to compute the denominator. The sign of the numerator suffices.

Julien
  • 45,674
  • Thank you very much. That was very quick. It is getting late, but I will read it tomorrow. If there is a built-in Matlab function for it, it is very hard to find. The Matlab files I saw posted on the Web looked a lot more complicated than your simple formula. – Stefan Smith May 03 '13 at 03:09
  • Though the product formula you gave is very simple, the inversion number can be computed in 3 lines of code using Matlab. I edited the question to explain how. – Stefan Smith May 03 '13 at 13:27
  • 1
    @StefanSmith Great. I'm glad you found what you were looking for. – Julien May 03 '13 at 13:53
  • @StefanSmith If you desire better performance, the sign of a permutation can be computed in $O(n)$ time and storage by simply decomposing into cycles. – Erick Wong May 03 '13 at 18:11
  • @ErickWong : The vectors I am working with are small enough that that probably is not an issue. But I'll try to remember that. Do you have a reference? – Stefan Smith May 03 '13 at 20:38
  • 1
    @StefanSmith The signature of a cycle of legnth $k$ is $(-1)^{k+1}$. To find the disjoint cycles that compose a given permutation, follow the orbit of the first element. Then the orbit of the first element left out. Etc. – Julien May 03 '13 at 21:19
2

The map that sends a sequence $(x_1,\ldots,x_n)$ to the permutation $(\pi_1,\ldots,\pi_n)$ such that $$ \pi_i<\pi_j \iff x_i<x_j \text{ or } (x_i=x_j \text{ and } i<j) \qquad\text{ for all $i,j$} $$ is often called standardisation (since your sequences have no repetition, you don't need the second part of the specification). What you compute would then be the sign(ature) of the standardisation of your sequence.

I should add the this use of "standardisation" is neither very old nor widespread, and is mainly used in relation to the combinatorics of tableaux, where it corresponds to the relation between semi-standard and standard tableaux.

  • Thank you, that is exactly what I asked for. Unfortunately, I already accepted another answer with a lot of helpful information, and that poster (julien) also had many helpful comments. I can only accept one answer, and I think it would be rude to unaccept his. – Stefan Smith May 03 '13 at 20:42
1

If you have a function or notation for the $N$th largest element of a list $L$, this is the parity of the permutation that takes $N$th[$L$] to $L[N]$ for all $N$.

zyx
  • 36,077