-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add error & test checks for matrix_operations.py #925
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
cclauss
merged 4 commits into
TheAlgorithms:master
from
StephenGemin:update_matrix_operations
Jul 20, 2019
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[pytest] | ||
markers = | ||
mat_ops: tests for matrix operations | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
""" | ||
Testing here assumes that numpy and linalg is ALWAYS correct!!!! | ||
|
||
If running from PyCharm you can place the following line in "Additional Arguments" for the pytest run configuration | ||
-vv -m mat_ops -p no:cacheprovider | ||
""" | ||
|
||
# standard libraries | ||
import sys | ||
import numpy as np | ||
import pytest | ||
import logging | ||
|
||
# Custom/local libraries | ||
from matrix import matrix_operation as matop | ||
|
||
mat_a = [[12, 10], [3, 9]] | ||
mat_b = [[3, 4], [7, 4]] | ||
mat_c = [[3, 0, 2], [2, 0, -2], [0, 1, 1]] | ||
mat_d = [[3, 0, -2], [2, 0, 2], [0, 1, 1]] | ||
mat_e = [[3, 0, 2], [2, 0, -2], [0, 1, 1], [2, 0, -2]] | ||
mat_f = [1] | ||
mat_h = [2] | ||
|
||
logger = logging.getLogger() | ||
logger.level = logging.DEBUG | ||
stream_handler = logging.StreamHandler(sys.stdout) | ||
logger.addHandler(stream_handler) | ||
|
||
|
||
@pytest.mark.mat_ops | ||
@pytest.mark.parametrize(('mat1', 'mat2'), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), | ||
(mat_f, mat_h)]) | ||
def test_addition(mat1, mat2): | ||
if (np.array(mat1)).shape < (2, 2) or (np.array(mat2)).shape < (2, 2): | ||
with pytest.raises(TypeError): | ||
logger.info(f"\n\t{test_addition.__name__} returned integer") | ||
matop.add(mat1, mat2) | ||
elif (np.array(mat1)).shape == (np.array(mat2)).shape: | ||
logger.info(f"\n\t{test_addition.__name__} with same matrix dims") | ||
act = (np.array(mat1) + np.array(mat2)).tolist() | ||
theo = matop.add(mat1, mat2) | ||
assert theo == act | ||
else: | ||
with pytest.raises(ValueError): | ||
logger.info(f"\n\t{test_addition.__name__} with different matrix dims") | ||
matop.add(mat1, mat2) | ||
|
||
|
||
@pytest.mark.mat_ops | ||
@pytest.mark.parametrize(('mat1', 'mat2'), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), | ||
(mat_f, mat_h)]) | ||
def test_subtraction(mat1, mat2): | ||
if (np.array(mat1)).shape < (2, 2) or (np.array(mat2)).shape < (2, 2): | ||
with pytest.raises(TypeError): | ||
logger.info(f"\n\t{test_subtraction.__name__} returned integer") | ||
matop.subtract(mat1, mat2) | ||
elif (np.array(mat1)).shape == (np.array(mat2)).shape: | ||
logger.info(f"\n\t{test_subtraction.__name__} with same matrix dims") | ||
act = (np.array(mat1) - np.array(mat2)).tolist() | ||
theo = matop.subtract(mat1, mat2) | ||
assert theo == act | ||
else: | ||
with pytest.raises(ValueError): | ||
logger.info(f"\n\t{test_subtraction.__name__} with different matrix dims") | ||
assert matop.subtract(mat1, mat2) | ||
|
||
|
||
@pytest.mark.mat_ops | ||
@pytest.mark.parametrize(('mat1', 'mat2'), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), | ||
(mat_f, mat_h)]) | ||
def test_multiplication(mat1, mat2): | ||
if (np.array(mat1)).shape < (2, 2) or (np.array(mat2)).shape < (2, 2): | ||
logger.info(f"\n\t{test_multiplication.__name__} returned integer") | ||
with pytest.raises(TypeError): | ||
matop.add(mat1, mat2) | ||
elif (np.array(mat1)).shape == (np.array(mat2)).shape: | ||
logger.info(f"\n\t{test_multiplication.__name__} meets dim requirements") | ||
act = (np.matmul(mat1, mat2)).tolist() | ||
theo = matop.multiply(mat1, mat2) | ||
assert theo == act | ||
else: | ||
with pytest.raises(ValueError): | ||
logger.info(f"\n\t{test_multiplication.__name__} does not meet dim requirements") | ||
assert matop.subtract(mat1, mat2) | ||
|
||
|
||
@pytest.mark.mat_ops | ||
def test_scalar_multiply(): | ||
act = (3.5 * np.array(mat_a)).tolist() | ||
theo = matop.scalar_multiply(mat_a, 3.5) | ||
assert theo == act | ||
|
||
|
||
@pytest.mark.mat_ops | ||
def test_identity(): | ||
act = (np.identity(5)).tolist() | ||
theo = matop.identity(5) | ||
assert theo == act | ||
|
||
|
||
@pytest.mark.mat_ops | ||
@pytest.mark.parametrize('mat', [mat_a, mat_b, mat_c, mat_d, mat_e, mat_f]) | ||
def test_transpose(mat): | ||
if (np.array(mat)).shape < (2, 2): | ||
with pytest.raises(TypeError): | ||
logger.info(f"\n\t{test_transpose.__name__} returned integer") | ||
matop.transpose(mat) | ||
else: | ||
act = (np.transpose(mat)).tolist() | ||
theo = matop.transpose(mat, return_map=False) | ||
assert theo == act |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.