Skip to content

Commit

Permalink
CLN: Extract function to encapsulate coefficient-solving step of Trap…
Browse files Browse the repository at this point in the history
…ping

This makes the algorithm in code look like algorithm in paper
  • Loading branch information
Jacob-Stevens-Haas committed Dec 5, 2023
1 parent 9610260 commit 2662af3
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions pysindy/optimizers/trapping_sr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ def _reduce(self, x, y):
self.history_ = []
n_samples, n_features = x.shape
n_tgts = y.shape[1]
var_len = n_features * n_tgts
n_feat_expected = int((n_tgts**2 + 3 * n_tgts) / 2.0)

# Only relevant if the stability term is turned on.
Expand Down Expand Up @@ -494,31 +495,19 @@ def _reduce(self, x, y):
if self.relax_optim:
if self.threshold > 0.0:
# sparse relax_and_split
xi, cost = self._create_var_and_part_cost(
n_features * n_tgts, x_expanded, y
)
cost = cost + cp.sum_squares(Pmatrix @ xi - A.flatten()) / self.eta
coef_sparse = self._update_coef_cvxpy(
xi, cost, n_tgts * n_features, coef_prev, self.eps_solver
coef_sparse = self._update_coef_sparse_rs(
var_len, x_expanded, y, Pmatrix, A, coef_prev
)
else:
pTp = np.dot(Pmatrix.T, Pmatrix)
H = xTx + pTp / self.eta
P_transpose_A = np.dot(Pmatrix.T, A.flatten())
coef_sparse = self._solve_nonsparse_relax_and_split(
H, xTy, P_transpose_A, coef_prev
coef_sparse = self._update_coef_nonsparse_rs(
Pmatrix, A, coef_prev, xTx, xTy
)
trap_prev_ctr, trap_ctr, A, tk_prev = self._solve_m_relax_and_split(
trap_prev_ctr, trap_ctr, A, coef_sparse, tk_prev
)
else:
var_len = n_tgts * n_features
xi, cost = self._create_var_and_part_cost(var_len, x_expanded, y)
cost += (
cp.lambda_max(cp.reshape(Pmatrix @ xi, (n_tgts, n_tgts))) / self.eta
)
coef_sparse = self._update_coef_cvxpy(
xi, cost, var_len, coef_prev, self.eps_solver
coef_sparse = self._update_coef_direct(
var_len, x_expanded, y, Pmatrix, coef_prev, n_tgts
)
trap_ctr = self._solve_m_direct(n_tgts, coef_sparse)

Expand Down Expand Up @@ -556,3 +545,19 @@ def _reduce(self, x, y):
)

self.coef_ = coef_sparse.T

def _update_coef_sparse_rs(self, var_len, x_expanded, y, Pmatrix, A, coef_prev):
xi, cost = self._create_var_and_part_cost(var_len, x_expanded, y)
cost = cost + cp.sum_squares(Pmatrix @ xi - A.flatten()) / self.eta
return self._update_coef_cvxpy(xi, cost, var_len, coef_prev, self.eps_solver)

def _update_coef_nonsparse_rs(self, Pmatrix, A, coef_prev, xTx, xTy):
pTp = np.dot(Pmatrix.T, Pmatrix)
H = xTx + pTp / self.eta
P_transpose_A = np.dot(Pmatrix.T, A.flatten())
return self._solve_nonsparse_relax_and_split(H, xTy, P_transpose_A, coef_prev)

def _update_coef_direct(self, var_len, x_expanded, y, Pmatrix, coef_prev, n_tgts):
xi, cost = self._create_var_and_part_cost(var_len, x_expanded, y)
cost += cp.lambda_max(cp.reshape(Pmatrix @ xi, (n_tgts, n_tgts))) / self.eta
return self._update_coef_cvxpy(xi, cost, var_len, coef_prev, self.eps_solver)

0 comments on commit 2662af3

Please sign in to comment.