This is shorter but no less clear.
Joseph Weston authored on 10/12/2019 16:20:53... | ... |
@@ -742,37 +742,37 @@ def is_vectorized(syst): |
742 | 742 |
return isinstance(syst, (FiniteVectorizedSystem, InfiniteVectorizedSystem)) |
743 | 743 |
|
744 | 744 |
|
745 |
-def _normalize_matrix_blocks(matrix_blocks, expected_length): |
|
745 |
+def _normalize_matrix_blocks(blocks, expected_length): |
|
746 | 746 |
"""Normalize a sequence of matrices into a single 3D numpy array |
747 | 747 |
|
748 | 748 |
Parameters |
749 | 749 |
---------- |
750 |
- matrix_blocks : sequence of complex array-like |
|
750 |
+ blocks : sequence of complex array-like |
|
751 | 751 |
expected_length : int |
752 | 752 |
""" |
753 | 753 |
try: |
754 |
- matrix_blocks = np.asarray(matrix_blocks, dtype=complex) |
|
754 |
+ blocks = np.asarray(blocks, dtype=complex) |
|
755 | 755 |
except TypeError: |
756 | 756 |
raise ValueError( |
757 | 757 |
"Matrix elements declared with incompatible shapes." |
758 | 758 |
) from None |
759 |
- if len(matrix_blocks.shape) == 0: # scalar → broadcast to vector of 1x1 matrices |
|
760 |
- matrix_blocks = np.tile(matrix_blocks, (expected_length, 1, 1)) |
|
761 |
- elif len(matrix_blocks.shape) == 1: # vector → interpret as vector of 1x1 matrices |
|
762 |
- matrix_blocks = matrix_blocks.reshape(-1, 1, 1) |
|
763 |
- elif len(matrix_blocks.shape) == 2: # matrix → broadcast to vector of matrices |
|
764 |
- matrix_blocks = np.tile(matrix_blocks, (expected_length, 1, 1)) |
|
765 |
- |
|
766 |
- if len(matrix_blocks.shape) != 3: |
|
759 |
+ if len(blocks.shape) == 0: # scalar → broadcast to vector of 1x1 matrices |
|
760 |
+ blocks = np.tile(blocks, (expected_length, 1, 1)) |
|
761 |
+ elif len(blocks.shape) == 1: # vector → interpret as vector of 1x1 matrices |
|
762 |
+ blocks = blocks.reshape(-1, 1, 1) |
|
763 |
+ elif len(blocks.shape) == 2: # matrix → broadcast to vector of matrices |
|
764 |
+ blocks = np.tile(blocks, (expected_length, 1, 1)) |
|
765 |
+ |
|
766 |
+ if len(blocks.shape) != 3: |
|
767 | 767 |
msg = ( |
768 | 768 |
"Vectorized value functions must return an array of" |
769 | 769 |
"scalars or an array of matrices." |
770 | 770 |
) |
771 | 771 |
raise ValueError(msg) |
772 |
- if matrix_blocks.shape[0] != expected_length: |
|
772 |
+ if blocks.shape[0] != expected_length: |
|
773 | 773 |
raise ValueError("Value functions must return a single value per " |
774 | 774 |
"onsite/hopping.") |
775 |
- return matrix_blocks |
|
775 |
+ return blocks |
|
776 | 776 |
|
777 | 777 |
|
778 | 778 |
|