indexing
Indexing Utilities
Utilities
index_dict
- quantecon.util.indexing.index_dict(values)[source]
Build a Numba typed dictionary mapping each value in values to its index.
- Parameters:
- valuesarray_like
Array of unique values, of shape (n,) for scalar values or (n, d) for vector values. dtype must be of float or integer kind (the values are stored as float64 or int64 keys); values must be finite.
- Returns:
- numba.typed.Dict
Dict mapping value -> index, with int64 indices. For an input of shape (n,), keys are scalars (float64 or int64, following the dtype kind of values); for shape (n, d), keys are d-tuples thereof. Usable both from the interpreter and inside Numba-jitted functions, and passable to jitted functions as an argument.
- Raises:
- ValueError
If values contains duplicate, non-finite, or negative-zero values, or is not 1- or 2-dimensional.
Notes
Lookup is by exact equality: query keys must be values drawn from values itself (or arithmetic that reproduces them bitwise). For approximate location on a sorted grid, use np.searchsorted instead.
Numba typed dicts hash floats by bit pattern, so -0.0 and 0.0 are distinct keys (unlike in a Python dict). values therefore must not contain -0.0; a query key that may have been computed as -0.0 can be normalized by adding 0.0 to it.
Examples
>>> d = index_dict(np.array([0.1, 1.0])) >>> d[1.0] 1 >>> d2 = index_dict(np.array([[0., 0.1], [0., 1.], [0.5, 0.1]])) >>> d2[(0.5, 0.1)] 2