ML Foundations
Track inserted values after multiple NumPy insertions
Convert insertion offsets measured in an original sorted array into positions in the expanded result.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
Insertion offsets describe the original array
NumPy searchsorted returns insertion positions in the input sorted array. Those offsets are not necessarily the final indices after inserting several values at once: earlier insertions move later elements to the right. NumPy insert returns a new array with values inserted before the supplied positions. [1, 2]
Suppose a sorted feature threshold table receives several new thresholds. If the caller needs the positions of the new entries in the result, using the original insertion offsets directly can pick an older element or a different inserted value. Keep the two coordinate systems separate.
Adjust offsets when the new values are already sorted
Run python examples/numpy_insert_positions.py. The example searches a sorted array for four values that are also sorted, including a repeated value. Searchsorted gives the positions relative to the original array. The example inserts the new values, then adds the number of preceding new values to each offset. [1, 2]
The original offsets are 1, 2, 2 and 4, while the final positions are 1, 3, 4 and 7. Indexing the expanded array with the adjusted positions retrieves the four inserted values: 1, 3, 3 and 8. An assertion checks that mapping on this fixture, including the two new values sharing one insertion offset.
The left insertion rule places new values before equal existing values. The adjustment shown relies on the new values being in nondecreasing order, so the number of earlier insertions corresponds to their order in the input.
Executed with Python 3.12.14 and NumPy 2.3.5; stdout and checksum are in private evidence.
import numpy as np
original = np.array([0, 2, 4, 6])
new_values = np.array([1, 3, 3, 8])
positions = np.searchsorted(original, new_values, side="left")
merged = np.insert(original, positions, new_values)
final_positions = positions + np.arange(new_values.size)
print("original offsets:", positions.tolist())
print("merged:", merged.tolist())
print("final positions:", final_positions.tolist())
print("inserted values:", merged[final_positions].tolist())
assert merged[final_positions].tolist() == new_values.tolist()
Recorded output
original offsets: [1, 2, 2, 4] merged: [0, 1, 2, 3, 3, 4, 6, 8] final positions: [1, 3, 4, 7] inserted values: [1, 3, 3, 8]
Define ordering and identity before generalizing
Do not apply this simple offset adjustment to unsorted new values: their order and insertion positions can differ. Sort them with a stable ordering and retain a mapping to their original identities if the caller needs positions in the original input order. This guide does not provide or verify that more general implementation.
The fixture uses finite one-dimensional integer arrays. Other dtypes, NaNs, multidimensional insertion, different tie policies and production-scale performance are outside the example. If equal values carry distinct metadata, test the identity mapping explicitly rather than comparing values alone.