amchugh89
amchugh89

Reputation: 1296

Question about dimensionality of numpy array

I am studying physics and numpy simultaneously. Numpy says my 3x3 matrix has 2-dimensions, but in my physics book or 3blue1brown 'Essence of linear Algebra' a 3x3 matrix is 3-D

#a '2d' array, created using identity
i2d = np.identity(3)  

print(i2d)

print('this is a %s-D array, shape is %s with %s elements'%(i2d.ndim, i2d.shape, i2d.size))


YIELDS:

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
this is a 2-D array, shape is (3, 3) with 9 elements

In linear algebra, this defines a 3-D space with 3 perpendicular basis vectors. Anyone know what I am missing.

Upvotes: 0

Views: 53

Answers (1)

Branden Ciranni
Branden Ciranni

Reputation: 492

In numpy, the shape attribute gives you (3,3) and represents the number of rows and columns - in a physical context, your 3 basis vectors of length three form the basis for 3D-space.

Numpy's ndim attribute for arrays references how many "nestings" of arrays there are. You have an array of arrays - so you have two dimensions.

In general, if the elements of an array are arrays themselves, your dimensionality is 2. To access any element in an array like this, you need 2 indices, one for each dimension. i.e. arr[i][j]

If the elements of an array are arrays, and the elements of those arrays are also arrays, your dimensionality is 3 and you need 3 indices to access any element i.e. arr[i][j][k]. You have a nested array structure like this:

[
  [ 
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
  ],
  [ 
    [ 1, 0, 0 ],
    [ 0, 1, 0 ],
    [ 0, 0, 1 ]
  ],
  ...
]

In a physical sense, the shape attribute should be what you pay attention to.

For the basis of 3-space, 3x3 matrix. For the basis of 4-space, 4x4 matrix, and so on.

Upvotes: 1

Related Questions