outer
The Ufuncs NumPy method handles multidimensional input the way you want, so you can do
numpy.multiply.outer(a, b)
instead of using numpy.outer
.
; multiply.outer
:
import numpy
import perfplot
def multiply_outer(data):
a, b = data
return numpy.multiply.outer(a, b)
def outer_reshape(data):
a, b = data
return numpy.outer(a, b).reshape((a.shape + b.shape))
def tensor_dot(data):
a, b = data
return numpy.tensordot(a, b, 0)
perfplot.save(
"out.png",
setup=lambda n: (numpy.random.rand(n, n), numpy.random.rand(n, n)),
kernels=[multiply_outer, outer_reshape, tensor_dot],
n_range=[2 ** k for k in range(7)],
logx=True,
logy=True,
)