Broadcasting involves 2 steps

  • give all arrays the same number of dimensions

  • expand the 1 dimensions to match the other arrays

With your inputs

(41,6) (41,)

one is 2d, the other 1d; broadcasting can change the 1d to (1, 41), but it does not automatically expand in the other direction (41,1).

(41,6) (1,41) 

Neither (41,41) or (6,41) matches the other.

So you need to change your y to (41,1) or the x to (6,41)

x.T*y
x*y[:,None]

I'm assuming, of course, that you want element by element multiplication, not the np.dot matrix product.

Answer from hpaulj on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.4 Manual
If the last dimension of x1 is not the same size as the second-to-last dimension of x2. If a scalar value is passed in. ... Complex-conjugating dot product for stacks of vectors. ... Matrix-vector product for stacks of matrices and vectors. ... Vector-matrix product for stacks of vectors and matrices. ... Sum products over arbitrary axes. ... Einstein summation convention. ... The behavior depends on the arguments in the following way. If both arguments are 2-D they are multiplied like conventional matrices.
Discussions

numpy - Multidimensional matrix multiplication in python - Stack Overflow
I want to multiply each of 1x2000x30 from A with matrix B to obtain new matrix of size 1x2000x5. i.e. A X B should give me a matrix of dimension 500x2000x5 · Obviously looping 500 times through matrix A is a solution but is there an efficient way to achieve this? ... That depends highly on how your data is represented. If you have numpy ... More on stackoverflow.com
🌐 stackoverflow.com
python - Multiplying specific dimension matrices - Stack Overflow
Numpy matrices are 2D only and matrix multiplication is achived with the * operator. (With arrays this perfroms element by element multiplication) ... To keep the dimensions straight I'm going to use variables. Often in testing I like to use different sizes in each dimension (e.g. More on stackoverflow.com
🌐 stackoverflow.com
How can arrays with different dimensions be multiplied? - Python - Data Science Dojo Discussions
I encountered an error while attempting to multiply two numpy arrays with different dimensions in my data science project. Could anyone kindly share insights on the correct ways to multiply matrices with disparate dimensions in the context of numpy arrays? Your assistance with a code snippet ... More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
3
0
February 28, 2023
Python Numpy matrix multiplication in high dimension - Stack Overflow
I am trying to look for a matrix operation in numpy that would speed up the following calculation. I have two 3D matrices A and B. the first dimension indicates the example, and both of them have More on stackoverflow.com
🌐 stackoverflow.com
May 10, 2014
🌐
DigitalOcean
digitalocean.com › community › tutorials › numpy-matrix-multiplication
NumPy Matrix Multiplication: Methods and Examples | DigitalOcean
August 4, 2022 - Then multiply the corresponding elements and then add them to reach the matrix product value. ... The matrix product of two arrays depends on the argument position. So matmul(A, B) might be different from matmul(B, A). The numpy dot() function returns the dot product of two arrays. The result is the same as the matmul() function for one-dimensional and two-dimensional arrays.
Top answer
1 of 2
5

If you have numpy arrays you can use the np.dot function for this:

np.dot(A, B)

It will do exactly what you want, i.e. "contract" the last axis of A with the first axis of B:

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the last axis of a and the second-to-last of b:

 dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
2 of 2
0

First of all any multidimensional array is a n times b split of a flatten array. The 2,3,2 dimension array, [ [ [A,B],[C,D],[E,F] ], [ [G,H],[I,J],[K,L] ] ] is simply divide A,B,C,D,E,F,G,H,I,J,K,L into 2 pieces , then 3 pieces, then 2 pieces again and there is your multidimensional array. Vectors are here two dimensional.

Secondly, two dimensional matrix multiplication is also fixed length vector multiplication with a combination. (n,m)*(m,l) dimensional matrix multiplication is actually term by term multiplication and sum of results of l different m vectors and n different m vectors. l times n combination of two different m vectors.

When you get that you simply can convert 20 dimension matrix to two dimension matrix with same vector size and multiply them and reshape it back.

Quick Example:

import numpy as np

a=np.random.random(120).reshape(2,5,3,4)
b=np.random.random(120).reshape(5,3,4,2)

br=np.rollaxis(b,3,2).reshape(30,4).T  
# converted all combinations to a series of 4 dimension vectors here 

test1=np.matmul(a.reshape(30,4),br).reshape(2,5,3,5,3,2)
test2=np.dot(a,b)

np.all(test1==test2)

returns

array(True)

lets do it basically for all combinations (dimensions)

sa=a.shape
sb=b.shape
res=np.ndarray([sa[0],sa[1],sb[0],sb[1],sa[2],sb[3]])
for i in range(a.shape[0]):
  for j in range(a.shape[1]):
    for k in range(b.shape[0]):
      for n in range(b.shape[1]):
        x=np.matmul(a[i,j],b[k,n])
        np.append(res,x)
print(np.any(res==np.dot(a,b).reshape(2,5,5,3,3,2)))

returns

True

USE CASE:

Suppose we have a case such as for 10 different materials and 20 different geometries 3 different physical quantity in 3 dimensions vectors will be matrix multiplied for a geometry and physics processing neural network layer which will be calculated with genetic selective algorithm which has neural connection coefficient vectors of 5 different groups of population each having 100 different gene sequence (population) and 20 neural nodes. In a such case you may benefit calculating it at once or you can somehow serialize this calculation to two flat arrays and send it to your gpu or cpu according to your concurrent free ram amount. In such case you may want to understand how this calculations work.

In any case of multidimensional matrix calculation combinations of vectors are calculated term by term You may want to multiply first term with seconds vectors last term and sum the rest. It is up to you but understanding how it works is important. So here is a simple illustration I have used to undestand this.

[ [ [A,B],[C,D],[E,F] ], [ [G,H],[I,J],[K,L] ] ] --> ABCDEFGHIJKL [ [ [1,2],[3,4],[5,6] ], [ [7,8],[9,0],[€,$] ] ] --> 1234567890€$

use operator(multiply) term by term shifting first array by amount of vector size (2)

ABCDEFGHIJKL   CDEFGHIJKLAB  FGHIJKLABCDE ...
1234567890€$   1234567890€$  1234567890€$ ...

Here comes all combinations

append all of them and reshape and use another operator (+)

[A+2*B],[C*3+D*4],[E*5,F*6] ...... [I*€+J*$] 

Hope this helps and saves time to grasp this.

Find elsewhere
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How can arrays with different dimensions be multiplied? - Python - Data Science Dojo Discussions
February 28, 2023 - I encountered an error while attempting to multiply two numpy arrays with different dimensions in my data science project. Could anyone kindly share insights on the correct ways to multiply matrices with disparate dimens…
Top answer
1 of 2
4

This is a typical application for np.tensordot():

sum = np.tensordot(A, B, [[0,2],[0,1]])

Timing

Using the following code:

import numpy as np

n_examples = 100
A = np.random.randn(n_examples, 20,30)
B = np.random.randn(n_examples, 30,5)

def sol1():
    sum = np.zeros([20,5])
    for i in range(len(A)):
      sum += np.dot(A[i],B[i])
    return sum

def sol2():
    return np.array(map(np.dot, A,B)).sum(0)

def sol3():
    return np.einsum('nmk,nkj->mj',A,B)

def sol4():
    return np.tensordot(A, B, [[2,0],[1,0]])

def sol5():
    return np.tensordot(A, B, [[0,2],[0,1]])

Results:

timeit sol1()
1000 loops, best of 3: 1.46 ms per loop

timeit sol2()
100 loops, best of 3: 4.22 ms per loop

timeit sol3()
1000 loops, best of 3: 1.87 ms per loop

timeit sol4()
10000 loops, best of 3: 205 µs per loop

timeit sol5()
10000 loops, best of 3: 172 µs per loop

on my computer the tensordot() was the fastest solution and changing the order that the axes are evaluated did not change the results neither the performance.

2 of 2
2

Ha, it can be done in just one line: np.einsum('nmk,nkj->mj',A,B).

See Einstein summation: http://docs.scipy.org/doc/numpy/reference/generated/numpy.einsum.html

Not the same problem but the idea is quite much the same, see discussions and alternative methods in this topic we just discussed: numpy multiply matrices preserve third axis

Don't name your variable sum, you override the build-in sum.

As @Jaime pointed out, the loop is actually faster for dimensions of these size. In fact a solution based on map and sum is, albeit simpler, even slower:

In [19]:

%%timeit
SUM = np.zeros([20,5])
for i in range(len(A)):
  SUM += np.dot(A[i],B[i])
10000 loops, best of 3: 115 µs per loop
In [20]:

%timeit np.array(map(np.dot, A,B)).sum(0)
1000 loops, best of 3: 445 µs per loop
In [21]:

%timeit np.einsum('nmk,nkj->mj',A,B)
1000 loops, best of 3: 259 µs per loop

Thing are different with larger dimension:

n_examples = 1000
A = np.random.randn(n_examples, 20,1000)
B = np.random.randn(n_examples, 1000,5)

And:

In [46]:

%%timeit
SUM = np.zeros([20,5])
for i in range(len(A)):
  SUM += np.dot(A[i],B[i])
1 loops, best of 3: 191 ms per loop
In [47]:

%timeit np.array(map(np.dot, A,B)).sum(0)
1 loops, best of 3: 164 ms per loop
In [48]:

%timeit np.einsum('nmk,nkj->mj',A,B)
1 loops, best of 3: 451 ms per loop
🌐
Reddit
reddit.com › r/learnpython › how to multiply matrices with different dimensions
r/learnpython on Reddit: How to multiply matrices with different dimensions
December 5, 2021 -

I'm trying to multiply A = 20x1x10 with B = 10x20 and I'm supposed to get a 20x1x20 matrix as the output. So far I've tried

C = numpy.matmul(A,B)
# and 
C = A @ B

But all of them seem to result a 20x20x20 matrix. Any idea how to proceed from here?

For better context, A = jacobian output of derivative of an activation function in a neural network, with shape of (N samples * 1 * values) and B = changes in output of the layer

🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to do matrix multiplication in numpy
How to do Matrix Multiplication in NumPy - Spark By {Examples}
March 27, 2024 - You can multiply a 2-dimensional matrix by another 2-dimensional matrix using np.dot(). When you multiply two matrices it should follow the order i.e. matrix X multiplied by matrix Y is not the same as matrix Y multiplied by matrix X. Let’s ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-3d-matrix-multiplication
NumPy - 3D matrix multiplication - GeeksforGeeks
July 23, 2025 - Here we will see two different examples of matrix multiplication where we have used different dimensions in each example.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.6.dev0 Manual
If the last dimension of x1 is not the same size as the second-to-last dimension of x2. If a scalar value is passed in. ... Complex-conjugating dot product for stacks of vectors. ... Matrix-vector product for stacks of matrices and vectors. ... Vector-matrix product for stacks of vectors and matrices. ... Sum products over arbitrary axes. ... Einstein summation convention. ... The behavior depends on the arguments in the following way. If both arguments are 2-D they are multiplied like conventional matrices.
🌐
DataCamp
datacamp.com › doc › numpy › matrix-multiplication
NumPy Matrix Multiplication
import numpy as np result = np.matmul(matrix_a, matrix_b) # or result = matrix_a @ matrix_b · In these syntaxes, matrix_a and matrix_b are arrays or matrices to be multiplied, producing a new matrix as the result. For higher-dimensional arrays, np.matmul() and @ handle the last two dimensions as matrices and broadcast the remaining dimensions, unlike np.dot(), which performs a more general dot product.
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v1.13 Manual
June 10, 2017 - If the second argument is 1-D, ... 1 to its dimensions. After matrix multiplication the appended 1 is removed. Multiplication by a scalar is not allowed, use * instead. Note that multiplying a stack of matrices with a vector will result in a stack of vectors, but matmul will not recognize it as such. ... Multiplication by scalars is not allowed. Stacks of matrices are broadcast together as if the matrices were elements. ... This function is preliminary and included in NumPy 1.10.0 for ...
🌐
Educative
educative.io › answers › how-to-multiply-matrices-in-numpy
How to multiply matrices in NumPy
For example, if matrix 1 has dimensions a * N and matrix 2 has dimensions N * b, then the resulting matrix has dimensions of a * b. ... To multiply two matrices use the dot() function of NumPy.
🌐
Educative
educative.io › blog › numpy-matrix-multiplication
NumPy matrix multiplication: Get started in 5 minutes
April 30, 2026 - When using this method, both matrices should have the same dimensions. ... We can pass certain rows, columns, or submatrices to the numpy.multiply() method. The sizes of the rows, columns, or submatrices that we pass as our operands should be ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › numpy tutorial › matrix multiplication in numpy
Matrix Multiplication in NumPy | Different Types of Matrix Multiplication
March 20, 2023 - If you wish to perform element-wise matrix multiplication, then use np.multiply() function. The dimensions of the input matrices should be the same. And if you have to compute matrix product of two given arrays/matrices then use np.matmul() function.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Medium
medium.com › @whyamit101 › different-ways-to-multiply-arrays-in-numpy-65aa2522e265
Different Ways to Multiply Arrays in NumPy | by why amit | Medium
February 9, 2025 - They don’t align, and NumPy won’t perform the operation. Here’s the good news: reshaping arrays can often solve this issue. The .reshape() method lets you adjust an array’s dimensions so they become compatible for multiplication.