Multiply matrices of complex numbers using NumPy in Python

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.