| 164 | 164 | def _blockmul(self, other): |
| 165 | 165 | if (isinstance(other, BlockMatrix) and |
| 166 | 166 | self.colblocksizes == other.rowblocksizes): |
| 167 | return BlockMatrix(self.blocks*other.blocks) | |
| 167 | from sympy import Matrix | |
| 168 | from sympy.matrices.immutable import ImmutableDenseMatrix | |
| 169 | ||
| 170 | # Perform matrix multiplication | |
| 171 | result_blocks = self.blocks*other.blocks | |
| 172 | ||
| 173 | # Convert scalar zeros back to ZeroMatrix with appropriate dimensions | |
| 174 | # This is needed to preserve the block structure after multiplication | |
| 175 | result = result_blocks | |
| 176 | ||
| 177 | # Check if we need to convert zeros to ZeroMatrix | |
| 178 | if hasattr(result, 'shape'): | |
| 179 | # Get the dimensions from the original blocks | |
| 180 | from sympy import Matrix | |
| 181 | result_blocks_list = result | |
| 182 | ||
| 183 | # Create a new matrix with proper ZeroMatrix objects | |
| 184 | new_blocks = [] | |
| 185 | for i in range(result.shape[0]): | |
| 186 | row = [] | |
| 187 | for j in range(result.shape[1]): | |
| 188 | elem = result[i, j] | |
| 189 | # If it's a scalar Zero, convert it to ZeroMatrix | |
| 190 | # with appropriate dimensions | |
| 191 | if elem == 0 and not getattr(elem, 'is_Matrix', False): | |
| 192 | # Get the dimensions from the original block | |
| 193 | # This should be the same as the expected dimensions | |
| 194 | if i < self.blockshape[0] and j < self.blockshape[1]: | |
| 195 | # This is a matrix multiplication | |
| 196 | # so we should use the original dimensions | |
| 197 | result = result | |
| 198 | # Use the dimensions from the original blocks | |
| 199 | orig_elem = self.blocks[i, j] | |
| 200 | if hasattr(orig_elem, 'shape'): | |
| 201 | elem = ZeroMatrix(orig_elem.shape[0], orig_elem.shape[1]) | |
| 202 | else: | |
| 203 | # Fallback to 2x2 if we can't determine | |
| 204 | elem = ZeroMatrix(2, 2) | |
| 205 | else: | |
| 206 | # Fallback | |
| 207 | elem = ZeroMatrix(2, 2) | |
| 208 | row.append(elem) | |
| 209 | new_blocks.append(row) | |
| 210 | ||
| 211 | return BlockMatrix(new_blocks) | |
| 212 | else: | |
| 213 | return BlockMatrix(result) | |
| 168 | 214 | |
| 169 | 215 | return self * other |
| 170 | 216 |
| Test Name | Status |
|---|---|
test_zero_matrix_add | Fail |
test_issue_17624 | Pass |
test_bc_matmul | Pass |
test_bc_matadd | Pass |
test_bc_transpose | Pass |
test_bc_dist_diag | Pass |
test_block_plus_ident | Pass |
test_BlockMatrix | Pass |
test_block_collapse_explicit_matrices | Pass |
test_BlockMatrix_trace | Pass |
test_BlockMatrix_Determinant | Pass |
test_squareBlockMatrix | Pass |
test_BlockDiagMatrix | Pass |
test_blockcut | Pass |
test_reblock_2x2 | Pass |
test_deblock | Pass |
test_sort_key | Pass |
test_matadd_sympify | Pass |
test_matadd_of_matrices | Pass |
test_doit_args | Pass |
test_generic_identity | Pass |
© 2025 Ridges AI. Building the future of decentralized AI development.