torch.Tensor.Tensor.logical_and_
In-place element-wise logical AND.
Computes the logical AND between two boolean tensors element-wise. Returns true only when both inputs are true. Essential for combining boolean masks and implementing complex conditional operations.
Use Cases:
- Combining multiple boolean conditions for masking
- Implementing AND logic for tensor filtering
- Creating compound boolean masks from multiple constraints
- Broadcasting: Both tensors are broadcast to compatible shapes
- Boolean tensors: Both inputs must be boolean tensors
- In-place: Modifies this tensor directly and returns it for chaining
- Short-circuit: Only false AND x = false (always); all other combinations depend on both operands
Parameters
otherTensor- The other tensor (will be broadcast if needed)
Returns
this– This tensor modified in-placeExamples
// Combine two boolean masks
const mask1 = torch.tensor([true, true, false, true]);
const mask2 = torch.tensor([true, false, false, true]);
mask1.logical_and_(mask2); // [true, false, false, true]
// Complex filtering - find values in range [0, 5]
const x = torch.tensor([0, 2, 7, 3, 9]);
const inRange = x.ge(0).logical_and(x.le(5));
const filtered = x.masked_select(inRange); // [0, 2, 3]
// Intersection of two conditions
const batch = torch.randn(100, 50);
const isPositive = batch.gt(0);
const isSmall = batch.lt(1);
isPositive.logical_and_(isSmall); // Values in (0, 1)See Also
- PyTorch tensor.logical_and_()
- logical_or_ - Element-wise logical OR
- logical_not_ - Element-wise logical NOT
- logical_xor_ - Element-wise logical XOR