DEV Community

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

Posted on • Updated on

logical_and(), logical_or(), logical_xor() and logical_not() in PyTorch

logical_and() can do logical AND with two of 0D or more D tensors as shown below:

*Memos:

  • logical_and() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • The 2nd argument(tensor of int, float, complex or bool) with torch or the 1st argument with a tensor(tensor of int, float, complex or bool) is other(Required).
  • Zero is True and nonzero is False.
import torch

tensor1 = torch.tensor(True)
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.logical_and(input=tensor1, other=tensor2)
tensor1.logical_and(other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[True, False, True, False],
#         [False, True, False, True]])

tensor1 = torch.tensor(False)
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.logical_and(input=tensor1, other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[False, False, False, False],
#         [False, False, False, False]])

tensor1 = torch.tensor([True, False])
tensor2 = torch.tensor([[[True, False], [True, False]],
                        [[False, True], [False, True]]])
torch.logical_and(input=tensor1, other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[[True, False], [True, False]],
#         [[False, False], [False, False]]])

tensor1 = torch.tensor([7, 0])
tensor2 = torch.tensor([[[1, 0], [-1, 0]],
                        [[0, 2], [0, -2]]])
torch.logical_and(input=tensor1, other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[[True, False], [True, False]],
#         [[False, False], [False, False]]])

tensor1 = torch.tensor([7., 0.3])
tensor2 = torch.tensor([[[1.3, 0.3], [-1.0, 0.0]],
                        [[0.3, 2.3], [0.0, -2.0]]])
torch.logical_and(input=tensor1, other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[[True, True], [True, False]],
#         [[True, True], [False, True]]])

tensor1 = torch.tensor([7.+3.j, 0.+3.j])
tensor2 = torch.tensor([[[1.+3.j, 0.+3.j], [-1.+0.j, 0.+0.j]],
                        [[0.+3.j, 2.+3.j], [0.+0.j, -2.+0.j]]])
torch.logical_and(input=tensor1, other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[[True, True], [True, False]],
#         [[True, True], [False, True]]])
Enter fullscreen mode Exit fullscreen mode

logical_or() can do logical OR with two of 0D or more D tensors as shown below:

*Memos:

  • logical_or() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • The 2nd argument(tensor of int, float, complex or bool) with torch or the 1st argument with a tensor(tensor of int, float, complex or bool) is other(Required).
  • Zero is True and nonzero is False.
import torch

tensor1 = torch.tensor(True)
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.logical_or(input=tensor1, other=tensor2)
tensor1.logical_or(other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[True, True, True, True],
#         [True, True, True, True]])

tensor1 = torch.tensor(False)
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.logical_or(input=tensor1, other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[True, False, True, False],
#         [False, True, False, True]])

tensor1 = torch.tensor([True, False])
tensor2 = torch.tensor([[[True, False], [True, False]],
                        [[False, True], [False, True]]])
torch.logical_or(input=tensor1, other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[[True, False], [True, False]],
#         [[True, True], [True, True]]])

tensor1 = torch.tensor([7, 0])
tensor2 = torch.tensor([[[1, 0], [-1, 0]],
                        [[0, 2], [0, -2]]])
torch.logical_or(input=tensor1, other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[[True, False], [True, False]],
#         [[True, True], [True, True]]])

tensor1 = torch.tensor([7.3, 0.3])
tensor2 = torch.tensor([[[1.3, 0.3], [-1.0, 0.0]],
                        [[0.3, 2.3], [0.0, -2.0]]])
torch.logical_or(input=tensor1, other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[[True, True], [True, True]],
#         [[True, True], [True, True]]])

tensor1 = torch.tensor([7.+3.j, 0.+3.j])
tensor2 = torch.tensor([[[1.+3.j, 0.+3.j], [-1.+0.j, 0.+0.j]],
                        [[0.+3.j, 2.+3.j], [0.+0.j, -2.+0.j]]])
torch.logical_or(input=tensor1, other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[[True, True], [True, True]],
#         [[True, True], [True, True]]])
Enter fullscreen mode Exit fullscreen mode

logical_xor() can do logical XOR with two of 0D or more D tensors as shown below:

*Memos:

  • logical_xor() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • The 2nd argument(tensor of int, float, complex or bool) with torch or the 1st argument with a tensor(tensor of int, float, complex or bool) is other(Required).
  • Zero is True and nonzero is False.
import torch

tensor1 = torch.tensor(True)
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.logical_xor(input=tensor1, other=tensor2)
tensor1.logical_xor(other=tensor2)
torch.logical_xor(input=tensor2, other=tensor1)
# tensor([[False, True, False, True],
#         [True, False, True, False]])

tensor1 = torch.tensor(False)
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.logical_xor(input=tensor1, other=tensor2)
torch.logical_xor(input=tensor2, other=tensor1)
# tensor([[True, False, True, False],
#         [False, True, False, True]])

tensor1 = torch.tensor([True, False])
tensor2 = torch.tensor([[[True, False], [True, False]],
                        [[False, True], [False, True]]])
torch.logical_xor(input=tensor1, other=tensor2)
torch.logical_xor(input=tensor2, other=tensor1)
# tensor([[[False, False], [False, False]],
#         [[True, True], [True, True]]])

tensor1 = torch.tensor([7, 0])
tensor2 = torch.tensor([[[1, 0], [-1, 0]],
                        [[0, 2], [0, -2]]])
torch.logical_xor(input=tensor1, other=tensor2)
torch.logical_xor(input=tensor2, other=tensor1)
# tensor([[[False, False], [False, False]],
#         [[True, True], [True, True]]])

tensor1 = torch.tensor([7.3, 0.3])
tensor2 = torch.tensor([[[1.3, 0.3], [-1.0, 0.0]],
                        [[0.3, 2.3], [0.0, -2.0]]])
torch.logical_xor(input=tensor1, other=tensor2)
torch.logical_xor(input=tensor2, other=tensor1)
# tensor([[[False, False], [False, True]],
#         [[False, False], [True, False]]])

tensor1 = torch.tensor([7.+3.j, 0.+3.j])
tensor2 = torch.tensor([[[1.+3.j, 0.+3.j], [-1.+0.j, 0.+0.j]],
                        [[0.+3.j, 2.+3.j], [0.+0.j, -2.+0.j]]])
torch.logical_xor(input=tensor1, other=tensor2)
torch.logical_xor(input=tensor2, other=tensor1)
# tensor([[[False, False], [False, True]],
#         [[False, False], [True, False]]])
Enter fullscreen mode Exit fullscreen mode

logical_not() can do logical NOT with a 0D or more D tensor as shown below:

*Memos:

  • logical_not() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • Zero is True and nonzero is False.
import torch

my_tensor = torch.tensor(True)

torch.logical_not(input=my_tensor)
my_tensor.logical_not()
# tensor(False)

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

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

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

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

my_tensor = torch.tensor([[[1, 0], [-1, 0]],
                          [[0, 2], [0, -2]]])
torch.logical_not(input=my_tensor)
# tensor([[[False, True], [False, True]],
#         [[True, False], [True, False]]])

my_tensor = torch.tensor([[[1.3, 0.3], [-1.0, 0.0]],
                          [[0.3, 2.3], [0.0, -2.0]]])
torch.logical_not(input=my_tensor)
# tensor([[[False, False], [False, True]],
#         [[False, False], [ True, False]]])

my_tensor  = torch.tensor([[[1.+3.j, 0.+3.j], [-1.+0.j, 0.+0.j]],
                           [[0.+3.j, 2.+3.j], [0.+0.j, -2.+0.j]]])
torch.logical_not(input=my_tensor)
# tensor([[[False, False], [False, True]],
#         [[False, False], [True, False]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)