DEV Community

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

Posted on • Updated on

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

*My post explains conj(), conj_physical(), is_conj() and resolve_conj().

real() can get the zero or more real parts of a 0D or more D tensor from the zero or more elements 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,
                           5.7923-4.1148j]])
torch.real(input=my_tensor)
# tensor([[4.7352,
#          2.3706,
#          -9.1648],
#         [1.5885,
#          -6.4314,
#          5.7923]])

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

my_tensor = torch.tensor([[4.7352, 2.3706, -9.1648],
                          [1.5885, -6.4314, 5.7923]])
torch.real(input=my_tensor)
# tensor([[4.7352, 2.3706, -9.1648],
#         [1.5885, -6.4314, 5.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 a 0D or more D tensor from 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, 5.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 a 0D or more D tensor from 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,
                           5.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],
#          [5.7923, -4.1148]]])
Enter fullscreen mode Exit fullscreen mode

view_as_complex() can get the one or more complex numbers of a 0D or more D tensor from 2 or more even number of the 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],
                          [5.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,
#         5.7923-4.1148j])
Enter fullscreen mode Exit fullscreen mode

complex() can get the zero or more complex numbers of a 0D or more D tensor from the zero or more floating point numbers of two of 0D or more D tensors as shown below:

*Memos:

  • complex() can be used with torch but not with a tensor.
  • The 1st argument(tensor of float) with torch is real(Required).
  • The 2st argument(tensor of float) with torch or the 1st argument(tensor of float) is imag(Required).
import torch

tensor1 = torch.tensor(4.7352)
tensor2 = torch.tensor([6.1584, -8.4339, -3.0342])

torch.complex(real=tensor1, imag=tensor2)
# tensor([4.7352+6.1584j, 4.7352-8.4339j, 4.7352-3.0342j])

torch.complex(real=tensor2, imag=tensor1)
# tensor([6.1584+4.7352j, -8.4339+4.7352j, -3.0342+4.7352j])

tensor1 = torch.tensor([4.7352, 2.3706, -9.1648])
tensor2 = torch.tensor([[6.1584, -8.4339, -3.0342],
                        [7.2316, 3.9501, -4.1148]])
torch.complex(real=tensor1, imag=tensor2)
# tensor([[4.7352+6.1584j, 2.3706-8.4339j, -9.1648-3.0342j],
#         [4.7352+7.2316j, 2.3706+3.9501j, -9.1648-4.1148j]])

torch.complex(real=tensor2, imag=tensor1)
# tensor([[6.1584+4.7352j, -8.4339+2.3706j, -3.0342-9.1648j],
#         [7.2316+4.7352j, 3.9501+2.3706j, -4.1148-9.1648j]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)