I have seen and executed a python program where a matrix has been sliced into a column vector using a[:,j] and passed into a function. The column vector has dimensions 40000x1. While tracing the output, I printed the dimensions of a in the function and it printed (40000,). In the function, this matrix is multiplied with a matrix b of dimensions 1x40000. I printed the dimensions of the result and it says the result is also 1x40000. How is this possible? I have read that a will be a column vector(obviously) but how can the product generate a 1x40000 matrix? It doesn't satisfy the matrix multiplication rule. I'm using numpy.
EDIT:
Code:
def update_state(xk, sk, wx, wRec):
print("Sizes:")
print(xk.shape)
print(wx.shape)
print((xk*wx).shape)
print((xk * wx + sk * wRec).shape)
return xk * wx + sk * wRec
def forward_states(X, wx, wRec):
S = np.zeros((X.shape[0], X.shape[1]+1))
for k in range(0, X.shape[1]):
S[:,k+1] = update_state(X[:,k], S[:,k], wx, wRec)
return S
Output:
Sizes:
(40000,)
(1, 40000)
(1, 40000)
(1, 40000)
I am assuming that you are referring to the numpy package. There are other array packages available for Python, but that seems to be the most popular one.
numpy deals with multidimensional arrays, not matrices. There is a big difference in that a 1D array of N elements can be interpreted as either a Nx1 or a 1xN matrix, depending on how you choose to interpret it.
Another thing to be aware of is that the function numpy.multiply, a.k.a. the * operator is not the same as numpy.dot, a.k.a the @ operator. The operations involving * you see in your code do element-by element multiplication, not matrix multiplication as you seem to think.
numpy provides a number of different mechanisms for mapping elements of arrays with compatible dimensions to each other. The most common way, which is being used in your example, is called broadcasting.
Let us apply the information in the link to the operation kx * wx in your code. I will use a simplified version of kx and wx that will make it easier to illustrate the issue at hand:
>>> kx = np.arange(10)
>>> kx
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> kx.shape
(10,)
>>> wx = np.arange(10).reshape(1, 10)
>>> wx
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> wx.shape
(1, 10)
Here is a condensed version of the relevant broadcasting rules:
When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when:
- they are equal, or
- one of them is 1
... The size of the resulting array is the maximum size along each dimension of the input arrays.
In your case, a (N,) array is multiplied by a (1,N) array, resulting in a (1,N) array that contains the element-wise product of the inputs:
>>> >>> kx*wx
array([[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81]])
This has nothing to do with matrix multiplication. The remaining operations can be analyzed by the same rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With