Ask Your Question

n-th power of matrices

asked 2016-10-16 03:56:39 -0500

iris gravatar image

updated 2017-08-01 05:05:00 -0500

FrédéricC gravatar image

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 flag offensive close merge delete

3 answers

Sort by » oldest newest most voted

answered 2018-04-02 16:40:14 -0500

slelievre gravatar image

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]
edit flag offensive delete link more

answered 2016-10-16 15:39:14 -0500

castor gravatar image

updated 2016-10-16 16:00:57 -0500

You can find an interactive Sage application here: 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.

edit flag offensive delete link more

answered 2016-10-16 07:29:37 -0500

tmonteil gravatar image

updated 2016-10-16 07:34:03 -0500

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
edit flag offensive delete link 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.

iris gravatar imageiris ( 2016-10-16 09:27:01 -0500 )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.

tmonteil gravatar imagetmonteil ( 2016-10-16 12:00:21 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2016-10-16 03:56:39 -0500

Seen: 300 times

Last updated: Apr 02