10
Challenge
Given a left- or right-stochastic matrix where the limit as x approaches infinity of the matrix to the power of x approaches a matrix with all finite values, return the matrix to which the matrix converges. Basically, you want to keep multiplying the matrix by itself until the result no longer changes.
Test Cases
[[7/10, 4/10], [3/10, 6/10]] -> [[4/7, 4/7], [3/7, 3/7]]
[[2/5, 4/5], [3/5, 1/5]] -> [[4/7, 4/7], [3/7, 3/7]]
[[1/2, 1/2], [1/2, 1/2]] -> [[1/2, 1/2], [1/2, 1/2]]
[[1/3, 2/3], [2/3, 1/3]] -> [[1/2, 1/2], [1/2, 1/2]]
[[1/10, 2/10, 3/10], [4/10, 5/10, 6/10], [5/10, 3/10, 1/10]] -> [[27/130, 27/130, 27/130], [66/130, 66/130, 66/130], [37/130, 37/130, 37/130]]
[[1/7, 2/7, 4/7], [2/7, 4/7, 1/7], [4/7, 1/7, 2/7]] -> [[1/3, 1/3, 1/3], [1/3, 1/3, 1/3], [1/3, 1/3, 1/3]]
Rules
- Standard Loopholes Apply
- You may choose whether you want a right- or a left-stochastic matrix
- You may use any reasonable number type, such as floats, rationals, infinite precision decimals, etc
- This is code-golf, so the shortest submission in bytes for each language is declared the winner for its language. No answer will be accepted
@FryAmTheEggman it seems like some earlier comments have been deleted, so this might be redundant, but reducible and periodic matrices are already excluded by "Given a left- or right-stochastic matrix where the limit as x approaches infinity of the matrix to the power of x approaches a matrix with all finite values", which I read as saying the input is guaranteed to converge to a unique solution. (i.e. the input matrix is guaranteed to be ergodic.) – Nathaniel – 2018-03-08T01:49:58.953
@Nathaniel That isn't quite true, as if the chain is reducible you can have a result (like for the identity matrix) that meets what you said but the chain it describes is not irreducible and therefore the input wouldn't be guaranteed to be ergodic (since it won't be positive recurrent). Guaranteeing ergodicity is what the OP wants, and I think they have that now, thanks to the additional constraint of all of the row values being identical. If you know a better way to constrain the input without needing to add an explanation of Markov chains, I'm sure HyperNeutrino would appreciate it! :) – FryAmTheEggman – 2018-03-08T02:06:24.223
1@FryAmTheEggman ah, you're right, sorry. I was thinking of power iteration rather than raising the matrix to a power. (So by "unique solution" I meant "one that's independent of the starting point of the iteration process", but that's not relevant here.) I agree that the 'all rows are identical' condition does the job. I suppose the OP could just say "the Markov chain is guaranteed to be ergodic," which would satisfy people like us who are likely to worry about it! – Nathaniel – 2018-03-08T02:56:02.393
Actually, if B is a solution to BA = B, then so is cB for any scalar constant c. So a non-zero solution cannot be strictly unique, unless you somehow fix the scale. (Requiring B to be stochastic would do it.) Also, obviously, whether its the rows or the columns of B that are equal will depend on whether A is left or right stochastic. – Ilmari Karonen – 2018-03-08T03:07:07.453
@IlmariKaronen that's a good point. I'll just remove that because that's not really a formal spec, more so just a way of thinking about it, but based on current answers all of them seem to get how to do it – HyperNeutrino – 2018-03-08T03:22:37.753
2
For anyone else who never learned about matrices during Math-class in high school and how to multiply them: https://www.mathsisfun.com/algebra/matrix-multiplying.html. I had to look it up to understand what was being asked.. Maybe it's common knowledge elsewhere on Earth.. :S
– Kevin Cruijssen – 2018-03-08T08:49:54.940@KevinCruijssen oh yeah whoops I just kind of assumed that everyone knew how to do matrix-matrix multiplication :P sorry. I learned it in grade 10 as one of the advanced placement units but idk when they actually teach it in standard curriculum – HyperNeutrino – 2018-03-08T11:30:41.610
So, the rules mean i can't use BigDecimal types? (Infinite precision decimal) – moonheart08 – 2018-03-08T15:48:49.560
@moonheart08 that was added by martin; I'll change it to allow all reasonable number types – HyperNeutrino – 2018-03-08T15:49:42.533
Thanks. I'm going to make a noncompeting (QUARK isn't done yet, i'm just adding the finishing touches needed to do this challenge at all) answer, and I wouldn't be able to use QUARK if I couldn't use infinite precision decimal (the only number type allowed) \o/ – moonheart08 – 2018-03-08T16:09:20.200
@moonheart08 BTW just to clarify, you can't submit invalid answers even as "noncompeting"; that word isn't meaningful here anymore – HyperNeutrino – 2018-03-08T16:16:42.637
Oh, Huh. So that changed. RIP – moonheart08 – 2018-03-08T17:51:29.680