0

I have a matrix A of size m X n and vector V of size m X 1

A = [1 2 3
     4 5 6
     8 9 10]
V = [1
     2
     3]

I want to compute A + V i.e., add the vector to every column of A

A = [2  3  4
     6  7  8
     11 12 13]

I have been reading through einsum notation, and also found similar questions answered for Scala, and Matlab. But, is it possible to define this operation in einsum notation?

  • 1
    Alternatively: `(A.T + V).T` works. – 9769953 Mar 05 '20 at 08:27
  • Thank you. But, in this case I am looking for an einsum notation. Would it be possible to elaborate on your answer? – Andrew Mathews Mar 05 '20 at 08:31
  • It's not even an answer: it's just a suggestion that there are other ways to obtain your answer. – 9769953 Mar 05 '20 at 08:33
  • `.T` transposes an array, which is required to make this column-wise, not row-wise, and the second `.T` untransposes the result. – AKX Mar 05 '20 at 08:33
  • 1
    @AndrewMathews I'm curious: why exactly do you need einsum notation for this? – AKX Mar 05 '20 at 08:33
  • I'm a tad doubtful einsum works here, because it would still multiply individual elements between the matrix and vector. Which is not what you want. – 9769953 Mar 05 '20 at 08:33
  • I am using a tool (distributed BLAS), and I need to express the same in einsum (the tool would take care about the distribution of data etc). – Andrew Mathews Mar 05 '20 at 08:35
  • I am having difficulty to understand how `(A.T + V).T` this would exactly work. I might be missing something very rudimentary. Any pointers as to where I need to look for to understand this better? Thank you. – Andrew Mathews Mar 05 '20 at 08:38
  • 1
    NumPy applies broadcasting, and will add the vector to every *row* of the matrix. Or rather, it will add *array* `V` to every inner array of the two-dimensional *array* `A`. Since you want to add to the columns, transposing `A`, then adding `V` with broadcasting, then transposing `A` back, does the trick. – 9769953 Mar 05 '20 at 08:55
  • `einsum` only does `sum of products`, a generalization of matrix multiplication, – hpaulj Mar 10 '20 at 20:24

0 Answers0