Finished
7979 def __new__(cls, *args, **kwargs):
8080 from sympy.matrices.immutable import ImmutableDenseMatrix
8181 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)
8384 if len(args) != 1 or \
8485 not is_sequence(args[0]) or \
8586 len(set([isMat(r) for r in args[0]])) != 1:
9697 if ok:
9798 # same number of rows for each matrix in a row
9899 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
100101 if not ok:
101102 break
102103 blocky = ok
103104 # same number of cols for each matrix in each col
104105 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
106107 for i in range(len(rows))])) == 1
107108 if not ok:
108109 break
109110 if not ok:
110111 # same total cols in each row
111112 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
113114 if blocky and ok:
114115 raise ValueError(filldedent('''
115116 Although this matrix is comprised of blocks,
130131
131132 @property
132133 def shape(self):
134 from sympy.core.numbers import Zero
133135 numrows = numcols = 0
134136 M = self.blocks
135137 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]
137154 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]
139171 return (numrows, numcols)
140172
141173 @property
148180
149181 @property
150182 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
152204
153205 @property
154206 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
156228
157229 def structurally_equal(self, other):
158230 return (isinstance(other, BlockMatrix)
Test NameStatus
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.