使用nextAfter(双启动,双向);在Android中

时间:2013-05-11 16:27:46

标签: android math

我需要帮助。我将计算一个测量变量,然后取这些变量的前100个值并对它们求平均值。请记住,我过去6周一直在教自己,对一些人来说显而易见的事情对我来说不一定是显而易见的。

实质上,说'double x'是变量,我有很多接近的值。我需要的是一种计算这些值的前100个的总和(然后是平均值)的方法。

在我的研究中,我能看到最接近我需要的东西是'nextAfter(双开始,双向);在此之前,使用'max'确定最大值,这是正确的起点:

double xm = max(x); static double(xm,x< xm);

我的问题是如何获得前100个值的总和(最大值和99个下一个值) - 平均值很容易 - 只需除以100。

1 个答案:

答案 0 :(得分:2)

要计算从源读取的最大n值的平均值,您需要至少存储这些值。由于在结束之前的任何给定点,您都不知道某些最大的n值是否会在以后出现,因此您需要跟踪到目前为止看到的最大n值。

一种简单的方法是将最大值存储在堆或优先级队列中,因为这样可以轻松添加新值并查找(和删除)最小的存储值。默认PriorityQueue非常适合此任务,因为它使用元素的自然顺序,因此poll删除最小的存储元素。如果想要计算n个最小元素的平均值,则需要使用带有自定义PriorityQueue的{​​{1}}(或者在这种特殊情况下,简单地否定所有值并使用自然排序也会起作用。)

实现所需的懒惰方式(更少的代码)是简单地将每个传入值添加到队列中,如果队列的大小超过Comparator [那么它必须是n]删除最小的队列中的元素:

n+1

稍微复杂一点的方法是首先检查是否需要添加新值,并在此情况下仅修改队列,

// vp is the value provider
while(vp.hasNext()) {
    // read the next value and add it to the queue
    pq.add(vp.nextValue());
    if (pq.size() > topSize) {
        pq.poll();
    }

这种方式可能更有效,因为向队列添加值并删除最小值都是double newValue = vp.nextValue(); // Check if we have to put the new value in the queue // that is the case when the queue is not yet full, or the smallest // stored value is smaller than the new if (pq.size() < topSize || pq.peek() < newValue) { // remove the smallest value from the queue only if it is full if (pq.size() == topSize()) { pq.poll(); } pq.add(newValue); } 操作,而与最小存储值进行比较的是O(log size)。因此,如果有许多值小于之前看到的O(1)最大值,则第二种方法可以节省一些工作。

如果性能至关重要,请注意n无法存储基本类型,如PriorityQueue,因此存储(和检索平均计算)涉及装箱(包装double值在double对象中)取消装箱(从Double对象中拉出double值),从而将队列的基础数组间接到实际值。通过使用原始Double自己实现基于堆的优先级队列,可以避免这些成本。 (但这很少是必要的,通常,拳击和间接费用仅占整个处理的一小部分。)

一个头脑简单的完整工作示例:

double[]

使用界面

import java.util.PriorityQueue;

/**
 * Example class to collect the largest values from a stream and compute their
 * average.
 */
public class Average {
    // number of values we want to save
    private int topSize;
    // number of values read so far
    private long count = 0;
    // priority queue to save the largest topSize values
    private PriorityQueue<Double> pq;
    // source of read values, could be a file reader, a device reader, or whatever
    private ValueProvider vp;

    /**
     * Construct an <code>Average</code> to sample the largest <code>n</code>
     * values from the source.
     *
     * @param tops Number of values to save for averaging.
     * @param v Source of the values to sample.
     *
     * @throws IllegalArgumentException when the specified number of values is less than one.
     */
    public Average(int tops, ValueProvider v) throws IllegalArgumentException {
        if (tops < 1) {
            throw new IllegalArgumentException("Can't get average of fewer than one values.");
        }
        topSize = tops;
        vp = v;
        // Initialise queue to needed capacity; topSize + 1, since we first add
        // and then poll. Thus no resizing should ever be necessary.
        pq = new PriorityQueue<Double>(topSize+1);
    }

    /**
     * Compute the average of the values stored in the <code>PriorityQueue<Double></code>
     *
     * @param prio The queue to average.
     * @return the average of the values stored in the queue.
     */
    public static double average(PriorityQueue<Double> prio) throws IllegalArgumentException {
        if (prio == null || prio.size() == 0) {
            throw new IllegalArgumentException("Priority queue argument is null or empty.");
        }
        double sum = 0;
        for(Double d : prio) {
            sum += d;
        }
        return sum/prio.size();
    }

    /**
     * Reads values from the provider until exhausted, reporting the average
     * of the largest <code>topSize</code> values read so far from time to time
     * and when the source is exhausted.
     */
    public void collectAverage() {
        while(vp.hasNext()) {
            // read the next value and add it to the queue
            pq.add(vp.nextValue());
            ++count;
            // If the queue was already full, we now have
            // topSize + 1 values in it, so we remove the smallest.
            // That is, conveniently, what the default PriorityQueue<Double>
            // gives us. If we wanted for example the smallest, we'd need
            // to use a PriorityQueue with a custom Comparator (or negate
            // the values).
            if (pq.size() > topSize) {
                pq.poll();
            }
            // Occasionally report the running average of the largest topSize
            // values read so far. This may not be desired.
            if (count % (topSize*25) == 0 || count < 11) {
                System.out.printf("Average of top %d values after collecting %d is %f\n",
                                   pq.size(), count, average(pq));
            }
        }
        // Report final average. Returning the average would be a natural choice too.
        System.out.printf("Average of top %d values of %d total is %f\n",
                           pq.size(), count, average(pq));
    }

    public static void main(String[] args) {
        Average a = new Average(100, new SimpleProvider(123456));
        a.collectAverage();
    }
}

并实施类

/**
 * Interface for a source of <code>double</code>s.
 */
public interface ValueProvider {
    /**
     * Gets the next value from the source.
     *
     * @return The next value if there is one.
     * @throws RuntimeException if the source is exhausted.
     */
    public double nextValue() throws RuntimeException;

    /**
     * Checks whether the source has more values to deliver.
     *
     * @return whether there is at least one more value to be obtained from the source.
     */
    public boolean hasNext();
}