| 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 by inferring dimensions from other blocks | |
| 155 | for j in range(self.blockshape[1]): | |
| 156 | other_block = self.blocks[i, j] | |
| 157 | if hasattr(other_block, 'rows'): | |
| 158 | return other_block.rows | |
| 159 | # If all blocks in this row are scalar, infer from first column | |
| 160 | for k in range(self.blockshape[0]): | |
| 161 | if k != i and hasattr(self.blocks[k, 0], 'rows'): | |
| 162 | return self.blocks[k, 0].rows | |
| 163 | # Fallback: assume 1x1 scalar | |
| 164 | return 1 | |
| 165 | return [_get_block_rows(self.blocks[i, 0], i) for i in range(self.blockshape[0])] | |
| 152 | 166 | |
| 153 | 167 | @property |
| 154 | 168 | def colblocksizes(self): |
| 155 | return [self.blocks[0, i].cols for i in range(self.blockshape[1])] | |
| 169 | def _get_block_cols(block, i): | |
| 170 | if hasattr(block, 'cols'): | |
| 171 | return block.cols | |
| 172 | # Handle scalar zeros by inferring dimensions from other blocks | |
| 173 | for j in range(self.blockshape[0]): | |
| 174 | other_block = self.blocks[j, i] | |
| 175 | if hasattr(other_block, 'cols'): | |
| 176 | return other_block.cols | |
| 177 | # If all blocks in this column are scalar, infer from first row | |
| 178 | for k in range(self.blockshape[1]): | |
| 179 | if k != i and hasattr(self.blocks[0, k], 'cols'): | |
| 180 | return self.blocks[0, k].cols | |
| 181 | # Fallback: assume 1x1 scalar | |
| 182 | return 1 | |
| 183 | return [_get_block_cols(self.blocks[0, i], i) for i in range(self.blockshape[1])] | |
| 156 | 184 | |
| 157 | 185 | def structurally_equal(self, other): |
| 158 | 186 | return (isinstance(other, BlockMatrix) |
| 164 | 192 | def _blockmul(self, other): |
| 165 | 193 | if (isinstance(other, BlockMatrix) and |
| 166 | 194 | self.colblocksizes == other.rowblocksizes): |
| 167 | return BlockMatrix(self.blocks*other.blocks) | |
| 195 | result_blocks = self.blocks*other.blocks | |
| 196 | # Convert scalar zeros back to ZeroMatrix objects with appropriate dimensions | |
| 197 | from sympy.matrices.expressions.matexpr import ZeroMatrix | |
| 198 | from sympy.core.numbers import Zero | |
| 199 | ||
| 200 | def _convert_scalar_to_matrix(block, i, j): | |
| 201 | if isinstance(block, Zero): | |
| 202 | # Determine the dimensions based on block structure | |
| 203 | row_size = self.rowblocksizes[i] | |
| 204 | col_size = other.colblocksizes[j] | |
| 205 | return ZeroMatrix(row_size, col_size) | |
| 206 | return block | |
| 207 | ||
| 208 | # Apply conversion with proper indices | |
| 209 | converted_data = [] | |
| 210 | for i in range(result_blocks.shape[0]): | |
| 211 | converted_row = [] | |
| 212 | for j in range(result_blocks.shape[1]): | |
| 213 | converted_row.append(_convert_scalar_to_matrix(result_blocks[i, j], i, j)) | |
| 214 | converted_data.append(converted_row) | |
| 215 | ||
| 216 | return BlockMatrix(converted_data) | |
| 168 | 217 | |
| 169 | 218 | return self * other |
| 170 | 219 |
| 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.