| 148 | 148 | |
| 149 | 149 | @property |
| 150 | 150 | def rowblocksizes(self): |
| 151 | return [self.blocks[i, 0].rows for i in range(self.blockshape[0])] | |
| 151 | def _get_block_rows(block, i): | |
| 152 | if hasattr(block, 'rows'): | |
| 153 | return block.rows | |
| 154 | # Handle scalar zeros - they should have the same dimensions as | |
| 155 | # the corresponding block in the first column | |
| 156 | # We need to infer this from the block structure | |
| 157 | from sympy import S | |
| 158 | if block == S.Zero: | |
| 159 | # For scalar zeros, we need to determine the expected dimensions | |
| 160 | # This is a workaround for when ZeroMatrix gets simplified to Zero | |
| 161 | # We'll use the fact that all blocks in a row should have the same height | |
| 162 | for j in range(self.blockshape[1]): | |
| 163 | other_block = self.blocks[i, j] | |
| 164 | if hasattr(other_block, 'rows'): | |
| 165 | return other_block.rows | |
| 166 | # If we can't determine, assume 1x1 (this shouldn't happen in valid block matrices) | |
| 167 | return 1 | |
| 168 | raise AttributeError(f"Block {block} does not have rows attribute") | |
| 169 | return [_get_block_rows(self.blocks[i, 0], i) for i in range(self.blockshape[0])] | |
| 152 | 170 | |
| 153 | 171 | @property |
| 154 | 172 | def colblocksizes(self): |
| 155 | return [self.blocks[0, i].cols for i in range(self.blockshape[1])] | |
| 173 | def _get_block_cols(block, j): | |
| 174 | if hasattr(block, 'cols'): | |
| 175 | return block.cols | |
| 176 | # Handle scalar zeros - they should have the same dimensions as | |
| 177 | # the corresponding block in the first row | |
| 178 | # We need to infer this from the block structure | |
| 179 | from sympy import S | |
| 180 | if block == S.Zero: | |
| 181 | # For scalar zeros, we need to determine the expected dimensions | |
| 182 | # This is a workaround for when ZeroMatrix gets simplified to Zero | |
| 183 | # We'll use the fact that all blocks in a column should have the same width | |
| 184 | for i in range(self.blockshape[0]): | |
| 185 | other_block = self.blocks[i, j] | |
| 186 | if hasattr(other_block, 'cols'): | |
| 187 | return other_block.cols | |
| 188 | # If we can't determine, assume 1x1 (this shouldn't happen in valid block matrices) | |
| 189 | return 1 | |
| 190 | raise AttributeError(f"Block {block} does not have cols attribute") | |
| 191 | return [_get_block_cols(self.blocks[0, j], j) for j in range(self.blockshape[1])] | |
| 156 | 192 | |
| 157 | 193 | def structurally_equal(self, other): |
| 158 | 194 | return (isinstance(other, BlockMatrix) |
| 164 | 200 | def _blockmul(self, other): |
| 165 | 201 | if (isinstance(other, BlockMatrix) and |
| 166 | 202 | self.colblocksizes == other.rowblocksizes): |
| 167 | return BlockMatrix(self.blocks*other.blocks) | |
| 203 | # Perform block multiplication | |
| 204 | result_blocks = self.blocks*other.blocks | |
| 205 | ||
| 206 | # Convert scalar Zero objects back to ZeroMatrix with appropriate dimensions | |
| 207 | from sympy import S | |
| 208 | from sympy.matrices.immutable import ImmutableDenseMatrix | |
| 209 | ||
| 210 | def _fix_zero_block(block, i, j): | |
| 211 | if block == S.Zero: | |
| 212 | # Determine the dimensions for this zero block | |
| 213 | # Row dimension comes from self.rowblocksizes[i] | |
| 214 | # Column dimension comes from other.colblocksizes[j] | |
| 215 | rows = self.rowblocksizes[i] | |
| 216 | cols = other.colblocksizes[j] | |
| 217 | return ZeroMatrix(rows, cols) | |
| 218 | return block | |
| 219 | ||
| 220 | # Apply the fix to all blocks | |
| 221 | fixed_data = [[_fix_zero_block(result_blocks[i, j], i, j) | |
| 222 | for j in range(result_blocks.shape[1])] | |
| 223 | for i in range(result_blocks.shape[0])] | |
| 224 | fixed_blocks = ImmutableDenseMatrix(fixed_data) | |
| 225 | ||
| 226 | return BlockMatrix(fixed_blocks) | |
| 168 | 227 | |
| 169 | 228 | return self * other |
| 170 | 229 |
| 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.