Skip to content

Commit a2c292d

Browse files
authored
Merge pull request #133 from ShikharJ/Issue132
Implemented DenseMatrix.reshape() function
2 parents 9e5d9bf + cb84754 commit a2c292d

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

symengine/lib/symengine.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ cdef extern from "<symengine/matrix.h>" namespace "SymEngine":
619619
DenseMatrix()
620620
DenseMatrix(unsigned i, unsigned j) nogil
621621
DenseMatrix(unsigned i, unsigned j, const vec_basic &v) nogil
622+
void resize(unsigned i, unsigned j) nogil
622623

623624
bool is_a_DenseMatrix "SymEngine::is_a<SymEngine::DenseMatrix>"(const MatrixBase &b) nogil
624625
DenseMatrix* static_cast_DenseMatrix "static_cast<SymEngine::DenseMatrix*>"(const MatrixBase *a)

symengine/lib/symengine_wrapper.pyx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,13 @@ cdef class DenseMatrix(MatrixBase):
17401740
def __get__(self):
17411741
return self.nrows()*self.ncols()
17421742

1743+
def reshape(self, rows, cols):
1744+
if len(self) != rows*cols:
1745+
raise ValueError("Invalid reshape parameters %d %d" % (rows, cols))
1746+
r = DenseMatrix(self)
1747+
deref(symengine.static_cast_DenseMatrix(r.thisptr)).resize(rows, cols)
1748+
return r
1749+
17431750
def _get_index(self, i, j):
17441751
nr = self.nrows()
17451752
nc = self.ncols()

symengine/tests/test_matrices.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ def test_shape():
289289
A = DenseMatrix(2, 2, [1, 2, 3, 4])
290290
assert A.shape == (2, 2)
291291

292+
def test_reshape():
293+
A = DenseMatrix(2, 2, [1, 2, 3, 4])
294+
B = DenseMatrix(4, 1, [1, 2, 3, 4])
295+
C = A.reshape(4, 1)
296+
assert C == B
297+
assert C != A
298+
292299
#@pytest.mark.skipif(not HAVE_NUMPY, reason='requires numpy')
293300
def test_dump_real():
294301
if not HAVE_NUMPY: # nosetests work-around

0 commit comments

Comments
 (0)