| 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 | from sympy.core.numbers import Zero | |
| 152 | sizes = [] | |
| 153 | for i in range(self.blockshape[0]): | |
| 154 | block = self.blocks[i, 0] | |
| 155 | if isinstance(block, Zero): | |
| 156 | # For Zero scalar, find the row size from another block in the same row | |
| 157 | row_size = None | |
| 158 | for j in range(self.blockshape[1]): | |
| 159 | other_block = self.blocks[i, j] | |
| 160 | if hasattr(other_block, 'rows'): | |
| 161 | row_size = other_block.rows | |
| 162 | break | |
| 163 | if row_size is None: | |
| 164 | # Fallback: if all blocks in row are Zero scalars, use 1 | |
| 165 | row_size = 1 | |
| 166 | sizes.append(row_size) | |
| 167 | else: | |
| 168 | sizes.append(block.rows) | |
| 169 | return sizes | |
| 152 | 170 | |
| 153 | 171 | @property |
| 154 | 172 | def colblocksizes(self): |
| 155 | return [self.blocks[0, i].cols for i in range(self.blockshape[1])] | |
| 173 | from sympy.core.numbers import Zero | |
| 174 | sizes = [] | |
| 175 | for i in range(self.blockshape[1]): | |
| 176 | block = self.blocks[0, i] | |
| 177 | if isinstance(block, Zero): | |
| 178 | # For Zero scalar, find the col size from another block in the same column | |
| 179 | col_size = None | |
| 180 | for j in range(self.blockshape[0]): | |
| 181 | other_block = self.blocks[j, i] | |
| 182 | if hasattr(other_block, 'cols'): | |
| 183 | col_size = other_block.cols | |
| 184 | break | |
| 185 | if col_size is None: | |
| 186 | # Fallback: if all blocks in column are Zero scalars, use 1 | |
| 187 | col_size = 1 | |
| 188 | sizes.append(col_size) | |
| 189 | else: | |
| 190 | sizes.append(block.cols) | |
| 191 | return sizes | |
| 156 | 192 | |
| 157 | 193 | def structurally_equal(self, other): |
| 158 | 194 | return (isinstance(other, BlockMatrix) |
| 164 | 200 | def _blockmul(self, other): |
| 165 | 201 | if (isinstance(other, BlockMatrix) and |
| 166 | 202 | self.colblocksizes == other.rowblocksizes): |
| 167 | return BlockMatrix(self.blocks*other.blocks) | |
| 203 | from sympy.core.numbers import Zero | |
| 204 | from sympy.matrices.expressions.matexpr import ZeroMatrix | |
| 205 | # Compute the block multiplication result | |
| 206 | result_blocks = self.blocks*other.blocks | |
| 207 | # Convert any Zero scalars to appropriate ZeroMatrix objects | |
| 208 | rows, cols = result_blocks.shape | |
| 209 | processed_blocks = [] | |
| 210 | for i in range(rows): | |
| 211 | row = [] | |
| 212 | for j in range(cols): | |
| 213 | block = result_blocks[i, j] | |
| 214 | if isinstance(block, Zero): | |
| 215 | # Determine dimensions from block structure | |
| 216 | # Row dimension from self's row i and other's column j | |
| 217 | row_dim = self.rowblocksizes[i] | |
| 218 | col_dim = other.colblocksizes[j] | |
| 219 | row.append(ZeroMatrix(row_dim, col_dim)) | |
| 220 | else: | |
| 221 | row.append(block) | |
| 222 | processed_blocks.append(row) | |
| 223 | return BlockMatrix(processed_blocks) | |
| 168 | 224 | |
| 169 | 225 | return self * other |
| 170 | 226 |
| 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.