cs102 lab7
http://www.maths.nuigalway.ie/~gettrick/teach/cs102/labs/l7.html
-
For this lab you must submit all source code (python
files), and, if applicable, the result(s) of running the program on (a few)
test cases. This should be sent in via BLACKBOARD (not directly by email).
The source code must
be well presented (indenting, spaces, reasonable variable/function names,
etc.) and must include comments (as a rough guideline - aim to have nearly as
many comments as lines of code). Any questions asked should be answered by
typing the answers into the relevant box on the BLACKBOARD upload form.
-
This material should be uploaded
before
the deadline of 5pm
Friday March 8th, 2013. You will lose 20% for each day
(or part of day) the lab is late.
-
Plagiarism (the unattributed copying of work from other sources
(internet, fellow students,....)) will not be tolerated. Please see
http://www.nuigalway.ie/engineering/documents/plagiarism_guide_students
_v4.pdf. You risk getting zero for your lab if it is found to be
plagiarized.
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:
-
One which will just multiply two matrices and give the
result. The 2x2 matrices can be represented simply as
lists of lists.
-
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
-
A*An-1 (or A*binPow(A,n-1)) if n is odd.
- (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
-
The 8th power of [[2,1],[-2,-4]]
-
The 17th power of [[1,2],[1,-1]]
And submit these results with your program.
©
NUI, Galway