You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Ajo Fod <aj...@gmail.com> on 2013/09/20 03:14:26 UTC

[Math] Could StatUtils use a method to find the index of the maximum value.

It currently has the maximum of a double array.

-Ajo.

Re: [Math] Could StatUtils use a method to find the index of the maximum value.

Posted by Ajo Fod <aj...@gmail.com>.
Here is a bit of code that ranks elements of a double array:

public class Rank {

    public static enum Dir {

        Ascending(1),
        Descending(-1);
        int side;

        Dir(int i) {
            side = i;
        }
    }

    /**
     * Rank elements of an array.
     *
     * @param dir : direction to rank elements in.
     * @param data : elements of the array.
     * @return
     */
    public static Integer[] rank(final Dir dir, final double[] data) {
        final Integer[] idx = new Integer[data.length];
        for (int i = 0; i < data.length; i++) {
            idx[i] = i;
        }

        Arrays.sort(idx, new Comparator<Integer>() {
            @Override
            public int compare(final Integer o1, final Integer o2) {
                return Double.compare(dir.side * data[o1], dir.side *
data[o2]);
            }
        });
        return idx;
    }

    public static void main(String[] args) {
        final double[] data = {1.7f, -0.3f, 2.1f, 0.5f};
        final Integer[] idx = rank(Dir.Descending, data);
        System.out.println(Arrays.toString(idx));
    }
}



On Thu, Sep 19, 2013 at 6:14 PM, Ajo Fod <aj...@gmail.com> wrote:

> It currently has the maximum of a double array.
>
> -Ajo.
>

Re: [Math] Could StatUtils use a method to find the index of the maximum value.

Posted by Ajo Fod <aj...@gmail.com>.
Yes, MathArrays is probably better than StatUtils.

I've historically needed it for things like processing the biggest/smallest
few elements of an array till some cost metric is satisfied or finding the
N'th element.

Technically making a TreeList from the Double elements is a close
substitute, and could be used to do something similar, but this is a lot
less code and duplicates are not swallowed.

-Ajo.


On Sat, Sep 21, 2013 at 10:55 AM, Phil Steitz <ph...@gmail.com> wrote:

> On 9/19/13 6:14 PM, Ajo Fod wrote:
> > It currently has the maximum of a double array.
> >
> > -Ajo.
> >
> If we were to add something like this it would likely fit better in
> util.MathArrays.  Where is it needed?
>
> Phil
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [Math] Could StatUtils use a method to find the index of the maximum value.

Posted by Phil Steitz <ph...@gmail.com>.
On 9/19/13 6:14 PM, Ajo Fod wrote:
> It currently has the maximum of a double array.
>
> -Ajo.
>
If we were to add something like this it would likely fit better in
util.MathArrays.  Where is it needed?

Phil

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org