cs102 lab7


http://www.maths.nuigalway.ie/~gettrick/teach/cs102/labs/l7.html



Please first study the program http://www.maths.nuigalway.ie/~gettrick/teach/cs102/progs/binPow.py covered in lectures.
Use the idea of Binary Powering covered in lectures to write a python program which can calculate the nth power of a 2x2 matrix A. You should ask the user to input the 4 numbers in A, and then to input the requested power n. Your program should have two functions:
  1. One which will just multiply two matrices and give the result. The 2x2 matrices can be represented simply as lists of lists.
  2. A recursive function that takes two (2) arguments, the matrix and the exponent.
(Hint: Remember the idea in binary powering is to re-write An (or in python notation, say the function binPow(A,n)) as
  1. A*An-1 (or A*binPow(A,n-1)) if n is odd.
  2. (A2)n/2 (or binPow(A*A,n/2)) if n is even.
Then use the idea that binPow(A,1) is just A to terminate the recursion).
Test your program by calculating
  1. The 8th power of [[2,1],[-2,-4]]
  2. The 17th power of [[1,2],[1,-1]]
And submit these results with your program.

© NUI, Galway