rydiqule.slicing.slicing.matrix_slice¶
- rydiqule.slicing.slicing.matrix_slice(*matrices: ndarray, edges: Iterable | None = None, n_slices: int | None = None) Iterator[Tuple[Tuple[slice, ...], Unpack]] [source]¶
Generator that returns parts of a stack of matrices.
Given a stack of n by n matrices, produces a genererator which returns the given matrices in the specified number of smaller stacks. For example, given a stack of matrices of shape
(10,10,4,4)
with 4 slices, generates 4 stacks of shape(5,4,4)
. Due to the nature of the slicing, the number of slices might be slightly greater that the number specified. Output matrices will be broadcastable by numpy’s broadcasting rules. Input arrays are interpreted as a stack, with the last 2 dimensions staying intact.- Parameters:
matrices (np.ndarray) – matrix stacks to be sliced. All matrices must be of shapes that can be broadcast by numpy’s broadcasting rules, with the additional restriction that all matrices must have the same number of dimensions, even if some dimensions are of size 1. For example, matrices of sizes
(10,1,4,4)
and(1,20,4,4)
can be sliced together.edges (iterable, optional) – The values along each axis that define the edges of bins on an n-dimensional grid. For example, to slice a grid of hamiltonians with stack_shape
(10,10)
into 4 pieces,edges
could be defined as[0,5,10]
for each of the 2 stack axes.n_slices (int, optional) – The number of slices into which to break the matrix stack. Ignored if the
edges
parameter is notNone
. Must be specified as an integer value ifedges
isNone
, ignored otherwise. Defaults to None.
- Yields:
tuple of slices – slicing for each corresponding matrix
numpy.ndarray – Slice of hamiltonian stack
Examples
>>> M1 = np.ones((1,20,4,4)) >>> M2 = np.ones((20,1,4,4)) >>> M3 = np.ones((20,20,4,4)) >>> axis0_edges = np.array([0,10,20]) >>> axis1_edges = np.array([0,10,20]) >>> for idx,m1,m2, m3 in rq.slicing.slicing.matrix_slice(M1, M2, M3, edges=[axis0_edges, axis1_edges]): ... print(m1.shape, m2.shape, m3.shape) (1, 10, 4, 4) (10, 1, 4, 4) (10, 10, 4, 4) (1, 10, 4, 4) (10, 1, 4, 4) (10, 10, 4, 4) (1, 10, 4, 4) (10, 1, 4, 4) (10, 10, 4, 4) (1, 10, 4, 4) (10, 1, 4, 4) (10, 10, 4, 4)