mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
4e0717c3cf
* Update matrix_operation.py 1. Adding error checks for integer inputs 2. Adding error checks for matrix operations where size requirements do not match up 3. Added matrix subtraction function 4. included error check so only integer is passed into identity function * Create test_matrix_operation.py * Update matrix_ops and Add Test Cases 1. Included error checks in matrix operation. There were some cases where the functions would not work correctly. 2. PEP8 changes to matrix_operations.py 3. added test cases for matrix operations using pytest. * Update pytest.ini Add carriage return to end of file
113 lines
4.0 KiB
Python
113 lines
4.0 KiB
Python
"""
|
|
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
|