Skip to content

fix array bounds issue #170

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

Merged
merged 3 commits into from
May 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions ot/lp/emd_wrap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ def emd_1d_sorted(np.ndarray[double, ndim=1, mode="c"] u_weights,
cost associated to the optimal transportation
"""
cdef double cost = 0.
cdef int n = u_weights.shape[0]
cdef int m = v_weights.shape[0]
cdef Py_ssize_t n = u_weights.shape[0]
cdef Py_ssize_t m = v_weights.shape[0]

cdef int i = 0
cdef Py_ssize_t i = 0
cdef double w_i = u_weights[0]
cdef int j = 0
cdef Py_ssize_t j = 0
cdef double w_j = v_weights[0]

cdef double m_ij = 0.
Expand All @@ -171,8 +171,8 @@ def emd_1d_sorted(np.ndarray[double, ndim=1, mode="c"] u_weights,
dtype=np.float64)
cdef np.ndarray[long, ndim=2, mode="c"] indices = np.zeros((n + m - 1, 2),
dtype=np.int)
cdef int cur_idx = 0
while i < n and j < m:
cdef Py_ssize_t cur_idx = 0
while True:
if metric == 'sqeuclidean':
m_ij = (u[i] - v[j]) * (u[i] - v[j])
elif metric == 'cityblock' or metric == 'euclidean':
Expand All @@ -188,6 +188,8 @@ def emd_1d_sorted(np.ndarray[double, ndim=1, mode="c"] u_weights,
indices[cur_idx, 0] = i
indices[cur_idx, 1] = j
i += 1
if i == n:
break
w_j -= w_i
w_i = u_weights[i]
else:
Expand All @@ -196,7 +198,10 @@ def emd_1d_sorted(np.ndarray[double, ndim=1, mode="c"] u_weights,
indices[cur_idx, 0] = i
indices[cur_idx, 1] = j
j += 1
if j == m:
break
w_i -= w_j
w_j = v_weights[j]
cur_idx += 1
cur_idx += 1
return G[:cur_idx], indices[:cur_idx], cost