[SciPy-user] logexpdot
Brian Lewis
brian.lewis17 at gmail.com
Mon Jun 15 18:21:59 EDT 2009
Hi all,
This is my first foray into np.newaxis. I needed a dot product for
logarithm arrays and since I am always working with matrices, I just did
2D. I'm looking for comments and suggestions for what I've implemented.
Also, is it easy to generalize this beyond 2D?
import numpy as np
def logdotexp2__1(x,y, out=None):
# horrible
r,c = x.shape[0], y.shape[1]
if out is None:
out = np.empty((r,c))
for i in xrange(r):
for j in xrange(c):
out[i,j] = np.logaddexp2.reduce(x[i,:]+y[:,j])
return out
def logdotexp2__2(x,y, out=None):
# less horrible
r,c = x.shape[0], y.shape[1]
if out is None:
out = np.empty((r,c))
for i in xrange(r):
np.logaddexp2.reduce(x[i,:,np.newaxis]+y, out=out[i,:])
return out
def logdotexp2__3(x,y, out=None):
# good?
r,c = x.shape[0], y.shape[1]
if out is None:
out = np.empty((r,c))
np.logaddexp2.reduce(x[:,:,np.newaxis]+y[np.newaxis,:,:], axis=1,
out=out)
return out
if __name__ == '__main__':
k = 10
a = np.arange(k**2).reshape((k,k))
b = np.arange(k**2,2*k**2).reshape((k,k))
# timeit logdotexp2__1(a,b)
# timeit logdotexp2__2(a,b)
# timeit logdotexp2__3(a,b)
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090615/e9d43c59/attachment.html>
More information about the SciPy-User
mailing list