Example usage for org.apache.mahout.math.function DoubleFunction apply

List of usage examples for org.apache.mahout.math.function DoubleFunction apply

Introduction

In this page you can find the example usage for org.apache.mahout.math.function DoubleFunction apply.

Prototype

public abstract double apply(double x);

Source Link

Document

Apply the function to the argument and return the result

Usage

From source file:com.mapr.synth.ForeignKeySamplerTest.java

License:Apache License

private void check(int n, DoubleFunction distribution, Sampler<JsonNode> s) {
    int[] counts = new int[n];
    for (int i = 0; i < 100000; i++) {
        counts[s.sample().asInt()]++;/*  ww w  . ja  va 2s.co m*/
    }

    double sum = 0;
    double[] p = new double[n];
    for (int i = 0; i < n; i++) {
        p[i] = distribution.apply(i);
        sum += p[i];
    }

    double z1 = 0;
    double z2 = 0;
    for (int i = 0; i < n; i++) {
        z1 += (p[i] - counts[i] / 100000.0) * Math.log(p[i] / sum);
        double expected = p[i] * 100000;
        double deviation = expected - counts[i];
        z2 += deviation * deviation / expected;
    }
    System.out.printf("%.4f %.4f\n", z1, z2);
}

From source file:org.carrot2.matrix.MatrixUtils.java

License:Open Source License

/**
 * Common implementation of finding extreme elements in columns.
 *//*w ww.j a  va  2  s . co m*/
private static int[] inColumns(DoubleMatrix2D A, int[] indices, double[] extValues,
        DoubleComparator doubleComparator, DoubleFunction transform) {
    if (indices == null) {
        indices = new int[A.columns()];
    }

    if (A.columns() == 0 || A.rows() == 0) {
        return indices;
    }

    if (extValues == null) {
        extValues = new double[A.columns()];
    }

    for (int c = 0; c < A.columns(); c++) {
        extValues[c] = transform.apply(A.getQuick(0, c));
    }
    Arrays.fill(indices, 0);

    for (int r = 1; r < A.rows(); r++) {
        for (int c = 0; c < A.columns(); c++) {
            final double transformed = transform.apply(A.getQuick(r, c));
            if (doubleComparator.compare(transformed, extValues[c]) > 0) {
                extValues[c] = transformed;
                indices[c] = r;
            }
        }
    }

    return indices;
}