Example usage for java.math BigInteger shiftLeft

List of usage examples for java.math BigInteger shiftLeft

Introduction

In this page you can find the example usage for java.math BigInteger shiftLeft.

Prototype

public BigInteger shiftLeft(int n) 

Source Link

Document

Returns a BigInteger whose value is (this << n) .

Usage

From source file:com.amazonaws.kinesis.agg.AggRecord.java

/**
 * Calculate a new explicit hash key based on the input partition key (following
 * the algorithm from the original KPL).
 * //from  w ww .  ja  v a  2  s . com
 * @param partitionKey
 *            The partition key to seed the new explicit hash key with
 * @return An explicit hash key based on the input partition key generated using
 *         an algorithm from the original KPL.
 */
private String createExplicitHashKey(final String partitionKey) {
    BigInteger hashKey = BigInteger.ZERO;

    this.md5.reset();
    byte[] pkDigest = this.md5.digest(partitionKey.getBytes(StandardCharsets.UTF_8));

    for (int i = 0; i < this.md5.getDigestLength(); i++) {
        BigInteger p = new BigInteger(String.valueOf((int) pkDigest[i] & 0xFF)); // convert
        // to
        // unsigned
        // integer
        BigInteger shifted = p.shiftLeft((16 - i - 1) * 8);
        hashKey = hashKey.add(shifted);
    }

    return hashKey.toString(10);
}

From source file:org.osgp.adapter.protocol.dlms.domain.commands.DlmsHelperService.java

private BigInteger byteArrayToBigInteger(final byte[] bitStringValue) {
    if (bitStringValue == null || bitStringValue.length == 0) {
        return null;
    }//from   w w  w.jav a2 s  .  c o m
    BigInteger value = BigInteger.valueOf(0);
    for (final byte element : bitStringValue) {
        value = value.shiftLeft(8);
        value = value.add(BigInteger.valueOf(element & 0xFF));
    }
    return value;
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * Euler-Mascheroni constant./* w ww.j  ava 2 s  .  c o m*/
 *
 * @param mc The required precision of the result.
 * @return 0.577...
 */
static public BigDecimal gamma(MathContext mc) {
    /* look it up if possible */
    if (mc.getPrecision() < GAMMA.precision()) {
        return GAMMA.round(mc);
    } else {
        double eps = prec2err(0.577, mc.getPrecision());
        /* Euler-Stieltjes as shown in Dilcher, Aequat Math 48 (1) (1994) 55-85
        14
         */
        MathContext mcloc = new MathContext(2 + mc.getPrecision());
        BigDecimal resul = BigDecimal.ONE;
        resul = resul.add(log(2, mcloc));
        resul = resul.subtract(log(3, mcloc));
        /* how many terms: zeta-1 falls as 1/2^(2n+1), so the
         * terms drop faster than 1/2^(4n+2). Set 1/2^(4kmax+2) < eps.
         * Leading term zeta(3)/(4^1*3) is 0.017. Leading zeta(3) is 1.2. Log(2) is 0.7
         */
        int kmax = (int) ((Math.log(eps / 0.7) - 2.) / 4.);
        mcloc = new MathContext(1 + err2prec(1.2, eps / kmax));
        for (int n = 1;; n++) {
            /* zeta is close to 1. Division of zeta-1 through
             * 4^n*(2n+1) means divion through roughly 2^(2n+1)
             */
            BigDecimal c = zeta(2 * n + 1, mcloc).subtract(BigDecimal.ONE);
            BigInteger fourn = new BigInteger("" + (2 * n + 1));
            fourn = fourn.shiftLeft(2 * n);
            c = divideRound(c, fourn);
            resul = resul.subtract(c);
            if (c.doubleValue() < 0.1 * eps) {
                break;
            }
        }
        return resul.round(mc);
    }
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The trigonometric tangent.//from w ww  .j av  a 2 s. c  o  m
 *
 * @param x the argument in radians.
 * @return the tan(x)
 */
static public BigDecimal tan(final BigDecimal x) {
    if (x.compareTo(BigDecimal.ZERO) == 0) {
        return BigDecimal.ZERO;
    } else if (x.compareTo(BigDecimal.ZERO) < 0) {
        return tan(x.negate()).negate();
    } else {
        /* reduce modulo pi
         */
        BigDecimal res = modpi(x);
        /* absolute error in the result is err(x)/cos^2(x) to lowest order
         */
        final double xDbl = res.doubleValue();
        final double xUlpDbl = x.ulp().doubleValue() / 2.;
        final double eps = xUlpDbl / 2. / Math.pow(Math.cos(xDbl), 2.);
        if (xDbl > 0.8) {
            /* tan(x) = 1/cot(x) */
            BigDecimal co = cot(x);
            MathContext mc = new MathContext(err2prec(1. / co.doubleValue(), eps));
            return BigDecimal.ONE.divide(co, mc);
        } else {
            final BigDecimal xhighpr = scalePrec(res, 2);
            final BigDecimal xhighprSq = multiplyRound(xhighpr, xhighpr);
            BigDecimal resul = xhighpr.plus();
            /* x^(2i+1) */
            BigDecimal xpowi = xhighpr;
            Bernoulli b = new Bernoulli();
            /* 2^(2i) */
            BigInteger fourn = new BigInteger("4");
            /* (2i)! */
            BigInteger fac = new BigInteger("2");
            for (int i = 2;; i++) {
                Rational f = b.at(2 * i).abs();
                fourn = fourn.shiftLeft(2);
                fac = fac.multiply(new BigInteger("" + (2 * i))).multiply(new BigInteger("" + (2 * i - 1)));
                f = f.multiply(fourn).multiply(fourn.subtract(BigInteger.ONE)).divide(fac);
                xpowi = multiplyRound(xpowi, xhighprSq);
                BigDecimal c = multiplyRound(xpowi, f);
                resul = resul.add(c);
                if (Math.abs(c.doubleValue()) < 0.1 * eps) {
                    break;
                }
            }
            MathContext mc = new MathContext(err2prec(resul.doubleValue(), eps));
            return resul.round(mc);
        }
    }
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The trigonometric co-tangent./*from  w  w  w. ja  v  a  2s.  co  m*/
 *
 * @param x the argument in radians.
 * @return the cot(x)
 */
static public BigDecimal cot(final BigDecimal x) {
    if (x.compareTo(BigDecimal.ZERO) == 0) {
        throw new ArithmeticException("Cannot take cot of zero " + x.toString());
    } else if (x.compareTo(BigDecimal.ZERO) < 0) {
        return cot(x.negate()).negate();
    } else {
        /* reduce modulo pi
         */
        BigDecimal res = modpi(x);
        /* absolute error in the result is err(x)/sin^2(x) to lowest order
         */
        final double xDbl = res.doubleValue();
        final double xUlpDbl = x.ulp().doubleValue() / 2.;
        final double eps = xUlpDbl / 2. / Math.pow(Math.sin(xDbl), 2.);
        final BigDecimal xhighpr = scalePrec(res, 2);
        final BigDecimal xhighprSq = multiplyRound(xhighpr, xhighpr);
        MathContext mc = new MathContext(err2prec(xhighpr.doubleValue(), eps));
        BigDecimal resul = BigDecimal.ONE.divide(xhighpr, mc);
        /* x^(2i-1) */
        BigDecimal xpowi = xhighpr;
        Bernoulli b = new Bernoulli();
        /* 2^(2i) */
        BigInteger fourn = new BigInteger("4");
        /* (2i)! */
        BigInteger fac = BigInteger.ONE;
        for (int i = 1;; i++) {
            Rational f = b.at(2 * i);
            fac = fac.multiply(new BigInteger("" + (2 * i))).multiply(new BigInteger("" + (2 * i - 1)));
            f = f.multiply(fourn).divide(fac);
            BigDecimal c = multiplyRound(xpowi, f);
            if (i % 2 == 0) {
                resul = resul.add(c);
            } else {
                resul = resul.subtract(c);
            }
            if (Math.abs(c.doubleValue()) < 0.1 * eps) {
                break;
            }
            fourn = fourn.shiftLeft(2);
            xpowi = multiplyRound(xpowi, xhighprSq);
        }
        mc = new MathContext(err2prec(resul.doubleValue(), eps));
        return resul.round(mc);
    }
}