| 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 | # Perform manual block matrix multiplication to preserve ZeroMatrix objects | |
| 168 | from sympy.matrices.expressions.matadd import MatAdd | |
| 169 | from sympy.matrices.expressions.matmul import MatMul | |
| 170 | from sympy.matrices.expressions.matexpr import ZeroMatrix | |
| 171 | from sympy.core.numbers import Zero | |
| 172 | ||
| 173 | # Get dimensions | |
| 174 | m, n = self.blockshape | |
| 175 | p = other.blockshape[1] | |
| 176 | ||
| 177 | # Create result blocks | |
| 178 | result_blocks = [] | |
| 179 | for i in range(m): | |
| 180 | row = [] | |
| 181 | for j in range(p): | |
| 182 | # Compute block [i,j] as sum of products | |
| 183 | terms = [] | |
| 184 | for k in range(n): | |
| 185 | A_ik = self.blocks[i, k] | |
| 186 | B_kj = other.blocks[k, j] | |
| 187 | ||
| 188 | # Handle zero cases explicitly to preserve ZeroMatrix | |
| 189 | if (isinstance(A_ik, ZeroMatrix) or | |
| 190 | isinstance(B_kj, ZeroMatrix) or | |
| 191 | isinstance(A_ik, Zero) or | |
| 192 | isinstance(B_kj, Zero)): | |
| 193 | # Product is zero, but we want to preserve matrix structure | |
| 194 | if isinstance(A_ik, (ZeroMatrix, Zero)) and isinstance(B_kj, (ZeroMatrix, Zero)): | |
| 195 | # Both are zero, result should be zero with proper dimensions | |
| 196 | zero_result = ZeroMatrix(A_ik.rows, B_kj.cols) | |
| 197 | terms.append(zero_result) | |
| 198 | elif isinstance(A_ik, ZeroMatrix) or isinstance(A_ik, Zero): | |
| 199 | # A_ik is zero, result is zero matrix with proper dimensions | |
| 200 | zero_result = ZeroMatrix(A_ik.rows if hasattr(A_ik, 'rows') else self.rowblocksizes[i], | |
| 201 | B_kj.cols if hasattr(B_kj, 'cols') else other.colblocksizes[j]) | |
| 202 | terms.append(zero_result) | |
| 203 | else: # B_kj is zero | |
| 204 | # B_kj is zero, result is zero matrix with proper dimensions | |
| 205 | zero_result = ZeroMatrix(A_ik.rows if hasattr(A_ik, 'rows') else self.rowblocksizes[i], | |
| 206 | B_kj.cols if hasattr(B_kj, 'cols') else other.colblocksizes[j]) | |
| 207 | terms.append(zero_result) | |
| 208 | else: | |
| 209 | # Regular multiplication | |
| 210 | terms.append(MatMul(A_ik, B_kj)) | |
| 211 | ||
| 212 | # Sum the terms | |
| 213 | if len(terms) == 0: | |
| 214 | # This shouldn't happen, but just in case | |
| 215 | row.append(ZeroMatrix(self.rowblocksizes[i], other.colblocksizes[j])) | |
| 216 | elif len(terms) == 1: | |
| 217 | row.append(terms[0]) | |
| 218 | else: | |
| 219 | row.append(MatAdd(*terms)) | |
| 220 | result_blocks.append(row) | |
| 221 | ||
| 222 | return BlockMatrix(result_blocks) | |
| 168 | 223 | |
| 169 | 224 | return self * other |
| 170 | 225 |
| 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.