-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
added support for inverse of 3x3 matrix #7355
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
Conversation
matrix/inverse_of_matrix.py
Outdated
... | ||
ValueError: This matrix has no inverse. | ||
|
||
More examples: | ||
|
||
>>> inverse_of_matrix([]) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Please provide a matrix of size 2x2 or 3x3. | ||
|
||
>>> inverse_of_matrix([[],[]]) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Please provide a matrix of size 2x2 or 3x3. | ||
|
||
>>> inverse_of_matrix([[1, 2], [3, 4], [5, 6]]) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Please provide a matrix of size 2x2 or 3x3. | ||
|
||
>>> inverse_of_matrix([[1, 2, 1], [0,3, 4]]) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Please provide a matrix of size 2x2 or 3x3. | ||
|
||
>>> inverse_of_matrix([[1, 2, 3], [7, 8, 9], [7, 8, 9]]) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: This matrix has no inverse. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should test that all lines between Traceback
and the Error are indented by at least four spaces.
... | |
ValueError: This matrix has no inverse. | |
More examples: | |
>>> inverse_of_matrix([]) | |
Traceback (most recent call last): | |
... | |
ValueError: Please provide a matrix of size 2x2 or 3x3. | |
>>> inverse_of_matrix([[],[]]) | |
Traceback (most recent call last): | |
... | |
ValueError: Please provide a matrix of size 2x2 or 3x3. | |
>>> inverse_of_matrix([[1, 2], [3, 4], [5, 6]]) | |
Traceback (most recent call last): | |
... | |
ValueError: Please provide a matrix of size 2x2 or 3x3. | |
>>> inverse_of_matrix([[1, 2, 1], [0,3, 4]]) | |
Traceback (most recent call last): | |
... | |
ValueError: Please provide a matrix of size 2x2 or 3x3. | |
>>> inverse_of_matrix([[1, 2, 3], [7, 8, 9], [7, 8, 9]]) | |
Traceback (most recent call last): | |
... | |
ValueError: This matrix has no inverse. | |
... | |
ValueError: This matrix has no inverse. | |
>>> inverse_of_matrix([]) | |
Traceback (most recent call last): | |
... | |
ValueError: Please provide a matrix of size 2x2 or 3x3. | |
>>> inverse_of_matrix([[],[]]) | |
Traceback (most recent call last): | |
... | |
ValueError: Please provide a matrix of size 2x2 or 3x3. | |
>>> inverse_of_matrix([[1, 2], [3, 4], [5, 6]]) | |
Traceback (most recent call last): | |
... | |
ValueError: Please provide a matrix of size 2x2 or 3x3. | |
>>> inverse_of_matrix([[1, 2, 1], [0,3, 4]]) | |
Traceback (most recent call last): | |
... | |
ValueError: Please provide a matrix of size 2x2 or 3x3. | |
>>> inverse_of_matrix([[1, 2, 3], [7, 8, 9], [7, 8, 9]]) | |
Traceback (most recent call last): | |
... | |
ValueError: This matrix has no inverse. |
matrix/inverse_of_matrix.py
Outdated
adjoint_matrix = [ | ||
[d(0.0), d(0.0), d(0.0)], | ||
[d(0.0), d(0.0), d(0.0)], | ||
[d(0.0), d(0.0), d(0.0)], | ||
] | ||
for i in range(3): | ||
for j in range(3): | ||
adjoint_matrix[i][j] = cofactor_matrix[j][i] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adjoint_matrix = [ | |
[d(0.0), d(0.0), d(0.0)], | |
[d(0.0), d(0.0), d(0.0)], | |
[d(0.0), d(0.0), d(0.0)], | |
] | |
for i in range(3): | |
for j in range(3): | |
adjoint_matrix[i][j] = cofactor_matrix[j][i] | |
adjoint_matrix = zip(*cofactor_matrix) |
https://www.geeksforgeeks.org/transpose-matrix-single-line-python
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok sir.
matrix/inverse_of_matrix.py
Outdated
and len(matrix[2]) == 3 | ||
): | ||
# Calculate the determinant of the matrix using Sarrus rule | ||
determinant = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
determinant = ( | |
determinant = float( | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am on it sir.
@cclauss Sir, the zip() method involves a list of tuples. And in my code, every matrix was a list of lists. So I used NumPy.array instead of zip. Also, I don't know what error doctest is giving, can u help me with that? |
@poyea @cclauss @dhruvmanila Sir, please review this also. And suggest changes if required. 🥲 |
Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com> Co-authored-by: Chris O <46587501+ChrisO345@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I applied all code review suggestions.
Thanks for your persistence!!
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}
.