List of usage examples for java.lang StrictMath sqrt
@HotSpotIntrinsicCandidate public static native double sqrt(double a);
From source file:Main.java
public static void main(String[] args) { double d1 = 123, d2 = -456, d3 = 0.0; System.out.println("square root of " + d1 + " = " + StrictMath.sqrt(d1)); System.out.println("square root of " + d2 + " = " + StrictMath.sqrt(d2)); System.out.println("square root of " + d3 + " = " + StrictMath.sqrt(d3)); }
From source file:net.nicoulaj.benchmarks.math.DoubleSqrt.java
@GenerateMicroBenchmark public void strictmath(BlackHole hole) { for (int i = 0; i < data.length - 1; i++) hole.consume(StrictMath.sqrt(data[i])); }
From source file:test.Perf.java
void generate(int rows) { // Get some references timer.start();//from w w w.j a v a 2s. c o m final HReference[] refs = new HReference[100]; table.readAll(Filter.ALL, row -> refs[RANDOM.nextInt(refs.length)] = row.id()); timer.mark("refs"); List<HMap> batch = new ArrayList<>(); for (int i = 0; i < rows; i++) { HMap row = new HMap(); HReference id = new HReference(); row.put("id", id); timer.mark("id"); int markerCount = RANDOM.nextInt(10); for (int j = 0; j < markerCount; j++) { int r = (int) StrictMath.sqrt(RANDOM.nextDouble() * markers.length * markers.length); if (r >= markers.length) r = markers.length - 1; row.put(markers[r]); } timer.mark("markers"); row.put("dis", dii[RANDOM.nextInt(dii.length)]); timer.mark("dis"); row.put("number", new HNumber(RANDOM.nextDouble() * 100)); timer.mark("number"); HReference ref = refs[RANDOM.nextInt(refs.length)]; if (ref != null) row.put("rowRef", ref); timer.mark("rowRef"); batch.add(row); timer.mark("add"); refs[RANDOM.nextInt(refs.length)] = id; timer.mark("newRef"); } table.batchInsert(batch); timer.mark("insert"); }
From source file:edu.cornell.med.icb.goby.alignments.AlleleFrequencyOutputFormat.java
public void writeRecord(DiscoverVariantIterateSortedAlignments iterator, SampleCountInfo[] sampleCounts, int referenceIndex, int position, DiscoverVariantPositionData list, int groupIndexA, int groupIndexB) { // report 1-based positions position = position + 1;//from w w w . ja v a 2 s . co m fillVariantCountArrays(sampleCounts); // records are only written for site where at least one sample is bi-allelic if (!eventObserved) return; int totalCount = 0; for (int sampleIndex = 0; sampleIndex < numberOfSamples; sampleIndex++) { SampleCountInfo sci = sampleCounts[sampleIndex]; int sumInSample = 0; for (int genotypeIndex = 0; genotypeIndex < sci.getGenotypeMaxIndex(); ++genotypeIndex) { final int genotypeCount = sci.getGenotypeCount(genotypeIndex); totalCount += genotypeCount; sumInSample += genotypeCount; assert genotypeCount >= 0 : "counts must not be negative."; } // must observe at least one base in each sample to write output for this position. if (sumInSample == 0) return; } if (totalCount == 0) return; statsWriter.setInfo(depthFieldIndex, totalCount); CharSequence currentReferenceId = iterator.getReferenceId(referenceIndex); statsWriter.setId("."); statsWriter.setInfo(biomartFieldIndex, String.format("%s:%d:%d", currentReferenceId, position, position)); statsWriter.setChromosome(currentReferenceId); statsWriter.setPosition(position); for (final GroupComparison comparison : groupComparisons) { Arrays.fill(valuesGroupsA[comparison.index], 0); Arrays.fill(valuesGroupsB[comparison.index], 0); Arrays.fill(valuesGroupAIndex, 0); Arrays.fill(valuesGroupBIndex, 0); } Arrays.fill(averageRPPerGroup, 0); Arrays.fill(numSamplesPerGroup, 0); for (int sampleIndex = 0; sampleIndex < numberOfSamples; sampleIndex++) { int numAlleles = 0; totalCount = 0; final SampleCountInfo sampleCount = sampleCounts[sampleIndex]; for (int genotypeIndex = 0; genotypeIndex < sampleCount.getGenotypeMaxIndex(); ++genotypeIndex) { final int count = sampleCount.getGenotypeCount(genotypeIndex); if (count > 0) numAlleles++; totalCount += count; } // estimate reference allele proportion: double refProportion = (double) sampleCounts[sampleIndex].refCount; refProportion /= sampleCounts[sampleIndex].refCount + sampleCounts[sampleIndex].varCount; statsWriter.setSampleValue(refPropFieldIndex, sampleIndex, refProportion); final int groupIndex = readerIndexToGroupIndex[sampleIndex]; final double transformedValue = StrictMath.asin(StrictMath.sqrt(refProportion)); averageRPPerGroup[groupIndex] += refProportion; for (final GroupComparison comparison : groupComparisons) { final int index = comparison.index; if (groupIndex == comparison.indexGroup1) { valuesGroupsA[index][valuesGroupAIndex[index]++] = transformedValue; } if (groupIndex == comparison.indexGroup2) { valuesGroupsB[index][valuesGroupBIndex[index]++] = transformedValue; } } numSamplesPerGroup[groupIndex]++; } for (int groupIndex = 0; groupIndex < numberOfGroups; groupIndex++) { averageRPPerGroup[groupIndex] /= numSamplesPerGroup[groupIndex]; statsWriter.setInfo(averageRPGroupsIndex[groupIndex], averageRPPerGroup[groupIndex]); } // write effect size: float effectSize = Math.abs(averageRPPerGroup[0] - averageRPPerGroup[1]); statsWriter.setInfo(effectSizeInfoIndex, effectSize); genotypeFormatter.writeGenotypes(statsWriter, sampleCounts, position); if (!statsWriter.hasAlternateAllele() || effectSize < minimumAllelelicDifference) { // do not write a record if the position does not have an alternate allele or if the effect size is negligible. return; } for (final GroupComparison comparison : groupComparisons) { double pValue = 1; final int index = comparison.index; try { if (valuesGroupAIndex[index] >= 2 && valuesGroupBIndex[index] >= 2) { // need two samples per group pValue = mathCommonsTTest.homoscedasticTTest(valuesGroupsA[index], valuesGroupsB[index]); } } catch (MathException e) { pValue = 1; } statsWriter.setInfo(pValueIndex[index], pValue); } statsWriter.writeRecord(); }
From source file:MersenneTwisterFast.java
public final double nextGaussian() { if (__haveNextNextGaussian) { __haveNextNextGaussian = false;/*from w ww . j av a 2s. c o m*/ return __nextNextGaussian; } else { double v1, v2, s; do { int y; int z; int a; int b; if (mti >= N) // generate N words at one time { int kk; final int[] mt = this.mt; // locals are slightly faster final int[] mag01 = this.mag01; // locals are slightly faster for (kk = 0; kk < N - M; kk++) { y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; } for (; kk < N - 1; kk++) { y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; } y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; mti = 0; } y = mt[mti++]; y ^= y >>> 11; // TEMPERING_SHIFT_U(y) y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) if (mti >= N) // generate N words at one time { int kk; final int[] mt = this.mt; // locals are slightly faster final int[] mag01 = this.mag01; // locals are slightly faster for (kk = 0; kk < N - M; kk++) { z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1]; } for (; kk < N - 1; kk++) { z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1]; } z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1]; mti = 0; } z = mt[mti++]; z ^= z >>> 11; // TEMPERING_SHIFT_U(z) z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z) z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z) z ^= (z >>> 18); // TEMPERING_SHIFT_L(z) if (mti >= N) // generate N words at one time { int kk; final int[] mt = this.mt; // locals are slightly faster final int[] mag01 = this.mag01; // locals are slightly faster for (kk = 0; kk < N - M; kk++) { a = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); mt[kk] = mt[kk + M] ^ (a >>> 1) ^ mag01[a & 0x1]; } for (; kk < N - 1; kk++) { a = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); mt[kk] = mt[kk + (M - N)] ^ (a >>> 1) ^ mag01[a & 0x1]; } a = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); mt[N - 1] = mt[M - 1] ^ (a >>> 1) ^ mag01[a & 0x1]; mti = 0; } a = mt[mti++]; a ^= a >>> 11; // TEMPERING_SHIFT_U(a) a ^= (a << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(a) a ^= (a << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(a) a ^= (a >>> 18); // TEMPERING_SHIFT_L(a) if (mti >= N) // generate N words at one time { int kk; final int[] mt = this.mt; // locals are slightly faster final int[] mag01 = this.mag01; // locals are slightly faster for (kk = 0; kk < N - M; kk++) { b = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); mt[kk] = mt[kk + M] ^ (b >>> 1) ^ mag01[b & 0x1]; } for (; kk < N - 1; kk++) { b = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); mt[kk] = mt[kk + (M - N)] ^ (b >>> 1) ^ mag01[b & 0x1]; } b = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); mt[N - 1] = mt[M - 1] ^ (b >>> 1) ^ mag01[b & 0x1]; mti = 0; } b = mt[mti++]; b ^= b >>> 11; // TEMPERING_SHIFT_U(b) b ^= (b << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(b) b ^= (b << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(b) b ^= (b >>> 18); // TEMPERING_SHIFT_L(b) /* derived from nextDouble documentation in jdk 1.2 docs, see top */ v1 = 2 * (((((long) (y >>> 6)) << 27) + (z >>> 5)) / (double) (1L << 53)) - 1; v2 = 2 * (((((long) (a >>> 6)) << 27) + (b >>> 5)) / (double) (1L << 53)) - 1; s = v1 * v1 + v2 * v2; } while (s >= 1 || s == 0); double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s) / s); __nextNextGaussian = v2 * multiplier; __haveNextNextGaussian = true; return v1 * multiplier; } }
From source file:org.cirdles.calamari.algorithms.WeightedMeanCalculators.java
/** * Adapted from Simon Bodorkos interpretation of Ludwig: * https://github.com/CIRDLES/ET_Redux/wiki/SHRIMP:-Sub-wtdLinCorr. Note the * logic is simplified and output values are stored in object of type * WtdLinCorrResults. Indexing in Java is 0-based, hence the use of i-1 and * minIndex - 1 in the calls to deletePoint. * * @param y/* ww w .ja v a 2 s . com*/ * @param sigRho * @param x * @return */ public static WtdLinCorrResults wtdLinCorr(double[] y, double[][] sigRho, double[] x) { WtdLinCorrResults wtdLinCorrResults = new WtdLinCorrResults(); boolean linReg = (y.length == x.length); int avg1LinRegr2 = linReg ? 2 : 1; int n = y.length; double[] mswdRatList = new double[] { 0.0, 0.1, 0.15, 0.2, 0.2, 0.25 }; double mswdRatToler = (n > 7) ? 0.3 : mswdRatList[n - avg1LinRegr2 - 1]; // int maxRej = (int) StrictMath.ceil((n - avg1LinRegr2) / 8.0); // incorrect statement found by Griffin Hiers Feb 2017 int maxRej = 1 + (int) StrictMath.floor((n - avg1LinRegr2) / 8.0); // boolean[] rej = new boolean[n]; // not used double minProb = 0.1; // double wLrej = 0; int pass = 0; int minIndex = -1; double minMSWD = 0.0; double maxProb = 0.0; double[] y1 = y.clone(); double[] y2 = y.clone(); double[] x1 = x.clone(); double[] x2 = x.clone(); double[][] sigRho1 = sigRho.clone(); double[][] sigRho2 = sigRho.clone(); double[] sigmaY = new double[n]; for (int i = 0; i < n; i++) { sigmaY[i] = sigRho[i][i]; } double f = StrictMath.max(TukeyBiweight.calculateMedian(sigmaY), 1e-10); for (int i = 0; i < n; i++) { sigRho1[i][i] = StrictMath.max(sigRho1[i][i], f); sigRho2[i][i] = sigRho1[i][i]; } boolean doContinue = true; int nw = n; DeletePointResults deletePointResults; double[] probW = new double[n + 1]; double[] mswdW = new double[n + 1]; double[] sigmaInterW = new double[n + 1]; double[] interW = new double[n + 1]; double[] slopeW = new double[n + 1]; double[] sigmaSlopeW = new double[n + 1]; double[] covSlopeInterW = new double[n + 1]; do { for (int i = 0; i < (n + 1); i++) { if (i > 0) { deletePointResults = deletePoint(i - 1, y1, sigRho1, x1); y2 = deletePointResults.getY2(); sigRho2 = deletePointResults.getSigRho2(); x2 = deletePointResults.getX2(); nw = n - 1; } if ((nw == 1) && !linReg) { probW[i] = 1.0; mswdW[i] = 0.0; sigmaInterW[i] = 1.0; interW[i] = 1.0; } else if (linReg) { WeightedLinearCorrResults weightedLinearCorrResults = weightedLinearCorr(y2, x2, sigRho2); slopeW[i] = weightedLinearCorrResults.getSlope(); interW[i] = weightedLinearCorrResults.getIntercept(); mswdW[i] = weightedLinearCorrResults.getMswd(); probW[i] = weightedLinearCorrResults.getProb(); sigmaSlopeW[i] = weightedLinearCorrResults.getSlopeSig(); sigmaInterW[i] = weightedLinearCorrResults.getInterceptSig(); covSlopeInterW[i] = weightedLinearCorrResults.getSlopeInterceptCov(); // bad is never used } else { WtdAvCorrResults wtdAvCorrResults = wtdAvCorr(y2, convertCorrelationsToCovariances(sigRho2)); interW[i] = wtdAvCorrResults.getMeanVal(); sigmaInterW[i] = wtdAvCorrResults.getSigmaMeanVal(); mswdW[i] = wtdAvCorrResults.getMswd(); probW[i] = wtdAvCorrResults.getProb(); } if (i == 0) { if (probW[0] > 0.1) { minIndex = 0; // minMSWD = mswdW[0]; // assignment never used // exit for loop of i break; } maxProb = probW[0]; } } // for loop of i if (minIndex == 0) { doContinue = false; } else { minIndex = 0; minMSWD = mswdW[0]; for (int i = 1; i < (n + 1); i++) { double mswdRat = mswdW[i] / StrictMath.max(1e-32, mswdW[0]); if ((mswdRat < mswdRatToler) && (mswdW[i] < minMSWD) && (probW[i] > minProb)) { // rej[i] = true; not used // wLrej++; not used minIndex = i; maxProb = probW[i]; minMSWD = mswdW[i]; } } pass++; // note check for pass > 0 in original code is redundant if ((minIndex == 0) || (pass == maxRej) || (maxProb > 0.1)) { doContinue = false; } else { deletePointResults = deletePoint(minIndex - 1, y1, sigRho1, x1); y2 = deletePointResults.getY2(); sigRho2 = deletePointResults.getSigRho2(); x2 = deletePointResults.getX2(); n -= 1; y1 = new double[n]; if (linReg) { x1 = new double[n]; } sigRho1 = new double[n][n]; for (int i = 0; i < n; i++) { y1[i] = y2[i]; if (linReg) { x1[i] = x2[i]; } System.arraycopy(sigRho2[i], 0, sigRho1[i], 0, n); } } } } while (doContinue); double intercept = interW[minIndex]; double sigmaIntercept = sigmaInterW[minIndex]; double mswd = mswdW[minIndex]; double probfit = probW[minIndex]; if (linReg && (minIndex > 0)) { wtdLinCorrResults.setSlope(slopeW[minIndex]); wtdLinCorrResults.setSigmaSlope(sigmaSlopeW[minIndex]); wtdLinCorrResults.setCovSlopeInter(covSlopeInterW[minIndex]); } if (probfit < 0.05) { sigmaIntercept *= StrictMath.sqrt(mswd); if (linReg) { wtdLinCorrResults.setSigmaSlope(wtdLinCorrResults.getSigmaSlope() * StrictMath.sqrt(mswd)); } } wtdLinCorrResults.setBad(false); wtdLinCorrResults.setIntercept(intercept); wtdLinCorrResults.setSigmaIntercept(sigmaIntercept); wtdLinCorrResults.setMswd(mswd); wtdLinCorrResults.setProbFit(probfit); wtdLinCorrResults.setMinIndex(minIndex); return wtdLinCorrResults; }
From source file:org.cirdles.calamari.algorithms.WeightedMeanCalculators.java
public static WeightedLinearCorrResults weightedLinearCorr(double[] y, double[] x, double[][] sigmaRhoY) { WeightedLinearCorrResults weightedLinearCorrResults = new WeightedLinearCorrResults(); RealMatrix omega = new BlockRealMatrix(convertCorrelationsToCovariances(sigmaRhoY)); RealMatrix invOmega = MatrixUtils.inverse(omega); int n = y.length; double mX = 0; double pX = 0; double pY = 0; double pXY = 0; double w = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { double invOm = invOmega.getEntry(i, j); w += invOm;//from w w w. j a v a2 s .co m pX += (invOm * (x[i] + x[j])); pY += (invOm * (y[i] + y[j])); pXY += (invOm * (((x[i] * y[j]) + (x[j] * y[i])))); mX += (invOm * x[i] * x[j]); } } double slope = ((2 * pXY * w) - (pX * pY)) / ((4 * mX * w) - (pX * pX)); double intercept = (pY - (slope * pX)) / (2 * w); RealMatrix fischer = new BlockRealMatrix(new double[][] { { mX, pX / 2.0 }, { pX / 2.0, w } }); RealMatrix fischerInv = MatrixUtils.inverse(fischer); double slopeSig = StrictMath.sqrt(fischerInv.getEntry(0, 0)); double interceptSig = StrictMath.sqrt(fischerInv.getEntry(1, 1)); double slopeInterceptCov = fischerInv.getEntry(0, 1); RealMatrix resid = new BlockRealMatrix(n, 1); for (int i = 0; i < n; i++) { resid.setEntry(i, 0, y[i] - (slope * x[i]) - intercept); } RealMatrix residT = resid.transpose(); RealMatrix mM = residT.multiply(invOmega).multiply(resid); double sumSqWtdResids = mM.getEntry(0, 0); double mswd = sumSqWtdResids / (n - 2); // http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/FDistribution.html FDistribution fdist = new org.apache.commons.math3.distribution.FDistribution((n - 2), 1E9); double prob = 1.0 - fdist.cumulativeProbability(mswd); weightedLinearCorrResults.setBad(false); weightedLinearCorrResults.setSlope(slope); weightedLinearCorrResults.setIntercept(intercept); weightedLinearCorrResults.setSlopeSig(slopeSig); weightedLinearCorrResults.setInterceptSig(interceptSig); weightedLinearCorrResults.setSlopeInterceptCov(slopeInterceptCov); weightedLinearCorrResults.setMswd(mswd); weightedLinearCorrResults.setProb(prob); return weightedLinearCorrResults; }
From source file:org.cirdles.calamari.algorithms.WeightedMeanCalculators.java
public static WtdAvCorrResults wtdAvCorr(double[] values, double[][] varCov) { // assume varCov is variance-covariance matrix (i.e. SigRho = false) WtdAvCorrResults results = new WtdAvCorrResults(); int n = varCov.length; RealMatrix omegaInv = new BlockRealMatrix(varCov); RealMatrix omega = MatrixUtils.inverse(omegaInv); double numer = 0.0; double denom = 0.0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { numer += (values[i] + values[j]) * omega.getEntry(i, j); denom += omega.getEntry(i, j); }//from w ww .jav a2 s .c om } // test denom if (denom > 0.0) { double meanVal = numer / denom / 2.0; double meanValSigma = StrictMath.sqrt(1.0 / denom); double[][] unwtdResidsArray = new double[n][1]; for (int i = 0; i < n; i++) { unwtdResidsArray[i][0] = values[i] - meanVal; } RealMatrix unwtdResids = new BlockRealMatrix(unwtdResidsArray); RealMatrix transUnwtdResids = unwtdResids.transpose(); RealMatrix product = transUnwtdResids.multiply(omega); RealMatrix sumWtdResids = product.multiply(unwtdResids); double mswd = 0.0; double prob = 0.0; if (n > 1) { mswd = sumWtdResids.getEntry(0, 0) / (n - 1); // http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/FDistribution.html FDistribution fdist = new org.apache.commons.math3.distribution.FDistribution((n - 1), 1E9); prob = 1.0 - fdist.cumulativeProbability(mswd); } results.setBad(false); results.setMeanVal(meanVal); results.setSigmaMeanVal(meanValSigma); results.setMswd(mswd); results.setProb(prob); } return results; }
From source file:org.esa.beam.util.math.FastMathPerformance.java
public void testSqrt() { System.gc();// ww w . j av a 2s. c o m double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) x += StrictMath.sqrt(i * F1); long strictTime = System.nanoTime() - time; System.gc(); double y = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) y += FastMath.sqrt(i * F1); long fastTime = System.nanoTime() - time; System.gc(); double z = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) z += Math.sqrt(i * F1); long mathTime = System.nanoTime() - time; report("sqrt", x + y + z, strictTime, fastTime, mathTime); }