0
votes

In python-numpy, it is possible to define two multi-dimensional tensors with different sizes and multiply them. For example, here array "a" has shape of (1,3) and "b" of (2,1). The product has shape of (2,3). The makes sense in tensor product as: c_ij = b_i a_j. Although, tensor products with same shape in Eigen (A C++ library) is possible, can we do the same in Eigen?

from numpy import array as arr
a = arr([[1,2,3]])
b = arr([[5], [10]])
c = a * b
# Outputs
a
array([[1, 2, 3]])
b
array([[ 5],
       [10]])
c
array([[ 5, 10, 15],
       [10, 20, 30]])
1
I believe this question was answered here. Although, if you are asking about contractions more generally (not just outer products) then check the docs for eigen tensor contraction - DavidAce

1 Answers

0
votes

Just use the @ operator - b@a.

You can also do np.matmul(b, a).

For arrays with more dimensions, you can use einsum. In this case, np.einsum("ij,ai->aj", a, b) would give you the result you are expecting.

Yet another way of doing this - np.tensordot(a, b, axes=((0), (1))).T.

The last two methords are really useful when dealing with massive multi-dim arrays.