np.logical_and: Numpy logical_and() Method in Python

Numpy logical_and() method is used to calculate the truth value of x1 AND x2 element-wise.

np.logical_and()

The np.logical_and() is a mathematical array function that calculates the result of ai AND bi for every element ai of array1 with the corresponding element bi of array2 and returns the result in an array. The logical_and() function takes the input arrays that must be of the same shape for the numpy logical_and() method to work.

Syntax

numpy.logical_and(arr1, arr2, out=None, where=True, dtype=None)

Parameter(s)

  1. arr1: Input array_like containing elements ai.
  2. arr2: Input array_like containing elements bi.
  3. Out: (ndarray, None, or tuple of ndarray) [Optional parameter] It defines the alternate output array in which the resulting product is placed. This must have the same or broadcastable shape as the expected output.
  4. where: (array_like) [Optional parameter] Where True, these are the positions where the operator is applied. Where False, these are the positions left alone in the output array.
  5. dtype : [Optional parameter] It defines the type of the returned array.

Return Value

The element-wise logical AND result in the form of an array.

Numpy logical_and Example

Consider the following examples.

The following example demonstrates the logical_and() method and establishes the truth table for the logical and operator.

import numpy as np

arr1 = [0, 0, 1, 1]
arr2 = [0, 1, 0, 1]
arr3 = np.logical_and(arr1, arr2)
print(arr3)

arr4 = [False, False, True, True]
arr5 = [False, True, False, True]
arr6 = np.logical_and(arr4, arr5)
print(arr6)

Output

[False False False  True]
[False False False  True]

Example 2

The following code demonstrates the case where an array element is a complex number.

import numpy as np

arr1 = [3+4j, 3+4j]
arr2 = [1, 0]
arr3 = np.logical_and(arr1, arr2)
print(arr3)

Output

[ True False]

Example 3

The following code demonstrates the case where an empty array is passed.

import numpy as np

arr1 = []
arr2 = []
arr3 = np.logical_and(arr1, arr2)
print(arr3)

Output

[]

Example 4

The following example demonstrates the case where arrays of different shapes are passed.

import numpy as np

arr1 = [0, 0, 1, 1]
arr2 = [1, 0, 1, 0, 1]
arr3 = np.logical_and(arr1, arr2)
print(arr3)

Output

Traceback (most recent call last):
  File "app.py", line 5, in <module>
    arr3 = np.logical_and(arr1, arr2)
ValueError: operands could not be broadcast together with shapes (4,) (5,)

Example 5

The following code demonstrates the use of the where parameter.

import numpy as np

arr1 = [0, 0, 1, 0]
arr2 = [0, 0, 1, 0]
arr3 = np.logical_and(arr1, arr2, where=[True, False, True, False])
arr4 = np.logical_and(arr1, arr2)
print(arr3)
print(arr4)

Output

[False  True  True  True]
[False False  True False]

Example 6

The following code demonstrates the case where the dtype is to specify the data type of the elements.

import numpy as np

arr1 = [0, 0, 1, 1]
arr2 = [0, 0, 1, 1]
arr3 = np.logical_and(arr1, arr2, dtype=np.double)
print(arr3.dtype == np.bool)
print(arr3.dtype == np.int)

Output

True
False

Example 7

The following code shows the application of this method in a simple programming context.

Given a sequence of numbers, find how many numbers fall within the range x to y (both included).

import numpy as np

n = int(input("Count: "))
numbers = []

x = int(input("x: "))
y = int(input("y: "))

for i in range(n):
    numbers.append(int(input()))

new_arr = np.array(numbers)
valid_arr = np.logical_and(new_arr >= x, new_arr <= y)

count = 0
for i in valid_arr:
    if(i):
        count += i

print("Result: ", count)

Output

Test Case 1:
Count: 5
x: 10
y: 20
1
2
11
12
21
Result:  2

Test Case 2:
Count: 4
x: 1
y: 3
1
2
3
4
Result:  3

That is it for numpy logical_and() function.

See also

Numpy cbrt()

Numpy greater_equal()

Numpy prod()

Numpy square()

Numpy polyfit()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.