I know in numpy if you have a matrix A and I subtract a vector v by performing A - v, v will be broadcasted so that v becomes the same dimension as A and an elementwise subtraction will be performed.
I was wondering if I had to perform the above operation many times with the same A but with different v, would I be able to do it using vectorization.
A naive implementation would be as follow
def foo(A, V):
"""
@params: A, V
A - an n by m matrix
V - a list of n by 1 column vectors
@returns: result
"""
result = []
for v in V:
result.append(A-v)
return result
I know that I could pass in V as a n by p matrix where each column represents a v. However, I couldn't think of any way of describing the above operation in terms of linear algebra
For example if
A = [[1 2 3],
[1 2 3]]
V = [[1 4],
[1 4]]
the output should be two matrices
[
[[0 1 2],
[0,1,2]],
[[-3 -2 -1],
[-3 -2, -1]]
]