| 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 | sizes = [] | |
| 152 | for i in range(self.blockshape[0]): | |
| 153 | block = self.blocks[i, 0] | |
| 154 | if hasattr(block, 'rows'): | |
| 155 | sizes.append(block.rows) | |
| 156 | else: | |
| 157 | # Handle Zero objects or other objects without rows attribute | |
| 158 | # This should not normally happen in well-formed BlockMatrix | |
| 159 | # but we need to be defensive | |
| 160 | sizes.append(1) # fallback size | |
| 161 | return sizes | |
| 152 | 162 | |
| 153 | 163 | @property |
| 154 | 164 | def colblocksizes(self): |
| 155 | return [self.blocks[0, i].cols for i in range(self.blockshape[1])] | |
| 165 | sizes = [] | |
| 166 | for i in range(self.blockshape[1]): | |
| 167 | block = self.blocks[0, i] | |
| 168 | if hasattr(block, 'cols'): | |
| 169 | sizes.append(block.cols) | |
| 170 | else: | |
| 171 | # Handle Zero objects or other objects without cols attribute | |
| 172 | # This should not normally happen in well-formed BlockMatrix | |
| 173 | # but we need to be defensive | |
| 174 | sizes.append(1) # fallback size | |
| 175 | return sizes | |
| 156 | 176 | |
| 157 | 177 | def structurally_equal(self, other): |
| 158 | 178 | return (isinstance(other, BlockMatrix) |
| 164 | 184 | def _blockmul(self, other): |
| 165 | 185 | if (isinstance(other, BlockMatrix) and |
| 166 | 186 | self.colblocksizes == other.rowblocksizes): |
| 167 | return BlockMatrix(self.blocks*other.blocks) | |
| 187 | # Multiply the blocks | |
| 188 | result_blocks = self.blocks*other.blocks | |
| 189 | ||
| 190 | # Convert Zero objects back to ZeroMatrix objects | |
| 191 | from sympy import S | |
| 192 | from sympy.matrices.expressions.matexpr import ZeroMatrix | |
| 193 | ||
| 194 | # Get row and column sizes directly from the original blocks | |
| 195 | # to avoid issues with Zero objects in the result | |
| 196 | print(f"DEBUG: self.blockshape[0] = {self.blockshape[0]}") | |
| 197 | self_row_sizes = [] | |
| 198 | for i in range(self.blockshape[0]): | |
| 199 | print(f"DEBUG: Getting block self.blocks[{i}, 0]") | |
| 200 | block = self.blocks[i, 0] | |
| 201 | print(f"DEBUG: block type = {type(block)}") | |
| 202 | if hasattr(block, 'rows'): | |
| 203 | print(f"DEBUG: block.rows = {block.rows}") | |
| 204 | self_row_sizes.append(block.rows) | |
| 205 | else: | |
| 206 | # If block doesn't have rows attribute, it might be a Zero | |
| 207 | # In this case, we need to infer the size from the structure | |
| 208 | # This should not happen in well-formed BlockMatrix | |
| 209 | print(f"DEBUG: Block has no rows attribute, using fallback") | |
| 210 | self_row_sizes.append(1) # fallback | |
| 211 | ||
| 212 | other_col_sizes = [] | |
| 213 | for j in range(other.blockshape[1]): | |
| 214 | print(f"DEBUG: Getting block other.blocks[0, {j}]") | |
| 215 | block = other.blocks[0, j] | |
| 216 | print(f"DEBUG: block type = {type(block)}") | |
| 217 | if hasattr(block, 'cols'): | |
| 218 | print(f"DEBUG: block.cols = {block.cols}") | |
| 219 | other_col_sizes.append(block.cols) | |
| 220 | else: | |
| 221 | # If block doesn't have cols attribute, it might be a Zero | |
| 222 | # In this case, we need to infer the size from the structure | |
| 223 | # This should not happen in well-formed BlockMatrix | |
| 224 | print(f"DEBUG: Block has no cols attribute, using fallback") | |
| 225 | other_col_sizes.append(1) # fallback | |
| 226 | ||
| 227 | # Create a new matrix with Zero objects replaced by ZeroMatrix objects | |
| 228 | print(f"DEBUG: result_blocks.shape = {result_blocks.shape}") | |
| 229 | new_blocks = [] | |
| 230 | for i in range(result_blocks.shape[0]): | |
| 231 | row = [] | |
| 232 | for j in range(result_blocks.shape[1]): | |
| 233 | elem = result_blocks[i, j] | |
| 234 | print(f"DEBUG: result_blocks[{i},{j}] = {elem}, type = {type(elem)}") | |
| 235 | print(f"DEBUG: elem == S.Zero: {elem == S.Zero}") | |
| 236 | print(f"DEBUG: hasattr(elem, 'is_Matrix'): {hasattr(elem, 'is_Matrix')}") | |
| 237 | if elem == S.Zero and not hasattr(elem, 'is_Matrix'): | |
| 238 | # Convert Zero to ZeroMatrix with appropriate dimensions | |
| 239 | print(f"DEBUG: Converting Zero to ZeroMatrix({self_row_sizes[i]}, {other_col_sizes[j]})") | |
| 240 | row_size = self_row_sizes[i] | |
| 241 | col_size = other_col_sizes[j] | |
| 242 | elem = ZeroMatrix(row_size, col_size) | |
| 243 | print(f"DEBUG: New elem type = {type(elem)}") | |
| 244 | row.append(elem) | |
| 245 | new_blocks.append(row) | |
| 246 | print(f"DEBUG: About to create BlockMatrix with new_blocks") | |
| 247 | ||
| 248 | return BlockMatrix(new_blocks) | |
| 168 | 249 | |
| 169 | 250 | return self * other |
| 170 | 251 |
| Test Name | Status |
|---|---|
test_issue_17624 | Fail |
test_zero_matrix_add | Fail |
test_bc_matmul | Fail |
test_BlockMatrix | Fail |
test_bc_matadd | Pass |
test_bc_transpose | Pass |
test_bc_dist_diag | Pass |
test_block_plus_ident | 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.