Skip to content

Added determinate function #1429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion linear_algebra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ This module contains some useful classes and functions for dealing with linear a
- changeComponent(x,y,value) : changes the specified component.
- component(x,y) : returns the specified component.
- width() : returns the width of the matrix
- height() : returns the height of the matrix
- height() : returns the height of the matrix
- det() : returns the determinate of the matrix if it is square
- operator + : implements the matrix-addition.
- operator - _ implements the matrix-subtraction

Expand Down
27 changes: 27 additions & 0 deletions linear_algebra/src/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,33 @@ def height(self):
"""
return self.__height

def det(self):
"""
returns the determinate of an nxn matrix using Laplace expansion
"""
if self.__height == self.__width and self.__width >= 2:
total = 0
if self.__width > 2:
for x in range(0, self.__width):
for y in range(0, self.__height):
total += (
self.__matrix[x][y]
* (-1) ** (x + y)
* Matrix(
self.__matrix[0:x] + self.__matrix[x + 1 :],
self.__width - 1,
self.__height - 1,
).det()
)
else:
return (
self.__matrix[0][0] * self.__matrix[1][1]
- self.__matrix[0][1] * self.__matrix[1][0]
)
return total
else:
raise Exception("matrix is not square")

def __mul__(self, other):
"""
implements the matrix-vector multiplication.
Expand Down
7 changes: 7 additions & 0 deletions linear_algebra/src/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ def test_str_matrix(self):
A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n", str(A))

def test_det(self):
"""
test for det()
"""
A = Matrix([[1, 1, 4, 5], [3, 3, 3, 2], [5, 1, 9, 0], [9, 7, 7, 9]], 4, 4)
self.assertEqual(-376, A.det())

Copy link
Member

@cclauss cclauss Oct 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the following function to the bottom of this file just above line 160.

def force_test() -> None:
    """
    This will ensure that pytest runs the unit tests above.
    >>> unittest.main()
    """
    pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can see that our tests are not being run:
https://travis-ci.com/TheAlgorithms/Python/builds/133339192#L386

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

def test__mul__matrix(self):
A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3, 3)
x = Vector([1, 2, 3])
Expand Down