Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use python 3 #!/usr/bin/python3 \'\'\'CPSC 254 - Assignment 09\'\'\' from test i

ID: 3758822 • Letter: U

Question

use python 3

#!/usr/bin/python3

'''CPSC 254 - Assignment 09'''

from test import test


def identity_mat(num):
'''Let a list of lists of numbers denote a matrix. Given an integer number,
return an identity matrix of size n, which is the n x n matrix with ones on
the main diagnal (top left to bottom right) and zeros elsewhere.

YOU MUST USE LIST COMPREHENSION.

For example: identity_mat(3) = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]

Hint:
* One is where row index is same as column index.
* You can cast a boolean value (True or False) into integer (0 or 1,
respectively)

Keyword arguments:
num -- size of the identity matrix

Return: list of lists of numbers
'''
# +++ADD code here+++


def main():
'''Provides some tests for identity_mat function.
'''
print('identity matrix')
test(identity_mat(1), [[1]])
test(identity_mat(2), [[1, 0], [0, 1]])
test(identity_mat(3), [[1, 0, 0], [0, 1, 0], [0, 0, 1]])


if __name__ == '__main__':
main()

Explanation / Answer

sage: A = matrix(QQ, [[-68,   69, -27, -11, -65,   9, -181, -32],
...                   [-52,   52, -27, -8, -52, -16, -133, -14],
...                   [ 92, -97, 47, 14, 90, 32, 241, 18],
...                   [139, -144, 60, 18, 148, -10, 362, 77],
...                   [ 40, -41, 12,   6, 45, -24, 105, 42],
...                   [-46,   48, -20, -7, -47,   0, -122, -22],
...                   [-26,   27, -13, -4, -29, -6, -66, -14],
...                   [-33,   34, -13, -5, -35,   7, -87, -23]])
sage: Z, U = A.matrix1(transformation=True)
sage: Z
[ 0   0   0 40| 1   0| 0   0]
[ 1   0   0 52| 0   0| 0   0]
[ 0   1   0 18| 0   0| 0   0]
[ 0   0   1 -1| 0   0| 0   0]
[---------------+-------+-------]
[ 0   0   0   0| 0   1| 0   0]
[ 0   0   0   0|-25 10| 0   0]
[---------------+-------+-------]
[ 0   0   0   0| 1   0| 0 -4]
[ 0   0   0   0| 0   0| 1 -4]
sage: U.inverse()*A*U == Z
True

sage: B = matrix(QQ, [[ 16, 69, -13,   2, -52, 143,   90, -3],
...                   [ 26, 54,   6, -5, -28,   73,   73, -48],
...                   [-16, -79, 12, -10, 64, -142, -115, 41],
...                   [ 27, -7, 21, -33, 39, -20, -42, 43],
...                   [ 8, -75, 34, -32, 86, -156, -130, 42],
...                   [ 2, -17,   7, -8, 20, -33, -31, 16],
...                   [-24, -80,   7, -3, 56, -136, -112, 42],
...                   [ -6, -19,   0, -1, 13, -28, -27, 15]])
sage: Z, U = B.matrix1(transformation=True)
sage: Z
[   0    0    0    0    0 1000|   0|   0]
[   1    0    0    0    0 900|   0|   0]
[   0    1    0    0    0 -30|   0|   0]
[   0    0    1    0    0 -153|   0|   0]
[   0    0    0    1    0    3|   0|   0]
[   0    0    0    0    1    9|   0|   0]
[-----------------------------+----+----]
[   0    0    0    0    0    0| -2|   0]
[-----------------------------+----+----]
[   0    0    0    0    0    0|   1| -2]
sage: U.inverse()*B*U == Z
True

sage: A.jordan_form() == B.jordan_form()
True