Search code examples
linear-algebranumpy-ndarray

is numpy representation a column vector?


Let's say I want to multiply the matrix w with vector v. (I just discovered that we don't have support for math in SO... annoying)

enter image description here

w = np.array([np.array([1,3]), np.array([2,4])])
v = np.array([np.array([5]), np.array([6])])
print(w)
print(v)
print(np.dot(w,v))

[[1 3]
 [2 4]]
[[5]
 [6]]
[[23]
 [34]]

This code seems completely unituitive. A vector is a representation of multiple components. So I expect the vector to be an array e.g. np.array([5,6]) This makes a lot of sense because we have a unit of a vector with 2 components.

It becomes even bizarre with the matrix. [1,2] are the components for the first basis vector, but we have to write it across 2 different vectors? I expected the syntax to be np.array([np.array([1,2]), np.array([3,4])]). If we write it this way, it makes all components of the first basis vector to sit nicely in an array.

Am I missing something here or numpy syntax is completely bonkers?


Solution

  • This code seems completely unituitive. A vector is a representation of multiple components.

    Strictly speaking, the code in the question isn't a multiplying a matrix times a vector. It's multiplying a matrix times a matrix, which is why the result is 2D.

    So I expect the vector to be an array e.g. np.array([5,6]) This makes a lot of sense because we have a unit of a vector with 2 components.

    You can do that. If you dot a 2D array with a 1D array, NumPy will interpret it as a matrix times a vector. Example:

    w = np.array([np.array([1,3]), np.array([2,4])])
    v = np.array([5, 6])
    
    print(w)
    print(v)
    print(np.dot(w,v))
    # [[1 3]
    #  [2 4]]
    # [5 6]
    # [23 34]
    

    It becomes even bizarre with the matrix. [1,2] are the components for the first basis vector, but we have to write it across 2 different vectors? I expected the syntax to be np.array([np.array([1,2]), np.array([3,4])]). If we write it this way, it makes all components of the first basis vector to sit nicely in an array.

    If you prefer to represent a matrix as being composed as a series of column vectors rather than row vectors, you can do that.

    Example 1:

    w = np.array([[1, 2], [3, 4]]).T
    

    This creates an array, then transposes it.

    Example 2:

    w = np.column_stack([[1, 2], [3, 4]])
    

    This directly creates the array via column vectors.

    Either approach seems like it would work.