| 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.core.numbers import Zero | |
| 168 | result_blocks = self.blocks*other.blocks | |
| 169 | ||
| 170 | # Convert Zero objects back to ZeroMatrix with appropriate dimensions | |
| 171 | def convert_zero_to_zeromatrix(block, row_idx, col_idx): | |
| 172 | if isinstance(block, Zero): | |
| 173 | # Get dimensions from the corresponding blocks in original matrices | |
| 174 | # The resulting block should have dimensions: | |
| 175 | # rows: self.blocks[row_idx, 0].rows (same as row block size) | |
| 176 | # cols: other.blocks[0, col_idx].cols (same as column block size) | |
| 177 | rows = self.blocks[row_idx, 0].rows | |
| 178 | cols = other.blocks[0, col_idx].cols | |
| 179 | return ZeroMatrix(rows, cols) | |
| 180 | return block | |
| 181 | ||
| 182 | # Apply the conversion to all blocks | |
| 183 | converted_blocks = result_blocks.applyfunc( | |
| 184 | lambda block: convert_zero_to_zeromatrix(block, 0, 0) if isinstance(block, Zero) else block | |
| 185 | ) | |
| 186 | ||
| 187 | # Need to handle each block individually to get correct indices | |
| 188 | from sympy.matrices.immutable import ImmutableDenseMatrix | |
| 189 | converted_data = [] | |
| 190 | for i in range(result_blocks.shape[0]): | |
| 191 | row_data = [] | |
| 192 | for j in range(result_blocks.shape[1]): | |
| 193 | block = result_blocks[i, j] | |
| 194 | if isinstance(block, Zero): | |
| 195 | rows = self.blocks[i, 0].rows | |
| 196 | cols = other.blocks[0, j].cols | |
| 197 | row_data.append(ZeroMatrix(rows, cols)) | |
| 198 | else: | |
| 199 | row_data.append(block) | |
| 200 | converted_data.append(row_data) | |
| 201 | ||
| 202 | return BlockMatrix(converted_data) | |
| 168 | 203 | |
| 169 | 204 | return self * other |
| 170 | 205 |
| 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.