jackson

Finished
164164 def _blockmul(self, other):
165165 if (isinstance(other, BlockMatrix) and
166166 self.colblocksizes == other.rowblocksizes):
167 return BlockMatrix(self.blocks*other.blocks)
167 return self._blockmul_preserve_zeros(other)
168168
169169 return self * other
170170
171 def _blockmul_preserve_zeros(self, other):
172 """Perform block matrix multiplication while preserving ZeroMatrix objects.
173
174 This method ensures that zero results from block multiplication are created
175 as ZeroMatrix objects with proper dimensions instead of scalar Zero objects.
176 """
177 from sympy.core.numbers import Zero
178 from sympy.matrices.immutable import ImmutableDenseMatrix
179
180 # Get block dimensions
181 n_rows, n_cols = self.blockshape
182 m_rows, m_cols = other.blockshape
183
184 # Result will have shape (n_rows, m_cols)
185 result_blocks = []
186
187 for i in range(n_rows):
188 row_blocks = []
189 for j in range(m_cols):
190 # Compute block (i,j) as sum of products
191 block_sum = None
192 for k in range(n_cols):
193 # Multiply blocks (i,k) and (k,j)
194 block_a = self.blocks[i, k]
195 block_b = other.blocks[k, j]
196
197 # Perform multiplication
198 product = block_a * block_b
199
200 # Convert scalar Zero to ZeroMatrix if needed
201 if product == 0 and not hasattr(product, 'cols'):
202 # Infer dimensions from the multiplication
203 rows = block_a.rows if hasattr(block_a, 'rows') else block_b.rows
204 cols = block_b.cols if hasattr(block_b, 'cols') else block_a.cols
205 product = ZeroMatrix(rows, cols)
206
207 # Add to sum
208 if block_sum is None:
209 block_sum = product
210 else:
211 block_sum = block_sum + product
212
213 # Handle case where addition converts ZeroMatrix to scalar Zero
214 if block_sum == 0 and not hasattr(block_sum, 'cols'):
215 rows = block_a.rows if hasattr(block_a, 'rows') else block_b.rows
216 cols = block_b.cols if hasattr(block_b, 'cols') else block_a.cols
217 block_sum = ZeroMatrix(rows, cols)
218
219 row_blocks.append(block_sum)
220 result_blocks.append(row_blocks)
221
222 return BlockMatrix(result_blocks)
223
171224 def _blockadd(self, other):
172225 if (isinstance(other, BlockMatrix)
173226 and self.structurally_equal(other)):
Test NameStatus
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.