Reputation: 1644
I'm looking to combine 3 matrixes in a particular way, I'm sure this will be easy for anyone used to using numpy:
w = np.array([[-0.46733567, 0.38864732],
[-0.42436867, -1.08760098],
[-1.01118741, 0.99096466]])
a = array([[ 0.63127368, 0.00167775, 0.97812284]])
d = [[-0.43252997]] # although d is length 1 in this example, its often >1 in length and code needs to be able to generalize, there will always be the same length of cols and and arrays between a and w, i.e in this example there are 3 arrays in w and 1 of length 3 in a (please excuse my terminology, I am new to linear algebra).
I am trying to combine them to find dx so that:
dx1 = [-0.46733567, 0.38864732] * 0.63127368 * -0.43252997
dx2 = [-0.42436867, -1.08760098] * 0.00167775 * -0.43252997
dx3 = [-1.01118741, 0.99096466] * 0.97812284 * -0.43252997
if d is > length 1, e.g. d= np.array([[-0.43252997],[0.87500009]]) then:
dx1_1 = [-0.46733567, 0.38864732] * 0.63127368 * -0.43252997
dx2_1 = [-0.42436867, -1.08760098] * 0.00167775 * -0.43252997
dx3_1 = [-1.01118741, 0.99096466] * 0.97812284 * -0.43252997
dx1_2 = [-0.46733567, 0.38864732] * 0.63127368 * 0.87500009
dx2_2 = [-0.42436867, -1.08760098] * 0.00167775 * 0.87500009
dx3_2 = [-1.01118741, 0.99096466] * 0.97812284 * 0.87500009
I tried all of these but with seemingly no success, unless I'm missing something?
out = np.dot(d, w) * a
out = np.dot(d, w) * a.T
out = np.dot(d, w.T) * a
out = np.dot(a, w) * d
out = np.dot(a, w.T) * d
I get an error in many cases:
ValueError: shapes (3,1) and (3,2) not aligned: 1 (dim 1) != 3 (dim 0)
any pointers to help solve this would be much appreciated
Upvotes: 0
Views: 1684
Reputation: 2124
You don't need any dot product, since you dont want to sum anything.
I am repeating your arrays in a third axis (this gets relevant if d is longer than 1).
And then multiply the appropriate axis...:
w = np.array([[-0.46733567, 0.38864732],
[-0.42436867, -1.08760098],
[-1.01118741, 0.99096466]])
a = np.array([[ 0.63127368, 0.00167775, 0.97812284]])
d = np.array([[-0.43252997]])
(dim1, dim2), dim3 = w.shape, len(d[0])
wnew = w.reshape(dim1, dim2, 1)
anew = a.reshape(dim1, 1, 1)
dnew = d.reshape(1, 1, dim3)
product = wnew * anew * dnew
i, j = 0, 0
print product[i, :, j]
the last statement prints what you called dx_ij
Upvotes: 3
Reputation: 221714
Going by the format of the input arrays, specifically that shapes of all input arrays are 2D
and assuming you would like to store the output in a 3D
array, here's an approach using broadcasting
-
(w.T)[:,:,None]*(a[0,:,None]*d[:,0])
Sample run -
In [48]: # Input arrays
...: w = np.array([[-0.46733567, 0.38864732],
...: [-0.42436867, -1.08760098],
...: [-1.01118741, 0.99096466]])
...:
...: a = np.array([[ 0.63127368, 0.00167775, 0.97812284]])
...:
...: d= np.array([[-0.43252997],[0.87500009]])
...:
...: dx1_1 = np.array([-0.46733567, 0.38864732]) * 0.63127368 * -0.43252997
...: dx2_1 = np.array([-0.42436867, -1.08760098]) * 0.00167775 * -0.43252997
...: dx3_1 = np.array([-1.01118741, 0.99096466]) * 0.97812284 * -0.43252997
...: dx1_2 = np.array([-0.46733567, 0.38864732]) * 0.63127368 * 0.87500009
...: dx2_2 = np.array([-0.42436867, -1.08760098]) * 0.00167775 * 0.87500009
...: dx3_2 = np.array([-1.01118741, 0.99096466]) * 0.97812284 * 0.87500009
...:
...: # Let's store these in a 3D array
...: p1 = np.column_stack((dx1_1,dx2_1,dx3_1))
...: p2 = np.column_stack((dx1_2,dx2_2,dx3_2))
...: out = np.dstack((p1,p2))
...:
Print outputs from original and proposed broadcasting based approaches for verification -
In [49]: out # Output from original approach
Out[49]:
array([[[ 1.27603568e-01, -2.58139646e-01],
[ 3.07954650e-04, -6.22986533e-04],
[ 4.27800472e-01, -8.65432403e-01]],
[[ -1.06118124e-01, 2.14674993e-01],
[ 7.89247187e-04, -1.59663239e-03],
[ -4.19244884e-01, 8.48124609e-01]]])
In [50]: (w.T)[:,:,None]*(a[0,:,None]*d[:,0]) # Output from proposed solution
Out[50]:
array([[[ 1.27603568e-01, -2.58139646e-01],
[ 3.07954650e-04, -6.22986533e-04],
[ 4.27800472e-01, -8.65432403e-01]],
[[ -1.06118124e-01, 2.14674993e-01],
[ 7.89247187e-04, -1.59663239e-03],
[ -4.19244884e-01, 8.48124609e-01]]])
If the input arrays a
and d
are 1D
arrays :
a = np.array([ 0.63127368, 0.00167775, 0.97812284])
d = np.array([-0.43252997,0.87500009])
, the solution would simplify to -
(w.T)[:,:,None]*(a[:,None]*d)
Upvotes: 2
Reputation: 10450
When you multiple two matrix they should have a shared dimension. In your example:
>>> out = np.dot(a, w)
>>> print out
[[-1.28479419 1.21280327]]
for more info: https://en.wikipedia.org/wiki/Matrix_multiplication If A is an n × m matrix and B is an m × p matrix,the matrix product AB (denoted without multiplication signs or dots) is defined to be the n × p matrix.
Best Regards, Yaron
Upvotes: 1