0

I am trying to compute a transform given by b = A*x. A is a (3,4) matrix. If x is one (4,1) vector the result is b (3,1).

Instead, for x I have a bunch of vectors concatenated into a matrix and I am trying to evaluate the transform for each value of x. So x is (20, 4). How do I broadcast this in numpy such that I get 20 resulting values for b (20,3)?

I could loop over each input and compute the output but it feels like there must be a better way using broadcasting.

Eg.

A = [[1,0,0,0],
[2,0,0,0],
[3,0,0,0]]

if x is:

x = [[1,1,1,1],
[2,2,2,2]]

b = [[1,2,3],
[2,4,6]]

Each row of x is multiplied with A and result is stored as a row in b.

user1159898
  • 25
  • 1
  • 6
  • "for x I have a bunch of vectors concatenated into a matrix and I am trying to evaluate the transform for each value of x. So x is (20, 4)." <- Can you elaborate more on this and give an example? – Gustavo Bezerra Feb 29 '16 at 03:03

1 Answers1

0

numpy dot

import numpy as np

A = np.random.normal(size=(3,4))

x = np.random.normal(size=(4,20))

y = np.dot(A,x)

print y.shape

Result: (3, 20)

And of course if you want (20,3) you can use np.transpose()

Keith Brodie
  • 657
  • 3
  • 17