Skip to content

Commit

Permalink
Merge pull request #963 from serge-sans-paille/fix/pshape-equality
Browse files Browse the repository at this point in the history
Fix pshape eq neq operator
  • Loading branch information
serge-sans-paille committed Jul 18, 2018
2 parents 89de1b8 + f068e0f commit 9ee5488
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pythran/pythonic/include/types/tuple.hpp
Expand Up @@ -945,7 +945,23 @@ namespace sutils
}

template <class S, class B>
bool equals(S const &s, B const &other)
typename std::enable_if<
std::tuple_size<S>::value == std::tuple_size<B>::value, bool>::type
equals(S const &s, B const &other)
{
return equals(
s, other,
std::integral_constant<size_t, std::tuple_size<S>::value - 1>());
}
template <class S, class B>
typename std::enable_if<
std::tuple_size<S>::value != std::tuple_size<B>::value, bool>::type
equals(S const &s, B const &other)
{
return false;
}
template <class S, class B>
bool equals(S const &s, B *other)
{
return equals(
s, other,
Expand Down
54 changes: 54 additions & 0 deletions pythran/tests/cases/_histogram.py
@@ -0,0 +1,54 @@
#pythran export histogram_neq_edges_weights(int32[][], int32[], int32[][])
#runas import numpy as np; d = np.arange(10, dtype=np.int32).reshape(5,2); w = np.ones_like(d, dtype=np.int32); b = np.arange(4, dtype=np.int32); histogram_neq_edges_weights(d, b, w)

import numpy as np



def _histogram_neq_edges_weights(data, bin_edges, weights):
_BLOCK = 65536

bin_edges_length = (len(bin_edges) - 1)

hist = np.zeros(len(bin_edges), weights.dtype)

# omp parallel for private(j, tmp_hist, i, value, bin_idx)
for j in range(0, len(data), _BLOCK):
tmp_hist = np.zeros(len(bin_edges), weights.dtype)
tmp_data = data[j:j + _BLOCK]
tmp_weights = weights[j:j + _BLOCK]

for i in range(0, len(tmp_data)):
value = tmp_data[i]

# Ignore data if NaN or not within range
if np.isnan(value) or not (bin_edges[0] <= value <= bin_edges[-1]):
continue

# Search for index
bin_idx = 0
while bin_idx < bin_edges_length and bin_edges[bin_idx + 1] <= value:
bin_idx += 1

tmp_hist[bin_idx] += tmp_weights[i]

# omp critical
hist += tmp_hist

# Last bin element is inclusive
hist[-2] += hist[-1]

# Remove the redundant extra bin
return hist[:-1], bin_edges



def histogram_neq_edges_weights(data, bin_edges, weights):
if weights.shape != data.shape:
raise ValueError('weights should have the same shape as data.')
weights = weights.ravel()

data = data.ravel()
return _histogram_neq_edges_weights(data, bin_edges, weights)


0 comments on commit 9ee5488

Please sign in to comment.