| 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 block matrix multiplication while preserving ZeroMatrix types | |
| 168 | from sympy.matrices.immutable import ImmutableDenseMatrix | |
| 169 | from sympy import S | |
| 170 | ||
| 171 | # Get the shapes | |
| 172 | m, n = self.blockshape | |
| 173 | n2, p = other.blockshape | |
| 174 | ||
| 175 | # Create result blocks | |
| 176 | result_blocks = [] | |
| 177 | for i in range(m): | |
| 178 | row = [] | |
| 179 | for j in range(p): | |
| 180 | # Compute the block at position (i, j) | |
| 181 | block_products = [] | |
| 182 | for k in range(n): | |
| 183 | block_product = self.blocks[i, k] * other.blocks[k, j] | |
| 184 | block_products.append(block_product) | |
| 185 | ||
| 186 | # Sum the block products properly | |
| 187 | if not block_products: | |
| 188 | # This shouldn't happen in normal matrix multiplication | |
| 189 | block_sum = S.Zero | |
| 190 | else: | |
| 191 | # Start with the first product | |
| 192 | block_sum = block_products[0] | |
| 193 | # Add the rest | |
| 194 | for product in block_products[1:]: | |
| 195 | block_sum = block_sum + product | |
| 196 | ||
| 197 | # If the result is Zero scalar, convert it back to ZeroMatrix | |
| 198 | # with appropriate dimensions | |
| 199 | if block_sum == S.Zero: | |
| 200 | # Determine the dimensions of this block | |
| 201 | rows = self.blocks[i, 0].rows if n > 0 else 0 | |
| 202 | cols = other.blocks[0, j].cols if n2 > 0 else 0 | |
| 203 | if rows > 0 and cols > 0: | |
| 204 | block_sum = ZeroMatrix(rows, cols) | |
| 205 | ||
| 206 | row.append(block_sum) | |
| 207 | result_blocks.append(row) | |
| 208 | ||
| 209 | return BlockMatrix(result_blocks) | |
| 168 | 210 | |
| 169 | 211 | return self * other |
| 170 | 212 |
| 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.