🌐
NumPy
numpy.org › doc › 2.4 › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.4 Manual
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › matrix-multiplication-in-numpy
Matrix Multiplication in NumPy - GeeksforGeeks
This method calculates dot product of two arrays, which is equivalent to matrix multiplication. ... Suposse there are two matrices A and B. A = [[1, 2], [2, 3]] B = [[4, 5], [6, 7]] So, A.B = [[1*4 + 2*6, 2*4 + 3*6], [1*5 + 2*7, 2*5 + 3*7] Result: ...
Published   September 22, 2025
🌐
Lawrence
www2.lawrence.edu › fast › GREGGJ › Python › numpy › numpyLA.html
numpy for Linear Algebra
For example, to print the bottom right entry in the matrix A we would do ... The first slice selects all rows in A, while the second slice selects just the middle entry in each row. To do a matrix multiplication or a matrix-vector multiplication we use the np.dot() method.
🌐
DataCamp
datacamp.com › doc › numpy › matrix-multiplication
NumPy Matrix Multiplication
Matrix multiplication in NumPy is used when you need to perform dot product operations between two matrices or a matrix and a vector. The numpy.matmul() or the @ operator can be used for this purpose.
🌐
DigitalOcean
digitalocean.com › community › tutorials › numpy-matrix-multiplication
NumPy Matrix Multiplication: Methods and Examples | DigitalOcean
August 4, 2022 - If you want element-wise matrix multiplication, you can use multiply() function. import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) arr_result = np.multiply(arr1, arr2) print(arr_result)
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.multiply.html
numpy.multiply — NumPy v2.4 Manual
numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'multiply'>#
🌐
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 - # Matrix multiplication matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) result = np.dot(matrix1, matrix2) # or use matrix1 @ matrix2 print("Matrix multiplication:\n", result) ... So, remember: * is for element-wise, np.dot() (or @) is for matrices. Use the right tool for the job! ... Yes, you can — but only if the shapes are compatible with NumPy broadcasting rules.
Find elsewhere
🌐
Medium
medium.com › @zackbunch › matrices-with-numpy-and-scipy-a260a65551c6
Python Matrices with NumPy and SciPy | Medium
January 11, 2022 - To multiply a matrix by a scalar, use NumPy’s * operator: i.e., c*A for matrix A and constant c.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.dot.html
numpy.dot — NumPy v2.4 Manual
If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.1 Manual
The matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors. ... 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. ... 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.
🌐
Reddit
reddit.com › r/learnpython › matrix multiplication in python using numpy
r/learnpython on Reddit: Matrix Multiplication in Python using Numpy
November 29, 2020 -

I solved the following problem on HackerRank:

https://www.hackerrank.com/challenges/np-dot-and-cross/problem

Here's my solution

import numpy as np
n = int(input())
arr_a = np.array([input().split() for _ in range(n)], int)
arr_b = np.array([input().split() for _ in range(n)], int)
print(np.matmul(arr_a, arr_b))

I used the matmul module but I feel this defeats the purpose of the question which I feel is to use cross and dot products.

How can I do this using dot and cross products?

🌐
Shishirkant
shishirkant.com › numpy-matrix-multiplication
NumPy Matrix Multiplication – Shishir Kant Singh
In NumPy, the Multiplication of matrix is basically an operation where we take two matrices as input and multiply rows of the first matrix to the columns of the second matrix, producing a single matrix as the output.
🌐
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 - The element-wise matrix multiplication of the given arrays is calculated in the following ways: ... The dot product of any two given matrices is basically their matrix product. The only difference is that in dot product we can have scalar values as well. ... import numpy as np A = np.array([1,2,3]) B = np.array([4,5,6]) print("Matrix A is:\n",A) print("Matrix A is:\n",B) C = np.dot(A,B) print("Matrix multiplication of matrix A and B is:\n",C)
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Codegive
codegive.com › blog › numpy_matrix_multiplication_function.php
Numpy matrix multiplication function
At its core, matrix multiplication is a fundamental operation in linear algebra where two matrices are combined to produce a third matrix. For two matrices A (m x n) and B (n x p), their product C (m x p) is calculated such that each element C_ij is the sum of the products of elements from ...
Top answer
1 of 1
475

Simplest solution

Use numpy.dot or a.dot(b). See the documentation here.

>>> a = np.array([[ 5, 1 ,3], 
                  [ 1, 1 ,1], 
                  [ 1, 2 ,1]])
>>> b = np.array([1, 2, 3])
>>> print a.dot(b)
array([16, 6, 8])

This occurs because numpy arrays are not matrices, and the standard operations *, +, -, / work element-wise on arrays.

Note that while you can use numpy.matrix (as of early 2021) where * will be treated like standard matrix multiplication, numpy.matrix is deprecated and may be removed in future releases.. See the note in its documentation (reproduced below):

It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

Thanks @HopeKing.


Other Solutions

Also know there are other options:

  • As noted below, if using python3.5+ and numpy v1.10+, the @ operator works as you'd expect:

    >>> print(a @ b)
    array([16, 6, 8])
    
  • If you want overkill, you can use numpy.einsum. The documentation will give you a flavor for how it works, but honestly, I didn't fully understand how to use it until reading this answer and just playing around with it on my own.

    >>> np.einsum('ji,i->j', a, b)
    array([16, 6, 8])
    
  • As of mid 2016 (numpy 1.10.1), you can try the experimental numpy.matmul, which works like numpy.dot with two major exceptions: no scalar multiplication but it works with stacks of matrices.

    >>> np.matmul(a, b)
    array([16, 6, 8])
    
  • numpy.inner functions the same way as numpy.dot for matrix-vector multiplication but behaves differently for matrix-matrix and tensor multiplication (see Wikipedia regarding the differences between the inner product and dot product in general or see this SO answer regarding numpy's implementations).

    >>> np.inner(a, b)
    array([16, 6, 8])
    
    # Beware using for matrix-matrix multiplication though!
    >>> b = a.T
    >>> np.dot(a, b)
    array([[35,  9, 10],
           [ 9,  3,  4],
           [10,  4,  6]])
    >>> np.inner(a, b) 
    array([[29, 12, 19],
           [ 7,  4,  5],
           [ 8,  5,  6]])
    
  • If you have multiple 2D arrays to dot together, you may consider the np.linalg.multi_dot function, which simplifies the syntax of many nested np.dots. Note that this only works with 2D arrays (i.e. not for matrix-vector multiplication).

      >>> np.dot(np.dot(a, a.T), a).dot(a.T)
      array([[1406,  382,  446],
             [ 382,  106,  126],
             [ 446,  126,  152]])
      >>> np.linalg.multi_dot((a, a.T, a, a.T))
      array([[1406,  382,  446],
             [ 382,  106,  126],
             [ 446,  126,  152]])
    

Rarer options for edge cases

  • If you have tensors (arrays of dimension greater than or equal to one), you can use numpy.tensordot with the optional argument axes=1:

    >>> np.tensordot(a, b, axes=1)
    array([16,  6,  8])
    
  • Don't use numpy.vdot if you have a matrix of complex numbers, as the matrix will be flattened to a 1D array, then it will try to find the complex conjugate dot product between your flattened matrix and vector (which will fail due to a size mismatch n*m vs n).

🌐
Benjaminjohnston
benjaminjohnston.com.au › matmul
Benjamin Johnston - Faster Matrix Multiplications in Numpy
However, if one of your matrices is constant, then ‘precomputation’ can pay off. Consider the multiplication y = matmul(A, x). Such a multiplication can be approximated by two lower rank multiplications: U, s, V = numpy.linalg.svd(A) # Very slow, so precompute!
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › multiply
Python Numpy multiply() - Multiply Array Elements | Vultr Docs
November 18, 2024 - Create a NumPy array. Multiply the array by a scalar value using numpy.multiply().
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
Matrix operations with NumPy in Python | note.nkmk.me
January 21, 2024 - If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred. numpy.dot — NumPy v1.26 Manual
🌐
Programiz
programiz.com › python-programming › numpy › methods › matmul
NumPy matmul() (With Examples)
For example, For A = (M x N) and B = (N x K) when we multiply, C = A * B the resulting matrix is of size C = (M x K). import numpy as np # create two matrices matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) # create an output array result = np.zeros((2, 2), dtype=int) ...
🌐
Reddit
reddit.com › r/learnprogramming › matrix multiplication in numpy
r/learnprogramming on Reddit: matrix multiplication in Numpy
October 30, 2023 -

Hi. I'm just learning python coming from matlab, and need to do some linear algebra for a program I'm writing. Naturally I am using numpy, but I'm very confused by how it handles matrix multiplication. The normal rules say that for an M by N matrix, multiplication is only defined if you right-multiply by an N by P matrix. yet I'm finding that it seems to want to do M by N with P by N.
e.g:
import numpy as np
a = np.asarray([0, 1, 2])
b = np.asarray([3, 4, 5])
print(np.matmul(a,b))
>>> 14
this seems wrong. This should be undefined since the # rows on the right don't match the # columns on the left. Is there an implicit transposition going on here? If this is truly how this is supposed to operate this way, it seems like it is flirting with disaster since it is will not produce the result that convention dictates it should, and for square matrices will make it very hard to track down where in your code your matrix multiplication got screwed up, since no error will get thrown. Am I missing something here?