def apply_along_axis(func1d, axis, arr, *args, **kwargs):
arr = asarray(arr)<EOL>nd = arr.ndim<EOL>if axis < <NUM_LIT:0>:<EOL><INDENT>axis += nd<EOL><DEDENT>if (axis >= nd):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>% (axis, nd))<EOL><DEDENT>ind = [<NUM_LIT:0>]*(nd-<NUM_LIT:1>)<EOL>i = zeros(nd, '<STR_LIT:O>')<EOL>indlist = list(range(nd))<EOL>indlist.remove(axis)<EOL>i[axis] = slice(None, None)<EOL>outshape = asarray(arr.shape).take(indlist)<EOL>i.put(indlist, ind)<EOL>res = func1d(arr[tuple(i.tolist())], *args, **kwargs)<EOL>if isscalar(res):<EOL><INDENT>outarr = zeros(outshape, asarray(res).dtype)<EOL>outarr[tuple(ind)] = res<EOL>Ntot = product(outshape)<EOL>k = <NUM_LIT:1><EOL>while k < Ntot:<EOL><INDENT>ind[-<NUM_LIT:1>] += <NUM_LIT:1><EOL>n = -<NUM_LIT:1><EOL>while (ind[n] >= outshape[n]) and (n > (<NUM_LIT:1>-nd)):<EOL><INDENT>ind[n-<NUM_LIT:1>] += <NUM_LIT:1><EOL>ind[n] = <NUM_LIT:0><EOL>n -= <NUM_LIT:1><EOL><DEDENT>i.put(indlist, ind)<EOL>res = func1d(arr[tuple(i.tolist())], *args, **kwargs)<EOL>outarr[tuple(ind)] = res<EOL>k += <NUM_LIT:1><EOL><DEDENT>return outarr<EOL><DEDENT>else:<EOL><INDENT>Ntot = product(outshape)<EOL>holdshape = outshape<EOL>outshape = list(arr.shape)<EOL>outshape[axis] = len(res)<EOL>outarr = zeros(outshape, asarray(res).dtype)<EOL>outarr[tuple(i.tolist())] = res<EOL>k = <NUM_LIT:1><EOL>while k < Ntot:<EOL><INDENT>ind[-<NUM_LIT:1>] += <NUM_LIT:1><EOL>n = -<NUM_LIT:1><EOL>while (ind[n] >= holdshape[n]) and (n > (<NUM_LIT:1>-nd)):<EOL><INDENT>ind[n-<NUM_LIT:1>] += <NUM_LIT:1><EOL>ind[n] = <NUM_LIT:0><EOL>n -= <NUM_LIT:1><EOL><DEDENT>i.put(indlist, ind)<EOL>res = func1d(arr[tuple(i.tolist())], *args, **kwargs)<EOL>outarr[tuple(i.tolist())] = res<EOL>k += <NUM_LIT:1><EOL><DEDENT>return outarr<EOL><DEDENT>
Apply a function to 1-D slices along the given axis.

Execute `func1d(a, *args)` where `func1d` operates on 1-D arrays and `a`
is a 1-D slice of `arr` along `axis`.

Parameters
----------
func1d : function
    This function should accept 1-D arrays. It is applied to 1-D
    slices of `arr` along the specified axis.
axis : integer
    Axis along which `arr` is sliced.
arr : ndarray
    Input array.
args : any
    Additional arguments to `func1d`.
kwargs: any
    Additional named arguments to `func1d`.

    .. versionadded:: 1.9.0


Returns
-------
apply_along_axis : ndarray
    The output array. The shape of `outarr` is identical to the shape of
    `arr`, except along the `axis` dimension, where the length of `outarr`
    is equal to the size of the return value of `func1d`.  If `func1d`
    returns a scalar `outarr` will have one fewer dimensions than `arr`.

See Also
--------
apply_over_axes : Apply a function repeatedly over multiple axes.

Examples
--------
>>> def my_func(a):
...     \"\"\"Average first and last element of a 1-D array\"\"\"
...     return (a[0] + a[-1]) * 0.5
>>> b = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> np.apply_along_axis(my_func, 0, b)
array([ 4.,  5.,  6.])
>>> np.apply_along_axis(my_func, 1, b)
array([ 2.,  5.,  8.])

For a function that doesn't return a scalar, the number of dimensions in
`outarr` is the same as `arr`.

>>> b = np.array([[8,1,7], [4,3,9], [5,2,6]])
>>> np.apply_along_axis(sorted, 1, b)
array([[1, 7, 8],
       [3, 4, 9],
       [2, 5, 6]])