10000 Merged in sergem/numpy/bincount (pull request #12) · prototype99/numpy@6b082f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b082f7

Browse files
committed
Merged in sergem/numpy/bincount (pull request ganesh-k13#12)
Implemented bincount
2 parents 725dcd4 + 3eb096f commit 6b082f7

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

numpy/core/multiarray.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from _numpypy.multiarray import *
22
from _numpypy.multiarray import _reconstruct
3+
from pypy_impl import bincount
34

45
def _fastCopyAndTranspose(a):
56
return a.T.copy()
@@ -39,6 +40,7 @@ def _copyto(dst, src, casting='same_kind', where=None):
3940
if 'copyto' not in globals():
4041
copyto = _copyto
4142

43+
4244
def format_longfloat(x, precision):
4345
return "%%.%df" % precision % x
4446

@@ -99,7 +101,7 @@ def __init__(self, *args, **kwargs):
99101
nested_iters
100102
broadcast empty_like fromiter fromfile frombuffer newbuffer getbuffer
101103
int_asbuffer set_numeric_ops promote_types digitize
102-
lexsort compare_chararrays putmask einsum inner bincount interp
104+
lexsort compare_chararrays putmask einsum inner interp
103105
_vec_string datetime_data correlate correlate2 vdot matmul _insert
104106
datetime_as_string busday_offset busday_count is_busday busdaycalendar
105107
ravel_multi_index unravel_index packbits unpackbits

numpy/core/pypy_impl/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from bincount import bincount
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from numpy.core.multiarray import dtype
2+
from numpy.core.multiarray import zeros
3+
from numpy.core.multiarray import array
4+
5+
6+
def bincount(x, weights=None, minlength=None):
7+
if minlength is None:
8+
minlength = 0
9+
else:
10+
if not isinstance(minlength, (int, long)):
11+
raise TypeError("an integer is required")
12+
if minlength <= 0:
13+
raise ValueError("minlength must be positive")
14+
15+
x = array(x)
16+
len_output = minlength
17+
if len(x) > 0:
18+
if x.min() < 0:
19+
raise ValueError("x must not be negative")
20+
len_output = max(len_output, x.max() + 1)
21+
22+
if x.dtype.kind not in 'ui':
23+
raise ValueError("x must be integer")
24+
25+
if weights is None:
26+
output = zeros(len_output, dtype=dtype('int'))
27+
for elem in x:
28+
output[elem] += 1
29+
else:
30+
if len(weights) != len(x):
31+
raise ValueError("x and weights arrays must have the same size")
32+
output = zeros(len_output, dtype=dtype('float'))
33+
for i in range(len(x)):
34+
output[x[i]] += weights[i]
35+
return output
36+

numpy/core/setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,9 @@ def generate_umath_c(ext, build_dir):
10011001
config.add_data_dir('tests')
10021002
config.add_data_dir('tests/data')
10031003

1004+
if is_pypy():
1005+
config.add_data_dir('pypy_impl')
1006+
10041007
config.make_svn_version_py()
10051008

10061009
return config

0 commit comments

Comments
 (0)
0