Skip to content
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

Add additional symnum.array module tests #7

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,33 @@ def non_zero_symbolic_array_literal(request):
operator.mul,
operator.truediv,
operator.floordiv,
operator.mod,
],
)
def binary_op(request):
return request.param


@pytest.fixture(
params=[
operator.eq,
operator.ne,
operator.lt,
operator.gt,
operator.le,
operator.ge,
],
)
def binary_comparison_op(request):
return request.param


@pytest.fixture(
params=[
abs,
operator.pos,
operator.neg,
],
)
def unary_op(request):
return request.param
77 changes: 75 additions & 2 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import sympy

import symnum
import symnum.array
from symnum.array import SymbolicArray


def test_array_binary_operator_numeric(shape, binary_op, rng):
numpy_array = rng.standard_normal(shape)
symbolic_array = SymbolicArray(sympy.sympify(numpy_array))
assert isinstance(symbolic_array, SymbolicArray)
assert numpy.allclose(
binary_op(numpy_array, numpy_array),
numpy.array(binary_op(symbolic_array, symbolic_array), dtype=numpy.float64),
Expand All @@ -19,13 +19,62 @@ def test_array_binary_operator_numeric(shape, binary_op, rng):
def test_array_binary_operator_symbolic(symbolic_array_literal, binary_op):
numpy_array = numpy.array(symbolic_array_literal)
symbolic_array = SymbolicArray(symbolic_array_literal)
assert isinstance(symbolic_array, SymbolicArray)
assert numpy.all(
binary_op(numpy_array, numpy_array)
== binary_op(symbolic_array, symbolic_array),
)


def test_array_binary_comparison_operator_numeric(shape, binary_comparison_op, rng):
numpy_array_1 = rng.standard_normal(shape)
numpy_array_2 = rng.standard_normal(shape)
symbolic_array_1 = SymbolicArray(sympy.sympify(numpy_array_1))
symbolic_array_2 = SymbolicArray(sympy.sympify(numpy_array_2))
assert numpy.allclose(
binary_comparison_op(numpy_array_1, numpy_array_2),
numpy.array(
binary_comparison_op(symbolic_array_1, symbolic_array_2),
dtype=bool,
),
)
assert numpy.allclose(
binary_comparison_op(numpy_array_2, numpy_array_1),
numpy.array(
binary_comparison_op(symbolic_array_2, symbolic_array_1),
dtype=bool,
),
)


def test_array_binary_comparison_operator_symbolic(
symbolic_array_literal, binary_comparison_op,
):
symbolic_array = SymbolicArray(symbolic_array_literal)
binary_comparison_array = binary_comparison_op(symbolic_array, symbolic_array)
assert all(
binary_comparison_op(s, s) == c
for s, c in zip(symbolic_array.flat, binary_comparison_array.flat)
)


def test_array_unary_operator_numeric(shape, unary_op, rng):
numpy_array = rng.standard_normal(shape)
symbolic_array = SymbolicArray(sympy.sympify(numpy_array))
assert numpy.allclose(
unary_op(numpy_array),
numpy.array(unary_op(symbolic_array), dtype=numpy.float64),
)


def test_array_unary_operator_symbolic(symbolic_array_literal, unary_op):
numpy_array = numpy.array(symbolic_array_literal)
symbolic_array = SymbolicArray(symbolic_array_literal)
assert isinstance(symbolic_array, SymbolicArray)
assert numpy.all(
unary_op(numpy_array) == unary_op(symbolic_array),
)


@pytest.mark.parametrize("method_name", ["sum", "prod"])
def test_reduction_method_numeric(method_name, shape, axis, rng):
numpy_array = rng.standard_normal(shape)
Expand All @@ -51,3 +100,27 @@ def test_reduction_method_symbolic(
getattr(numpy_array, method_name)(axis=axis)
== getattr(symbolic_array, method_name)(axis=axis),
)


def test_as_symbolic_array_sympy_array(symbolic_array_literal):
sympy_array = sympy.NDimArray(symbolic_array_literal)
symbolic_array = symnum.array.as_symbolic_array(sympy_array)
assert isinstance(symbolic_array, SymbolicArray)
assert all(
s1 == s2
for s1, s2 in zip(symbolic_array.flat, sympy_array.reshape(symbolic_array.size))
)


def test_as_symbolic_array_numpy_array(shape, rng):
numpy_array = rng.standard_normal(shape)
symbolic_array = symnum.array.as_symbolic_array(numpy_array)
assert isinstance(symbolic_array, SymbolicArray)
assert numpy.all(
numpy_array == numpy.array(symbolic_array, dtype=numpy.float64),
)


def test_as_symbolic_array_identity(symbolic_array_literal):
symbolic_array = SymbolicArray(symbolic_array_literal)
assert symnum.array.as_symbolic_array(symbolic_array) is symbolic_array