Saturday, September 08, 2012
PS1 P9: I == P^k in Octave
(This problem is an investigation on a computer.)
- Create an identity matrix (for example, in Octave, I=eye(5)) and a permutation vector (example p=[3 4 1 2 5])
- Create a permutation matrix by permuting the columns (example P=I(:,p))
- Compute matrix powers (P,P^2,P^3,P^4,...)
- Which is the smallest positive power that returns P to the identity ?
- Find five different p's, each with the property that P^k = I, for k = 1,2,3,4,5 and k is the smallest such value
- Hint: when k = 1, the answer is p=[1 2 3 4 5]. When k=5, the answer is p=[2 3 4 5 1]. Now you should find the answers when k=2,3, and 4
== Solution == I am sure there exists better solutions. Anyhow, here is one solved via Python and Octave: First I generated the 120 permutations via Python, and save the result to a file, permute_5.txtimport itertools for i in itertools.permutations([1,2,3,4,5]): print ' '.join(map(str, i))Next is the Octave code to find the answers based on the permutation file. I know the max value of k is 6 via experimentation.[a b c d e] = textread('permute_5.txt','%d %d %d %d %d'); I = eye(5); distinct_ks=[0 0 0 0 0 0]; for i = 1:size(a)(1) p=[a(i) b(i) c(i) d(i) e(i)]; P=I(:,p); k=1; while (!all(all(I == P^k))) k = k + 1; endwhile if distinct_ks(k) k p endif distinct_ks(k) = distinct_ks(k) + 1; endfor distinct_ksHere is a summary of the interesting results:P^1 == I when p = [1 2 3 4 5] P^2 == I when p = [1 2 3 5 4] P^3 == I when p = [1 2 4 5 3] P^4 == I when p = [1 3 4 5 2] P^5 == I when p = [2 3 4 5 1] P^6 == I when p = [2 1 4 5 3]Distributions:k : 1 2 3 4 5 6 P^k == I: 1 25 20 30 24 20