Reputation: 993
I am having difficulty to understand why neither B or C equals A. How can I extract a specific row from f and calculate the same result as in A
?
import numpy as np
L = np.array([
[2.66667,1.33333],
[0.8,1.6]
])
f = np.array([[0.5,0.333333]])
A = L*f.T
B = L*f[0,:]
C = L*f[0,:].T
print(20*'*')
print(A)
print(20*'*')
print(B)
print(20*'*')
print(C)
print(20*'*')
Output:
********************
[[1.333335 0.666665 ]
[0.2666664 0.5333328]]
********************
[[1.333335 0.44444289]
[0.4 0.5333328 ]]
********************
[[1.333335 0.44444289]
[0.4 0.5333328 ]]
********************
Upvotes: 0
Views: 30
Reputation: 231615
L = np.array([
[2.66667,1.33333],
[0.8,1.6]
])
f = np.array([[0.5,0.333333]])
L
is (2,2), f
is (1,2). f.T
is (2,1)
A = L*f.T
This broadcasts the (2,2) with (2,1), replicating the the f.T
columns to (2,2).
B = L*f[0,:]
C = L*f[0,:].T
f[0,:]
is (2,) shape, 1d. f[0,:].T
is the same because there's only one 1 axis to switch.
Here (2,2) times (2,) => (2,2) times (1,2), That's the same as L*f
.
In A
you are multiplying the columns of L
by f
:
In [235]: L[0,:]*f[0,:]
Out[235]: array([1.333335 , 0.44444289])
In B
it's the rows of L
:
In [236]: L[:,0]*f[0,:]
Out[236]: array([1.333335 , 0.2666664])
The mix and match of rows and columns could be clearer with L
was (2,3) shaped.
In [34]: np.array([L[:,i]*f[0] for i in range(2)]).T
Out[34]:
array([[1.333335 , 0.666665 ],
[0.2666664, 0.5333328]])
In [35]: np.array([L[i,:]*f[0] for i in range(2)])
Out[35]:
array([[1.333335 , 0.44444289],
[0.4 , 0.5333328 ]])
I got [235] and [236] switched.
Upvotes: 1