| 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 Zero objects back to ZeroMatrix with appropriate dimensions | |
| 169 | from sympy.core.numbers import Zero | |
| 170 | from sympy.matrices.immutable import ImmutableDenseMatrix | |
| 171 | ||
| 172 | # Create a new matrix with Zero objects replaced by ZeroMatrix | |
| 173 | new_data = [] | |
| 174 | for i in range(result_blocks.shape[0]): | |
| 175 | row = [] | |
| 176 | for j in range(result_blocks.shape[1]): | |
| 177 | elem = result_blocks[i, j] | |
| 178 | if isinstance(elem, Zero): | |
| 179 | # Determine the dimensions for the ZeroMatrix | |
| 180 | # The row dimension comes from self.rowblocksizes[i] | |
| 181 | # The column dimension comes from other.colblocksizes[j] | |
| 182 | rows = self.rowblocksizes[i] | |
| 183 | cols = other.colblocksizes[j] | |
| 184 | elem = ZeroMatrix(rows, cols) | |
| 185 | row.append(elem) | |
| 186 | new_data.append(row) | |
| 187 | ||
| 188 | return BlockMatrix(ImmutableDenseMatrix(new_data)) | |
| 168 | 189 | |
| 169 | 190 | return self * other |
| 170 | 191 | |
| 171 | 192 | def _blockadd(self, other): |
| 172 | 193 | if (isinstance(other, BlockMatrix) |
| 173 | 194 | and self.structurally_equal(other)): |
| 174 | return BlockMatrix(self.blocks + other.blocks) | |
| 195 | result_blocks = self.blocks + other.blocks | |
| 196 | # Convert Zero objects back to ZeroMatrix with appropriate dimensions | |
| 197 | from sympy.core.numbers import Zero | |
| 198 | from sympy.matrices.immutable import ImmutableDenseMatrix | |
| 199 | ||
| 200 | # Create a new matrix with Zero objects replaced by ZeroMatrix | |
| 201 | new_data = [] | |
| 202 | for i in range(result_blocks.shape[0]): | |
| 203 | row = [] | |
| 204 | for j in range(result_blocks.shape[1]): | |
| 205 | elem = result_blocks[i, j] | |
| 206 | if isinstance(elem, Zero): | |
| 207 | # Determine the dimensions for the ZeroMatrix | |
| 208 | # For addition, both matrices have the same dimensions | |
| 209 | rows = self.rowblocksizes[i] | |
| 210 | cols = self.colblocksizes[j] | |
| 211 | elem = ZeroMatrix(rows, cols) | |
| 212 | row.append(elem) | |
| 213 | new_data.append(row) | |
| 214 | ||
| 215 | return BlockMatrix(new_data) | |
| 175 | 216 | |
| 176 | 217 | return self + other |
| 177 | 218 |
| 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.