| 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 | # Manual block multiplication to preserve ZeroMatrix objects | |
| 168 | from sympy.matrices.immutable import ImmutableDenseMatrix | |
| 169 | from sympy.matrices.expressions.matexpr import ZeroMatrix | |
| 170 | from sympy import Add | |
| 171 | ||
| 172 | # Get the shapes | |
| 173 | rows, cols = self.blockshape[0], other.blockshape[1] | |
| 174 | k_terms = self.blockshape[1] # or other.blockshape[0] | |
| 175 | ||
| 176 | # Create result matrix | |
| 177 | result_blocks = [[None for _ in range(cols)] for _ in range(rows)] | |
| 178 | ||
| 179 | # Compute each block | |
| 180 | for i in range(rows): | |
| 181 | for j in range(cols): | |
| 182 | # Compute the sum of products for block (i,j) | |
| 183 | terms = [] | |
| 184 | for k in range(k_terms): | |
| 185 | term = self.blocks[i, k] * other.blocks[k, j] | |
| 186 | # Preserve ZeroMatrix objects by not simplifying them to Zero | |
| 187 | if not (isinstance(term, ZeroMatrix) or | |
| 188 | (hasattr(term, 'is_ZeroMatrix') and term.is_ZeroMatrix) or | |
| 189 | (hasattr(term, 'is_zero') and term.is_zero)): | |
| 190 | terms.append(term) | |
| 191 | ||
| 192 | # Handle the result | |
| 193 | if not terms: # All terms are zero | |
| 194 | result_blocks[i][j] = ZeroMatrix(self.blocks[i, 0].rows, other.blocks[0, j].cols) | |
| 195 | elif len(terms) == 1: | |
| 196 | result_blocks[i][j] = terms[0] | |
| 197 | else: | |
| 198 | result_blocks[i][j] = Add(*terms) | |
| 199 | ||
| 200 | return BlockMatrix(ImmutableDenseMatrix(result_blocks)) | |
| 168 | 201 | |
| 169 | 202 | return self * other |
| 170 | 203 |
| 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.