DEV Community

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

Posted on • Updated on

conj(), conj_physical(), is_conj() and resolve_conj() in PyTorch

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

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

*Memos:

  • conj() or conj_physical() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch is input(Required). *If input is not complex type, input is just returned.
  • 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 outputted by conj() is conjugated as shown below:

*Memos:

  • is_conj() can be used with torch or 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.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 a new 0D or more D tensor from the 0D or more D tensor conjugated from conj() as shown below:

*Memos:

  • resolve_conj() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch is input(Required). *If input is not conjugated by conj(), input 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)