<p dir="auto">Starting with <a href="http://scicomp.stackexchange.com/questions/3229/quickly-permute-a-large-square-matrix-in-python-numpy" rel="nofollow">this SO question</a>, I arrived at take() to a in-place array permutation, however when the call is made with the "clip" argument (to avoid creation of a backup in case of exception) the function can be seen to duplicate rows/columns.</p>
<p dir="auto">Exact test code demonstrating this issue was provided by <a href="http://scicomp.stackexchange.com/users/1744/stefano-m" rel="nofollow">Stefano M</a> from the SO post comment stream, and appears below.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
print np.version.version

N = 10

# gen ref matrix and permutation
ref = np.arange(N*N)
ref.resize(N,N)
rr = range(N)
np.random.shuffle(rr)

arr = ref.copy()
res = (arr.take(rr,axis=0) == ref[rr,:]).all()
print &quot;test take, not overwriting:&quot;, res

arr = ref.copy()
arr.take(rr,axis=0,out=arr,mode=&quot;raise&quot;)
print &quot;test not-in-place take:&quot;, (arr == ref[rr,:]).all()

arr = ref.copy()
arr.take(rr,axis=0,out=arr,mode=&quot;clip&quot;)
res = (arr == ref[rr,:]).all()
print &quot;test in-place take:&quot;, res
if not res:
    print &quot;rr&quot;, rr
    print &quot;arr&quot;, arr[:,0]
    print &quot;ref&quot;, ref[rr,0] "><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">print</span> <span class="pl-s1">np</span>.<span class="pl-s1">version</span>.<span class="pl-s1">version</span>

<span class="pl-v">N</span> <span class="pl-c1">=</span> <span class="pl-c1">10</span>

<span class="pl-c"># gen ref matrix and permutation</span>
<span class="pl-s1">ref</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-v">N</span><span class="pl-c1">*</span><span class="pl-v">N</span>)
<span class="pl-s1">ref</span>.<span class="pl-en">resize</span>(<span class="pl-v">N</span>,<span class="pl-v">N</span>)
<span class="pl-s1">rr</span> <span class="pl-c1">=</span> <span class="pl-en">range</span>(<span class="pl-v">N</span>)
<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">shuffle</span>(<span class="pl-s1">rr</span>)

<span class="pl-s1">arr</span> <span class="pl-c1">=</span> <span class="pl-s1">ref</span>.<span class="pl-en">copy</span>()
<span class="pl-s1">res</span> <span class="pl-c1">=</span> (<span class="pl-s1">arr</span>.<span class="pl-en">take</span>(<span class="pl-s1">rr</span>,<span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">0</span>) <span class="pl-c1">==</span> <span class="pl-s1">ref</span>[<span class="pl-s1">rr</span>,:]).<span class="pl-en">all</span>()
<span class="pl-k">print</span> <span class="pl-s">"test take, not overwriting:"</span>, <span class="pl-s1">res</span>

<span class="pl-s1">arr</span> <span class="pl-c1">=</span> <span class="pl-s1">ref</span>.<span class="pl-en">copy</span>()
<span class="pl-s1">arr</span>.<span class="pl-en">take</span>(<span class="pl-s1">rr</span>,<span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">0</span>,<span class="pl-s1">out</span><span class="pl-c1">=</span><span class="pl-s1">arr</span>,<span class="pl-s1">mode</span><span class="pl-c1">=</span><span class="pl-s">"raise"</span>)
<span class="pl-k">print</span> <span class="pl-s">"test not-in-place take:"</span>, (<span class="pl-s1">arr</span> <span class="pl-c1">==</span> <span class="pl-s1">ref</span>[<span class="pl-s1">rr</span>,:]).<span class="pl-en">all</span>()

<span class="pl-s1">arr</span> <span class="pl-c1">=</span> <span class="pl-s1">ref</span>.<span class="pl-en">copy</span>()
<span class="pl-s1">arr</span>.<span class="pl-en">take</span>(<span class="pl-s1">rr</span>,<span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">0</span>,<span class="pl-s1">out</span><span class="pl-c1">=</span><span class="pl-s1">arr</span>,<span class="pl-s1">mode</span><span class="pl-c1">=</span><span class="pl-s">"clip"</span>)
<span class="pl-s1">res</span> <span class="pl-c1">=</span> (<span class="pl-s1">arr</span> <span class="pl-c1">==</span> <span class="pl-s1">ref</span>[<span class="pl-s1">rr</span>,:]).<span class="pl-en">all</span>()
<span class="pl-k">print</span> <span class="pl-s">"test in-place take:"</span>, <span class="pl-s1">res</span>
<span class="pl-k">if</span> <span class="pl-c1">not</span> <span class="pl-s1">res</span>:
    <span class="pl-k">print</span> <span class="pl-s">"rr"</span>, <span class="pl-s1">rr</span>
    <span class="pl-k">print</span> <span class="pl-s">"arr"</span>, <span class="pl-s1">arr</span>[:,<span class="pl-c1">0</span>]
    <span class="pl-k">print</span> <span class="pl-s">"ref"</span>, <span class="pl-s1">ref</span>[<span class="pl-s1">rr</span>,<span class="pl-c1">0</span>] </pre></div>
<p dir="auto">The output is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.8.0
test take, not overwriting: True
test not-in-place take: True
test in-place take: False
rr [7, 8, 6, 3, 9, 0, 5, 1, 2, 4]
arr [70 80 60 30 90 70 70 80 60 90]
ref [70 80 60 30 90  0 50 10 20 40]"><pre class="notranslate"><code class="notranslate">1.8.0
test take, not overwriting: True
test not-in-place take: True
test in-place take: False
rr [7, 8, 6, 3, 9, 0, 5, 1, 2, 4]
arr [70 80 60 30 90 70 70 80 60 90]
ref [70 80 60 30 90  0 50 10 20 40]
</code></pre></div>
<p dir="auto">Also tested in numpy 1.6.2. and 1.7.1, where it seems broken as well.</p>
<p dir="auto">EDITED to update with items from <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/eric-wieser/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/eric-wieser">@eric-wieser</a> and <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/shoyer/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/shoyer">@shoyer</a>. Feel free to edit to add other items.</p>
<p dir="auto">A reminder of clean-up after <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="187577313" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/8247" data-hovercard-type="pull_request" data-hovercard-url="/numpy/numpy/pull/8247/hovercard" href="https://github.com/numpy/numpy/pull/8247">#8247</a> that could most usefully be done:</p>
<ul class="contains-task-list">
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> Currently, arguments in ufuncs are normalized in <code class="notranslate">get_ufunc_arguments</code> (in <code class="notranslate">ufunc_object.c</code>), well after a check for an override is done; the latter code separately normalized arguments in <code class="notranslate">override.c</code>. Obviously, it would be better if only one of those was done (to the extend possible). As part of this, the type of exception raised should also be made consistent with python, i.e., <code class="notranslate">TypeError</code> for too many/too few arguments, things occurring both as positional and keyword arguments, etc.)</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> BUG: <code class="notranslate">__array_ufunc__</code> on arguments like <code class="notranslate">axis</code> would also override the ufunc (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="228170289" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/9107" data-hovercard-type="issue" data-hovercard-url="/numpy/numpy/issues/9107/hovercard" href="https://github.com/numpy/numpy/issues/9107">#9107</a>).</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> <code class="notranslate">np.dot</code> and <code class="notranslate">np.matmul</code> are no longer overridden, but it would be good to override <code class="notranslate">np.matmul</code> in particular (as it implements a binop). This would need (1) turn <code class="notranslate">np.matmul</code> into a generalized ufunc (or have it use a new gufunc, after prepending/appending of axes as appropriate); (2) use the same generalized ufunc where feasible inside <code class="notranslate">np.dot</code> (and <code class="notranslate">ndarray.dot</code>), so that the most common cases can be overridden (for other cases, we might want to error on the presence of <code class="notranslate">__array_ufunc__</code>).</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> (<a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/eric-wieser/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/eric-wieser">@eric-wieser</a>) Set the <code class="notranslate">__name__</code> field of the generated <code class="notranslate">NDArrayOperatorsMixin</code> methods (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="224921025" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/9012" data-hovercard-type="pull_request" data-hovercard-url="/numpy/numpy/pull/9012/hovercard" href="https://github.com/numpy/numpy/pull/9012">#9012</a>)</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> (<a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/shoyer/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/shoyer">@shoyer</a>) Need to switch ufunc overrides to always raise <code class="notranslate">TypeError</code> if any arguments set <code class="notranslate">__array_ufunc__ = None</code> (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="224903716" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/9011" data-hovercard-type="issue" data-hovercard-url="/numpy/numpy/issues/9011/hovercard" href="https://github.com/numpy/numpy/issues/9011">#9011</a>)</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> (<a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/shoyer/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/shoyer">@shoyer</a>) Choose the recommended way of implementing binary ops (calling the ufunc directly, or calling <code class="notranslate">__array_ufunc__</code>), and clarify guidance in the NEP (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="225303570" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/9027" data-hovercard-type="pull_request" data-hovercard-url="/numpy/numpy/pull/9027/hovercard" href="https://github.com/numpy/numpy/pull/9027">#9027</a>)</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> (<a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/shoyer/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/shoyer">@shoyer</a>) Backwards compatible mixin like NDArrayOperatorsMixin as a separate project (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="224981407" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/9016" data-hovercard-type="issue" data-hovercard-url="/numpy/numpy/issues/9016/hovercard" href="https://github.com/numpy/numpy/issues/9016">#9016</a>).</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> (<a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/shoyer/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/shoyer">@shoyer</a>) BUG: no way to override matmul/@ if <code class="notranslate">__array_ufunc__</code> is set (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="225304528" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/9028" data-hovercard-type="issue" data-hovercard-url="/numpy/numpy/issues/9028/hovercard" href="https://github.com/numpy/numpy/issues/9028">#9028</a>)</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> (<a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/mhvk/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/mhvk">@mhvk</a>) BUG: no way to override <code class="notranslate">__eq__</code> for structured <code class="notranslate">ndarray</code> (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="230230273" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/9153" data-hovercard-type="issue" data-hovercard-url="/numpy/numpy/issues/9153/hovercard" href="https://github.com/numpy/numpy/issues/9153">#9153</a>)</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> ENH/MAINT: let <code class="notranslate">ndarray.__array_ufunc__</code> be the overall gateway to applying ufuncs on <code class="notranslate">ndarray</code>; that way, this function can just participate in the different trials of objects to see if they can handle the ufunc. As part of that, should they just turn subclasses into regular arrays (since those have had a chance anyway? see discussion in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="227439179" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/9079" data-hovercard-type="issue" data-hovercard-url="/numpy/numpy/issues/9079/hovercard" href="https://github.com/numpy/numpy/issues/9079">#9079</a>)</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> (<a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/shoyer/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/shoyer">@shoyer</a>) Document the table of ndarray arithmetic special methods and corresponding ufuncs in the proper docs (e.g., <code class="notranslate">reference/array.classes.rst</code>, not the NEP).</p>
</li>
</ul>