DEV Community

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

Posted on • Edited on

real, frac, view_as_real, view_as_complex and complex in PyTorch

Buy Me a Coffee

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

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

*Memos:

  • real() can be used with torch but not with a tensor.
  • The 1st argument with torch is input(Required-Type:tensor of int, float, complex or bool).
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 0D or more D tensor of zero or more decimal parts from the a 0D or more D tensor of zero or more floating point numbers as shown below:

*Memos:

  • frac() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of float).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
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 view of the 0D or more D tensor of zero or more real and imaginary parts from the 0D or more D tensor of zero or more complex numbers as shown below:

*Memos:

  • view_as_real() can be used with torch but not with a tensor.
  • The 1st argument with torch is input(Required-Type:tensor of complex).
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 view of the 0D or more D tensor of one or more complex numbers from the 1D or more D tensor of 2 or more even floating point numbers as shown below:

*Memos:

  • view_as_complex() can be used with torch but not with a tensor.
  • The 1st argument with torch is input(Required-Type:tensor of float).
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 0D or more D tensor of zero or more complex numbers from two of the 0D or more D tensors of zero or more floating point numbers as shown below:

*Memos:

  • complex() can be used with torch but not with a tensor.
  • The 1st argument with torch is real(Required-Type:tensor of float).
  • The 2st argument with torch or the 1st argument is imag(Required-Type:tensor of float).
  • There is out argument with torch(Optional-Defalut:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay