Sunday, November 17, 2024
Google search engine
HomeLanguagesHow to Construct a Complex Tensor With the Given Real and Imaginary...

How to Construct a Complex Tensor With the Given Real and Imaginary Parts in PyTorch?

In this article, we will see how to construct a complex tensor with the given Real and Imaginary Parts in PyTorch.

Now we will see how to construct complex number in pytorch using torch.complex() method with the given input as real and imaginary numbers.

Syntax: torch.complex(real, imag)

Parameter:

  • real: The real part of the complex tensor. Must be float or double.
  • imag: The imaginary part of the complex tensor. Must be same dtype as real.

Return: complex number, If the inputs are torch.float32, must be torch.complex64. If the inputs are torch.float64, must be torch.complex128.

Example 1:

In this example, we are first create a two variables that is a real type and another is of imaginary float type with the help of torch.tensor and we will convert it into a complex number using torch.comple method. Here, real is the real number and image is the imaginary number such that both of these can be only either float type or imaginary type. Finally, we will displaying the datatype of the complex number.

Python3




# import the torch module
import torch
 
# create real and img with float type
real = torch.tensor([78.2, 23.2], dtype=torch.float32)
img = torch.tensor([32, 41], dtype=torch.float32)
 
# display
print(real)
print(img)
 
# display  the complex number
print(torch.complex(real, img))
 
# display  the datatype of complex number
print(torch.complex(real, img).dtype)


Output:

tensor([78.2000, 23.2000])

tensor([32., 41.])

tensor([78.2000+32.j, 23.2000+41.j])

torch.complex64

Example 2:

In this example, we are creating 2 real and imaginary parts of dtype=torch.double type and construct a complex output. Finally, we are displaying the datatype of the complex number which is complex 128.

Python3




# import the torch module
import torch
 
# create real and img with double type
real = torch.tensor([78, 23], dtype=torch.double)
img = torch.tensor([32, 41], dtype=torch.double)
 
# display
print(real)
print(img)
 
# display  the complex number
print(torch.complex(real, img))
 
# display  the datatype of complex number
print(torch.complex(real, img).dtype)


Output:

tensor([78., 23.], dtype=torch.float64)

tensor([32., 41.], dtype=torch.float64)

tensor([78.+32.j, 23.+41.j], dtype=torch.complex128)

torch.complex128

Example 3:

In this example, we are creating 3 real float type with dype=torch.float64, 3 double type with dtype=torch.double and construct a  complex number with the help of torch.complex.

Python3




# import the torch module
import torch
 
# create real and img with double type
real = torch.tensor([78, 23, 45], dtype=torch.float64)
img = torch.tensor([32, 41, 9], dtype=torch.double)
 
# display
print(real)
print(img)
 
# display  the complex number
print(torch.complex(real, img))
 
# display  the datatype of complex number
print(torch.complex(real, img).dtype)


Output:

tensor([78., 23., 45.], dtype=torch.float64)

tensor([32., 41.,  9.], dtype=torch.float64)

tensor([78.+32.j, 23.+41.j, 45.+9.j], dtype=torch.complex128)

torch.complex128

        [78., 23., 45.]], dtype=torch.float64)

tensor([[32., 41.,  9.],

        [78., 23., 45.]], dtype=torch.float64)

tensor([[78.+32.j, 23.+41.j, 45.+9.j],

        [78.+78.j, 23.+23.j, 45.+45.j]], dtype=torch.complex128)

torch.complex128

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments