-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
EHN: Implement cartesian_nearest_index
#660
Conversation
quantecon/gridtools.py
Outdated
|
||
>>> x = [(-0.1, 1.2), (2, 0)] | ||
>>> qe.cartesian_nearest_index(x, nodes) | ||
array([1, 4] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
array([1, 4])
Thanks @oyamad !! This is clever. In the following example I map [ins] In [14]: nodes = (np.linspace(0, 1, 5), np.linspace(0, 2, 3), np.linspace(0, 1, 4
...: ))
[ins] In [15]: nodes
Out[15]:
(array([0. , 0.25, 0.5 , 0.75, 1. ]),
array([0., 1., 2.]),
array([0. , 0.33333333, 0.66666667, 1. ]))
[ins] In [16]: prod = qe.cartesian(nodes)
[ins] In [17]: qe.cartesian_nearest_index((0.1, 0.1, 0.1), prod)
Out[17]: 22
[ins] In [18]: prod[22]
Out[18]: array([0.25 , 2. , 0.66666667]) |
@jstac Thanks for your review! You are supposed to pass
In your call |
b6c4aa3
to
7be79c0
Compare
Many thanks @oyamad , this looks good to me, and is greatly appreciated. |
Maybe used in #640. (Implement
_find_closest_state_index
part in #640 (comment).)Given a point
x
, find the index of the point nearest tox
among those in the Cartesian product generatednodes
.If
nodes
hasn
arrays of grid points (so the product isn
dimensional) and there arem
grid points in each dimension, this function does binary search for each dimension, so that the complexity isO(n log(m))
, while if one actually constructs the Cartesian product and does linear search, then the complexity isO(m^n)
.