torch.logical_and
function logical_and<S1 extends Shape, S2 extends Shape>(input: Tensor<S1>, other: Tensor<S2>): Tensor<BroadcastShape<S1, S2>>Computes the element-wise logical AND.
Returns true only where BOTH input and other are non-zero (true). Essential for:
- Combining filters: find elements matching BOTH conditions
- Mask intersection: get elements valid in both masks
- Conditional logic: "if condition1 AND condition2"
- Multi-criterion selection: filtering by multiple criteria simultaneously
- Feature masking: find regions meeting multiple constraints
- Data validation: elements passing all checks
Broadcasting: Automatically broadcasts shapes. [3] AND [1] broadcasts to [3].
- Short-circuit possible: Implementation may stop checking once False found
- Non-commutative evaluation: May differ from or operations in lazy evaluation
- Broadcasting: Shapes automatically broadcast to common shape
- Boolean output: Always returns boolean dtype
- With AND mask: Common pattern: torch.logical_and(a, b, out=mask) for efficiency
- Broadcasting shapes: Verify expected shape - [3] AND [1] gives [3]
- All must be true: Single false makes result false (strict requirement)
- Type coercion: Non-zero values treated as true in numerical inputs
Parameters
inputTensor<S1>- First tensor (any dtype and shape)
otherTensor<S2>- Second tensor (must be broadcastable with input)
Returns
Tensor<BroadcastShape<S1, S2>>– Boolean tensor with shape = broadcast(input.shape, other.shape)Examples
// Combine two boolean masks
const mask1 = torch.tensor([true, true, false, false]);
const mask2 = torch.tensor([true, false, true, false]);
torch.logical_and(mask1, mask2); // [true, false, false, false]// Multi-criterion filtering: find values in a range
const values = torch.tensor([1, 5, 3, 8, 2, 7]);
const min_threshold = 3;
const max_threshold = 7;
const in_range_low = torch.ge(values, min_threshold); // [false, true, true, true, false, true]
const in_range_high = torch.le(values, max_threshold); // [true, true, true, false, true, false]
const in_range = torch.logical_and(in_range_low, in_range_high);
// [false, true, true, false, false, false] - only 5, 3 are in [3, 7]// Data validation: elements passing multiple checks
const data = torch.tensor([1.5, 2.0, NaN, 3.5, 0]);
const is_positive = torch.gt(data, 0); // [true, true, false, true, false]
const is_valid = torch.logical_not(torch.isnan(data)); // [true, true, false, true, true]
const pass_checks = torch.logical_and(is_positive, is_valid);
// [true, true, false, true, false]// Broadcasting: scalar-like conditions with tensors
const a = torch.tensor([0, 1, 2]); // Shape [3]
const b = torch.tensor([[true], [false]]); // Shape [2, 1]
torch.logical_and(a, b); // Broadcasts to [2, 3]
// [[false, true, true], [false, false, false]]// Masking with AND: find elements in valid region matching pattern
const image = torch.randn(100, 100);
const valid_region = torch.ones(100, 100); // Mark valid pixels
valid_region.slice(0, 0, 50).slice(1, 0, 50).fill(0); // Top-left invalid
const high_intensity = torch.gt(image, 0.5);
const result = torch.logical_and(valid_region, high_intensity);
// Elements that are both in valid region AND high intensitySee Also
- PyTorch torch.logical_and(input, other)
- logical_or - OR: true if EITHER condition is true
- logical_not - Negate conditions before AND
- logical_xor - XOR: true if ONLY ONE condition is true
- where - Select elements based on AND results