0

As the title says I want to calculate the component-wise product of all column combinations of two matrices. I already found a solution using numpy.einsum and numpy.hstack. I wonder if there is a solution without hstack.

Let a = [a_1, a_2, ..., a_n] be a d x n matrix and b = [b_1, b_2, ..., b_m] a d x m matrix. I want to calculate

[a_1b_1, a_1b_2, ..., a_nb_{n-1}, a_nb_n],

where a_kb_l is the component wise product, i.e. a_kb_l = [a_{1,k}*b{1,l}, ..., a_{d,k}*b{d,l}].T.

My solution is the following. np.hstack(np.einsum('...j,...l -> j...l', a, b))

Can I go without the h_stack?

mbauman
  • 30,958
  • 4
  • 88
  • 123
paul
  • 31
  • 2

1 Answers1

0

The following improvement replaces the hstack with a call of reshape. This releases quite a bit memory pressure when d is high.

np.einsum('...j,...l -> ...jl', a, b).reshape(d, -1)

paul
  • 31
  • 2