| 80 | 80 | from sympy.matrices.immutable import ImmutableDenseMatrix |
| 81 | 81 | from sympy.utilities.iterables import is_sequence |
| 82 | 82 | isMat = lambda i: getattr(i, 'is_Matrix', False) |
| 83 | if len(args) != 1 or \ | |
| 84 | not is_sequence(args[0]) or \ | |
| 85 | len(set([isMat(r) for r in args[0]])) != 1: | |
| 83 | # Check if we have a valid sequence | |
| 84 | if len(args) != 1 or not is_sequence(args[0]): | |
| 85 | raise ValueError(filldedent(''' | |
| 86 | expecting a sequence of 1 or more rows | |
| 87 | containing Matrices.''')) | |
| 88 | ||
| 89 | # Check if all elements are either matrices or Zero objects (which can result from matrix operations) | |
| 90 | rows = args[0] | |
| 91 | all_valid = True | |
| 92 | for row in rows: | |
| 93 | for element in row: | |
| 94 | if not (isMat(element) or (hasattr(element, 'is_zero') and element.is_zero)): | |
| 95 | all_valid = False | |
| 96 | break | |
| 97 | if not all_valid: | |
| 98 | break | |
| 99 | ||
| 100 | if not all_valid: | |
| 86 | 101 | raise ValueError(filldedent(''' |
| 87 | 102 | expecting a sequence of 1 or more rows |
| 88 | 103 | containing Matrices.''')) |
| 148 | 163 | |
| 149 | 164 | @property |
| 150 | 165 | def rowblocksizes(self): |
| 151 | return [self.blocks[i, 0].rows for i in range(self.blockshape[0])] | |
| 166 | result = [] | |
| 167 | for i in range(self.blockshape[0]): | |
| 168 | block = self.blocks[i, 0] | |
| 169 | if hasattr(block, 'rows'): | |
| 170 | result.append(block.rows) | |
| 171 | else: | |
| 172 | # Handle Zero objects by finding dimensions from other blocks in the same column | |
| 173 | for j in range(self.blockshape[0]): | |
| 174 | other_block = self.blocks[j, 0] | |
| 175 | if hasattr(other_block, 'rows'): | |
| 176 | result.append(other_block.rows) | |
| 177 | break | |
| 178 | else: | |
| 179 | # Fallback: if all blocks in this column are Zero objects, use shape property | |
| 180 | if hasattr(block, 'shape'): | |
| 181 | result.append(block.shape[0]) | |
| 182 | else: | |
| 183 | # Last resort: assume 1x1 scalar | |
| 184 | result.append(1) | |
| 185 | return result | |
| 152 | 186 | |
| 153 | 187 | @property |
| 154 | 188 | def colblocksizes(self): |
| 155 | return [self.blocks[0, i].cols for i in range(self.blockshape[1])] | |
| 189 | result = [] | |
| 190 | for i in range(self.blockshape[1]): | |
| 191 | block = self.blocks[0, i] | |
| 192 | if hasattr(block, 'cols'): | |
| 193 | result.append(block.cols) | |
| 194 | else: | |
| 195 | # Handle Zero objects by finding dimensions from other blocks in the same row | |
| 196 | for j in range(self.blockshape[1]): | |
| 197 | other_block = self.blocks[0, j] | |
| 198 | if hasattr(other_block, 'cols'): | |
| 199 | result.append(other_block.cols) | |
| 200 | break | |
| 201 | else: | |
| 202 | # Fallback: if all blocks in this row are Zero objects, use shape property | |
| 203 | if hasattr(block, 'shape'): | |
| 204 | result.append(block.shape[1]) | |
| 205 | else: | |
| 206 | # Last resort: assume 1x1 scalar | |
| 207 | result.append(1) | |
| 208 | return result | |
| 156 | 209 | |
| 157 | 210 | def structurally_equal(self, other): |
| 158 | 211 | return (isinstance(other, BlockMatrix) |
| Test Name | Status |
|---|---|
test_issue_17624 | Fail |
test_zero_matrix_add | Fail |
test_BlockMatrix | Fail |
test_block_collapse_explicit_matrices | Fail |
test_BlockMatrix_trace | Fail |
test_reblock_2x2 | 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_Determinant | Pass |
test_squareBlockMatrix | Pass |
test_BlockDiagMatrix | Pass |
test_blockcut | 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.