Add `Q`, `R`, `C`, and `GF2` fields
- add `lalib.fields.base.Field`, a blueprint for all concrete fields,
providing a unified interface to be used outside of the
`lalib.fields` sub-package
- implement `lalib.fields.complex_.ComplexField`, or `C` for short,
the field over the complex numbers (modeled as `complex` numbers)
- implement `lalib.fields.galois.GaloisField2`, or `GF2` for short,
the (finite) field over the two elements `one` and `zero`
+ adapt `lalib.elements.galois.GF2Element.__eq__()` to return
`NotImplemented` instead of `False` for non-castable `other`s
=> this fixes a minor issue with `pytest.approx()`
- implement `lalib.fields.rational.RationalField`, or `Q` for short,
the field over the rational numbers (modeled as `fractions.Fraction`s)
- implement `lalib.fields.real.RealField`, or `R` for short,
the field over the real numbers (modeled as `float`s)
- organize top-level imports for `lalib.fields`,
making `Q`, `R`, `C`, and `GF2` importable with
`from lalib.fields import *`
- provide extensive unit and integration tests for the new objects:
+ test generic and common behavior in `tests.fields.test_base`
+ test specific behavior is other modules
+ test the well-known math axioms for all fields (integration tests)
+ test the new objects' docstrings
+ add "pytest-repeat" to run randomized tests many times
2024-10-14 15:17:42 +02:00
|
|
|
"""Tests for the `lalib.fields.complex_.ComplexField` only."""
|
|
|
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from lalib import fields
|
|
|
|
from tests.fields import utils
|
|
|
|
|
|
|
|
|
2024-10-15 01:49:32 +02:00
|
|
|
# None of the test cases below contributes towards higher coverage
|
|
|
|
pytestmark = pytest.mark.overlapping_test
|
|
|
|
|
|
|
|
|
Add `Q`, `R`, `C`, and `GF2` fields
- add `lalib.fields.base.Field`, a blueprint for all concrete fields,
providing a unified interface to be used outside of the
`lalib.fields` sub-package
- implement `lalib.fields.complex_.ComplexField`, or `C` for short,
the field over the complex numbers (modeled as `complex` numbers)
- implement `lalib.fields.galois.GaloisField2`, or `GF2` for short,
the (finite) field over the two elements `one` and `zero`
+ adapt `lalib.elements.galois.GF2Element.__eq__()` to return
`NotImplemented` instead of `False` for non-castable `other`s
=> this fixes a minor issue with `pytest.approx()`
- implement `lalib.fields.rational.RationalField`, or `Q` for short,
the field over the rational numbers (modeled as `fractions.Fraction`s)
- implement `lalib.fields.real.RealField`, or `R` for short,
the field over the real numbers (modeled as `float`s)
- organize top-level imports for `lalib.fields`,
making `Q`, `R`, `C`, and `GF2` importable with
`from lalib.fields import *`
- provide extensive unit and integration tests for the new objects:
+ test generic and common behavior in `tests.fields.test_base`
+ test specific behavior is other modules
+ test the well-known math axioms for all fields (integration tests)
+ test the new objects' docstrings
+ add "pytest-repeat" to run randomized tests many times
2024-10-14 15:17:42 +02:00
|
|
|
C = fields.C
|
|
|
|
|
|
|
|
|
|
|
|
class TestCastAndValidateFieldElements:
|
|
|
|
"""Test specifics for `C.cast()` and `C.validate()`."""
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("pre_value", [1, 0, +42, -42])
|
|
|
|
def test_complex_number_is_field_element(self, pre_value):
|
|
|
|
"""`C` must be able to process `complex` numbers."""
|
|
|
|
value = complex(pre_value, 0)
|
|
|
|
utils.is_field_element(C, value)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("pre_value", ["NaN", "+inf", "-inf"])
|
|
|
|
def test_non_finite_complex_number_is_not_field_element(self, pre_value):
|
|
|
|
"""For now, we only allow finite numbers as field elements.
|
|
|
|
|
|
|
|
This also holds true for `complex` numbers
|
|
|
|
with a non-finite `.real` part.
|
|
|
|
"""
|
|
|
|
value = complex(pre_value)
|
|
|
|
utils.is_not_field_element(C, value)
|
|
|
|
|
|
|
|
|
|
|
|
class TestIsZero:
|
|
|
|
"""Test specifics for `C.zero` and `C.is_zero()`."""
|
|
|
|
|
|
|
|
def test_is_almost_zero(self):
|
|
|
|
"""`value` is within an acceptable threshold of `C.zero`."""
|
|
|
|
value = 0.0 + utils.WITHIN_THRESHOLD
|
|
|
|
|
|
|
|
assert pytest.approx(C.zero, abs=utils.DEFAULT_THRESHOLD) == value
|
|
|
|
assert C.is_zero(value)
|
|
|
|
|
|
|
|
def test_is_slightly_not_zero(self):
|
|
|
|
"""`value` is not within an acceptable threshold of `C.zero`."""
|
|
|
|
value = 0.0 + utils.NOT_WITHIN_THRESHOLD
|
|
|
|
|
|
|
|
assert pytest.approx(C.zero, abs=utils.DEFAULT_THRESHOLD) != value
|
|
|
|
assert not C.is_zero(value)
|
|
|
|
|
|
|
|
|
|
|
|
class TestIsOne:
|
|
|
|
"""Test specifics for `C.one` and `C.is_one()`."""
|
|
|
|
|
|
|
|
def test_is_almost_one(self):
|
|
|
|
"""`value` is within an acceptable threshold of `C.one`."""
|
|
|
|
value = 1.0 + utils.WITHIN_THRESHOLD
|
|
|
|
|
|
|
|
assert pytest.approx(C.one, abs=utils.DEFAULT_THRESHOLD) == value
|
|
|
|
assert C.is_one(value)
|
|
|
|
|
|
|
|
def test_is_slightly_not_one(self):
|
|
|
|
"""`value` is not within an acceptable threshold of `C.one`."""
|
|
|
|
value = 1.0 + utils.NOT_WITHIN_THRESHOLD
|
|
|
|
|
|
|
|
assert pytest.approx(C.one, abs=utils.DEFAULT_THRESHOLD) != value
|
|
|
|
assert not C.is_one(value)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.repeat(utils.N_RANDOM_DRAWS)
|
|
|
|
class TestDrawRandomFieldElement:
|
|
|
|
"""Test specifics for `C.random()`."""
|
|
|
|
|
|
|
|
def test_draw_elements_with_custom_bounds(self):
|
|
|
|
"""Draw a random element from `C` ...
|
|
|
|
|
|
|
|
... within the bounds passed in as arguments.
|
|
|
|
|
|
|
|
For `C`, the bounds are interpreted in a 2D fashion.
|
|
|
|
"""
|
|
|
|
lower = complex(
|
|
|
|
200 * random.random() - 100, # noqa: S311
|
|
|
|
200 * random.random() - 100, # noqa: S311
|
|
|
|
)
|
|
|
|
upper = complex(
|
|
|
|
200 * random.random() - 100, # noqa: S311
|
|
|
|
200 * random.random() - 100, # noqa: S311
|
|
|
|
)
|
|
|
|
|
|
|
|
element = C.random(lower=lower, upper=upper)
|
|
|
|
|
|
|
|
l_r, u_r = min(lower.real, upper.real), max(lower.real, upper.real)
|
|
|
|
l_i, u_i = min(lower.imag, upper.imag), max(lower.imag, upper.imag)
|
|
|
|
|
|
|
|
assert l_r <= element.real <= u_r
|
|
|
|
assert l_i <= element.imag <= u_i
|