List of usage examples for org.apache.commons.math3.util MathArrays ebeAdd
public static double[] ebeAdd(double[] a, double[] b)
From source file:mase.app.soccer.SoccerIndEvalAdjusted.java
@Override protected void evaluate(MasonSimState sim) { super.evaluate(null); Soccer soc = (Soccer) sim;//from ww w . j ava 2 s. c o m for (int a = 0; a < soc.leftTeam.size(); a++) { double[] d = computeDistances(soc.leftTeam.get(a), (Soccer) sim); accumDists[a] = accumDists[a] == null ? d : MathArrays.ebeAdd(accumDists[a], d); } }
From source file:conceptor.chaos.DynamicalSystem.java
/** Standard 4th order Runge-Kutta method as outlined here: http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods <p>/*from ww w. j a v a 2 s. co m*/ While it is possible to use the commons-math ode package instead, this keeps the integration visible and doesn't require instantiation of complex objects */ private double[] integrate(double[] x, double tStart, double dt, int num) { double[] evolved = new double[x.length]; System.arraycopy(x, 0, evolved, 0, x.length); double t = tStart; for (int i = 0; i < num; i++) { computeDerivatives(t, evolved, k1); // k1 double[] xPlusK1 = MathArrays.ebeAdd(MathArrays.scale(dt / 2.0, k1), evolved); computeDerivatives(t + 0.5 * dt, xPlusK1, k2); double[] xPlusK2 = MathArrays.ebeAdd(MathArrays.scale(dt / 2.0, k2), evolved); computeDerivatives(t + 0.5 * dt, xPlusK2, k3); double[] xPlusK3 = MathArrays.ebeAdd(MathArrays.scale(dt, k3), evolved); computeDerivatives(t + dt, xPlusK3, k4); double[] kSum = MathArrays.ebeAdd(MathArrays.ebeAdd(k1, k4), MathArrays.scale(2.0, MathArrays.ebeAdd(k2, k3))); evolved = MathArrays.ebeAdd(evolved, MathArrays.scale(dt / 6.0, kSum)); t += dt; } return evolved; }
From source file:it.unibo.alchemist.model.implementations.positions.ContinuousGenericEuclidean.java
@Override public Position add(final Position other) { return new ContinuousGenericEuclidean(false, MathArrays.ebeAdd(c, other.getCartesianCoordinates())); }
From source file:org.apache.solr.client.solrj.io.eval.EBEAddEvaluator.java
@Override public Object doWork(Object first, Object second) throws IOException { if (null == first) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the first value", toExpression(constructingFactory))); }//w ww.j a va2 s . c om if (null == second) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the second value", toExpression(constructingFactory))); } if (!(first instanceof List<?>)) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a list of numbers", toExpression(constructingFactory), first.getClass().getSimpleName())); } if (!(second instanceof List<?>)) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a list of numbers", toExpression(constructingFactory), first.getClass().getSimpleName())); } double[] result = MathArrays.ebeAdd( ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(), ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()); List<Number> numbers = new ArrayList(); for (double d : result) { numbers.add(d); } return numbers; }
From source file:org.broadinstitute.gatk.tools.walkers.genotyper.afcalc.AlleleFrequencyCalculator.java
/** * Compute the probability of the alleles segregating given the genotype likelihoods of the samples in vc * * @param vc the VariantContext holding the alleles and sample information. The VariantContext * must have at least 1 alternative allele * @param refSnpIndelPseudocounts a total hack. A length-3 vector containing Dirichlet prior pseudocounts to * be given to ref, alt SNP, and alt indel alleles. Hack won't be necessary when we destroy the old AF calculators * @return result (for programming convenience) *//* w ww .j ava 2s.c o m*/ @Override public AFCalculationResult getLog10PNonRef(final VariantContext vc, final int defaultPloidy, final int maximumAlternativeAlleles, final double[] refSnpIndelPseudocounts) { Utils.nonNull(vc, "vc is null"); final int numAlleles = vc.getNAlleles(); final List<Allele> alleles = vc.getAlleles(); Utils.validateArg(numAlleles > 1, "VariantContext has only a single reference allele, but getLog10PNonRef requires at least one at all " + vc); final double[] priorPseudocounts = alleles.stream().mapToDouble( a -> a.isReference() ? refPseudocount : (a.length() > 1 ? snpPseudocount : indelPseudocount)) .toArray(); double[] alleleCounts = new double[numAlleles]; final double flatLog10AlleleFrequency = -MathUtils.Log10Cache.get(numAlleles); // log10(1/numAlleles) double[] log10AlleleFrequencies = new IndexRange(0, numAlleles).mapToDouble(n -> flatLog10AlleleFrequency); double alleleCountsMaximumDifference = Double.POSITIVE_INFINITY; while (alleleCountsMaximumDifference > THRESHOLD_FOR_ALLELE_COUNT_CONVERGENCE) { final double[] newAlleleCounts = effectiveAlleleCounts(vc, log10AlleleFrequencies); alleleCountsMaximumDifference = Arrays.stream(MathArrays.ebeSubtract(alleleCounts, newAlleleCounts)) .map(Math::abs).max().getAsDouble(); alleleCounts = newAlleleCounts; final double[] posteriorPseudocounts = MathArrays.ebeAdd(priorPseudocounts, alleleCounts); // first iteration uses flat prior in order to avoid local minimum where the prior + no pseudocounts gives such a low // effective allele frequency that it overwhelms the genotype likelihood of a real variant // basically, we want a chance to get non-zero pseudocounts before using a prior that's biased against a variant log10AlleleFrequencies = new Dirichlet(posteriorPseudocounts).log10MeanWeights(); } double[] log10POfZeroCountsByAllele = new double[numAlleles]; double log10PNoVariant = 0; for (final Genotype g : vc.getGenotypes()) { if (!g.hasLikelihoods()) { continue; } final int ploidy = g.getPloidy() == 0 ? defaultPloidy : g.getPloidy(); final GenotypeLikelihoodCalculator glCalc = GL_CALCS.getInstance(ploidy, numAlleles); final double[] log10GenotypePosteriors = log10NormalizedGenotypePosteriors(g, glCalc, log10AlleleFrequencies); //the total probability log10PNoVariant += log10GenotypePosteriors[HOM_REF_GENOTYPE_INDEX]; // per allele non-log space probabilities of zero counts for this sample // for each allele calculate the total probability of genotypes containing at least one copy of the allele final double[] log10ProbabilityOfNonZeroAltAlleles = new double[numAlleles]; Arrays.fill(log10ProbabilityOfNonZeroAltAlleles, Double.NEGATIVE_INFINITY); for (int genotype = 0; genotype < glCalc.genotypeCount(); genotype++) { final double log10GenotypePosterior = log10GenotypePosteriors[genotype]; glCalc.genotypeAlleleCountsAt(genotype) .forEachAlleleIndexAndCount((alleleIndex, count) -> log10ProbabilityOfNonZeroAltAlleles[alleleIndex] = MathUtils .log10SumLog10(log10ProbabilityOfNonZeroAltAlleles[alleleIndex], log10GenotypePosterior)); } for (int allele = 0; allele < numAlleles; allele++) { // if prob of non hom ref == 1 up to numerical precision, short-circuit to avoid NaN if (log10ProbabilityOfNonZeroAltAlleles[allele] >= 0) { log10POfZeroCountsByAllele[allele] = Double.NEGATIVE_INFINITY; } else { log10POfZeroCountsByAllele[allele] += MathUtils .log10OneMinusPow10(log10ProbabilityOfNonZeroAltAlleles[allele]); } } } // unfortunately AFCalculationResult expects integers for the MLE. We really should emit the EM no-integer values // which are valuable (eg in CombineGVCFs) as the sufficient statistics of the Dirichlet posterior on allele frequencies final int[] integerAlleleCounts = Arrays.stream(alleleCounts).mapToInt(x -> (int) Math.round(x)).toArray(); final int[] integerAltAlleleCounts = Arrays.copyOfRange(integerAlleleCounts, 1, numAlleles); //skip the ref allele (index 0) final Map<Allele, Double> log10PRefByAllele = IntStream.range(1, numAlleles).boxed() .collect(Collectors.toMap(alleles::get, a -> log10POfZeroCountsByAllele[a])); // we compute posteriors here and don't have the same prior that AFCalculationResult expects. Therefore, we // give it our posterior as its "likelihood" along with a flat dummy prior final double[] dummyFlatPrior = { -1e-10, -1e-10 }; //TODO: HACK must be negative for AFCalcResult final double[] log10PosteriorOfNoVariantYesVariant = { log10PNoVariant, MathUtils.log10OneMinusPow10(log10PNoVariant) }; return new AFCalculationResult(integerAltAlleleCounts, DUMMY_N_EVALUATIONS, alleles, log10PosteriorOfNoVariantYesVariant, dummyFlatPrior, log10PRefByAllele); }