How to construct a complex tensor with the given real and imaginary parts in PyTorch?

PyTorchServer Side ProgrammingProgramming

With given real and imaginary parts, we can construct a complex number in PyTorch using torch.complex() method. The real and imaginary parts must be float or double. Both the real and imaginary parts must be of the same type. If the real part is float, then the imaginary must also be float.

  • If the inputs are torch.float32, then the constructed complex tensor must be torch.complex64.

  • If the inputs are torch.float64, then the complex tensor must be torch.complex128.

Syntax

torch.complex(real, imag)

Parameters

  • real and imag − Real and imaginary parts of the complex tensor. Both must be of the same dtype, float or double only.

Steps

We could use the following steps to construct a complex tensor with given real and imag

  • Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.

import torch
  • Define two torch tensors − real and imag. The number of elements in both tensors must be the same.

real = torch.randn(2,3, dtype = torch.double)
imag = torch.randn(2,3, dtype= torch.double)
  • Construct a complex tensor with given real and imag.

z = torch.complex(real, imag)
  • Print the above-computed complex tensor.

print("Complex Number:\n", z)
  • Print the dtype of the complex tensor.

print("dtype of Complex number:",z.dtype)

Example 1

import torch
real = torch.tensor([1, 2], dtype = torch.float32)
imag = torch.tensor([3, 4], dtype= torch.float32)
print("Real Part:\n", real)
print("Imaginary Part:\n", imag)

z = torch.complex(real, imag)

'''
Both real and imaginary parts should be of same data types;
it supports float and double
'''

print("Complex Number:\n", z)
print("dtype of Complex number:",z.dtype)

Output

Real Part:
   tensor([1., 2.])
Imaginary Part:
   tensor([3., 4.])
Complex Number:
   tensor([1.+3.j, 2.+4.j])
dtype of Complex number: torch.complex64

In the above example, we constructed a complex tensor of dtype=torch.complx64. The real and imaginary tensors are of dtype=torch.float32.

Example 2

# Import the required library
import torch

# define real and imag parts as double
real = torch.randn(2,3, dtype = torch.double)
imag = torch.randn(2,3, dtype= torch.double)
print("Real Part:\n", real)
print("Imaginary Part:\n", imag)

# create complex number using the above
z = torch.complex(real, imag)

'''
Both real and imaginary parts should be of same data types;
it supports float and double
'''

print("Complex number:\n",z)
print("dtype of complex number:\n", z.dtype)

Output

Real Part:
   tensor([[-0.6168, -0.8757, 1.5826],
      [-0.7090, 1.4633, -0.6376]], dtype=torch.float64)
Imaginary Part:
   tensor([[-0.1338, -0.1485, -0.1314],
      [ 1.8149, -0.1233, -0.0694]], dtype=torch.float64)
Complex number:
   tensor([[-0.6168-0.1338j, -0.8757-0.1485j, 1.5826-0.1314j],
      [-0.7090+1.8149j, 1.4633-0.1233j, -0.6376-0.0694j]],
      dtype=torch.complex128)
dtype of complex number:
torch.complex128

In the above example, we constructed a complex tensor of dtype=torch.complex128. The real and imaginary tensors are of dtype=torch.double.

raja
Updated on 25-Jan-2022 08:51:23

Advertisements