sourdough

Finished
11from __future__ import print_function, division
22
33from sympy import ask, Q
4from sympy.core import Basic, Add
4from sympy.core import Basic, Add, S
55from sympy.core.compatibility import range
66from sympy.strategies import typed, exhaust, condition, do_one, unpack
77from sympy.strategies.traverse import bottom_up
161161 and self.rowblocksizes == other.rowblocksizes
162162 and self.colblocksizes == other.colblocksizes)
163163
164 def _convert_zero_to_zeromatrix(self, blocks):
165 """
166 Convert Zero scalar objects in blocks to ZeroMatrix objects with appropriate dimensions.
167 This is needed because block multiplication can convert ZeroMatrix objects to Zero scalars.
168 """
169 from sympy.matrices.immutable import ImmutableDenseMatrix
170
171 def convert_block(block, row_idx, col_idx):
172 # If the block is a Zero scalar, convert it to a ZeroMatrix
173 if block is S.Zero:
174 # Get the dimensions from the corresponding row and column block sizes
175 rows = self.rowblocksizes[row_idx]
176 cols = self.colblocksizes[col_idx]
177 return ZeroMatrix(rows, cols)
178 return block
179
180 # Convert each block in the matrix
181 converted_blocks = []
182 for i in range(blocks.shape[0]):
183 row = []
184 for j in range(blocks.shape[1]):
185 row.append(convert_block(blocks[i, j], i, j))
186 converted_blocks.append(row)
187
188 return ImmutableDenseMatrix(converted_blocks)
189
164190 def _blockmul(self, other):
165191 if (isinstance(other, BlockMatrix) and
166192 self.colblocksizes == other.rowblocksizes):
167 return BlockMatrix(self.blocks*other.blocks)
193 result_blocks = self.blocks * other.blocks
194 # Convert Zero scalars back to ZeroMatrix objects
195 result_blocks = self._convert_zero_to_zeromatrix(result_blocks)
196 return BlockMatrix(result_blocks)
168197
169198 return self * other
170199
171200 def _blockadd(self, other):
172201 if (isinstance(other, BlockMatrix)
173202 and self.structurally_equal(other)):
174 return BlockMatrix(self.blocks + other.blocks)
203 result_blocks = self.blocks + other.blocks
204 # Convert Zero scalars back to ZeroMatrix objects
205 result_blocks = self._convert_zero_to_zeromatrix(result_blocks)
206 return BlockMatrix(result_blocks)
175207
176208 return self + other
177209
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.