Write your code: Minor of Matrix
Contents
Write your code: Minor of Matrix#
Write a function that returns the Minor of Matrix of a 3x3 Matrix for a given line \((i)\) and column \((j)\)
Rules#
Function returns a Numpy ndarray of shape=2x2
Function should work for all possible \(i,j\) combination
Solution#
import numpy as np
def get_minor_ij(A, i, j):
# write your code here
File "/tmp/ipykernel_1917/1952751800.py", line 3
# write your code here
^
SyntaxError: unexpected EOF while parsing
A = np.array([[ 9, 12, 18],
[ 2, -2, 5],
[11,-17, 19]]
)
get_minor_ij(A, 2, 3)
Testing#
Check if your function returns the expected value using the cell below.
import unittest
class UnitTests(unittest.TestCase):
def setUp(self):
self.A = np.array([[ -7, 4, -2],
[ 2, -3, 5],
[-18, 10, 0]]
)
def test_type(self):
self.assertTrue(isinstance(get_minor_ij(A, 1, 2), np.ndarray), 'The function should return a NumPy array')
def test_shape(self):
self.assertTrue(get_minor_ij(self.A, 1, 2).shape == (2, 2), "The function should return an 2x2 array")
def test_minor11(self):
self.assertEqual(get_minor_ij(self.A, 1, 1).tolist(), [[-3, 5], [10,0]])
def test_minor12(self):
self.assertEqual(get_minor_ij(self.A, 1, 2).tolist(), [[2,5], [-18,0]])
def test_minor13(self):
self.assertEqual(get_minor_ij(self.A, 1, 3).tolist(), [[2,-3],[-18,10]])
def test_minor21(self):
self.assertEqual(get_minor_ij(self.A, 2, 1).tolist(), [[4,-2],[10,0]])
def test_minor22(self):
self.assertEqual(get_minor_ij(self.A, 2, 2).tolist(), [[-7,-2],[-18,0]])
def test_minor23(self):
self.assertEqual(get_minor_ij(self.A, 2, 3).tolist(), [[-7,4],[-18,10]])
def test_minor31(self):
self.assertEqual(get_minor_ij(self.A, 3, 1).tolist(), [[4,-2],[-3,5]])
def test_minor32(self):
self.assertEqual(get_minor_ij(self.A, 3, 2).tolist(), [[-7,-2],[2, 5]])
def test_minor33(self):
self.assertEqual(get_minor_ij(self.A, 3, 3).tolist(), [[-7,4],[2,-3]])
unittest.main(argv=[''], verbosity=2,exit=False)