Skip to content

Commit

Permalink
Improve calc_nondecreasing_segments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mv77 committed Jan 12, 2022
1 parent 0d5e047 commit a6b6cd3
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions HARK/dcegm.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,19 @@ def calc_nondecreasing_segments(x, y):
if len(x) == 0 or len(y) == 0 or len(y) != len(x):
raise Exception("x and y must be non-empty arrays of the same size.")

# Initialize
starts = [0]
ends = []
# Find points with decreases in x or y
nd = np.logical_or(x[1:] < x[:-1], y[1:] < y[:-1])
idx = np.where(nd)[0]

for i in range(1, len(x)):
# Find the starts and ends of non-decreasing segments. Convoluted because
# numba does not support np.concatenate.
starts = np.zeros(len(idx) + 1, dtype=np.int64)
ends = np.zeros(len(idx) + 1, dtype=np.int64)

# Check if grid decreases in x or v
x_dec = x[i] < x[i - 1]
y_dec = y[i] < y[i - 1]
starts[1:] = idx + 1

if x_dec or y_dec:

ends.append(i - 1)
starts.append(i)

i = i + 1

# The last segment always ends in the last point
ends.append(len(y) - 1)

starts = np.array(starts)
ends = np.array(ends)
ends[:-1] = idx
ends[-1] = len(y) - 1

return starts, ends

Expand Down

0 comments on commit a6b6cd3

Please sign in to comment.