| 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 | from sympy.core.numbers import Zero | |
| 83 | isMat = lambda i: getattr(i, 'is_Matrix', False) or isinstance(i, Zero) | |
| 83 | 84 | if len(args) != 1 or \ |
| 84 | 85 | not is_sequence(args[0]) or \ |
| 85 | 86 | len(set([isMat(r) for r in args[0]])) != 1: |
| 96 | 97 | if ok: |
| 97 | 98 | # same number of rows for each matrix in a row |
| 98 | 99 | for r in rows: |
| 99 | ok = len(set([i.rows for i in r])) == 1 | |
| 100 | ok = len(set([i.rows if hasattr(i, 'rows') else 1 for i in r])) == 1 | |
| 100 | 101 | if not ok: |
| 101 | 102 | break |
| 102 | 103 | blocky = ok |
| 103 | 104 | # same number of cols for each matrix in each col |
| 104 | 105 | for c in range(len(rows[0])): |
| 105 | ok = len(set([rows[i][c].cols | |
| 106 | ok = len(set([rows[i][c].cols if hasattr(rows[i][c], 'cols') else 1 | |
| 106 | 107 | for i in range(len(rows))])) == 1 |
| 107 | 108 | if not ok: |
| 108 | 109 | break |
| 109 | 110 | if not ok: |
| 110 | 111 | # same total cols in each row |
| 111 | 112 | ok = len(set([ |
| 112 | sum([i.cols for i in r]) for r in rows])) == 1 | |
| 113 | sum([i.cols if hasattr(i, 'cols') else 1 for i in r]) for r in rows])) == 1 | |
| 113 | 114 | if blocky and ok: |
| 114 | 115 | raise ValueError(filldedent(''' |
| 115 | 116 | Although this matrix is comprised of blocks, |
| 130 | 131 | |
| 131 | 132 | @property |
| 132 | 133 | def shape(self): |
| 134 | from sympy.core.numbers import Zero | |
| 133 | 135 | numrows = numcols = 0 |
| 134 | 136 | M = self.blocks |
| 135 | 137 | for i in range(M.shape[0]): |
| 136 | numrows += M[i, 0].shape[0] | |
| 138 | block = M[i, 0] | |
| 139 | if isinstance(block, Zero): | |
| 140 | # For scalar Zero objects, infer size from another block in the same row | |
| 141 | row_size = None | |
| 142 | for j in range(M.shape[1]): | |
| 143 | other_block = M[i, j] | |
| 144 | if hasattr(other_block, 'shape'): | |
| 145 | row_size = other_block.shape[0] | |
| 146 | break | |
| 147 | if row_size is not None: | |
| 148 | numrows += row_size | |
| 149 | else: | |
| 150 | # Fallback: use 1 as default size | |
| 151 | numrows += 1 | |
| 152 | else: | |
| 153 | numrows += block.shape[0] | |
| 137 | 154 | for i in range(M.shape[1]): |
| 138 | numcols += M[0, i].shape[1] | |
| 155 | block = M[0, i] | |
| 156 | if isinstance(block, Zero): | |
| 157 | # For scalar Zero objects, infer size from another block in the same column | |
| 158 | col_size = None | |
| 159 | for j in range(M.shape[0]): | |
| 160 | other_block = M[j, i] | |
| 161 | if hasattr(other_block, 'shape'): | |
| 162 | col_size = other_block.shape[1] | |
| 163 | break | |
| 164 | if col_size is not None: | |
| 165 | numcols += col_size | |
| 166 | else: | |
| 167 | # Fallback: use 1 as default size | |
| 168 | numcols += 1 | |
| 169 | else: | |
| 170 | numcols += block.shape[1] | |
| 139 | 171 | return (numrows, numcols) |
| 140 | 172 | |
| 141 | 173 | @property |
| 148 | 180 | |
| 149 | 181 | @property |
| 150 | 182 | def rowblocksizes(self): |
| 151 | return [self.blocks[i, 0].rows for i in range(self.blockshape[0])] | |
| 183 | from sympy.core.numbers import Zero | |
| 184 | result = [] | |
| 185 | for i in range(self.blockshape[0]): | |
| 186 | block = self.blocks[i, 0] | |
| 187 | if isinstance(block, Zero): | |
| 188 | # For scalar Zero objects, infer size from another block in the same row | |
| 189 | # that has valid dimensions | |
| 190 | row_size = None | |
| 191 | for j in range(self.blockshape[1]): | |
| 192 | other_block = self.blocks[i, j] | |
| 193 | if hasattr(other_block, 'rows'): | |
| 194 | row_size = other_block.rows | |
| 195 | break | |
| 196 | if row_size is not None: | |
| 197 | result.append(row_size) | |
| 198 | else: | |
| 199 | # Fallback: use 1 as default size | |
| 200 | result.append(1) | |
| 201 | else: | |
| 202 | result.append(block.rows) | |
| 203 | return result | |
| 152 | 204 | |
| 153 | 205 | @property |
| 154 | 206 | def colblocksizes(self): |
| 155 | return [self.blocks[0, i].cols for i in range(self.blockshape[1])] | |
| 207 | from sympy.core.numbers import Zero | |
| 208 | result = [] | |
| 209 | for i in range(self.blockshape[1]): | |
| 210 | block = self.blocks[0, i] | |
| 211 | if isinstance(block, Zero): | |
| 212 | # For scalar Zero objects, infer size from another block in the same column | |
| 213 | # that has valid dimensions | |
| 214 | col_size = None | |
| 215 | for j in range(self.blockshape[0]): | |
| 216 | other_block = self.blocks[j, i] | |
| 217 | if hasattr(other_block, 'cols'): | |
| 218 | col_size = other_block.cols | |
| 219 | break | |
| 220 | if col_size is not None: | |
| 221 | result.append(col_size) | |
| 222 | else: | |
| 223 | # Fallback: use 1 as default size | |
| 224 | result.append(1) | |
| 225 | else: | |
| 226 | result.append(block.cols) | |
| 227 | return result | |
| 156 | 228 | |
| 157 | 229 | def structurally_equal(self, other): |
| 158 | 230 | 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.