w3resource

NumPy: Multiply a matrix by another matrix of complex numbers and create a new matrix of complex numbers

NumPy Mathematics: Exercise-12 with Solution

Write a NumPy program to multiply a matrix by another matrix of complex numbers and create a new matrix of complex numbers.

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Creating the first complex array
x = np.array([1+2j, 3+4j])

# Displaying the first complex array
print("First array:")
print(x)

# Creating the second complex array
y = np.array([5+6j, 7+8j])

# Displaying the second complex array
print("Second array:")
print(y)

# Calculating the vector dot product of the two arrays
z = np.vdot(x, y)

# Displaying the product of the two arrays
print("Product of above two arrays:")
print(z) 

Sample Output:

First array:                                                           
[ 1.+2.j  3.+4.j]                                                      
Second array:                                                          
[ 5.+6.j  7.+8.j]                                                      
Product of above two arrays:                                           
(70-8j)

Explanation:

numpy.vdot(a, b) - Return the dot product of two vectors.

The vdot(a, b) function handles complex numbers differently than dot(a, b). If the first argument is complex the complex conjugate of the first argument is used for the calculation of the dot product.

In the above exercise –

x = np.array([1+2j,3+4j]) – This line creates an array x of size 2x1 containing complex numbers.

y = np.array([5+6j,7+8j]) – This line creates an array y of size 2x1 containing complex numbers.

z = np.vdot(x, y) – This line computes the dot product of the two arrays x and y.

Python-Numpy Code Editor:

Previous: Write a NumPy program to multiply a 5x3 matrix by a 3x2 matrix and create a real matrix product.
Next: Write a NumPy program to create an inner product of two arrays.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/numpy/python-numpy-math-exercise-12.php