Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for incompatibility of numpy2.* in the finite difference routines #595

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading