| 79 | 79 | def __new__(cls, *args, **kwargs): |
| 80 | 80 | from sympy.matrices.immutable import ImmutableDenseMatrix |
| 81 | 81 | from sympy.utilities.iterables import is_sequence |
| 82 | isMat = lambda i: getattr(i, 'is_Matrix', False) | |
| 82 | isMat = lambda i: getattr(i, 'is_Matrix', False) or (hasattr(i, 'is_Number') and getattr(i, 'is_zero', False)) | |
| 83 | 83 | if len(args) != 1 or \ |
| 84 | 84 | not is_sequence(args[0]) or \ |
| 85 | 85 | len(set([isMat(r) for r in args[0]])) != 1: |
| 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 | # Try to find a block in this row with valid dimensions | |
| 154 | row_size = None | |
| 155 | for j in range(self.blockshape[1]): | |
| 156 | block = self.blocks[i, j] | |
| 157 | if hasattr(block, 'rows'): | |
| 158 | row_size = block.rows | |
| 159 | break | |
| 160 | ||
| 161 | # If all blocks in this row are Zero objects, infer from corresponding row | |
| 162 | if row_size is None: | |
| 163 | # In a valid block matrix, all blocks in row i have the same number of rows | |
| 164 | # Look at any column j to find the row size from the corresponding block in row 0 | |
| 165 | # (assuming consistent block structure) | |
| 166 | if self.blockshape[0] > 0 and self.blockshape[1] > 0: | |
| 167 | # Use the row size from the corresponding row (row 0) as reference | |
| 168 | # This assumes all rows have the same structure | |
| 169 | reference_row = 0 if i != 0 else 1 if self.blockshape[0] > 1 else 0 | |
| 170 | for j in range(self.blockshape[1]): | |
| 171 | ref_block = self.blocks[reference_row, j] | |
| 172 | if hasattr(ref_block, 'rows'): | |
| 173 | row_size = ref_block.rows | |
| 174 | break | |
| 175 | ||
| 176 | # If still None, we have a problem | |
| 177 | if row_size is None: | |
| 178 | raise ValueError('Could not determine row size for block at position (%d, %d)' % (i, 0)) | |
| 179 | ||
| 180 | sizes.append(row_size) | |
| 181 | return sizes | |
| 152 | 182 | |
| 153 | 183 | @property |
| 154 | 184 | def colblocksizes(self): |
| 155 | return [self.blocks[0, i].cols for i in range(self.blockshape[1])] | |
| 185 | sizes = [] | |
| 186 | for i in range(self.blockshape[1]): | |
| 187 | # Try to find a block in this column with valid dimensions | |
| 188 | col_size = None | |
| 189 | for j in range(self.blockshape[0]): | |
| 190 | block = self.blocks[j, i] | |
| 191 | if hasattr(block, 'cols'): | |
| 192 | col_size = block.cols | |
| 193 | break | |
| 194 | ||
| 195 | # If all blocks in this column are Zero objects, infer from structure | |
| 196 | if col_size is None: | |
| 197 | # Look at row sizes to infer column dimensions | |
| 198 | # In a valid block matrix, all blocks in a column have the same number of columns | |
| 199 | # We can try to infer this from other columns or from the overall matrix shape | |
| 200 | ||
| 201 | # Simple approach: if this is a square block matrix, use symmetry | |
| 202 | if self.blockshape[0] == self.blockshape[1]: | |
| 203 | # Try to find a corresponding row with non-zero blocks | |
| 204 | for k in range(self.blockshape[1]): | |
| 205 | row_block = self.blocks[i, k] # Transpose position | |
| 206 | if hasattr(row_block, 'rows'): | |
| 207 | col_size = row_block.rows | |
| 208 | break | |
| 209 | ||
| 210 | # If still None, we have a problem | |
| 211 | if col_size is None: | |
| 212 | raise ValueError('Could not determine column size for block at position (%d, %d)' % (0, i)) | |
| 213 | ||
| 214 | sizes.append(col_size) | |
| 215 | return sizes | |
| 156 | 216 | |
| 157 | 217 | def structurally_equal(self, other): |
| 158 | 218 | return (isinstance(other, BlockMatrix) |
| Test Name | Status |
|---|---|
test_issue_17624 | Fail |
test_zero_matrix_add | Fail |
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.