Sahand
Sahand

Reputation: 8380

using j in array indices, what does it mean?

I found this in the Numpy docs.

>>> np.square([-1j, 1])
array([-1.-0.j,  1.+0.j])

What does the j in the array indices do?

Upvotes: 0

Views: 478

Answers (2)

wohe1
wohe1

Reputation: 775

j is pythons notation for the imaginary number i (which is sqrt(-1))

Upvotes: 3

hpaulj
hpaulj

Reputation: 231738

This isn't an index, it's a list of numeric values.

In [10]: x = [-1j, 1]     
In [11]: x
Out[11]: [(-0-1j), 1]    # list with 2 numbers, one of which is complex
In [12]: np.square(x)
Out[12]: array([-1.+0.j,  1.+0.j])  # array of dtype complex

Make an array from the list:

In [13]: y = np.array(x)
In [14]: y
Out[14]: array([-0.-1.j,  1.+0.j])
In [15]: np.square(y)
Out[15]: array([-1.+0.j,  1.+0.j])
In [16]: y*y               # same as square
Out[16]: array([-1.+0.j,  1.+0.j])

Imaginary -1j squared is -1.

np.square is a function that takes an array, or something that can be made into an array, as argument. Here it was given a list.

Upvotes: 4

Related Questions