symengine.py icon indicating copy to clipboard operation
symengine.py copied to clipboard

segfault when creating empty DenseMatrix

Open rikardn opened this issue 5 years ago • 6 comments

The following python code segfaults in ipython using the latest version of symengine from PyPI.

import symengine

symengine.DenseMatrix(4, 4)

Please let me know if I should have filed this issue to the python bindings instead.

rikardn avatar Apr 02 '20 13:04 rikardn

I think that the constructor with just the rows and cols should be made private. I assume that you want a zero matrix right? There's a zeros method for that.

isuruf avatar Apr 02 '20 17:04 isuruf

I think it's useful to be able to create an empty matrix also, like NumPy's empty. But either way it should not segfault, thanks @rikardn for reporting it.

certik avatar Apr 02 '20 18:04 certik

You can create an empty matrix, but you can't print it.

isuruf avatar Apr 02 '20 18:04 isuruf

Adding a check to all function calls to check if it's empty can be done in the python wrapper. Doing it in C++ to each function might be too costly.

isuruf avatar Apr 02 '20 18:04 isuruf

Yes, we should not be doing it in C++.

certik avatar Apr 02 '20 18:04 certik

The constructor for the empty DenseMatrix is as follows:

DenseMatrix::DenseMatrix(unsigned row, unsigned col) : row_(row), col_(col)
{
    m_ = std::vector<RCP<const Basic>>(row * col);
}

It creates the vector and the RCPs, but they don't point to anything. We can assign to them in python with:

import symengine

A = symengine.DenseMatrix(2, 2)
A[0, 1] = 2

but we cannot print the entire matrix until all elements have been assigned. I assume RCP will default to nullptr so I guess checking for nullptrs before printing in the python wrapper is the solution here.

rikardn avatar Jul 21 '21 14:07 rikardn