AppDividend
Latest Code Tutorials

Numpy logical_xor Example | np logical_xor() in Python

0

Numpy logical_xor() function calculates the result of ai XOR bi, for every element ai of array1 with the corresponding element bi of array2 and returns the result in the form of an array. Both the input arrays must be of the same shape for this method to work. 

Numpy logical_xor

Numpy exclusive-OR or XOR operator returns True when either one value is True, but not both.

Syntax

numpy.logical_xor(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 a resulting product is to be 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 to be applied. Where False, these are the positions to be left alone in the output array.
  5. dtype : [Optional parameter] It defines the type of the returned array. 

Return Value

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

Numpy logical_xor

 

Note

  1. If the input arrays are blank, then this method returns an empty array.
  2. This method can be used to print the truth table of the logical xor operator.
  3. This method returns ValueError when arrays of different shapes are passed.
  4. This method can be used with complex numbers as well.

Consider the following examples:

The following example demonstrates the use of this method and establishes the truth table for the logical xor operator.

import numpy as np

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

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

Output

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

Example 2

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

import numpy as np

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

Output

[False]

Example 3

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

import numpy as np

arr1 = []
arr2 = []
arr3 = np.logical_xor(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_xor(arr1, arr2)
print(arr3)

Output

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

Example 5

The following code shows the use of the where parameter.

import numpy as np

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

Output

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

Example 6

The following example demonstrates the case where 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_xor(arr1, arr2, dtype=np.double)
print(arr3.dtype == np.int)

Output

False

Example 7

The following example demonstrates the application of this method in a simple programming context.

A two-way switch controls an electrical circuit in such a manner that the circuit is closed only when exactly one of the switch is on. Given the status of switches in column1 with their counterpart in column2 for different circuits, determine which circuits are closed.

See the following code.

import numpy as np

n = int(input("Count: "))
column1 = []
column2 = []

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

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

on_status = np.logical_xor(column1, column2)

print("Closed circuit: ", on_status)

Output

Test Case 1:
Count: 2
0
0
1
1
Closed circuit:  [ True  True]

Test Case 2:
Count: 3
0
1
0
1
1
1
Closed circuit:  [ True False  True]

That is it for the numpy logical_xor() method.

See also

Numpy logical_not()

Numpy logical_and()

Leave A Reply

Your email address will not be published.

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