General power of a matrix?
Is there a way to compute a general power of a matrix? For example:
((2,-1),(1,0))^k = ((k+1,-k),(k,1-k))
WolframAlpha: https://is.gd/8cseex
Is Sage able to compute that?
Is there a way to compute a general power of a matrix? For example:
((2,-1),(1,0))^k = ((k+1,-k),(k,1-k))
WolframAlpha: https://is.gd/8cseex
Is Sage able to compute that?
My answer elaborates on using the Jordan normal form to implement a broad class of matrix functions, the matrix power included. It relies on the jordan_form()
method, available for exact rings.
The mathematical preliminaries can be found in extending scalar function for matrix functions and in the excellent book Functions of Matrices by N. Higham.
(using 𝚂𝚊𝚐𝚎𝙼𝚊𝚝𝚑𝚟𝚎𝚛𝚜𝚒𝚘𝚗𝟽.𝟺,𝚁𝚎𝚕𝚎𝚊𝚜𝚎𝙳𝚊𝚝𝚎:𝟸𝟶𝟷𝟼⎯𝟷𝟶⎯𝟷𝟾)
Let
sage: A = matrix(QQ, [[2, -1], [1, 0]])
sage: var('k')
sage: A^k
we get
sage: NotImplementedError: non-integral exponents not supported
The code below implements f(A) using the Jordan normal form of A, see [N. Higham, Functions of Matrices, Sec. 1.2] for details.
def matrix_function_Jordan(A, f):
# returns jordan matrix J and invertible matrix P such that A = P*J*~P
[J, P] = A.jordan_form(transformation=True)
fJ = zero_matrix(SR, J.ncols())
num_Jordan_blocks = 1+len(J.subdivisions()[0])
fJ.subdivide(J.subdivisions())
for k in range(num_Jordan_blocks):
# get Jordan block Jk
Jk = J.subdivision(k, k)
# dimension of Jordan block Jk
mk = Jk.ncols();
fJk = zero_matrix(SR, mk, mk)
# compute the first row of f(Jk)
vk = [f.derivative(x, i)(Jk[i][i])/factorial(i) for i in range(mk)]
# insert vk into each row (above the main diagonal)
for i in range(mk):
row_Jk_i = vector(SR, zero_vector(SR, i).list() + vk[0:mk-i])
fJk.set_row(i, row_Jk_i)
fJ.set_block(k, k, fJk)
fA = P*fJ*~P
return fA
First, we define our matrix function:
sage: var('k'); pow_sym(x) = x^k
Let's consider the OP's example, a 2×2 matrix with one Jordan block of size 2:
sage: A = matrix(QQ, [[2, -1], [1, 0]])
This matrix is not diagonalizable.
sage: A.is_diagonalizable()
sage: False
Calling our function,
sage: matrix_function_Jordan(A, pow_sym)
gives (k+1−kk−k+1).
A limitation of this approach is that we need the Jordan form. Notice that it is only implemented for exact rings (the Jordan form is unstable for inexact rings). Moreover, the spectrum should belong to the same ring. Consider for instance:
sage: A = matrix(QQ, [[0,4,1],[-1,1,5],[2,0,-89]]);
sage: [J, P] = A.jordan_form(transformation=True)
gives:
RuntimeError: Some eigenvalue does not exist in Rational Field.
This is not a piece of cake for WolframAlpha either: we get Standard computation time exceeded...
Thanks for this good answer ! I have some comments and improvements, but they are too big to enter as a comment so i made a new answer. On this problem, Sage shows a better behaviour than WolframAlpha ;)
What answer shall I choose as correct? Both are correct.
In many cases the following works:
A=matrix(SR,[[1,2],[2,1]])
D,P=A.eigenmatrix_right()
n=var('n')
An=P*matrix(SR,[[D[0,0]^n,0],[0,D[1,1]^n]])*P.inverse()
An
Here one obtains An as follows:
[1/2*3^n + 1/2*(-1)^n 1/2*3^n - 1/2*(-1)^n]
[1/2*3^n - 1/2*(-1)^n 1/2*3^n + 1/2*(-1)^n].
It does not work if the given matrix is not similar to a diagonal matrix.
Thanks, but unfortunately your method does not work in my case. I cannot imagine that the mighty Sage is not able to manage such easy calculations. Where are the Sage experts?
Well, this is not so bad, as it shows us the direction to look at, since Jordan form can be seen as a generalization of diagonalization !
You are right, of course.
EDIT : this feature should be soon part of Sage, see trac ticket 22523 !
Let me complement @mforets answer.
First, we can bypass the pitfall by working in the field of algebraic numbers, QQbar
, which is both exact and algebraically closed:
sage: A = matrix(QQ, [[2,1],[1,1]])
sage: matrix_function_Jordan(A, pow_sym)
RuntimeError: Some eigenvalue does not exist in Rational Field.
But:
sage: A = A.change_ring(QQbar)
sage: matrix_function_Jordan(A, pow_sym)
[0.7236067977499790?*2.618033988749895?^k + 0.2763932022500211?*0.3819660112501051?^k 0.4472135954999580?*2.618033988749895?^k - 0.4472135954999580?*0.3819660112501051?^k]
[0.4472135954999580?*2.618033988749895?^k - 0.4472135954999580?*0.3819660112501051?^k 0.2763932022500211?*2.618033988749895?^k + 0.7236067977499790?*0.3819660112501051?^k]
Second, you may not be happy with such a representation of the result, whose entries belong to the symbolic ring bundled with algerbraic numbers (written in a non-symbolic form).
However, some algebraic numbers have a symbolic representation by radicals. So we can use that representation of algebraic entries of the jordan form before mixing them with symbolic expressions. Let me propose the following change in @mforets code (my comments begin with ##
):
def matrix_function_Jordan(A, f):
# returns jordan matrix J and invertible matrix P such that A = P*J*~P
## We change the matrix into a matrix on the field of algebraic numbers
[J, P] = A.change_ring(QQbar).jordan_form(transformation=True);
fJ = zero_matrix(SR, J.ncols())
num_Jordan_blocks = 1+len(J.subdivisions()[0])
fJ.subdivide(J.subdivisions());
for k in range(num_Jordan_blocks):
# get Jordan block Jk
Jk = J.subdivision(k, k)
# dimension of Jordan block Jk
mk = Jk.ncols();
fJk = zero_matrix(SR, mk, mk);
# compute the first row of f(Jk)
## Before applying a symbolic function to the coefficients of J, we change them into symbolic expressions
vk = [f.derivative(x, i)(Jk[i][i].radical_expression())/factorial(i) for i in range(mk)]
# insert vk into each row (above the main diagonal)
for i in range(mk):
row_Jk_i = vector(SR, zero_vector(SR, i).list() + vk[0:mk-i])
fJk.set_row(i, row_Jk_i)
fJ.set_block(k, k, fJk)
## We change the entries of P and P^-1 into symbolic expressions
Psym = P.apply_map(AlgebraicNumber.radical_expression)
Psyminv = (~P).apply_map(AlgebraicNumber.radical_expression)
fA = Psym*fJ*Psyminv
return fA
Now, we have:
sage: A = matrix(QQ, [[2,1],[1,1]])
sage: matrix_function_Jordan(A, pow_sym)
[ 1/10*(1/2*sqrt(5) + 3/2)^k*(sqrt(5) + 5) - 1/10*(-1/2*sqrt(5) + 3/2)^k*(sqrt(5) - 5) 1/5*sqrt(5)*(1/2*sqrt(5) + 3/2)^k - 1/5*sqrt(5)*(-1/2*sqrt(5) + 3/2)^k]
[1/20*(1/2*sqrt(5) + 3/2)^k*(sqrt(5) + 5)*(sqrt(5) - 1) + 1/20*(-1/2*sqrt(5) + 3/2)^k*(sqrt(5) + 1)*(sqrt(5) - 5) 1/10*sqrt(5)*(-1/2*sqrt(5) + 3/2)^k*(sqrt(5) + 1) + 1/10*sqrt(5)*(1/2*sqrt(5) + 3/2)^k*(sqrt(5) - 1)]
Typeset:
(110(12√5+32)k(√5+5)−110(−12√5+32)k(√5−5)15√5(12√5+32)k−15√5(−12√5+32)k120(12√5+32)k(√5+5)(√5−1)+120(−12√5+32)k(√5+1)(√5−5)110√5(−12√5+32)k(√5+1)+110√5(12√5+32)k(√5−1))
The function also works with matrix(QQ, [[0,4,1],[-1,1,5],[2,0,-89]])
but the result is so huge that your browser will not like it:
(−1106517166(53258583(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13(i√3+1)+−1154521i√3+1154521(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13−35505722)(−12(23√17752861−72021727)13(i√3+1)−−8005i√3+800518(23√17752861−72021727)13−883)k−1106517166(53258583(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13(−i√3+1)+1154521i√3+1154521(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13−35505722)(−12(23√17752861−72021727)13(−i√3+1)−8005i√3+800518(23√17752861−72021727)13−883)k+153258583((23√17752861−72021727)13+80059(23√17752861−72021727)13−883)k(53258583(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13+1154521(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13+17752861)−1319551498(159775749(51521123556821580961113270008463041√114184979435291050069−407608369)13(i√3+1)+2(32133662i√3−32133662)(51521123556821580961113270008463041√114184979435291050069−407608369)13)(−12(23√17752861−72021727)13(i√3+1)−−8005i√3+800518(23√17752861−72021727)13−883)k−1319551498(159775749(51521123556821580961113270008463041√114184979435291050069−407608369)13(−i√3+1)+2(−32133662i√3−32133662)(51521123556821580961113270008463041√114184979435291050069−407608369)13)((23√17752861−72021727)13+80059(23√17752861−72021727)13−883)k+1159775749(−12(23√17752861−72021727)13(−i√3+1)−8005i√3+800518(23√17752861−72021727)13−883)k(159775749(51521123556821580961113270008463041√114184979435291050069−407608369)13−64267324(51521123556821580961113270008463041√114184979435291050069−407608369)13)−1319551498(159775749(8771691721580961113270008463041√114184979435291050069−402536123)13(i√3+1)−−188329i√3+188329(8771691721580961113270008463041√114184979435291050069−402536123)13)(−12(23√17752861−72021727)13(i√3+1)−−8005i√3+800518(23√17752861−72021727)13−883)k−1319551498(159775749(8771691721580961113270008463041√114184979435291050069−402536123)13(−i√3+1)−188329i√3+188329(8771691721580961113270008463041√114184979435291050069−402536123)13)((23√17752861−72021727)13+80059(23√17752861−72021727)13−883)k+1159775749(−12(23√17752861−72021727)13(−i√3+1)−8005i√3+800518(23√17752861−72021727)13−883)k(159775749(8771691721580961113270008463041√114184979435291050069−402536123)13−188329(8771691721580961113270008463041√114184979435291050069−402536123)13)19394814041200(53258583(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13(i√3+1)+−1154521i√3+1154521(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13−35505722)(44100(11225√2536123√7+877169179261000)13(−i√3+1)+188329i√3+188329(11225√2536123√7+877169179261000)13−194460)(−12(23√17752861−72021727)13(i√3+1)−−8005i√3+800518(23√17752861−72021727)13−883)k−14697407020600(53258583(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13(−i√3+1)+1154521i√3+1154521(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13−35505722)(−12(23√17752861−72021727)13(−i√3+1)−8005i√3+800518(23√17752861−72021727)13−883)k(44100(11225√2536123√7+877169179261000)13+188329(11225√2536123√7+877169179261000)13+97230)−14697407020600(44100(11225√2536123√7+877169179261000)13(i√3+1)+−188329i√3+188329(11225√2536123√7+877169179261000)13−194460)((23√17752861−72021727)13+80059(23√17752861−72021727)13−883)k(53258583(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13+1154521(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13+17752861)128184442123600(159775749(51521123556821580961113270008463041√114184979435291050069−407608369)13(i√3+1)+2(32133662i√3−32133662)(51521123556821580961113270008463041√114184979435291050069−407608369)13)(44100(11225√2536123√7+877169179261000)13(−i√3+1)+188329i√3+188329(11225√2536123√7+877169179261000)13−194460)(−12(23√17752861−72021727)13(i√3+1)−−8005i√3+800518(23√17752861−72021727)13−883)k+128184442123600(44100(11225√2536123√7+877169179261000)13(i√3+1)+−188329i√3+188329(11225√2536123√7+877169179261000)13−194460)(159775749(51521123556821580961113270008463041√114184979435291050069−407608369)13(−i√3+1)+2(−32133662i√3−32133662)(51521123556821580961113270008463041√114184979435291050069−407608369)13)((23√17752861−72021727)13+80059(23√17752861−72021727)13−883)k+17046110530900(−12(23√17752861−72021727)13(−i√3+1)−8005i√3+800518(23√17752861−72021727)13−883)k(44100(11225√2536123√7+877169179261000)13+188329(11225√2536123√7+877169179261000)13+97230)(159775749(51521123556821580961113270008463041√114184979435291050069−407608369)13−64267324(51521123556821580961113270008463041√114184979435291050069−407608369)13)128184442123600(159775749(8771691721580961113270008463041√114184979435291050069−402536123)13(i√3+1)−−188329i√3+188329(8771691721580961113270008463041√114184979435291050069−402536123)13)(44100(11225√2536123√7+877169179261000)13(−i√3+1)+188329i√3+188329(11225√2536123√7+877169179261000)13−194460)(−12(23√17752861−72021727)13(i√3+1)−−8005i√3+800518(23√17752861−72021727)13−883)k+128184442123600(44100(11225√2536123√7+877169179261000)13(i√3+1)+−188329i√3+188329(11225√2536123√7+877169179261000)13−194460)(159775749(8771691721580961113270008463041√114184979435291050069−402536123)13(−i√3+1)−188329i√3+188329(8771691721580961113270008463041√114184979435291050069−402536123)13)((23√17752861−72021727)13+80059(23√17752861−72021727)13−883)k+17046110530900(−12(23√17752861−72021727)13(−i√3+1)−8005i√3+800518(23√17752861−72021727)13−883)k(44100(11225√2536123√7+877169179261000)13+188329(11225√2536123√7+877169179261000)13+97230)(159775749(8771691721580961113270008463041√114184979435291050069−402536123)13−188329(8771691721580961113270008463041√114184979435291050069−402536123)13)12348703510300(11025(13675√2536123√7−644014044461157625)13(−i√3+1)+16066831i√3+16066831(13675√2536123√7−644014044461157625)13+841260)(53258583(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13(−i√3+1)+1154521i√3+1154521(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13−35505722)(−12(23√17752861−72021727)13(−i√3+1)−8005i√3+800518(23√17752861−72021727)13−883)k−11174351755150(53258583(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13(i√3+1)+−1154521i√3+1154521(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13−35505722)(−12(23√17752861−72021727)13(i√3+1)−−8005i√3+800518(23√17752861−72021727)13−883)k(11025(13675√2536123√7−644014044461157625)13+16066831(13675√2536123√7−644014044461157625)13−420630)−11174351755150(11025(13675√2536123√7−644014044461157625)13(i√3+1)+−16066831i√3+16066831(13675√2536123√7−644014044461157625)13+841260)((23√17752861−72021727)13+80059(23√17752861−72021727)13−883)k(53258583(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13+1154521(1404864529721580961113270008463041√114184979435291050069−3668486479327247)13+17752861)17046110530900(11025(13675√2536123√7−644014044461157625)13(i√3+1)+−16066831i√3+16066831(13675√2536123√7−644014044461157625)13+841260)(159775749(51521123556821580961113270008463041√114184979435291050069−407608369)13(−i√3+1)+2(−32133662i√3−32133662)(51521123556821580961113270008463041√114184979435291050069−407608369)13)((23√17752861−72021727)13+80059(23√17752861−72021727)13−883)k−13523055265450(159775749(51521123556821580961113270008463041√114184979435291050069−407608369)13(i√3+1)+2(32133662i√3−32133662)(51521123556821580961113270008463041√114184979435291050069−407608369)13)(−12(23√17752861−72021727)13(i√3+1)−−8005i√3+800518(23√17752861−72021727)13−883)k(11025(13675√2536123√7−644014044461157625)13+16066831(13675√2536123√7−644014044461157625)13−420630)−13523055265450(11025(13675√2536123√7−644014044461157625)13(−i√3+1)+16066831i√3+16066831(13675√2536123√7−644014044461157625)13+841260)(−12(23√17752861−72021727)13(−i√3+1)−8005i√3+800518(23√17752861−72021727)13−883)k(159775749(51521123556821580961113270008463041√114184979435291050069−407608369)13−64267324(51521123556821580961113270008463041√114184979435291050069−407608369)13)17046110530900(11025(13675√2536123√7−644014044461157625)13(i√3+1)+−16066831i√3+16066831(13675√2536123√7−644014044461157625)13+841260)(159775749(8771691721580961113270008463041√114184979435291050069−402536123)13(−i√3+1)−188329i√3+188329(8771691721580961113270008463041√114184979435291050069−402536123)13)((23√17752861−72021727)13+80059(23√17752861−72021727)13−883)k−13523055265450(159775749(8771691721580961113270008463041√114184979435291050069−402536123)13(i√3+1)−−188329i√3+188329(8771691721580961113270008463041√114184979435291050069−402536123)13)(−12(23√17752861−72021727)13(i√3+1)−−8005i√3+800518(23√17752861−72021727)13−883)k(11025(13675√2536123√7−644014044461157625)13+16066831(13675√2536123√7−644014044461157625)13−420630)−13523055265450(11025(13675√2536123√7−644014044461157625)13(−i√3+1)+16066831i√3+16066831(13675√2536123√7−644014044461157625)13+841260)(−12(23√17752861−72021727)13(−i√3+1)−8005i√3+800518(23√17752861−72021727)13−883)k(159775749(8771691721580961113270008463041√114184979435291050069−402536123)13−188329(8771691721580961113270008463041√114184979435291050069−402536123)13))
Great answer so far, but is there a way to deal with matrices containing non algebraic numbers like pi? What about {{pi,-1},{1,0}}^k?
@Tommy Angelo: Notice that such matrix can be dealt with the original matrix_function_Jordan(A, f)
but with A = matrix(SR, [[pi, -1],[1, 0]])
(just use SR instead of QQ). My observation is that although this trick works quite generally (even so when the matrix has complex eigenvalues), the quality of the answer by @tmonteil's approach will be far better than using "plain" SR (and with wolframalpha, as you can check with some toy examples). Here by quality I mean simplified formulas (as well as a smaller computational time), so this is crucial in practice.
Asked: 8 years ago
Seen: 3,230 times
Last updated: Mar 06 '17
Related: