You can use np.einsum to calculate the dot products and create the matrix of the desired shape:

Copynp.einsum('ijk,ikl->ijl', A, B)
Answer from Alex Riley on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › user › basics.broadcasting.html
Broadcasting — NumPy v2.4 Manual
The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations ...
Discussions

What is the difference between numpy Broadcasting and Matrix Multiplication?
Broadcasting in numpy is used to refer to the fact that you can do stuff like: np.arange(12).reshape(3, 4) + np.arange(3).reshape(3, 1) which adds np.arange(3).reshape(3, 1) to each column in np.arange(12).reshape(3, 4) In fact, broadcasting has very little to do with matrix multiplication; for more explanation see this . More on reddit.com
🌐 r/learnpython
3
1
December 10, 2020
Broadcasting rules for linalg.solve are different to those for matmul
The issue is with the line numpy/numpy/linalg/linalg.py Line 402 in d35cd07 if b.ndim == a.ndim - 1: matmul uses the matrix-matrix interpretatin most of the time. It expects the last axis of a to m... More on github.com
🌐 github.com
2
May 10, 2024
python - How do I broadcast other dimensions using numpy.matmul? - Stack Overflow
I have an 640x480 RGB image with numpy shape (480, 640, 3) -- 3 for the R, G, B channels and I would like to at each pixel, transform the RGB value according to: # for each pixel for i in r... More on stackoverflow.com
🌐 stackoverflow.com
python - Using numpy matmul in row-wise manner with broadcasting - Stack Overflow
I have an array of 3D points (n,3) that are to be rotated about the origin using a 3x3rotation matrix that is stored in the form of a nx3x3 array. At the moment I'm simply doing this in a for loop ... More on stackoverflow.com
🌐 stackoverflow.com
April 6, 2021
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.4 Manual
If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly. If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_matmul.htm
numpy.matmul()
The numpy.matmul() function returns the matrix product of two arrays. While it returns a normal product for 2-D arrays, if dimensions of either argument is >2, it is treated as a stack of matrices residing in the last two indexes and is broadcast
🌐
SciPy
docs.scipy.org › doc › numpy-1.12.0 › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v1.12 Manual
If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 02.05-computation-on-arrays-broadcasting.html
Computation on Arrays: Broadcasting | Python Data Science Handbook
Broadcasting allows these types of binary operations to be performed on arrays of different sizes–for example, we can just as easily add a scalar (think of it as a zero-dimensional array) to an array: ... We can think of this as an operation that stretches or duplicates the value 5 into the ...
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.3 Manual
If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly. If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
Find elsewhere
🌐
DataCamp
datacamp.com › doc › numpy › matrix-multiplication
NumPy Matrix Multiplication
Broadcasting in this context refers to the automatic expansion of dimensions to enable element-wise operations. Ensure dimensional compatibility. The number of columns in the first matrix must equal the number of rows in the second matrix for multiplication. Use the @ operator for simplicity.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.1 Manual
If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly. If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.1 › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v1.10 Manual
If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › Broadcasting.html
Array Broadcasting — Python Like You Mean It
Here we provide a simple real-world example where broadcasting is useful. Suppose you have a grade book for 6 students, each of whom have taken 3 exams; naturally, you store these scores in a shape-(6,3) array: # grades for 6 students who have taken 3 exams # axis-0 (rows): student # axis-1 (columns): exams >>> import numpy as np >>> grades = np.array([[ 0.79, 0.84, 0.84], ...
🌐
Finxter
blog.finxter.com › home › learn python blog › numpy broadcasting – a simple tutorial
NumPy Broadcasting - A Simple Tutorial - Be on the Right Side of Change
July 28, 2021 - Broadcasting describes how NumPy automatically brings two arrays with different shapes to a compatible shape during arithmetic operations. Generally, the smaller array is “repeated” multiple times until both arrays have the same shape.
🌐
NumPy
numpy.org › doc › 1.20 › user › theory.broadcasting.html
Array Broadcasting in Numpy — NumPy v1.20 Manual
We can think of the scalar b being stretched during the arithmetic operation into an array with the same shape as a. The new elements in b, as shown in Figure 1, are simply copies of the original scalar. The stretching analogy is only conceptual. numpy is smart enough to use the original scalar value without actually making copies so that broadcasting operations are as memory and computationally efficient as possible.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.5.dev0 Manual
If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly. If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
🌐
GitHub
github.com › numpy › numpy › issues › 26421
Broadcasting rules for linalg.solve are different to those for matmul · Issue #26421 · numpy/numpy
May 10, 2024 - matmul only uses the vector version if an array has exactly one dimension. ... With solve, on the other hand, it will use the vector version when b has one fewer dimension than a: >>> from numpy.linalg import solve >>> solve(a, b) ValueError: solve1: Input operand 1 has a mismatch in its core dimension 0...
Published   May 10, 2024
Author   subhylahiri
🌐
Stack Overflow
stackoverflow.com › questions › 64200362 › how-do-i-broadcast-other-dimensions-using-numpy-matmul
python - How do I broadcast other dimensions using numpy.matmul? - Stack Overflow
I have an 640x480 RGB image with numpy shape (480, 640, 3) -- 3 for the R, G, B channels and I would like to at each pixel, transform the RGB value according to: # for each pixel for i in range(img.shape[0]): for j in range(img.shape[1]): # set the new RGB value at that pixel to # (new RGB vector) = (const matrix A) * (old RGB vector) + (const vector B) img[i,j,:] = np.reshape((np.matmul(A, np.expand_dims(img[i,j,:], axis = 1)) + B), (3,))
🌐
GitHub
github.com › onnx › onnx › issues › 2897
Does MatMul operator implement broadcasting for inputs having more than 2 dimensions? · Issue #2897 · onnx/onnx
July 14, 2020 - numpy.matmul seems to do broadcasting if either argument is N-D with N > 2, ("each input treated as a stack of matrices residing in the last two indexes and broadcast accordingly").
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-array-broadcasting
NumPy Array Broadcasting - GeeksforGeeks
December 5, 2025 - Broadcasting in NumPy allows us to perform arithmetic operations on arrays of different shapes without reshaping them. It automatically adjusts the smaller array to match the larger array's shape by replicating its values along the necessary ...