How does multiplication work when a 3D array is dotted with a 1D array?

554 Views Asked by At

EDIT: cleaned up as suggested by comments

I know the shortcut to seeing if a matrix multiplication will work out okay:

enter image description here

But how does this rule generalize when you have multidimensional arrays? I ran across a situation when doing some computations on images.

I have an array that consists of 2,048 7 x 7 images. So the dims are (7, 7, 2048).

I have an array that is just 2048 scalars. Dims are (2048,).

How come, when I dot these using a standard linear algebra library (numpy, in Python), I get back an array of dims (7, 7)??

I'm trying to think visualize this in my head, and on scratch paper, using the dimension property from the graphic above: (7 x 7 x 2048) (2048) --> (7 x 7). Very confusing. I don't understand what action this operation will perform on my 3D volume of image data. I looked at this and thought for sure this couldn't be computed by a basic linear algebra library, but I was wrong.

Here is the problem in python, a quick copypasta and run will explain:

import numpy as np
def make_3d_array(dim_x, dim_y, dim_z, val_per_z):
    '''
    this will make a 3D array. Each slice (x, y plane) will have a value you give it, from a list called val_per_z.
    '''
    print(len(val_per_z))
    print(dim_z)
    assert len(val_per_z) == dim_z
    l = []  # store arrays in list
    for n in range(dim_z):  
        two_d_array = np.full((dim_x, dim_y, 1), val_per_z[n])
        l.append(two_d_array)
    a = np.concatenate(l, axis=2)
    return a

val_per_z = np.random.uniform(low=0.0, high=1.0, size=(2048,))

a = make_3d_array(7,7,2048, val_per_z)
print("here's the shape of my array:", a.shape)

b = np.random.uniform(low=0.0, high=1.0, size=(2048,))
print("here's the shape of my other array:", b.shape)

ans = a.dot(b)
print("This works, but the dimensions are super confusing! How can you dot a 3D array with a 1D vector and get a 2D result?? Here's the shape:", ans.shape)