# n-th power of matrices

Is there any way to calculate the n-th power of a (upper unitriangular) matrix in Sage? Here n is an integer variable.

For example, if y=matrix([[1,0,0],[0,1,1],[0,0,1]]), then I want to obtain a formula in n for y^n. In this case this would be y^n=matrix([[1,0,0],[0,1,n],[0,0,1]]).

I tried the following: sage: y=matrix([[1,0,0],[0,1,1],[0,0,1]]); var('n'); sage: y^n

This resulted in the error: "NotImplementedError: non-integral exponents not supported"

Adding sage: assume(n, 'integer') has no effect at all.

edit retag close merge delete

Sort by » oldest newest most voted

This was asked again in Ask Sage question 35658, and an initial implementation was proposed as an answer to that question, and included in SageMath at Sage Trac ticket 22523.

A bug was reported in Ask Sage question 41622 and a fix for this bug was provided as an answer to that question and is ready for inclusion in SageMath at Sage Trac ticket 25082.

This makes it possible to compute the matrix power in the question here.

sage: n = SR.var('n')
sage: y = matrix([[1, 0, 0], [0, 1, 1], [0, 0, 1]]); y
[1 0 0]
[0 1 1]
[0 0 1]
sage: z = y^n; z
[1 0 0]
[0 1 n]
[0 0 1]


Then one can substitute for particular values:

sage: z.subs({n: 5})
[1 0 0]
[0 1 5]
[0 0 1]
sage: y^5
[1 0 0]
[0 1 5]
[0 0 1]

more

You can find an interactive Sage application here: http://shrek.unideb.hu/~tengely/Magya... the last part is "Mátrixok n-edik hatványának zárt alakja", it is in Hungarian, but the mathematics and Sage command will help I think. In case of your example this code will not work, the eigenvalue 1 has multiplicity larger than 1. To make it work the computation related to the eigenvectors should be modified. At the above page the matrix is given by matrix([[1,2,1],[6,-1,0],[-1,-2,-1]]) and after pushing the "Számolás" button you see the general form of the $n$th power of this matrix. If you have a diagonalizable matrix the above code should do the job.

more

Unless i misunderstand you question, if M is your matrix and n is an integer, you can just do:

sage: M^n


or, if you prefer to be Python compatible:

sage: M**n

more

Thank you for answering. However, I think you misinterpreted my question. To be clear, in my question n is a variable (as stated), while the matrix I want to calculate the n-th power of is known. I reformulated my question.

( 2016-10-16 09:27:01 -0600 )edit

I see. The name variable could be misleading, since it can stand for a Python name, a polynomial indeterminate or a symbol. Thanks for having made your question more precise.

( 2016-10-16 12:00:21 -0600 )edit