Java mean meanSlow(final double[] values)

Here you can find the source of meanSlow(final double[] values)

Description

mean Slow

License

Open Source License

Declaration

public final static double meanSlow(final double[] values) 

Method Source Code

//package com.java2s;
/*/* w w  w.  j  a  va 2s. c  o m*/
 * ====================================================
 * Copyright (C) 2013 by Idylwood Technologies, LLC. All rights reserved.
 *
 * Developed at Idylwood Technologies, LLC.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The License should have been distributed to you with the source tree.
 * If not, it can be found at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Charles Cooper
 * Date: 2013
 * ====================================================
 */

import java.util.Arrays;

public class Main {
    public final static double meanSlow(final double[] values) {
        return sumSlow(values) / values.length;
    }

    /** Numerically precise implementation of sum
     * Optimized version of an implementation of Schewchuk's algorithm
     * which keeps full precision by keeping O(n) space
     * for the error, unlike Kahan's algorithm which keeps O(1) space.
     * The tradeoff is that this is fully precise, but Kahan's algorithm
     * is almost always precise anyways. It is about 12x slower
     * than the naive implementation, but in turn about 10x faster than calculating
     * the sum to full precision and then truncating.
     * @param values
     * @return Sum of the values
     */
    public final static double sumSlow(double... values) {
        final double[] partials = new double[values.length];
        int size = 0;
        for (double x : values) {
            int i = 0;
            for (int j = 0; j < size; ++j) // size not necessarily == partials.length
            {
                double y = partials[j];

                if (abs(x) < abs(y)) {
                    final double tmp = x;
                    x = y;
                    y = tmp;
                }
                double hi = x + y;
                double lo = y - (hi - x);
                if (lo != 0.0)
                    partials[i++] = lo;
                x = hi;
            }
            if (i < size) {
                partials[i] = x;
                Arrays.fill(partials, i + 1, size, 0);
            } else {
                partials[size++] = x;
            }
        }
        double sum = 0;
        for (double d : partials)
            sum += d;
        return sum;
    }

    /**
     * Returns the absolute value of d (without branching).
     * Faster than OpenJDK implementation.
     * @param d
     * @return
     */
    public static final double abs(final double d) {
        return Double.longBitsToDouble(Long.MAX_VALUE & Double.doubleToRawLongBits(d));
    }

    /**
     * Returns the absolute value of f (without branching).
     * Faster than OpenJDK implementation.
     * @param f
     * @return
     */
    public static final float abs(final float f) {
        return Float.intBitsToFloat(Integer.MAX_VALUE & Float.floatToRawIntBits(f));
    }

    /**
     * Returns the absolute value of l (without branching).
     * Faster than OpenJDK implementation.
     * @param l
     * @return
     */
    public static final long abs(final long l) {
        final long sign = l >>> 63;
        return (l ^ (~sign + 1)) + sign;
    }

    /**
     * Returns the absolute value of i (without branching).
     * Faster than OpenJDK implementation.
     * @param d
     * @return
     */
    public static final int abs(final int i) {
        final int sign = i >>> 31;
        return (i ^ (~sign + 1)) + sign;
    }
}

Related

  1. meanFilter(float[] weights, int context)
  2. meanGreenwichSideralTime(double t)
  3. meanImage(float[][]... images)
  4. meanLow(final int a, final int b)
  5. means(double[][] input)
  6. meanSml(final int a, final int b)
  7. meanSquare(float[] a, int off, int length)
  8. meanSquaredError(double[] x, double[] y)
  9. meanSquaredError(double[][] vectorBatch1, double[][] vectorBatch2)