Hello,
I am trying to solve a problem in matplotlib where I would have an array of floating point numbers and I want to quickly determine what is the closest common offset to a power of 10. In other words, if given:
[12373.43, 12375.89, 12370.18],
I would want returned something like either 12370.0, or the lowest common order of magnitude (in this case, 10).
Is there some sort of neat math/numpy trick to figure this out? I already have a brute-force method with a while loop, but I am looking for something a little bit more elegant.
Thanks, Ben Root
On Wed, Sep 15, 2010 at 2:34 PM, Benjamin Root ben.root@ou.edu wrote:
Hello,
I am trying to solve a problem in matplotlib where I would have an array of floating point numbers and I want to quickly determine what is the closest common offset to a power of 10. In other words, if given:
[12373.43, 12375.89, 12370.18],
I would want returned something like either 12370.0, or the lowest common order of magnitude (in this case, 10).
Is there some sort of neat math/numpy trick to figure this out? I already have a brute-force method with a while loop, but I am looking for something a little bit more elegant.
Thanks, Ben Root
NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Probably not being very fail-safe here is my first guess:
a = np.array([12373.43, 12375.89, 12370.18]) np.floor(a).min()
On Wed, Sep 15, 2010 at 1:34 PM, Benjamin Root ben.root@ou.edu wrote:
Hello,
I am trying to solve a problem in matplotlib where I would have an array of floating point numbers and I want to quickly determine what is the closest common offset to a power of 10. In other words, if given:
[12373.43, 12375.89, 12370.18],
I would want returned something like either 12370.0, or the lowest common order of magnitude (in this case, 10).
Is there some sort of neat math/numpy trick to figure this out? I already have a brute-force method with a while loop, but I am looking for something a little bit more elegant.
Something along the lines of
In [14]: ceil(log10(a.max() - a.min())) Out[14]: 1.0
? I think this approach can be fixed up for whatever it is. I wasn't clear on that.
Chuck
On Wed, Sep 15, 2010 at 2:54 PM, Charles R Harris <charlesr.harris@gmail.com
wrote:
On Wed, Sep 15, 2010 at 1:34 PM, Benjamin Root ben.root@ou.edu wrote:
Hello,
I am trying to solve a problem in matplotlib where I would have an array of floating point numbers and I want to quickly determine what is the closest common offset to a power of 10. In other words, if given:
[12373.43, 12375.89, 12370.18],
I would want returned something like either 12370.0, or the lowest common order of magnitude (in this case, 10).
Is there some sort of neat math/numpy trick to figure this out? I already have a brute-force method with a while loop, but I am looking for something a little bit more elegant.
Something along the lines of
In [14]: ceil(log10(a.max() - a.min())) Out[14]: 1.0
? I think this approach can be fixed up for whatever it is. I wasn't clear on that.
Chuck
Chuck,
That does seem to work for most cases (and it even did better than my brute force approach when it comes to negative numbers). But there are still some edge cases. In particular a case like [0.99, 1.01] will be interpreted as having a common significant digit. I suspect another edge case would be if there are no common sig digs, but I could check for that by seeing if the order of magnitude of the range is less than the order of magnitude of the maximum value and if the order of magnitude of the max equals the order of magnitude of the min.
I will look into it further, but if anyone is interested, I have attached a test script.
Thanks, Ben Root