DEV Community

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

Posted on • Edited on

conj, conj_physical, is_conj and resolve_conj in PyTorch

Buy Me a Coffee

*My post explains real(), frac(), view_as_real() and view_as_complex().

conj() or conj_physical() can get the 0D or more D tensor of zero or more conjugated elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • conj() or conj_physical() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool). *If it's not complex type, the original tensor is just returned.
  • For conj_physical(), there is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • conj() can work with is_conj() and resolve_conj which I explain later while conj_physical() cannot work with them.
import torch

my_tensor = torch.tensor(4.7352+6.1584j)

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

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

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.conj(input=my_tensor)
torch.conj_physical(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]])

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

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

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

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

my_tensor = torch.tensor([[True, False, True], [False, True, False]])

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

is_conj() can check if the 0D or more D tensor of zero or more elements outputted by conj() is conjugated, getting the scalar of True of False as shown below:

*Memos:

  • is_conj() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
import torch

my_tensor = torch.tensor(4.7352+6.1584j)

torch.is_conj(input=my_tensor)
my_tensor.is_conj()
# False

""" conj() """

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

torch.is_conj(input=c_tensor)
# True

c_c_tensor = torch.conj(input=c_tensor)
c_c_tensor
# tensor(4.7352+6.1584j)

torch.is_conj(input=c_c_tensor)
# False

""" End """

""" conj_physical """

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

torch.is_conj(input=cp_tensor)
# False

cp_cp_tensor = torch.conj_physical(input=cp_tensor)
cp_cp_tensor
# tensor(4.7352+6.1584j)

torch.is_conj(input=cp_cp_tensor)
# False

""" End """

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

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.is_conj(input=my_tensor)
# False

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

torch.is_conj(input=my_tensor)
# False

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

torch.is_conj(input=my_tensor)
# False

my_tensor = torch.tensor([[True, False, True], [False, True, False]])

torch.is_conj(input=my_tensor)
# False
Enter fullscreen mode Exit fullscreen mode

resolve_conj() can get the new 0D or more D tensor of zero or more elements from the 0D or more D tensor of the zero or more elements conjugated from conj() as shown below:

*Memos:

  • resolve_conj() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool). *If it's not conjugated by conj(), the original tensor is just returned.
import torch

my_tensor = torch.tensor(4.7352+6.1584j)

torch.resolve_conj(input=my_tensor)
my_tensor.resolve_conj()
# tensor(4.7352+6.1584j)

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

torch.is_conj(input=c_tensor)
# True

rc_tensor = torch.resolve_conj(input=c_tensor)
rc_tensor
# tensor(4.7352-6.1584j)

rc_tensor.is_conj(input=rc_tensor)
# False

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

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.resolve_conj(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]])

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

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

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

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

my_tensor = torch.tensor([[True, False, True], [False, True, False]])

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

Top comments (0)