Skip to content

Commit

Permalink
ENH: Allow all polynomial library library options in trapping
Browse files Browse the repository at this point in the history
(other than degree, which has to be 2)
  • Loading branch information
Jacob-Stevens-Haas committed Dec 10, 2023
1 parent 018eb9a commit 0870f57
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions pysindy/optimizers/trapping_sr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,13 +822,15 @@ def _reduce(self, x, y):
self.objective_history = objective_history


def _make_constraints(n_tgts: int):
def _make_constraints(n_tgts: int, **kwargs):
"""Create constraints for the Quadratic terms in TrappingSR3.
These are the constraints from equation 5 of the Trapping SINDy paper.
Args:
n_tgts: number of coordinates or modes for which you're fitting an ODE.
kwargs: Keyword arguments to PolynomialLibrary such as
``include_bias``.
Returns:
A tuple of the constraint zeros, and a constraint matrix to multiply
Expand All @@ -841,7 +843,7 @@ def _make_constraints(n_tgts: int):
reshaping.
"""
n_terms = n_poly_features(n_tgts, degree=2, include_bias=False)
lib = PolynomialLibrary(2, include_bias=False).fit(np.zeros((1, n_tgts)))
lib = PolynomialLibrary(2, **kwargs).fit(np.zeros((1, n_tgts)))
terms = [(t_ind, exps) for t_ind, exps in enumerate(lib.powers_)]

# index of tgt -> index of its pure quadratic term
Expand Down
2 changes: 1 addition & 1 deletion test/test_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ def test_remove_and_decrement():

def test_trapping_constraints():
# x, y, x^2, xy, y^2
constraint_rhs, constraint_lhs = _make_constraints(2)
constraint_rhs, constraint_lhs = _make_constraints(2, include_bias=False)
stable_coefs = np.array([[0, 0, 0, 1, -1], [0, 0, -1, 1, 0]])
result = np.tensordot(constraint_lhs, stable_coefs, ((1, 2), (1, 0)))
np.testing.assert_array_equal(constraint_rhs, result)
Expand Down

0 comments on commit 0870f57

Please sign in to comment.