Skip to content
Related Articles

Related Articles

Python PyTorch – torch.polar() Function

View Discussion
Improve Article
Save Article
  • Last Updated : 10 Jun, 2022

In this article, we will discuss the torch.polar() method in Pytorch using Python.

torch.polar() method

torch.polar() method is used to construct a complex number using absolute value and angle. The data types of these absolute values and angles must be either float or double. If the absolute value type is float type, then the angle type must also be in float. Below is the syntax of torch.polar method.

Syntax: torch.polar(abs, angle)

Parameters:

  • abs  is the  absolute length of the complex tensor.
  • angle is the angle of the complex tensor.

Return: It will return a complex tensor from the given absolute value and angle.

Example 1

In this example, we will construct the absolutes length of 5, and 5 angles with float type and display the type of the complex tensor constructed along with the data type.

Python3




import torch
import numpy
  
# create absolute lengths of 5 with float type
abs = torch.tensor([23, 45, 67, 54, 32], dtype=torch.float32)
  
# create 5 angles with float type
angle = torch.tensor([numpy.pi / 2, numpy.pi / 4, numpy.pi /
                      3, numpy.pi / 5, 0], dtype=torch.float32)
  
# construct complex tensor
print(torch.polar(abs, angle))
  
# construct complex tensor and display
# the datatype
print(torch.polar(abs, angle).dtype)


Output:

tensor([-1.0054e-06+23.0000j,  3.1820e+01+31.8198j,  3.3500e+01+58.0237j,

         4.3687e+01+31.7404j,  3.2000e+01+0.0000j])

torch.complex64

Example 2

In this example, we will construct the absolutes length of 5, and 5 angles with double type and display the type of the complex tensor constructed along with the data type.

Python3




import torch
import numpy
  
# create absolute lengths of 5 with double type
abs = torch.tensor([23, 45, 67, 54, 32], dtype=torch.double)
  
# create 5 angles with float type
angle = torch.tensor([numpy.pi / 2, numpy.pi / 4, numpy.pi /
                      3, numpy.pi / 5, 0], dtype=torch.double)
  
# construct complex tensor
print(torch.polar(abs, angle))
  
# construct complex tensor and display the datatype
print(torch.polar(abs, angle).dtype)


Output:

tensor([1.4083e-15+23.0000j, 3.1820e+01+31.8198j, 3.3500e+01+58.0237j,

        4.3687e+01+31.7404j, 3.2000e+01+0.0000j], dtype=torch.complex128)

torch.complex128

Example 3

In this example, we will construct the complex tensor from 2 absolutes with float type and angles with double type and display the type of the complex tensor constructed.

Python3




import torch
import numpy
  
# create absolute lengths of 2 with float type
abs = torch.tensor([3, 2], dtype=torch.float64)
  
# create 2 angles with float type
angle = torch.tensor([numpy.pi / 2, numpy.pi / 4], 
                     dtype=torch.double)
  
# construct complex tensor
print(torch.polar(abs, angle))
  
# construct complex tensor and display 
# the datatype
print(torch.polar(abs, angle).dtype)


Output:

tensor([1.8370e-16+3.0000j, 1.4142e+00+1.4142j], dtype=torch.complex128)

torch.complex128


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!