| 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 | result_blocks = self.blocks*other.blocks | |
| 168 | # Convert scalar Zero objects back to ZeroMatrix with correct dimensions | |
| 169 | from sympy.core.numbers import Zero | |
| 170 | fixed_blocks = self._fix_zero_blocks(result_blocks, other) | |
| 171 | return BlockMatrix(fixed_blocks) | |
| 168 | 172 | |
| 169 | 173 | return self * other |
| 170 | 174 | |
| 175 | def _fix_zero_blocks(self, blocks, other): | |
| 176 | """Convert scalar Zero objects to ZeroMatrix with correct dimensions.""" | |
| 177 | from sympy.core.numbers import Zero | |
| 178 | from sympy.matrices.immutable import ImmutableDenseMatrix | |
| 179 | ||
| 180 | if not hasattr(blocks, 'shape'): | |
| 181 | return blocks | |
| 182 | ||
| 183 | rows, cols = blocks.shape | |
| 184 | fixed_data = [] | |
| 185 | ||
| 186 | for i in range(rows): | |
| 187 | row_data = [] | |
| 188 | for j in range(cols): | |
| 189 | block = blocks[i, j] | |
| 190 | if isinstance(block, Zero): | |
| 191 | # Determine the correct dimensions for this ZeroMatrix | |
| 192 | # The result block at (i,j) should have dimensions: | |
| 193 | # rows: self.rowblocksizes[i] x other.colblocksizes[j] | |
| 194 | # But we need to get this information from the context | |
| 195 | # For now, we'll use the dimensions from the original blocks | |
| 196 | # This is a heuristic that should work for most cases | |
| 197 | if hasattr(self, 'rowblocksizes') and hasattr(other, 'colblocksizes'): | |
| 198 | nrows = self.rowblocksizes[i] | |
| 199 | ncols = other.colblocksizes[j] | |
| 200 | block = ZeroMatrix(nrows, ncols) | |
| 201 | else: | |
| 202 | # Fallback: try to determine dimensions from context | |
| 203 | # This is less reliable but better than nothing | |
| 204 | block = ZeroMatrix(1, 1) # Default to 1x1 | |
| 205 | row_data.append(block) | |
| 206 | fixed_data.append(row_data) | |
| 207 | ||
| 208 | return ImmutableDenseMatrix(fixed_data) | |
| 209 | ||
| 171 | 210 | def _blockadd(self, other): |
| 172 | 211 | if (isinstance(other, BlockMatrix) |
| 173 | 212 | and self.structurally_equal(other)): |
| 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.