(This docstring should be overwritten)
(func1d, axis, arr, *args, **kwargs)
| 348 | |
| 349 | |
| 350 | def apply_along_axis(func1d, axis, arr, *args, **kwargs): |
| 351 | """ |
| 352 | (This docstring should be overwritten) |
| 353 | """ |
| 354 | arr = array(arr, copy=False, subok=True) |
| 355 | nd = arr.ndim |
| 356 | axis = normalize_axis_index(axis, nd) |
| 357 | ind = [0] * (nd - 1) |
| 358 | i = np.zeros(nd, 'O') |
| 359 | indlist = list(range(nd)) |
| 360 | indlist.remove(axis) |
| 361 | i[axis] = slice(None, None) |
| 362 | outshape = np.asarray(arr.shape).take(indlist) |
| 363 | i.put(indlist, ind) |
| 364 | res = func1d(arr[tuple(i.tolist())], *args, **kwargs) |
| 365 | # if res is a number, then we have a smaller output array |
| 366 | asscalar = np.isscalar(res) |
| 367 | if not asscalar: |
| 368 | try: |
| 369 | len(res) |
| 370 | except TypeError: |
| 371 | asscalar = True |
| 372 | # Note: we shouldn't set the dtype of the output from the first result |
| 373 | # so we force the type to object, and build a list of dtypes. We'll |
| 374 | # just take the largest, to avoid some downcasting |
| 375 | dtypes = [] |
| 376 | if asscalar: |
| 377 | dtypes.append(np.asarray(res).dtype) |
| 378 | outarr = zeros(outshape, object) |
| 379 | outarr[tuple(ind)] = res |
| 380 | Ntot = np.prod(outshape) |
| 381 | k = 1 |
| 382 | while k < Ntot: |
| 383 | # increment the index |
| 384 | ind[-1] += 1 |
| 385 | n = -1 |
| 386 | while (ind[n] >= outshape[n]) and (n > (1 - nd)): |
| 387 | ind[n - 1] += 1 |
| 388 | ind[n] = 0 |
| 389 | n -= 1 |
| 390 | i.put(indlist, ind) |
| 391 | res = func1d(arr[tuple(i.tolist())], *args, **kwargs) |
| 392 | outarr[tuple(ind)] = res |
| 393 | dtypes.append(asarray(res).dtype) |
| 394 | k += 1 |
| 395 | else: |
| 396 | res = array(res, copy=False, subok=True) |
| 397 | j = i.copy() |
| 398 | j[axis] = ([slice(None, None)] * res.ndim) |
| 399 | j.put(indlist, ind) |
| 400 | Ntot = np.prod(outshape) |
| 401 | holdshape = outshape |
| 402 | outshape = list(arr.shape) |
| 403 | outshape[axis] = res.shape |
| 404 | dtypes.append(asarray(res).dtype) |
| 405 | outshape = flatten_inplace(outshape) |
| 406 | outarr = zeros(outshape, object) |
| 407 | outarr[tuple(flatten_inplace(j.tolist()))] = res |
searching dependent graphs…