Skip to content
Related Articles

Related Articles

Multiply matrices of complex numbers using NumPy in Python
  • Last Updated : 05 Sep, 2020

In this article, we will discuss how to multiply two matrices containing complex numbers using NumPy but first, let’s know what is a complex number. A Complex Number is any number that can be represented in the form of x+yj where x is the real part and y is the imaginary part. Multiplication of two complex numbers can be done using the below formula – 

(a+ib) \times (x+iy)=ax+i^2by+i(bx+ay)=ax-by+i(bx+ay)

NumPy provides the vdot() method that returns the dot product of vectors a and b. This function handles complex numbers differently than dot(a, b).

Syntax:

numpy.vdot(vector_a, vector_b)

Example 1:



Python3




# importing numpy as library
import numpy as np
  
  
# creating matrix of complex number
x = np.array([2+3j, 4+5j])
print("Printing First matrix:")
print(x)
  
y = np.array([8+7j, 5+6j])
print("Printing Second matrix:")
print(y)
  
# vector dot product of two matrices
z = np.vdot(x, y)
print("Product of first and second matrices are:")
print(z)

Output:

Printing First matrix:
[2.+3.j 4.+5.j]
Printing Second matrix:
[8.+7.j 5.+6.j]
Product of first and second matrices are:
(87-11j)

Example 2: Now suppose we have 2D matrix: 

Python3




# importing numpy as library
import numpy as np
  
  
# creating matrix of complex number
x = np.array([[2+3j, 4+5j], [4+5j, 6+7j]])
print("Printing First matrix:")
print(x)
  
y = np.array([[8+7j, 5+6j], [9+10j, 1+2j]])
print("Printing Second matrix:")
print(y)
  
# vector dot product of two matrices
z = np.vdot(x, y)
print("Product of first and second matrices are:")
print(z)

Output:

Printing First matrix:
[[2.+3.j 4.+5.j]
 [4.+5.j 6.+7.j]]
Printing Second matrix:
[[8. +7.j 5. +6.j]
 [9.+10.j 1. +2.j]]
Product of first and second matrices are:
(193-11j)

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :