-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from vprusso/improve-rand-tests
Improve rand tests
- Loading branch information
Showing
57 changed files
with
730 additions
and
698 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,44 +1,28 @@ | ||
"""Test inner_product.""" | ||
import numpy as np | ||
import pytest | ||
|
||
from toqito.matrix_ops import inner_product | ||
|
||
|
||
def test_inner_product(): | ||
"""Test with two vectors, no complications.""" | ||
|
||
v1, v2 = np.array([1, 2, 3]), np.array([4, 5, 6]) | ||
expected_res = 32 | ||
np.testing.assert_equal(inner_product(v1, v2), expected_res) | ||
|
||
|
||
def test_inner_product_negative_input(): | ||
"""Test with two vectors, with negative input value.""" | ||
|
||
v1, v2 = np.array([-1, 2, 3]), np.array([4, 5, 6]) | ||
expected_res = 24 | ||
np.testing.assert_equal(inner_product(v1, v2), expected_res) | ||
|
||
|
||
def test_inner_product_negative_output(): | ||
"""Test with two vectors, with negative expected output.""" | ||
|
||
v1, v2 = np.array([1, 2, -3]), np.array([4, 5, 6]) | ||
expected_res = -4 | ||
np.testing.assert_equal(inner_product(v1, v2), expected_res) | ||
|
||
|
||
def test_inner_product_different_dimensions(): | ||
"""Test with two vectors of different dimensions.""" | ||
|
||
v1, v2 = np.array([1, 2, 3]), np.array([4, 5, 6, 7]) | ||
with np.testing.assert_raises(ValueError): | ||
inner_product(v1, v2) | ||
|
||
|
||
def test_inner_product_different_dimensions_2(): | ||
"""Test with a vector and a 2d array.""" | ||
|
||
v1, v2 = np.array([1, 2, 3]), np.array([[4, 5, 6], [7, 8, 9]]) | ||
with np.testing.assert_raises(ValueError): | ||
@pytest.mark.parametrize("v1, v2, expected_result", [ | ||
# Test with two vectors, no complications. | ||
(np.array([1, 2, 3]), np.array([4, 5, 6]), 32), | ||
# Test with two vectors, with negative input value. | ||
(np.array([-1, 2, 3]), np.array([4, 5, 6]), 24), | ||
# Test with two vectors, with negative expected output. | ||
(np.array([1, 2, -3]), np.array([4, 5, 6]), -4), | ||
]) | ||
def test_inner_product(v1, v2, expected_result): | ||
assert inner_product(v1, v2) == expected_result | ||
|
||
|
||
@pytest.mark.parametrize("v1, v2", [ | ||
# Different dimensions of vectors. | ||
(np.array([1, 2, 3]), np.array([4, 5, 6, 7])), | ||
# Vector and 2D array. | ||
(np.array([1, 2, 3]), np.array([[4, 5, 6], [7, 8, 9]])), | ||
]) | ||
def test_inner_product_invalid_input(v1, v2): | ||
with pytest.raises(ValueError): | ||
inner_product(v1, v2) |
This file contains 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 |
---|---|---|
@@ -1,36 +1,26 @@ | ||
"""Test outer_product.""" | ||
import numpy as np | ||
import pytest | ||
|
||
from toqito.matrix_ops import outer_product | ||
|
||
|
||
def test_outer_product(): | ||
"""Test with two vectors, no complications.""" | ||
|
||
v1, v2 = np.array([1, 2, 3]), np.array([4, 5, 6]) | ||
expected_res = np.array([[4, 5, 6], [8, 10, 12], [12, 15, 18]]) | ||
np.testing.assert_equal(outer_product(v1, v2), expected_res) | ||
|
||
|
||
def test_outer_product_negative(): | ||
"""Test with two vectors, with negative input/output values.""" | ||
|
||
v1, v2 = np.array([-1, 2, 3]), np.array([4, 5, 6]) | ||
expected_res = np.array([[-4, -5, -6], [8, 10, 12], [12, 15, 18]]) | ||
np.testing.assert_equal(outer_product(v1, v2), expected_res) | ||
|
||
|
||
def test_outer_product_different_dimensions(): | ||
"""Test with two vectors of different dimensions.""" | ||
|
||
v1, v2 = np.array([1, 2, 3]), np.array([4, 5, 6, 7]) | ||
with np.testing.assert_raises(ValueError): | ||
outer_product(v1, v2) | ||
|
||
|
||
def test_outer_product_different_dimensions_2(): | ||
"""Test with a vector and a 2d array.""" | ||
|
||
v1, v2 = np.array([1, 2, 3]), np.array([[4, 5, 6], [7, 8, 9]]) | ||
with np.testing.assert_raises(ValueError): | ||
@pytest.mark.parametrize("v1, v2, expected_result", [ | ||
# Test with two vectors, no complications. | ||
(np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([[4, 5, 6], [8, 10, 12], [12, 15, 18]])), | ||
# Test with two vectors, with negative input/output values. | ||
(np.array([-1, 2, 3]), np.array([4, 5, 6]), np.array([[-4, -5, -6], [8, 10, 12], [12, 15, 18]])), | ||
]) | ||
def test_outer_product(v1, v2, expected_result): | ||
np.testing.assert_array_equal(outer_product(v1, v2), expected_result) | ||
|
||
|
||
@pytest.mark.parametrize("v1, v2", [ | ||
# Different dimensions of vectors. | ||
(np.array([1, 2, 3]), np.array([4, 5, 6, 7])), | ||
# Vector and 2D array. | ||
(np.array([1, 2, 3]), np.array([[4, 5, 6], [7, 8, 9]])), | ||
]) | ||
def test_outer_product_invalid_input(v1, v2): | ||
with pytest.raises(ValueError): | ||
outer_product(v1, v2) |
This file contains 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 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 |
---|---|---|
@@ -1,28 +1,15 @@ | ||
"""Test unvec.""" | ||
import numpy as np | ||
import pytest | ||
|
||
from toqito.matrix_ops import unvec | ||
|
||
|
||
def test_unvec(): | ||
"""Test standard unvec operation on a vector.""" | ||
expected_res = np.array([[1, 2], [3, 4]]) | ||
|
||
test_input_vec = np.array([1, 3, 2, 4]) | ||
|
||
res = unvec(test_input_vec) | ||
|
||
bool_mat = np.isclose(res, expected_res) | ||
np.testing.assert_equal(np.all(bool_mat), True) | ||
|
||
|
||
def test_unvec_custom_dim(): | ||
"""Test standard unvec operation on a vector with custom dimension.""" | ||
expected_res = np.array([[1], [3], [2], [4]]) | ||
|
||
test_input_vec = np.array([1, 3, 2, 4]) | ||
|
||
res = unvec(test_input_vec, [4, 1]) | ||
|
||
bool_mat = np.isclose(res, expected_res) | ||
np.testing.assert_equal(np.all(bool_mat), True) | ||
@pytest.mark.parametrize("vector, shape, expected_result", [ | ||
# Test standard unvec operation on a vector. | ||
(np.array([1, 3, 2, 4]), None, np.array([[1, 2], [3, 4]])), | ||
# Test standard unvec operation on a vector with custom dimension. | ||
(np.array([1, 3, 2, 4]), [4, 1], np.array([[1], [3], [2], [4]])), | ||
]) | ||
def test_unvec(vector, shape, expected_result): | ||
np.testing.assert_array_equal(unvec(vector, shape), expected_result) |
This file contains 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 |
---|---|---|
@@ -1,16 +1,13 @@ | ||
"""Test vec.""" | ||
import numpy as np | ||
import pytest | ||
|
||
from toqito.matrix_ops import vec | ||
|
||
|
||
def test_vec(): | ||
"""Test standard vec operation on a matrix.""" | ||
expected_res = np.array([[1], [3], [2], [4]]) | ||
|
||
test_input_mat = np.array([[1, 2], [3, 4]]) | ||
|
||
res = vec(test_input_mat) | ||
|
||
bool_mat = np.isclose(res, expected_res) | ||
np.testing.assert_equal(np.all(bool_mat), True) | ||
@pytest.mark.parametrize("vector, expected_result", [ | ||
# Test standard vec operation on a vector. | ||
(np.array(np.array([[1, 2], [3, 4]])), np.array([[1], [3], [2], [4]])), | ||
]) | ||
def test_vec(vector, expected_result): | ||
np.testing.assert_array_equal(vec(vector), expected_result) |
Oops, something went wrong.