List of usage examples for java.math BigInteger multiply
BigInteger multiply(long v)
From source file:libra.preprocess.common.kmerhistogram.KmerRangePartitioner.java
public KmerRangePartition[] getHistogramPartitions(KmerHistogramRecord[] records, long samples) { KmerRangePartition[] partitions = new KmerRangePartition[this.numPartitions]; // calc 4^kmerSize String As = ""; String Ts = ""; for (int i = 0; i < this.kmerSize; i++) { As += "A"; Ts += "T"; }//from w w w . j a v a 2 s.c o m long partitionWidth = samples / this.numPartitions; long partitionWidthRemain = partitionWidth; int partitionIdx = 0; int recordIdx = 0; long curRecordRemain = records[0].getFrequency(); BigInteger nextPartitionBegin = BigInteger.ZERO; while (partitionIdx < this.numPartitions) { long diff = partitionWidthRemain - curRecordRemain; if (diff > 0) { partitionWidthRemain -= curRecordRemain; recordIdx++; if (recordIdx < records.length) { curRecordRemain = records[recordIdx].getFrequency(); } else { break; } } else if (diff == 0) { BigInteger partitionBegin = nextPartitionBegin; BigInteger partitionEnd = null; if (partitionIdx == this.numPartitions - 1) { partitionEnd = SequenceHelper.convertToBigInteger(Ts); } else { partitionEnd = SequenceHelper .convertToBigInteger((records[recordIdx].getKmer() + Ts).substring(0, this.kmerSize)); } BigInteger partitionSize = partitionEnd.subtract(partitionBegin); partitions[partitionIdx] = new KmerRangePartition(this.kmerSize, this.numPartitions, partitionIdx, partitionSize, partitionBegin, partitionEnd); nextPartitionBegin = partitionEnd.add(BigInteger.ONE); partitionIdx++; recordIdx++; if (recordIdx < records.length) { curRecordRemain = records[recordIdx].getFrequency(); } else { break; } partitionWidthRemain = partitionWidth; } else { // in between BigInteger partitionBegin = nextPartitionBegin; BigInteger partitionEnd = null; if (partitionIdx == this.numPartitions - 1) { partitionEnd = SequenceHelper.convertToBigInteger(Ts); } else { BigInteger recordBegin = SequenceHelper .convertToBigInteger((records[recordIdx].getKmer() + As).substring(0, this.kmerSize)); BigInteger recordEnd = SequenceHelper .convertToBigInteger((records[recordIdx].getKmer() + Ts).substring(0, this.kmerSize)); BigInteger recordWidth = recordEnd.subtract(recordBegin); BigInteger curWidth = recordWidth.multiply(BigInteger.valueOf(partitionWidthRemain)) .divide(BigInteger.valueOf(records[recordIdx].getFrequency())); BigInteger bigger = null; if (recordBegin.compareTo(partitionBegin) > 0) { bigger = recordBegin; } else { bigger = partitionBegin; } partitionEnd = bigger.add(curWidth); } BigInteger partitionSize = partitionEnd.subtract(partitionBegin); partitions[partitionIdx] = new KmerRangePartition(this.kmerSize, this.numPartitions, partitionIdx, partitionSize, partitionBegin, partitionEnd); nextPartitionBegin = partitionEnd.add(BigInteger.ONE); partitionIdx++; curRecordRemain -= partitionWidthRemain; partitionWidthRemain = partitionWidth; } } return partitions; }
From source file:gedi.util.math.stat.distributions.OccupancyNumberDistribution.java
private void computeRational() { int maxA = b == 0 ? k : Math.min(k, n / b); aToProb = new double[maxA + 1]; BigFraction[] aToProb = new BigFraction[maxA + 1]; BigInteger bfac = factorial(b); long start = System.currentTimeMillis(); double maxDiff = 0; aToProb[maxA] = BigFraction.ONE;// w w w . j a v a2s.c om for (int a = maxA - 1; a >= 0; a--) { int m = Math.min(k - a + 1, aToProb.length - a); aToProb[a] = BigFraction.ZERO; for (int i = 1; i < m; i++) { BigInteger rat = binomialCoefficientLargeInteger(k - a, i).multiply(factorial(n - a * b, i * b)); if (n - a * b - i * b > 0) rat = rat.multiply(BigInteger.valueOf(k - a - i).pow(n - a * b - i * b)); if (m - i > 0) rat = rat.multiply(bfac.pow(m - i)); aToProb[a] = aToProb[a].add(new BigFraction(rat, BigInteger.ONE).multiply(aToProb[a + i])); } BigInteger rat = bfac.pow(m).multiply(BigInteger.valueOf(k - a).pow(n - a * b)); aToProb[a] = BigFraction.ONE.subtract(aToProb[a].multiply(new BigFraction(BigInteger.ONE, rat))); this.aToProb[a] = new BigFraction(binomialCoefficientLargeInteger(k, a), BigInteger.ONE) .multiply(aToProb[a].multiply(rationalv(a, b, k, n))).doubleValue(); maxDiff = max(maxDiff, abs(this.aToProb[a] - approximateProbability(a))); if (System.currentTimeMillis() - start > 500) { aToProxProb = this.aToProb = computeApproximateNormal(); return; } } // System.out.printf(Locale.US,"%d\t%d\t%d\t%d\t%.4g\t%.4f\n",b,k,n,maxDigit,maxDiff,(System.currentTimeMillis()-start)/1000.0); }
From source file:burstcoin.observer.service.AssetService.java
private String convertPrice(String priceString, int decimals) { BigInteger price = new BigInteger(priceString); BigInteger amount = price.multiply(new BigInteger("" + (long) Math.pow(10, decimals))); String negative = ""; String afterComma = ""; String fractionalPart = amount.mod(new BigInteger("100000000")).toString(); amount = amount.divide(new BigInteger("100000000")); if (amount.compareTo(BigInteger.ZERO) < 0) { amount = amount.abs();/* w ww . j a v a2s . c o m*/ negative = "-"; } if (!fractionalPart.equals("0")) { afterComma = "."; for (int i = fractionalPart.length(); i < 8; i++) { afterComma += "0"; } afterComma += fractionalPart.replace("0+$", ""); } String result = negative + amount + afterComma; while (result.lastIndexOf("0") == result.length() - 1 && result.contains(".")) { result = result.substring(0, result.length() - 1); } if (result.lastIndexOf(".") == result.length() - 1) { result = result.substring(0, result.length() - 1); } return result; }
From source file:org.apache.hadoop.hive.serde2.teradata.TeradataBinaryDataOutputStream.java
/** * Write DECIMAL(P, S).// w ww. ja v a2 s .c o m * The representation of decimal in Teradata binary format is: * the byte number to read is decided solely by the precision(P), * HiveDecimal is constructed through the byte array and scale. * the rest of byte will use 0x00 to pad (positive) and use 0xFF to pad (negative). * the null DECIMAL will use 0x00 to pad. * * @param writable the writable * @param byteNum the byte num * @throws IOException the io exception */ public void writeDecimal(HiveDecimalWritable writable, int byteNum, int scale) throws IOException { if (writable == null) { byte[] pad = new byte[byteNum]; write(pad); return; } // since the HiveDecimal will auto adjust the scale to save resource // we need to adjust it back otherwise the output bytes will be wrong int hiveScale = writable.getHiveDecimal().scale(); BigInteger bigInteger = writable.getHiveDecimal().unscaledValue(); if (hiveScale < scale) { BigInteger multiplicand = new BigInteger("1" + join("", Collections.nCopies(scale - hiveScale, "0"))); bigInteger = bigInteger.multiply(multiplicand); } byte[] content = bigInteger.toByteArray(); int signBit = content[0] >> 7 & 1; ArrayUtils.reverse(content); write(content); if (byteNum > content.length) { byte[] pad; if (signBit == 0) { pad = new byte[byteNum - content.length]; } else { pad = new byte[byteNum - content.length]; Arrays.fill(pad, (byte) 255); } write(pad); } }
From source file:libra.preprocess.common.kmerhistogram.KmerRangePartitioner.java
public KmerRangePartition[] getEqualRangePartitions() { KmerRangePartition[] partitions = new KmerRangePartition[this.numPartitions]; // calc 4^kmerSize BigInteger kmerend = BigInteger.valueOf(4).pow(this.kmerSize); BigInteger slice_width = kmerend.divide(BigInteger.valueOf(this.numPartitions)); if (kmerend.mod(BigInteger.valueOf(this.numPartitions)).intValue() != 0) { slice_width = slice_width.add(BigInteger.ONE); }/* w ww. java 2s . co m*/ for (int i = 0; i < this.numPartitions; i++) { BigInteger slice_begin = slice_width.multiply(BigInteger.valueOf(i)); if (slice_begin.add(slice_width).compareTo(kmerend) > 0) { slice_width = kmerend.subtract(slice_begin); } BigInteger slice_end = slice_begin.add(slice_width).subtract(BigInteger.ONE); KmerRangePartition slice = new KmerRangePartition(this.kmerSize, this.numPartitions, i, slice_width, slice_begin, slice_end); partitions[i] = slice; } return partitions; }
From source file:eu.dety.burp.joseph.attacks.bleichenbacher_pkcs1.BleichenbacherPkcs1DecryptionAttackExecutor.java
private BigInteger step3ComputeLowerBound(final BigInteger s, final BigInteger modulus, final BigInteger lowerIntervalBound) { BigInteger lowerBound = lowerIntervalBound.multiply(s); lowerBound = lowerBound.subtract(BigInteger.valueOf(3).multiply(this.bigB)); lowerBound = lowerBound.add(BigInteger.ONE); lowerBound = lowerBound.divide(modulus); return lowerBound; }
From source file:eu.dety.burp.joseph.attacks.bleichenbacher_pkcs1.BleichenbacherPkcs1DecryptionAttackExecutor.java
private BigInteger step3ComputeUpperBound(final BigInteger s, final BigInteger modulus, final BigInteger upperIntervalBound) { BigInteger upperBound = upperIntervalBound.multiply(s); upperBound = upperBound.subtract(BigInteger.valueOf(2).multiply(bigB)); // ceil//from ww w . j a v a2 s .co m BigInteger[] tmp = upperBound.divideAndRemainder(modulus); if (BigInteger.ZERO.compareTo(tmp[1]) != 0) { upperBound = BigInteger.ONE.add(tmp[0]); } else { upperBound = tmp[0]; } return upperBound; }
From source file:org.apache.hama.util.Bytes.java
/** * Split passed range. Expensive operation relatively. Uses BigInteger math. * //from www .jav a 2 s. c o m * @param a Beginning of range * @param b End of range * @param num Number of times to split range. Pass 1 if you want to split the * range in two; i.e. one split. * @return Array of dividing values */ public static byte[][] split(final byte[] a, final byte[] b, final int num) { byte[] aPadded; byte[] bPadded; if (a.length < b.length) { aPadded = padTail(a, b.length - a.length); bPadded = b; } else if (b.length < a.length) { aPadded = a; bPadded = padTail(b, a.length - b.length); } else { aPadded = a; bPadded = b; } if (compareTo(aPadded, bPadded) >= 0) { throw new IllegalArgumentException("b <= a"); } if (num <= 0) { throw new IllegalArgumentException("num cannot be < 0"); } byte[] prependHeader = { 1, 0 }; BigInteger startBI = new BigInteger(add(prependHeader, aPadded)); BigInteger stopBI = new BigInteger(add(prependHeader, bPadded)); BigInteger diffBI = stopBI.subtract(startBI); BigInteger splitsBI = BigInteger.valueOf(num + 1); if (diffBI.compareTo(splitsBI) < 0) { return null; } BigInteger intervalBI; try { intervalBI = diffBI.divide(splitsBI); } catch (Exception e) { LOG.error("Exception caught during division", e); return null; } byte[][] result = new byte[num + 2][]; result[0] = a; for (int i = 1; i <= num; i++) { BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i))); byte[] padded = curBI.toByteArray(); if (padded[1] == 0) padded = tail(padded, padded.length - 2); else padded = tail(padded, padded.length - 1); result[i] = padded; } result[num + 1] = b; return result; }
From source file:eu.dety.burp.joseph.attacks.bleichenbacher_pkcs1.BleichenbacherPkcs1DecryptionAttackExecutor.java
private boolean stepFour() { boolean resultFound = false; if (this.m.length == 1 && this.m[0].lower.compareTo(this.m[0].upper) == 0) { BigInteger solution = this.s0.modInverse(this.pubKey.getModulus()); solution = solution.multiply(this.m[0].upper).mod(this.pubKey.getModulus()); publish(solution);/*from w w w .java 2 s . c o m*/ this.result = solution.toByteArray(); loggerInstance.log(getClass(), "====> Solution found!\n" + solution, Logger.LogLevel.INFO); resultFound = true; } return resultFound; }
From source file:co.rsk.remasc.Remasc.java
private void payIncludedSiblings(List<Sibling> siblings, BigInteger topReward) { long perLateBlockPunishmentDivisor = remascConstants.getLateUncleInclusionPunishmentDivisor(); for (Sibling sibling : siblings) { long processingBlockNumber = executionBlock.getNumber() - remascConstants.getMaturity(); long numberOfBlocksLate = sibling.getIncludedHeight() - processingBlockNumber - 1L; BigInteger lateInclusionPunishment = topReward.multiply(BigInteger.valueOf(numberOfBlocksLate)) .divide(BigInteger.valueOf(perLateBlockPunishmentDivisor)); transfer(sibling.getCoinbase(), topReward.subtract(lateInclusionPunishment)); provider.addToBurnBalance(lateInclusionPunishment); }//from w w w . j av a 2 s .c om }