Skip to content

Commit eb0d904

Browse files
committed
Fix scipy gradient compatibility for Python 3.13
- Update objf_prime to return 1D gradient array instead of 2D - Fixes 'Input array gradx must be 1D' error in amss2 and amss3 - Resolves compatibility issue with scipy.optimize.fmin_slsqp in Python 3.13
1 parent 940f676 commit eb0d904

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lectures/_static/lecture_specific/amss2/recursive_allocation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,15 @@ def objf_prime(x):
231231

232232
epsilon = 1e-7
233233
x0 = np.asarray(x, dtype=float)
234-
f0 = np.atleast_1d(objf(x0))
235-
jac = np.zeros([len(x0), len(f0)])
234+
f0 = objf(x0)
235+
grad = np.zeros(len(x0))
236236
dx = np.zeros(len(x0))
237237
for i in range(len(x0)):
238238
dx[i] = epsilon
239-
jac[i] = (objf(x0+dx) - f0)/epsilon
239+
grad[i] = (objf(x0+dx) - f0)/epsilon
240240
dx[i] = 0.0
241241

242-
return jac.transpose()
242+
return grad
243243

244244
def cons(z):
245245
c, n, xprime, T = z[:S], z[S:2 * S], z[2 * S:3 * S], z[3 * S:]

0 commit comments

Comments
 (0)