| 1 | 1 | from __future__ import print_function, division |
| 2 | 2 | |
| 3 | 3 | from sympy import ask, Q |
| 4 | from sympy.core import Basic, Add | |
| 4 | from sympy.core import Basic, Add, S | |
| 5 | 5 | from sympy.core.compatibility import range |
| 6 | 6 | from sympy.strategies import typed, exhaust, condition, do_one, unpack |
| 7 | 7 | from sympy.strategies.traverse import bottom_up |
| 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 | result = [] | |
| 152 | for i in range(self.blockshape[0]): | |
| 153 | block = self.blocks[i, 0] | |
| 154 | if block is S.Zero: | |
| 155 | # For scalar zero, infer rows from another block in the same row | |
| 156 | # All blocks in the same row have the same number of rows | |
| 157 | for j in range(1, self.blockshape[1]): | |
| 158 | other_block = self.blocks[i, j] | |
| 159 | if other_block is not S.Zero: | |
| 160 | result.append(other_block.rows) | |
| 161 | break | |
| 162 | else: | |
| 163 | # All blocks in this row are scalar zeros | |
| 164 | # This shouldn't happen in a valid block matrix, but handle gracefully | |
| 165 | result.append(0) | |
| 166 | else: | |
| 167 | result.append(block.rows) | |
| 168 | return result | |
| 152 | 169 | |
| 153 | 170 | @property |
| 154 | 171 | def colblocksizes(self): |
| 155 | return [self.blocks[0, i].cols for i in range(self.blockshape[1])] | |
| 172 | result = [] | |
| 173 | for i in range(self.blockshape[1]): | |
| 174 | block = self.blocks[0, i] | |
| 175 | if block is S.Zero: | |
| 176 | # For scalar zero, infer cols from another block in the same column | |
| 177 | # All blocks in the same column have the same number of columns | |
| 178 | for j in range(1, self.blockshape[0]): | |
| 179 | other_block = self.blocks[j, i] | |
| 180 | if other_block is not S.Zero: | |
| 181 | result.append(other_block.cols) | |
| 182 | break | |
| 183 | else: | |
| 184 | # All blocks in this column are scalar zeros | |
| 185 | # This shouldn't happen in a valid block matrix, but handle gracefully | |
| 186 | result.append(0) | |
| 187 | else: | |
| 188 | result.append(block.cols) | |
| 189 | return result | |
| 156 | 190 | |
| 157 | 191 | def structurally_equal(self, other): |
| 158 | 192 | return (isinstance(other, BlockMatrix) |
| 164 | 198 | def _blockmul(self, other): |
| 165 | 199 | if (isinstance(other, BlockMatrix) and |
| 166 | 200 | self.colblocksizes == other.rowblocksizes): |
| 167 | return BlockMatrix(self.blocks*other.blocks) | |
| 201 | # Multiply the blocks | |
| 202 | result_blocks = self.blocks * other.blocks | |
| 203 | ||
| 204 | # Convert scalar zeros to ZeroMatrix objects | |
| 205 | from sympy.matrices.immutable import ImmutableDenseMatrix | |
| 206 | rows, cols = result_blocks.shape | |
| 207 | converted_data = [] | |
| 208 | for i in range(rows): | |
| 209 | row_data = [] | |
| 210 | for j in range(cols): | |
| 211 | block = result_blocks[i, j] | |
| 212 | if block is S.Zero: | |
| 213 | # Determine dimensions from the input matrices' structure | |
| 214 | # Row dimension comes from self's row i (all blocks in that row have same rows) | |
| 215 | row_dim = self.rowblocksizes[i] | |
| 216 | # Column dimension comes from other's column j (all blocks in that column have same cols) | |
| 217 | col_dim = other.colblocksizes[j] | |
| 218 | # Create ZeroMatrix with appropriate dimensions | |
| 219 | block = ZeroMatrix(row_dim, col_dim) | |
| 220 | row_data.append(block) | |
| 221 | converted_data.append(row_data) | |
| 222 | ||
| 223 | # Create matrix with converted blocks | |
| 224 | converted_matrix = ImmutableDenseMatrix(converted_data) | |
| 225 | return BlockMatrix(converted_matrix) | |
| 168 | 226 | |
| 169 | 227 | return self * other |
| 170 | 228 |
| 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.