Example usage for java.lang Integer highestOneBit

List of usage examples for java.lang Integer highestOneBit

Introduction

In this page you can find the example usage for java.lang Integer highestOneBit.

Prototype

public static int highestOneBit(int i) 

Source Link

Document

Returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value.

Usage

From source file:org.briljantframework.math.transform.DiscreteFourierTransform.java

private static void transformBluestein(ComplexArray a) {
    // Find a power-of-2 convolution length m such that m >= n * 2 + 1
    int n = a.size();
    if (n >= 0x20000000) { // n >= 536870912
        throw new IllegalArgumentException("");
    }/*from ww  w.  j a  v a 2 s .  c o  m*/
    int m = Integer.highestOneBit(n * 2 + 1) << 1;

    // Trigonometric tables
    DoubleArray cosTable = DoubleArray.zeros(n);
    DoubleArray sinTable = DoubleArray.zeros(n);
    for (int i = 0; i < n; i++) {
        int j = (int) ((long) i * i % (n * 2));
        cosTable.set(i, Math.cos(Math.PI * j / n));
        sinTable.set(i, Math.sin(Math.PI * j / n));
    }

    ComplexArray an = ComplexArray.zeros(m);
    ComplexArray bn = ComplexArray.zeros(m);

    bn.set(0, new Complex(cosTable.get(0), sinTable.get(0)));
    for (int i = 0; i < n; i++) {
        double cos = cosTable.get(i);
        double sin = sinTable.get(i);
        Complex complex = a.get(i);
        double real = complex.getReal() * cos + complex.getImaginary() * sin;
        double imag = -complex.getReal() * sin + complex.getImaginary() * cos;
        an.set(i, new Complex(real, imag));

        int j = i + 1;
        if (j < n) {
            Complex bcVal = Complex.valueOf(cosTable.get(j), sinTable.get(j));
            bn.set(j, bcVal);
            bn.set(m - j, bcVal);
        }
    }

    // Convolution
    ComplexArray cc = convolve(an, bn);
    for (int i = 0; i < n; i++) {
        double cos = cosTable.get(i);
        double sin = sinTable.get(i);

        Complex cv = cc.get(i);
        double real = cv.getReal() * cos + cv.getImaginary() * sin;
        double imag = -cv.getReal() * sin + cv.getImaginary() * cos;
        a.set(i, new Complex(real, imag));
    }
}

From source file:mil.nga.giat.mage.sdk.utils.MediaUtility.java

private static int getPowerOfTwoForSampleRatio(double ratio) {
    int k = Integer.highestOneBit((int) Math.floor(ratio));

    if (k == 0)//  w  w w.  java 2s  .c  o m
        return 1;
    else
        return k;
}

From source file:com.xtructure.xutil.BinaryIndexedTree.java

/**
 * Creates a new {@link BinaryIndexedTree} with max index the smallest power
 * of 2 larger than the given size./*ww  w .j  a v  a  2 s.c om*/
 * 
 * @param size the size
 * @throws IllegalArgumentException
 *             if size is not greater than 0
 */
public BinaryIndexedTree(int size) {
    validateArg("size", size, isGreaterThan(0));
    this.maxIndex = Integer.bitCount(size) == 1 ? size : Integer.highestOneBit(size) << 1;
    this.tree = new long[maxIndex + 1];
    this.freq = new long[maxIndex + 1];
}

From source file:gedi.util.math.stat.distributions.PoissonBinomial.java

private void preprocess() {
    int n = pp.length;
    m = n + 1;/*  w  w w. ja v  a2s. co m*/
    int nextPowerOf2 = Integer.highestOneBit(m);
    if (nextPowerOf2 != m)
        nextPowerOf2 <<= 1;
    m = nextPowerOf2;
    n = m - 1;

    int ins = 0;
    int start = 0;
    for (int i = 1; i < pp.length; i++) {
        if (Math.abs(pp[i] - pp[start]) > 1E-10) {
            if (i - start > 1) {
                double p = pp[start];
                pp[ins++] = -(i - start);
                pp[ins++] = p;
            } else {
                pp[ins++] = pp[i - 1];
            }
            start = i;
        }
    }

    if (pp.length - start > 1) {
        double p = pp[start];
        pp[ins++] = -(pp.length - start);
        pp[ins++] = p;
    } else {
        pp[ins++] = pp[pp.length - 1];
    }

    double delta = 2 * Math.PI / m;
    z = new Complex[m];
    z[0] = new Complex(1, 0);

    for (int i = 1; i <= Math.ceil(n / 2.0); i++) {
        double tt = i * delta;

        //         for(int j=0;j<pp.length;j++)
        //         {
        //            double pj=j<opp.length?opp[j]:0;
        //            double ax=1-pj+pj*Math.cos(tt);
        //            double bx=pj*Math.sin(tt);
        //            double tmp1=Math.sqrt(ax*ax+bx*bx);
        //            double tmp2=Math.atan2(bx,ax); //atan2(x,y)
        //            c1o+=Math.log(tmp1);
        //            c2o+=tmp2;
        //         }

        double c1 = 0.00;
        double c2 = 0.00;
        for (int j = 0; j < ins; j++) {
            double pj = pp[j];
            double f = 1;
            if (pj < 0) {
                f = -pj;
                pj = pp[++j];
            }

            double ax = 1 - pj + pj * Math.cos(tt);
            double bx = pj * Math.sin(tt);
            double tmp1 = Math.sqrt(ax * ax + bx * bx);
            double tmp2 = Math.atan2(bx, ax); //atan2(x,y)
            c1 += Math.log(tmp1) * f;
            c2 += tmp2 * f;
        }
        z[i] = new Complex(Math.exp(c1) * Math.cos(c2), Math.exp(c1) * Math.sin(c2));
        z[z.length - i] = z[i].conjugate();
    }
    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    z = fft.transform(z, TransformType.FORWARD);
}

From source file:org.apache.hadoop.hdfs.util.LightWeightGSet.java

private static int actualArrayLength(int recommended) {
    if (recommended > MAX_ARRAY_LENGTH) {
        return MAX_ARRAY_LENGTH;
    } else if (recommended < MIN_ARRAY_LENGTH) {
        return MIN_ARRAY_LENGTH;
    } else {/*from  w  w  w. ja  v  a  2 s  . co m*/
        final int a = Integer.highestOneBit(recommended);
        return a == recommended ? a : a << 1;
    }
}

From source file:org.zaproxy.zap.extension.pscan.scanner.StatsPassiveScanner.java

@Override
public void scanHttpResponseReceive(HttpMessage msg, int id, Source source) {
    try {//from w  w  w. j  a va 2 s  . c  o  m
        String site = SessionStructure.getHostName(msg);
        Stats.incCounter(site, CODE_STATS_PREFIX + msg.getResponseHeader().getStatusCode());
        String contentType = msg.getResponseHeader().getHeader(HttpHeader.CONTENT_TYPE);
        if (contentType != null) {
            Stats.incCounter(site, CONTENT_TYPE_STATS_PREFIX + contentType);
        }
        // Multiply by 2 so we inc the 'next highest' stat
        Stats.incCounter(site,
                RESPONSE_TIME_STATS_PREFIX + (Integer.highestOneBit(msg.getTimeElapsedMillis()) * 2));
    } catch (URIException e) {
        // Ignore
    }
}

From source file:ro.hasna.ts.math.representation.DiscreteChebyshevTransform.java

@Override
public double[] transform(double[] values) {
    int length = values.length;
    if (length < 3) {
        int n = length - 1;
        double[] result = new double[length];
        for (int i = 0; i <= n; i++) {
            double sum = 0;
            for (int j = 0; j <= n; j++) {
                if (j == 0 || j == n) {
                    sum += values[j] * FastMath.cos(i * j * FastMath.PI / n) / 2;
                } else {
                    sum += values[j] * FastMath.cos(i * j * FastMath.PI / n);
                }//  w  w  w  . j a v  a2  s  .  c  om
            }
            if (i == 0 || i == n) {
                result[i] = sum / n;
            } else {
                result[i] = sum * 2 / n;
            }
        }
        return result;
    }

    int end = length * 2 - 2;

    // pad the input array with zeros so as to have a length == 2^k-1
    int powerOfTwo = Integer.highestOneBit(end);
    if (end != powerOfTwo) {
        powerOfTwo = powerOfTwo << 1;
    }
    double[] copy = new double[powerOfTwo];
    System.arraycopy(values, 0, copy, 0, length);
    // copy 1..n-1 in reverse order at the back of the array
    for (int i = 1; i < length - 1; i++) {
        copy[end - i] = values[i];
    }

    Complex[] complexes = fourierTransformer.transform(copy, TransformType.FORWARD);
    double[] result = new double[length];
    result[0] = complexes[0].getReal() / powerOfTwo;
    for (int i = 1; i < length && i < complexes.length; i++) {
        result[i] = 2 * complexes[i].getReal() / powerOfTwo;
    }
    result[length - 1] /= 2;
    return result;
}

From source file:ro.hasna.ts.math.representation.DiscreteFourierTransformTest.java

@Test
public void testTransformSineWave() throws Exception {
    double signalFrequency = 100;
    double amplitude = 30;
    double displacement = 17;
    double samplingFrequency = signalFrequency * 8; //it should be at least double (Shannon Theorem)
    int len = 1000;
    double[] v = new double[len];
    for (int i = 0; i < len; i++) {
        v[i] = amplitude * Math.sin(2 * Math.PI * signalFrequency * i / samplingFrequency) + displacement;
    }//from   ww w  .  j  a va2  s .  c o m

    double[] result = new DiscreteFourierTransform().transform(v);

    double max = 0;
    int pos = 0;
    for (int i = 1; i < result.length; i++) {
        if (max < result[i]) {
            max = result[i];
            pos = i;
        }
    }

    int powerOfTwo = Integer.highestOneBit(len);
    if (len != powerOfTwo) {
        powerOfTwo = powerOfTwo << 1;
    }

    Assert.assertEquals(amplitude, max, TimeSeriesPrecision.EPSILON);
    Assert.assertEquals(signalFrequency, pos * samplingFrequency / powerOfTwo, TimeSeriesPrecision.EPSILON);
    Assert.assertEquals(displacement, result[0], TimeSeriesPrecision.EPSILON);
}

From source file:WorkerRunnable.java

public WorkerRunnable(int numTopics, double[][] alpha, double alphaSum, double[][] beta, double betaSum,
        int newTypes, int oldDocs, Randoms random, ArrayList<TopicAssignment> data, int[][] typeTopicCounts,
        int[] tokensPerTopic, int startDoc, int numDocs) {

    this.data = data;

    this.numTopics = numTopics;
    this.numTypes = typeTopicCounts.length;

    if (Integer.bitCount(numTopics) == 1) {
        // exact power of 2
        topicMask = numTopics - 1;//  ww  w.ja va2  s.c o m
        topicBits = Integer.bitCount(topicMask);
    } else {
        // otherwise add an extra bit
        topicMask = Integer.highestOneBit(numTopics) * 2 - 1;
        topicBits = Integer.bitCount(topicMask);
    }

    this.typeTopicCounts = typeTopicCounts;
    this.tokensPerTopic = tokensPerTopic;

    this.alphaSum = alphaSum;
    this.alpha = alpha;
    this.betaSum = betaSum;
    this.beta = beta;
    //DEBUG
    //System.out.println("Is beta 0? " + beta[0][0]);
    //this.betaSum = 0;
    this.betaAvg = new double[numTopics];
    int topic = 0;
    for (double[] i : beta) {
        for (double j : i) {
            this.betaAvg[topic] += j;
            //      this.betaSum += j; 
        }
        this.betaAvg[topic] /= i.length;
        topic++;
    }

    this.alphaAvg = new double[numTopics];
    for (topic = 0; topic < numTopics; topic++) {
        for (int doc = startDoc; doc < data.size() && doc < startDoc + numDocs; doc++) {
            alphaAvg[topic] += alpha[doc][topic];
        }
        this.betaAvg[topic] /= numDocs;
    }

    this.random = random;

    this.startDoc = startDoc;
    this.numDocs = numDocs;

    cachedCoefficients = new double[numTopics];
    this.topicDocCounts = new int[numTopics][numDocs];
    this.docStorageArray = new int[numTopics][numDocs];

    //System.err.println("WorkerRunnable Thread: " + numTopics + " topics, " + topicBits + " topic bits, " + 
    //               Integer.toBinaryString(topicMask) + " topic mask");

}

From source file:lirmm.inria.fr.math.OpenLongToDoubleHashMap.java

/**
 * Compute the capacity needed for a given size.
 *
 * @param expectedSize expected size of the map
 * @return capacity to use for the specified size
 *//*from  w  w  w  .j a v a2 s . c  o  m*/
private static int computeCapacity(final int expectedSize) {
    if (expectedSize == 0) {
        return 1;
    }
    final int capacity = (int) FastMath.ceil(expectedSize / LOAD_FACTOR);
    final int powerOfTwo = Integer.highestOneBit(capacity);
    if (powerOfTwo == capacity) {
        return capacity;
    }
    return nextPowerOfTwo(capacity);
}