DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

real(), frac(), view_as_real() and view_as_complex() in PyTorch

real() can get the zero or more real parts of the zero or more values of a 0D or more D tensor as shown below:

*Memos:

  • real() can be used with torch but not with a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch is input(Required).
import torch

my_tensor = torch.tensor(-4.7352-6.1584j)

torch.real(input=my_tensor)
# tensor(-4.7352)

my_tensor = torch.tensor([-4.7352-6.1584j,
                          2.3706-8.4339j,
                          -9.1648+3.0342j])
torch.real(input=my_tensor)
# tensor([-4.7352, 
#         2.3706,
#         -9.1648])

my_tensor = torch.tensor([[-4.7352-6.1584j,
                           2.3706-8.4339j,
                           -9.1648+3.0342j],
                          [1.5885+7.2316j,
                           -6.4314+3.9501j,
                           2.7923-4.1148j]])
torch.real(input=my_tensor)
# tensor([[-4.7352,
#          2.3706,
#          -9.1648],
#         [1.5885,
#          -6.4314,
#          2.7923]])

my_tensor = torch.tensor([[-4, 2, -9],
                          [1, -6, 2]])
torch.real(input=my_tensor)
# tensor([[-4, 2, -9],
#         [1, -6, 2]])

my_tensor = torch.tensor([[-4.7352, 2.3706, -9.1648],
                          [1.5885, -6.4314, 2.7923]])
torch.real(input=my_tensor)
# tensor([[-4.7352, 2.3706, -9.1648],
#         [1.5885, -6.4314, 2.7923]])

my_tensor = torch.tensor([[True, False, True],
                          [False, True, False]])
torch.real(input=my_tensor)
# tensor([[True, False, True],
#         [False, True, False]])
Enter fullscreen mode Exit fullscreen mode

frac() can get the zero or more decimal parts of the zero or more floating point numbers of a 0D or more D tensor as shown below:

*Memos:

  • frac() can be used with torch or a tensor.
  • The 1st argument(tensor of float) with torch or using a tensor(tensor of float) is input(Required).
import torch

my_tensor = torch.tensor(-4.7352)

torch.frac(input=my_tensor)
my_tensor.frac()
# tensor(-0.7352)

my_tensor = torch.tensor([-4.7352, 2.3706, -9.1648])

torch.frac(input=my_tensor)
# tensor([-0.7352, 0.3706, -0.1648])

my_tensor = torch.tensor([[-4.7352, 2.3706, -9.1648],
                          [1.5885, -6.4314, 2.7923]])
torch.frac(input=my_tensor)
# tensor([[-0.7352, 0.3706, -0.1648],
#         [0.5885, -0.4314, 0.7923]])
Enter fullscreen mode Exit fullscreen mode

view_as_real() can get the zero or more real and imaginary parts of the zero or more complex numbers of a 0D or more D tensor as shown below:

*Memos:

  • view_as_real() can be used with torch but not with a tensor.
  • The 1st argument(tensor of complex) with torch is input(Required).
import torch

my_tensor = torch.tensor(-4.7352-6.1584j)

torch.view_as_real(input=my_tensor)
# tensor([-4.7352, -6.1584])

my_tensor = torch.tensor([-4.7352-6.1584j,
                          2.3706-8.4339j, 
                          -9.1648+3.0342j])
torch.view_as_real(input=my_tensor)
# tensor([[-4.7352, -6.1584],
#         [2.3706, -8.4339],
#         [-9.1648, 3.0342]])

my_tensor = torch.tensor([[-4.7352-6.1584j, 
                           2.3706-8.4339j, 
                           -9.1648+3.0342j],
                          [1.5885+7.2316j,
                           -6.4314+3.9501j,
                           2.7923-4.1148j]])
torch.view_as_real(input=my_tensor)
# tensor([[[-4.7352, -6.1584],
#          [2.3706, -8.4339],
#          [-9.1648, 3.0342]],
#         [[ 1.5885, 7.2316],
#          [-6.4314, 3.9501],
#          [2.7923, -4.1148]]])
Enter fullscreen mode Exit fullscreen mode

view_as_complex() can create the one or more complex numbers with 2 or more even number of floating point numbers of a 1D or more D tensor as shown below:

*Memos:

  • view_as_complex() can be used with torch but not with a tensor.
  • The 1st argument(tensor of float) with torch is input(Required).
import torch

my_tensor = torch.tensor([-4.7352, -6.1584])

torch.view_as_complex(input=my_tensor)
# tensor(-4.7352-6.1584j)

my_tensor = torch.tensor([[-4.7352, -6.1584],
                          [2.3706, -8.4339], 
                          [-9.1648, 3.0342]])
torch.view_as_complex(input=my_tensor)
# tensor([-4.7352-6.1584j,
#         2.3706-8.4339j,
#         -9.1648+3.0342j])

my_tensor = torch.tensor([[-4.7352, -6.1584], 
                          [2.3706, -8.4339], 
                          [-9.1648, 3.0342],
                          [1.5885, 7.2316],
                          [-6.4314, 3.9501],
                          [2.7923, -4.1148]])
torch.view_as_complex(input=my_tensor)
# tensor([-4.7352-6.1584j,
#         2.3706-8.4339j,
#         -9.1648+3.0342j,
#         1.5885+7.2316j,
#         -6.4314+3.9501j,
#         2.7923-4.1148j])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)