Skip to content

Commit

Permalink
BUG(_make_constraints): Request correct num of terms if include_bias
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob-Stevens-Haas committed Jan 8, 2024
1 parent 3996040 commit 3730a91
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pysindy/optimizers/trapping_sr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,8 @@ def _make_constraints(n_tgts: int, **kwargs):
To get "target" order constraints, transpose axis 1 and 2 before
reshaping.
"""
n_terms = n_poly_features(n_tgts, degree=2, include_bias=False)
include_bias = kwargs.get("include_bias", False)
n_terms = n_poly_features(n_tgts, degree=2, include_bias=include_bias)
lib = PolynomialLibrary(2, **kwargs).fit(np.zeros((1, n_tgts)))
terms = [(t_ind, exps) for t_ind, exps in enumerate(lib.powers_)]

Expand Down
9 changes: 7 additions & 2 deletions test/test_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,13 +1131,18 @@ def test_remove_and_decrement():
np.testing.assert_array_equal(expected, result)


def test_trapping_constraints():
@pytest.mark.parametrize("include_bias", (True, False))
def test_trapping_constraints(include_bias):
# x, y, x^2, xy, y^2
constraint_rhs, constraint_lhs = _make_constraints(2, include_bias=False)
constraint_rhs, constraint_lhs = _make_constraints(2, include_bias=include_bias)
stable_coefs = np.array([[0, 0, 0, 1, -1], [0, 0, -1, 1, 0]])
if include_bias:
stable_coefs = np.concatenate(([[0], [0]], stable_coefs), axis=1)
result = np.tensordot(constraint_lhs, stable_coefs, ((1, 2), (1, 0)))
np.testing.assert_array_equal(constraint_rhs, result)


def test_trapping_mixed_only():
# xy, xz, yz
stable_coefs = np.array([[0, 0, -1], [0, 0.5, 0], [0.5, 0, 0]])
mixed_terms = {frozenset((0, 1)): 0, frozenset((0, 2)): 1, frozenset((1, 2)): 2}
Expand Down

0 comments on commit 3730a91

Please sign in to comment.