Skip to content

happy.ndarray

Jan Kukačka edited this page Mar 9, 2022 · 1 revision

happy.ndarray

N-D array manipulation

Array manipulation


collapse_diag(array):

Convert matrix represented by the last 2 axes of an array to 1D array containing the diagonal of the matrix, leave preceding dimensions unchanged.

Arguments:

  • array: numpy array of shape [..., N, N]

Returns:

  • array: numpy array of shape [..., N] with elements of the diagonal of the last 2 dimensions in the last dimension

Example:

collapse_diag(np.array([[[1, 0, 0],
                         [0, 2, 0],
                         [0, 0, 3]],
                        [[4, 0, 0],
                         [0, 5, 0],
                         [0, 0, 6]]]))
[[1,2,3],
 [4,5,6]]

diag(array):

Convert last axis of an array to a diagonal matrix, leave preceding dimensions unchanged.

Arguments:

  • array: numpy array of shape [..., N]

Returns:

  • array: numpy array of shape [..., N, N] with elements of the input array on the diagonal of the last 2 dimensions

Example:

diag(np.array([[1,2,3],
               [4,5,6]]))
[[[1, 0, 0],
  [0, 2, 0],
  [0, 0, 3]],
 [[4, 0, 0],
  [0, 5, 0],
  [0, 0, 6]]]
Clone this wiki locally