Skip to content

Commit

Permalink
as of numpy 2.0 np.linalg.solve requires that the 'b' matrix has shap…
Browse files Browse the repository at this point in the history
…e (M,) or (..., M, K) with corresponding output (..., M) or (..., M, K), so add axis K = 1 then squeeze that axis away.

This change is compatible with numpy 1.*
  • Loading branch information
ZachMcBreartyUU committed Jan 31, 2025
1 parent cbb6863 commit 37397dd
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pysindy/differentiation/finite_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ def _coefficients(self, t):
matrices = dt_endpoints[:, "power", :] ** pows
b = AxesArray(np.zeros((1, self.n_stencil)), {"ax_time": 0, "ax_power": 1})
b[0, self.d] = factorial(self.d)
return np.linalg.solve(matrices, b)
# as of numpy 2.0:
# solve requires that the 'b' matrix has shape (M,) or (..., M, K)
# with corresponding output (..., M) or (..., M, K),
# so add axis K = 1 then squeeze that axis away
# This change also propagates to all other _coefficients functions
# and is compatible with numpy 1.*
return np.squeeze(np.linalg.solve(matrices, b[..., np.newaxis]), axis=2)

def _coefficients_boundary_forward(self, t):
# use the same stencil for each boundary point,
Expand Down Expand Up @@ -145,7 +151,7 @@ def _coefficients_boundary_forward(self, t):

b = np.zeros(self.stencil_inds.shape).T
b[:, self.d] = factorial(self.d)
return np.linalg.solve(matrices, b)
return np.squeeze(np.linalg.solve(matrices, b[..., np.newaxis]), axis=2)

def _coefficients_boundary_periodic(self, t):
# use centered periodic stencils
Expand Down Expand Up @@ -198,7 +204,7 @@ def _coefficients_boundary_periodic(self, t):

b = np.zeros(self.stencil_inds.shape).T
b[:, self.d] = factorial(self.d)
return np.linalg.solve(matrices, b)
return np.squeeze(np.linalg.solve(matrices, b[..., np.newaxis]), axis=2)

def _constant_coefficients(self, dt):
pows = np.arange(self.n_stencil)[:, np.newaxis]
Expand Down

0 comments on commit 37397dd

Please sign in to comment.