Why the square of the difference of these two arrays is 3?

Viewed 35
import numpy as np

vector = np.array([1])
vector2 = np.array([0])
(vector - vector2)^2

answer:

array([3])

Can someone explain what does [3] means?

2 Answers

When you create arrays with int objects, the dtype will be an integer type. That means that bit twiddling is supported.

^ is the bitwise XOR operator in python. ** is exponentiation. Numpy implements both operations elwment-wise, as expected.

1 ^ 2 -> 0001 XOR 0010 -> 0011 -> 3

That isn't a square. ^ is the xor (eXclusive OR) operator. Squaring a variable would be done with the ** operator (i.e., (vector-vector2)**2).

vector - vector2 returns array([1]). When you xor it is a scalar, each element (or here - the single element) is xored individually, and 1 xor 2 is 3.

Related