feat(Function): Add __eq__ method and improve quadratic_solve stability
All checks were successful
All checks were successful
Implements two features for the Function class: 1. Adds the `__eq__` operator (`==`) to allow for logical comparison of two Function objects based on their coefficients. 2. Replaces the standard quadratic formula with a numerically stable version in `quadratic_solve` to prevent "catastrophic cancellation" errors and improve accuracy.
This commit was merged in pull request #23.
This commit is contained in:
@@ -37,6 +37,12 @@ def m_func_2() -> Function:
|
||||
f.set_coeffs([5, -4])
|
||||
return f
|
||||
|
||||
@pytest.fixture
|
||||
def base_func():
|
||||
f = Function(2)
|
||||
f.set_coeffs([1, 2, 3])
|
||||
return f
|
||||
|
||||
# --- Core Functionality Tests ---
|
||||
|
||||
def test_solve_y(quadratic_func):
|
||||
@@ -95,6 +101,32 @@ def test_function_multiplication(m_func_1, m_func_2):
|
||||
assert result.largest_exponent == 3
|
||||
assert np.array_equal(result.coefficients, [10, 7, -7, -4])
|
||||
|
||||
def test_equality(base_func):
|
||||
"""Tests the __eq__ method for the Function class."""
|
||||
|
||||
# 1. Test for equality with a new, identical object
|
||||
f_identical = Function(2)
|
||||
f_identical.set_coeffs([1, 2, 3])
|
||||
assert base_func == f_identical
|
||||
|
||||
# 2. Test for inequality (different coefficients)
|
||||
f_different = Function(2)
|
||||
f_different.set_coeffs([1, 9, 3])
|
||||
assert base_func != f_different
|
||||
|
||||
# 3. Test for inequality (different degree)
|
||||
f_diff_degree = Function(1)
|
||||
f_diff_degree.set_coeffs([1, 2])
|
||||
assert base_func != f_diff_degree
|
||||
|
||||
# 4. Test against a different type
|
||||
assert base_func != "some_string"
|
||||
assert base_func != 123
|
||||
|
||||
# 5. Test against an uninitialized Function
|
||||
f_uninitialized = Function(2)
|
||||
assert base_func != f_uninitialized
|
||||
|
||||
# --- Genetic Algorithm Root-Finding Tests ---
|
||||
|
||||
def test_get_real_roots_numpy(quadratic_func):
|
||||
|
||||
Reference in New Issue
Block a user