I have tried using
np.array_equal(A,B)
but of course this doesn't acknowledge the different signs the two will have.
My arrays have the same number but they are either positive or negative.
I have tried using
np.array_equal(A,B)
but of course this doesn't acknowledge the different signs the two will have.
My arrays have the same number but they are either positive or negative.
If you want to check if two arrays are equal element-wise disregarding the sign of each element, use their absolute values1:
np.array_equal(abs(A), abs(B))
# [-1, 2, 3], [1, 2, -3] → True
# [-1, 2, 3], [1, -2, -3] → True
If you want to check if two arrays are equal while every pair of elements has exact opposite signs (a stricter comparison than the first case), use the negation of one array:
np.array_equal(A, -B)
# [-1, 2, 3], [1, 2, -3] → False
# [-1, 2, 3], [1, -2, -3] → True
1The built-in abs
function will call np.abs
under the hood.
You can use the absolute()
function in the numpy library as:
np.array_equal(np.abs(A), np.abs(B))