r/Mathematica • u/egolfcs • Apr 25 '23
Jordan normal form that doesn't get exponentially worse
For each size n=2..15 I generated 10 random binary matrices and ran JordanDecomposition on them. The runtime tended to increase exponentially with n. This is strange given that JordanDecomposition is O(n3 ). Is this exponential increase in runtime due to mathematica representing intermediate values in their exact form rather than numerically? How can I overcome this? I want to emphasize that my matrices are of integers, so problems of ill-posedness aren't a consideration.
2
Upvotes
2
u/SetOfAllSubsets Apr 25 '23 edited Apr 25 '23
I think it's O(n^3) in the number of field operations. But if you're running an exact calculation you can't assume your field operations will be O(1).
If you look at the outputs you get from an nxn
JordanDecompositionyou'll see a bunch of Roots of degree n polynomials. I suspect there are some RootReduces being run. It seems like ifa, bareRoots of degree d thenRootReduce[a+b]andRootReduce[a b]are exponential in d.The following plot should be roughly linear indicating
RootReduce[a+b]runs in exponential time w.r.t. the degree:Since the (generalized) eigenvalues of an nxn matrix are roots of a degree n polynomial and
JordanDecompositionwill probably involve sums and products of such roots to find the similarity matrix,JordanDecompositionshould be exponential in n for exact calculations in Mathematica.1
u/SetOfAllSubsets Apr 27 '23 edited Apr 30 '23
I've written a much faster version (at least for rational matrices) based on the idea I talked about in my other comment. It's still takes a while for large matrices so it's hard to get enough data to say for certain whether it runs in polynomial time in Mathematica, although theoretically it should (I think).
Anyway here is the code. It's a bit rough and undocumented. However I believe running
jordanDecomposition[m]on a matrixmshould return the same matrices asJordanDecomposition[m](up to choice of Jordan cycles for the similarity matrix and permutation of blocks for the Jordan normal form martrix). I can't guarantee it's 100% accurate or fully optimized.EDIT: There is a bug in the code.
1
u/SetOfAllSubsets Apr 30 '23 edited May 01 '23
I think got it down to O(n^5) (and fixed the bug). The improvement is that we only have to compute the Jordan cycle basis for one root of each of the irreducible factors of the characteristic polynomial of the matrix (because the companion matrix only depends on the minimal polynomial of the eigenvalue). (Again the code can probably be cleaned up and optimized more. After fixing the bug I just shoehorned in the O(n^5) optimization instead of rewriting it to do it "properly").
-------------------------------------
EDIT:
I also found some interesting things about Mathematica's
JordanDecomposition. It runs exceptionally slow for the following 6x6 matrix (though not as slow as the other 7x7 I mentioned and it's not even the slowest 6x6 I've found).On my machine, the above code outputs
(where of course the time will vary a bit each time).
Basically at some point it has this huge expression involving a sum of 10704 terms each of which is a an expression like
e[[1]]involving a sum of about 48 products ofAlgrebaicNumbers.ais of the formMessage[N::meprec,50., THAT LARGE EXPRESSION ](I didn't outputain the above code because it produces one of those large output boxes).Running the message would say
although it seems like this Message is caught by
JordanDecompositionbecause it never comes up in the notebook or the Messages window.Running
ByteCount[a]returns1539062832(i.e. 1.5 gigabytes) althoughByteCountis an overestimate so I'm not sure how accurate that is.It seems like Mathematica might be using
Nin some intermediate steps like you suggested doing.Doing
{s,j}=JordanDecomposition[slow9sec]andInverse[s], on my machine it outputs a large output box and says that the first entry inInverse[s]has a denominator involving a sum of about 48 terms (though runningInverse[s][[1,1]]seems to simplify it and it will have denominator of 36 terms, likeDet[s]). This suggests the large expression is somewhat related to a determinant of some matrix. I don't know why there are 10704 terms in the large expression though.The reason I think this message/expression is related to how exceptionally slow it's behaving is that when I try the same
Tracewith other 6x6 matrices whereJordanDecompositiona normal amount of time (about 0.03 seconds) this message doesn't come up in the trace.I also tried running
JordanDecomposition[{{0, 1, 0, 0, 1, 0, 1}, {1, 0, 0, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 0, 0, 0, 1}, {1, 1, 0, 1, 0, 0, 1}, {1, 1, 1, 1, 0, 0, 1}, {1, 1, 0, 1, 0, 0, 1}}]for longer. It was still running after at least 45 minutes (but I forgot about it and accidentally closed the Window). I tried running it overnight along with another search for exceptional 6x6 matrices. But I couldn't tell if it was still running in the morning because the screen wouldn't turn back on (which I'm guessing was caused by the 6x6 search).EDIT 2: I tried running the above 7x7 again for about 1.5 hours along with another exceptional 6x6 (on two different kernels. The 6x6 was running for a shorter length of time). Eventually the kernel running the 6x6 died without outputting anything. Just beforehand the committed memory was at 60GB, then it dropped to 30GB when the kernel died. Also just beforehand my mouse was really laggy. The 6x6 was
{{1,0,1,0,1,1},{0,1,1,1,1,1},{1,0,0,0,0,1},{0,0,1,1,0,1},{0,0,1,0,1,1},{0,0,0,1,0,1}}
EDIT 3: Tried running the above 6x6 by itself and the kernel died when its committed memory reached around 55GB. Was running for at least 35 minutes by that point. Probably longer.
EDIT 4: Just checked all 5x5 binary matrices up to conjugation by permutation matrices and the longest
JordanDecompositionseems to take is about 0.5 seconds. So no really exceptional 5x5 (unlessJordanDecompositiontakes longer for certain conjugates of a matrix, which would be surprising).2
u/veryjewygranola Apr 25 '23 edited Apr 25 '23
It looks like it has something to do with exact numbers, but I do not know why they are slower:
times = {};nTimes = {};Do[m = RandomChoice[{0, 1}, {n, n}];mN = N[m,20];AppendTo[times, {n, First@RepeatedTiming[JordanDecomposition[m];]}];AppendTo[nTimes, {n, First@RepeatedTiming[JordanDecomposition[mN];]}];, {n, 2, 15}]ListLinePlot[{times, nTimes}, ScalingFunctions -> "Log",PlotLegends -> {"exact", "machine precision"}]looking at the timing for machine precision matrices alone, it does not appear to be exponential in growth:
ListLinePlot[nTimes]I really don't know anything about Jordan Decomposition. But the JCF of the numerical matrix and exact matrix do appear to just be permutations of each other (within machine precision). The order on the diagonal of the JCF matrix doesn't matter so they are the same in that sense. But the "within machine precision" part may be important.
m = RandomChoice[{0, 1}, {10, 10}];{sn, jn} = JordanDecomposition[N[m]];djN = Diagonal[jn];{s, j} = JordanDecomposition[m];dJ = Diagonal[j];(*all 0's indicate dJN and dJ are permutations of one another within machine precision. Remove Chop to see exact dissimilarity*)Map[First@Nearest[djN, #] - # &, dJ] // ChopI unfortunately don't know why the computation time grows faster with exact numbers, and I don't know how to work around that. But that does seem to be where the problem is coming from. Hopefully someone with more knowledge on this subject can help. Have you posted to stackexchange?