harold.e_i

harold.e_i(width, nth=0, output='c')

Returns the nth column(s) of the identity matrix with shape (width,width). Slicing is permitted with the nth parameter.

The output is returned without slicing an intermediate identity matrix hence can be used without allocating the whole array.

Parameters:
  • width (int) – The size of the identity matrix from which the columns are taken
  • nth (1D index array) – A sequence/index expression that selects the requested columns/rows of the identity matrix. The index starts with zero denoting the first column.
  • output (str) – This switches the shape of the output; if 'r' is given then the rows are returned. The default is 'c' which returns columns
Returns:

E (ndarray) – The resulting row/column subset of the identity matrix

Examples

>>> e_i(7, 5, output='r') # The 5th row of 7x7 identity matrix
array([[ 0.,  0.,  0.,  0.,  0.,  1.,  0.]])
>>> e_i(5, [0, 4, 4, 4, 1])  # Sequences can also be used
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  0.]])
>>> e_i(5,np.s_[1:3])  # or NumPy index expressions
array([[ 0.,  0.],
       [ 1.,  0.],
       [ 0.,  1.],
       [ 0.,  0.],
       [ 0.,  0.]])
>>> e_i(5,slice(1,5,2),output='r')  # or Python slice objects
array([[ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.]])