Skip to content

Bug in ravel_multi_index for big indexes #7546

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

Closed
cmick opened this issue Apr 14, 2016 · 5 comments
Closed

Bug in ravel_multi_index for big indexes #7546

cmick opened this issue Apr 14, 2016 · 5 comments

Comments

@cmick
Copy link

cmick commented Apr 14, 2016

I'm working with big sparse multidimensional arrays using scipy and numpy and I've found a bug in numpy.ravel_multi_index. Specifically, for big index values (greater than 2^32) sometimes the returned value is invalid or even negative (it should be always nonnegative using the default ‘raise’ mode). Probably due to casting integers to int32 in ravel_multi_index.

Sample code that reproduces the problem:

>>> numpy.ravel_multi_index(([0], [1], [3], [19], [2379], [2], [0]), (41, 7, 120, 36, 2706, 8, 6))
array([577726140], dtype=int64)

OK

>>> numpy.ravel_multi_index(([1], [3], [3], [19], [2379], [2], [0]), (41, 7, 120, 36, 2706, 8, 6))
array([1332804284], dtype=int64)

Invalid result, should be [5627771580]

>>> numpy.ravel_multi_index(([29], [5], [117], [2], [1284], [2], [1]), (41, 7, 120, 36, 2706, 8, 6))
array([-7294480627], dtype=int64)

Invalid result, should be [117259570957]

I know that in such a case the maximum index value is huge (1,6104E+11), but not that huge.. I get the same error even when explicitly specifying all integers as numpy.int64. Setting modes {‘raise’, ‘wrap’, ‘clip’} does not have any effect either.

Tested using numpy 1.9.2 (Python 3.4.3 64bit) on Windows, and numpy 1.11.0 (Python 3.4.3 64bit) on Ubuntu.

@charris
Copy link
Member

charris commented Apr 14, 2016

Hmm... I think this should not be too difficult to fix.

@jaimefrio
Copy link
Member

There's an int s variable declared at the begining of arr_ravel_multi_index in compiled_base.c, see here, that should be a npy_intp s. We should probably also be checking for overflow, i.e. dimensions define too big an array, when using s to fill up the strides, similarly to how @seberg did in #7463 using npy_mul_with_overflow_intp.

@charris
Copy link
Member

charris commented Jul 10, 2016

Still broken as of july 10, 2016.

charris added a commit that referenced this issue Oct 22, 2016
Fix bug in ravel_multi_index for big indices (Issue #7546)
@lagru
Copy link
Contributor

lagru commented Jan 30, 2020

This issue seems to have been fixed in #8109 and can be closed now.

@mattip
Copy link
Member

mattip commented Jan 30, 2020

Thanks

@mattip mattip closed this as completed Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants