Skip to content

Commit 1abed2f

Browse files
Create logical_and.md (#7640)
* Create logical_and.md * minor content fixes * Format fix and fixed the file error ---------
1 parent 2004a9b commit 1abed2f

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
Title: '.logical_and()'
3+
Description: 'Performs an element-wise logical AND operation on two input tensors, returning a tensor of Boolean values.'
4+
Subjects:
5+
- 'Data Science'
6+
- 'Machine Learning'
7+
Tags:
8+
- 'Logical'
9+
- 'Operators'
10+
- 'Python'
11+
- 'PyTorch'
12+
- 'Tensors'
13+
CatalogContent:
14+
- 'intro-to-py-torch-and-neural-networks'
15+
- 'py-torch-for-classification'
16+
---
17+
18+
The **`torch.logical_and()`** function in PyTorch performs an element-wise logical AND operation between two [tensors](https://www.codecademy.com/resources/docs/pytorch/tensors). It returns a new tensor with boolean values (`True` or `False`) depending on whether the corresponding elements in both input tensors evaluate to `True`.
19+
20+
This operation is often used in tensor-based computations where conditional checks need to be applied element-wise, such as in masking or filtering data.
21+
22+
## Syntax
23+
24+
```pseudo
25+
torch.logical_and(input, other, *, out=None)
26+
```
27+
28+
**Parameters:**
29+
30+
- `input` (Tensor): The first tensor for the logical AND operation.
31+
- `other` (Tensor): The second tensor, must be broadcastable to the shape of `input`.
32+
- `out` (Tensor, optional): The output tensor to store the result.
33+
34+
**Return value:**
35+
36+
A tensor of type `torch.bool` containing the result of the element-wise logical AND operation.
37+
38+
## Example 1: Basic Usage
39+
40+
In this example, two Boolean tensors are compared element-wise using `torch.logical_and()`:
41+
42+
```py
43+
import torch
44+
45+
a = torch.tensor([True, False, True])
46+
b = torch.tensor([True, True, False])
47+
48+
result = torch.logical_and(a, b)
49+
print(result)
50+
```
51+
52+
The output of this code is as follows:
53+
54+
```shell
55+
tensor([True, False, False])
56+
```
57+
58+
## Example 2: Using with Integer Tensors
59+
60+
In this example, integer tensors are treated as Boolean values, with nonzero as `True` and 0 as `False`:
61+
62+
```py
63+
import torch
64+
65+
x = torch.tensor([1, 0, 3])
66+
y = torch.tensor([2, 0, 0])
67+
68+
result = torch.logical_and(x, y)
69+
print(result)
70+
```
71+
72+
The output of this code is:
73+
74+
```shell
75+
tensor([True, False, False])
76+
```
77+
78+
## Example 3: Broadcasting in `torch.logical_and()`
79+
80+
In this example, [broadcasting](https://pytorch.org/docs/stable/notes/broadcasting.html) allows a smaller tensor to be compared across the rows of a larger tensor.
81+
82+
```py
83+
import torch
84+
85+
m = torch.tensor([[1, 0], [0, 1]])
86+
n = torch.tensor([1, 0])
87+
88+
result = torch.logical_and(m, n)
89+
print(result)
90+
```
91+
92+
The output of this code will be:
93+
94+
```shell
95+
tensor([[True, False],
96+
[False, False]])
97+
```

0 commit comments

Comments
 (0)