Write your code: Matrix Determinant
Contents
Write your code: Matrix Determinant#
Write functions that return the determinant of square matrix of orders (\(2\times2\)), (\(3\times3\)), and (\(n\times n\)).
Rules#
Function returns a integer.
You must not use any external function to calculate the determinant.
Useful Links#
Function Definition#
import numpy as np
def get_minor_ij(A, i, j):
M = np.delete(A, i-1, axis=0) # remove a row (axis=0)
M = np.delete(M, j-1, axis=1) # remove a column (axis=1)
return M
Order 2#
A = np.array([[ 2, 5],
[ 1, -3]]
)
# numpy determinant function:
round(np.linalg.det(A))
-11
def get_determinant_2(A):
# write your function here
get_determinant_2(A)
File "/tmp/ipykernel_1856/682844987.py", line 5
get_determinant_2(A)
^
IndentationError: expected an indented block
Order 3#
A = np.array([[ 9, 12, 18],
[ 2, -2, 5],
[11,-17, 19]]
)
# numpy determinant function:
round(np.linalg.det(A))
def get_determinant_3(A):
# write your function here
get_determinant_3(A)
Larger Orders: recursive solution#
A = np.array([[5, 2, 1, 4, 6],
[9, 4, 2, 5, 2],
[11, 5, 7, 3, 9],
[5, 6, 6, 7, 2],
[7, 5, 9, 3, 3]])
# numpy determinant function:
round(np.linalg.det(A))
def get_determinant_n(A):
# write your function here
get_determinant_n(A)