Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scipy.sparse.bmat fails if all submatrices have same number of rows #8190

Open
andreasbuhr opened this issue Nov 27, 2017 · 7 comments
Open
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse

Comments

@andreasbuhr
Copy link

andreasbuhr commented Nov 27, 2017

scipy.sparse.bmat fails if all submatrices have same number of rows

Reproducing code example:

import numpy as np
import scipy
import scipy.sparse

blocks = [
[ np.ones((2,3)), np.ones((2,4))],
[ np.ones((2,3)), np.ones((2,4))]
]

scipy.sparse.bmat(blocks)

Error message:

Traceback (most recent call last):
  File "bug.py", line 12, in <module>
    scipy.sparse.bmat(blocks)
  File "/usr/lib/python2.7/dist-packages/scipy/sparse/construct.py", line 544, in bmat
    blocks = np.asarray(blocks, dtype='object')
  File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 482, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not broadcast input array from shape (2,3) into shape (2)

Scipy/Numpy/Python version information:

('0.17.0', '1.11.0', sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0))
@pv
Copy link
Member

pv commented Nov 27, 2017 via email

@andreasbuhr
Copy link
Author

yes, sorry, was a typo while reducing the reproducer. Now the code is as intended, after edit

@pv
Copy link
Member

pv commented Nov 27, 2017 via email

@perimosocordiae
Copy link
Member

Reproduced with scipy 1.0.0 and numpy 1.13.3. It seems that np.array has trouble handling the nested lists containing submatrices, despite the dtype=object argument.

@perimosocordiae perimosocordiae added defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse labels Nov 27, 2017
@pv
Copy link
Member

pv commented Nov 27, 2017 via email

@andreasbuhr
Copy link
Author

I worked around it by appending a row of matrices with zero rows each to "blocks".

@perimosocordiae
Copy link
Member

I'm not familiar with how np.array coercion logic works, but this seems like a bug to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse
Projects
None yet
Development

No branches or pull requests

3 participants