[Numpy-discussion] Trick for fast

josef.pktd@gmai... josef.pktd@gmai...
Fri Feb 3 15:56:42 CST 2012


On Fri, Feb 3, 2012 at 4:49 PM, Alan G Isaac <alan.isaac@gmail.com> wrote:
> On 2/3/2012 3:37 PM, josef.pktd@gmail.com wrote:
>> res = - np.dot(x.T, mass*x)
>> res[np.arange(3), np.arange(3)] -= np.trace(res)
>
>
> Nice!
> Get some speed gain with slicing:
>
> res = - np.dot(x.T, mass*x)
> res.flat[slice(0,None,4)] -= np.trace(res)

Actually, I thought about the most readable version using diag_indices

>>> di = np.diag_indices(3)
>>> res = - np.dot(x.T, mass*x)
>>> res[di] -= np.trace(res)

(but I thought I said already enough)

Josef

>
> Alan
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list