Skip to content

Commit

Permalink
Merge 49bc761 into 43ca7b7
Browse files Browse the repository at this point in the history
  • Loading branch information
ayush9398 committed Apr 15, 2019
2 parents 43ca7b7 + 49bc761 commit 08da432
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
12 changes: 12 additions & 0 deletions tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,15 @@ def test_determinant():
for i in mat.determinant():
a += i.__str__()
assert a == "{1.0}"


def test_cofactor():
mat = getTokens('[1,2;3,4]')
if mat.isSquare():
assert str(mat.cofactor()) == '[{4.0},{-3.0};{-2.0},{1.0}]'
mat = getTokens('[1,2,3;0,4,5;1,0,6]')
if mat.isSquare():
assert str(mat.cofactor()) == '[{24.0},{5.0},{-4.0};{-12.0},{3.0},{2.0};{-2.0},{-5.0},{4.0}]'
mat = getTokens('[1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16]')
if mat.isSquare():
assert str(mat.cofactor()) == '[{0},{0},{0},{0};{0},{0},{0},{0};{0},{0},{0},{0};{0},{0},{0},{0}]'
28 changes: 25 additions & 3 deletions visma/matrix/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ def isDiagonal(self):
def inverse(self):
pass

def cofactor(self):
pass

def dimension(self):
"""Gets the dimension of the matrix
Expand Down Expand Up @@ -196,6 +193,31 @@ def traceMat(self):
trace, _, _, _, _ = simplify(trace)
return trace

def cofactor(self):
"""Calculates cofactors matrix of the Square Matrix
Returns:
An object of type SquareMat
"""
mat1 = SquareMat()
mat1.value = []
for i in range(self.dim[0]):
if(i % 2 == 0):
coeff = -1
else:
coeff = 1
mat1.value.append([])
for j in range(self.dim[1]):
coeff *= -1
mat = SquareMat()
temp = np.array(self.value)
mat.value = np.concatenate((np.concatenate((temp[:i, :j], temp[i+1:, :j])), np.concatenate((temp[:i, j+1:], temp[i+1:, j+1:]))), axis=1).tolist()
val = mat.determinant()[0]
val.value *= coeff
mat1.value[i].append([val])
mat1.dimension()
return mat1


class DiagMat(SquareMat):
"""Class for Diagonal matrix
Expand Down

0 comments on commit 08da432

Please sign in to comment.