Open In App

Parallel matrix-vector multiplication in NumPy

Last Updated : 23 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to do matrix-vector multiplication in NumPy.

Matrix multiplication with Vector

For a matrix-vector multiplication, there are certain important points:

  • The end product of a matrix-vector multiplication is a vector.
  • Each element of this vector is obtained by performing a dot product between each row of the matrix and the vector being multiplied.
  • The number of columns in the matrix is equal to the number of elements in the vector.

# a and b are matrices
prod = numpy.matmul(a,b)

For matrix-vector multiplication, we will use np.matmul() function of NumPy, we will define a 4 x 4 matrix and a vector of length 4.

Python3




import numpy as np
  
a = np.array([[1, 2, 3, 13],
              [4, 5, 6, 14],
              [7, 8, 9, 15],
              [10, 11, 12, 16]])
  
b = np.array([10, 20, 30, 40])
  
print("Matrix a =", a)
print("Matrix b =", b)
print("Product of a and b =",
      np.matmul(a, b))


Output:

Matrix multiplication with another Matrix

We use the dot product to do matrix-matrix multiplication. We will use the same function for this also.

prod = numpy.matmul(a,b)  # a and b are matrices

For a matrix-matrix multiplication, there are certain important points:

  • The number of columns in the first matrix should be equal to the number of rows in the second matrix.
  • If we are multiplying a matrix of dimensions m x n with another matrix of dimensions n x p, then the resultant product will be a matrix of dimensions m x p

We will define two 3 x 3 matrix:

Python3




import numpy as np
  
a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])
  
b = np.array([[11, 22, 33],
              [44, 55, 66],
              [77, 88, 99]])
  
print("Matrix a =", a)
print("Matrix b =", b)
print("Product of a and b =", np.matmul(a, b))


Output:



Similar Reads

NumPy | Vector Multiplication
Vector multiplication is of three types: Scalar Product Dot Product Cross Product Scalar Multiplication: Scalar multiplication can be represented by multiplying a scalar quantity by all the elements in the vector matrix. Code: Python code explaining Scalar Multiplication # importing libraries import numpy as np import matplotlib.pyplot as plt impor
1 min read
Matrix Multiplication in NumPy
Let us see how to compute matrix multiplication with NumPy. We will be using the numpy.dot() method to find the product of 2 matrices. For example, for 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] So the computed answer will be: [[16, 26], [19, 31]] In Python numpy.dot() m
2 min read
NumPy - 3D matrix multiplication
Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. What is 3D Matrix Multiplication?A 3D matrix is nothing but a collection (or a stack) of many 2D matrices, just like how a 2D m
2 min read
Find a matrix or vector norm using NumPy
To find a matrix or vector norm we use function numpy.linalg.norm() of Python library Numpy. This function returns one of the seven matrix norms or one of the infinite vector norms depending upon the value of its parameters. Syntax: numpy.linalg.norm(x, ord=None, axis=None)Parameters: x: input ord: order of norm axis: None, returns either a vector
2 min read
Parallel Programming with NumPy and SciPy
Parallel computing is a type of computation in which many calculations or the execution of processes are carried out simultaneously. Large problems can often be divided into smaller ones, which can then be solved at the same time. Required Modules: pip install scipy pip install numpy pip install cupyParallel Programming with NumPy NumPy is a popula
6 min read
Python | Numpy numpy.matrix.any()
With the help of Numpy numpy.matrix.any() method, we are able to compare each and every element of one matrix with another or we can provide the axis on the we want to apply comparison if any of the element matches it return true. Syntax : numpy.matrix.any() Return : Return true if any match found else false Example #1 : In this example we can see
1 min read
Python | Numpy numpy.matrix.all()
With the help of Numpy numpy.matrix.all() method, we are able to compare each and every element of one matrix with another or we can provide the axis on the we want to apply comparison. Syntax : numpy.matrix.all() Return : Return true if found match else false Example #1 : In this example we can see that with the help of matrix.all() method, we are
1 min read
Python | Numpy numpy.matrix.A()
With the help of Numpy numpy.matrix.A() method, we can get the same matrix as self. It means through this method we can get the identical matrix. Syntax : numpy.matrix.A() Return : Return self matrix Example #1 : In this example we can see that with the help of matrix.A() method, we are able to get the self matrix. # import the important module in
1 min read
Python | Numpy numpy.matrix.T()
With the help of Numpy numpy.matrix.T() method, we can make a Transpose of any matrix either having dimension one or more than more. Syntax : numpy.matrix.T() Return : Return transpose of every matrix Example #1 : In this example we can see that with the help of matrix.T() method, we are able to transform any type of matrix. # import the important
1 min read
Python | Numpy numpy.matrix.H()
With the help of Numpy numpy.matrix.H() method, we can make a conjugate Transpose of any complex matrix either having dimension one or more than more. Syntax : numpy.matrix.H() Return : Return conjugate transpose of every complex matrix Example #1 : In this example we can see that with the help of matrix.H() method, we are able to transform any typ
1 min read
Practice Tags :