List of usage examples for java.lang Math log10
@HotSpotIntrinsicCandidate public static double log10(double a)
From source file:org.dspace.util.SolrImportExport.java
/** * Creates a filename for the export batch. * * @param indexName The name of the index being exported. * @param exportStart The start timestamp of the export * @param totalRecords The total number of records in the export. * @param index The index of the current batch. * @return A file name that is appropriate to use for exporting the batch of data described by the parameters. *///from ww w. ja v a 2 s .c om private static String makeExportFilename(String indexName, Date exportStart, long totalRecords, int index) { String exportFileNumber = ""; if (totalRecords > ROWS_PER_FILE) { exportFileNumber = StringUtils.leftPad("" + (index / ROWS_PER_FILE), (int) Math.ceil(Math.log10(totalRecords / ROWS_PER_FILE)), "0"); } return indexName + EXPORT_SEP + EXPORT_DATE_FORMAT.get().format(exportStart) + (StringUtils.isNotBlank(exportFileNumber) ? "_" + exportFileNumber : "") + ".csv"; }
From source file:fastcall.FastCallSNP.java
public int[] getGTLikelihood(int[] cnt) { int n = cnt.length * (cnt.length + 1) / 2; int[] likelihood = new int[n]; int sum = 0;/*from w ww . j a v a2s . com*/ for (int i = 0; i < cnt.length; i++) sum += cnt[i]; double coe = factorial(sum); for (int i = 0; i < cnt.length; i++) coe = coe / factorial(cnt[i]); for (int i = 0; i < cnt.length; i++) { for (int j = i; j < cnt.length; j++) { int index = (j * (j + 1) / 2) + i; if (i == j) { likelihood[index] = (int) Math .round(-Math.log10(coe * Math.pow((1 - 0.75 * this.sequencingErrorRate), cnt[i]) * Math.pow(this.sequencingErrorRate / 4, (sum - cnt[i])))); } else { likelihood[index] = (int) Math .round(-Math.log10(coe * Math.pow((0.5 - this.sequencingErrorRate / 4), cnt[i] + cnt[j]) * Math.pow(this.sequencingErrorRate / 4, (sum - cnt[i] - cnt[j])))); } } } return likelihood; }
From source file:com.udojava.evalex.Expression.java
/** * Creates a new expression instance from an expression string with a given * default match context./*w ww .j av a 2s . c om*/ * * @param expression The expression. E.g. <code>"2.4*sin(3)/(2-4)"</code> or * <code>"sin(y)>0 & max(z, 3)>3"</code> */ public Expression(String expression, LinkedList<String> hist, Variables vars) { this.history = hist; this.expression = expression; mainVars = vars; addOperator(new Operator("+", 20, true, "Addition") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { if (v1.type == ValueType.ARRAY) { MyComplex vo = new MyComplex(v1.list); vo.list.add(v2); return vo; } return v1.add(v2); } }); addOperator(new Operator("-", 20, true, "Subtraction") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { if (v1.type == ValueType.ARRAY) { MyComplex vo = new MyComplex(v1.list); vo.list.removeIf(o -> o.equals(v2)); return vo; } return v1.subtract(v2); } }); addOperator(new Operator("*", 30, true, "Real number multiplication") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { return v1.multiply(v2); } }); addOperator(new Operator("/", 30, true, "Real number division") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { return v1.divide(v2); } }); addOperator(new Operator("%", 30, true, "Remainder of integer division") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { double r = v1.real % v2.real; return new MyComplex(r); } }); addOperator( new Operator("^", 40, false, "Exponentation. See: https://en.wikipedia.org/wiki/Exponentiation") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { return v1.pow(v2); } }); addOperator(new Operator("&&", 4, false, "Logical AND. Evaluates to 1 if both operands are not 0") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { boolean b1 = (v1.real == 0.0 && v2.real == 0.0); return new MyComplex(b1 ? 1 : 0); } }); addOperator(new Operator("||", 2, false, "Logical OR. Evaluates to 0 if both operands are 0") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { boolean b1 = (v1.real == 0.0 && v2.real == 0.0); return new MyComplex(b1 ? 0 : 1); } }); addOperator(new Operator(">", 10, false, "Greater than. See: See: https://en.wikipedia.org/wiki/Inequality_(mathematics)") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) { return new MyComplex(v1.real > v2.real ? 1 : 0); } else { return new MyComplex(v1.abs() > v2.abs() ? 1 : 0); } } }); addOperator(new Operator(">=", 10, false, "Greater or equal") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) { return new MyComplex(v1.real >= v2.real ? 1 : 0); } else { return new MyComplex(v1.abs() >= v2.abs() ? 1 : 0); } } }); addOperator(new Operator("<", 10, false, "Less than. See: https://en.wikipedia.org/wiki/Inequality_(mathematics)") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) { return new MyComplex(v1.real < v2.real ? 1 : 0); } else { return new MyComplex(v1.abs() < v2.abs() ? 1 : 0); } } }); addOperator(new Operator("<=", 10, false, "less or equal") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) { return new MyComplex(v1.real <= v2.real ? 1 : 0); } else { return new MyComplex(v1.abs() <= v2.abs() ? 1 : 0); } } }); addOperator(new Operator("->", 7, false, "Set variable v to new value ") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { if (v1 instanceof PitDecimal) { PitDecimal target = (PitDecimal) v1; String s = target.getVarToken(); setVariable(s, v2); return v2; } throw new ExpressionException("LHS not variable"); } }); addOperator(new Operator("=", 7, false, "Equality") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) { return new MyComplex(v1.real == v2.real ? 1 : 0); } else { return new MyComplex(v1.abs() == v2.abs() ? 1 : 0); } } }); addOperator(new Operator("!=", 7, false, "Inequality. See: https://en.wikipedia.org/wiki/Inequality_(mathematics)") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) { return new MyComplex(v1.real != v2.real ? 1 : 0); } else { return new MyComplex(v1.abs() != v2.abs() ? 1 : 0); } } }); addOperator( new Operator("or", 7, false, "Bitwise OR. See: https://en.wikipedia.org/wiki/Logical_disjunction") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { return new MyComplex((long) v1.real | (long) v2.real); } }); addOperator(new Operator("and", 7, false, "Bitwise AND. See: https://en.wikipedia.org/wiki/Logical_conjunction") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { return new MyComplex((long) v1.real & (long) v2.real); } }); addOperator(new Operator("xor", 7, false, "Bitwise XOR, See: https://en.wikipedia.org/wiki/Exclusive_or") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { return new MyComplex((long) v1.real ^ (long) v2.real); } }); addOperator(new Operator("!", 50, true, "Factorial. See https://en.wikipedia.org/wiki/Factorial") { public BigInteger factorial(long n) { BigInteger factorial = BigInteger.ONE; for (long i = 1; i <= n; i++) { factorial = factorial.multiply(BigInteger.valueOf(i)); } return factorial; } @Override public MyComplex eval(MyComplex v1, MyComplex v2) { BigInteger fact = factorial((long) v1.real); return new MyComplex(fact, BigInteger.ZERO); } }); addOperator(new Operator("~", 8, false, "Bitwise negation") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { BigInteger bi = v2.toBigIntegerReal(); int c = bi.bitLength(); if (c == 0) { return new MyComplex(1); } for (int s = 0; s < c; s++) { bi = bi.flipBit(s); } return new MyComplex(bi); } }); addOperator(new Operator("shl", 8, false, "Left Bit shift") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { return new MyComplex((long) v1.real << (long) v2.real); } }); addOperator(new Operator("shr", 8, false, "Right bit shift") { @Override public MyComplex eval(MyComplex v1, MyComplex v2) { return new MyComplex((long) v1.real >>> (long) v2.real); } }); addFunction(new Function("NOT", 1, "evaluates to 0 if argument != 0") { @Override public MyComplex eval(List<MyComplex> parameters) { boolean zero = parameters.get(0).abs() == 0; return new MyComplex(zero ? 1 : 0); } }); addFunction(new Function("RND", 2, "Give random number in the range between first and second argument") { @Override public MyComplex eval(List<MyComplex> parameters) { double low = parameters.get(0).real; double high = parameters.get(1).real; return new MyComplex(low + Math.random() * (high - low)); } }); MersenneTwister mers = new MersenneTwister(System.nanoTime()); addFunction(new Function("MRS", 0, "Mersenne twister random generator") { @Override public MyComplex eval(List<MyComplex> parameters) { return new MyComplex(mers.nextDouble()); } }); addFunction(new Function("BIN", 2, "Binomial Coefficient 'n choose k'") { @Override public MyComplex eval(List<MyComplex> parameters) { int n = (int) parameters.get(0).real; int k = (int) parameters.get(1).real; double d = CombinatoricsUtils.binomialCoefficientDouble(n, k); return new MyComplex(d); } }); addFunction(new Function("STIR", 2, "Stirling number of 2nd kind: http://mathworld.wolfram.com/StirlingNumberoftheSecondKind.html") { @Override public MyComplex eval(List<MyComplex> parameters) { int n = (int) parameters.get(0).real; int k = (int) parameters.get(1).real; double d = CombinatoricsUtils.stirlingS2(n, k); return new MyComplex(d); } }); addFunction(new Function("SIN", 1, "Sine function") { @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).sin(); } }); addFunction(new Function("COS", 1, "Cosine function") { @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).cos(); } }); addFunction(new Function("TAN", 1, "Tangent") { @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).tan(); } }); addFunction(new Function("ASIN", 1, "Reverse Sine") { // added by av @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).asin(); } }); addFunction(new Function("ACOS", 1, "Reverse Cosine") { // added by av @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).acos(); } }); addFunction(new Function("ATAN", 1, "Reverse Tangent") { // added by av @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).atan(); } }); addFunction(new Function("SINH", 1, "Hyperbolic Sine") { @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).sinh(); } }); addFunction(new Function("COSH", 1, "Hyperbolic Cosine") { @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).cosh(); } }); addFunction(new Function("TANH", 1, "Hyperbolic Tangent") { @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).tanh(); } }); addFunction(new Function("RAD", 1, "Transform degree to radian") { @Override public MyComplex eval(List<MyComplex> parameters) { double d = Math.toRadians(parameters.get(0).real); return new MyComplex(d); } }); addFunction(new Function("DEG", 1, "Transform radian to degree") { @Override public MyComplex eval(List<MyComplex> parameters) { double d = Math.toDegrees(parameters.get(0).real); return new MyComplex(d); } }); addFunction(new Function("MAX", -1, "Find the biggest value in a list") { @Override public MyComplex eval(List<MyComplex> parameters) { MyComplex save = new MyComplex(Double.MIN_VALUE); if (parameters.size() == 0) { throw new ExpressionException("MAX requires at least one parameter"); } // if (parameters.get(0).type == ValueType.ARRAY) // parameters = parameters.get(0).list; if (parameters.get(0).type == ValueType.COMPLEX) { for (MyComplex parameter : parameters) { if (parameter.abs() > save.abs()) { save = parameter; } } save.type = ValueType.COMPLEX; } else { for (MyComplex parameter : parameters) { if (parameter.real > save.real) { save = parameter; } } save.type = ValueType.REAL; } return save; } }); /////////////////////////////////////////////////////// addFunction(new Function("IF", 3, "Conditional: give param3 if param1 is 0, otherwise param2") { @Override public MyComplex eval(List<MyComplex> parameters) { if (parameters.get(0).real == 0.0) { return parameters.get(2); } return parameters.get(1); } }); addFunction(new Function("PERC", 2, "Get param1 percent of param2") { @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).divide(new MyComplex(100)).multiply(parameters.get(1)); } }); addFunction(new Function("PER", 2, "How many percent is param1 of param2") { @Override public MyComplex eval(List<MyComplex> parameters) { return parameters.get(0).multiply(new MyComplex(100)).divide(parameters.get(1)); } }); addFunction(new Function("H", 1, "Evaluate _history element") { @Override public MyComplex eval(List<MyComplex> parameters) { int i = (int) parameters.get(0).real; Expression ex = new Expression(history.get(i), history, mainVars); return ex.eval(); } }); addFunction(new Function("MERS", 1, "Calculate Mersenne Number") { @Override public MyComplex eval(List<MyComplex> parameters) { MyComplex p = parameters.get(0); return new MyComplex(2).pow(p).subtract(new MyComplex(1)); } }); addFunction(new Function("GCD", 2, "Find greatest common divisor of 2 values") { @Override public MyComplex eval(List<MyComplex> parameters) { double a = parameters.get(0).real; double b = parameters.get(1).real; long r = ArithmeticUtils.gcd((long) a, (long) b); return new MyComplex(r); } }); addFunction(new Function("LCM", 2, "Find least common multiple of 2 values") { @Override public MyComplex eval(List<MyComplex> parameters) { double a = parameters.get(0).real; double b = parameters.get(1).real; long r = ArithmeticUtils.lcm((long) a, (long) b); return new MyComplex(r); } }); addFunction(new Function("AMEAN", -1, "Arithmetic mean of a set of values") { @Override public MyComplex eval(List<MyComplex> parameters) { if (parameters.size() == 0) { throw new ExpressionException("MEAN requires at least one parameter"); } Mean m = new Mean(); double[] d = MyComplex.getRealArray(parameters); double d2 = m.evaluate(d); return new MyComplex(d2); } }); // addFunction(new Function("BYT", -1, // "Value from sequence of bytes") // { // @Override // public MyComplex eval (List<MyComplex> parameters) // { // if (parameters.size() == 0) // { // return MyComplex.ZERO; // } // BigInteger res = BigInteger.ZERO; // for (MyComplex parameter : parameters) // { // if (parameter.intValue() < 0 || parameter.intValue() > 255) // { // throw new ExpressionException("not a byte value"); // } // res = res.shiftLeft(8); // res = res.or(parameter.toBigInteger()); // } // return new MyComplex(res, BigInteger.ZERO); // } // }); addFunction(new Function("SEQ", 3, "Generate Sequence p1=start, p2=step, p3=count") { @Override public MyComplex eval(List<MyComplex> parameters) { double start = parameters.get(0).real; ArrayList<MyComplex> arr = new ArrayList<>(); for (int s = 0; s < (int) (parameters.get(2).real); s++) { arr.add(new MyComplex(start)); start += parameters.get(1).real; } return new MyComplex(arr); } }); addFunction(new Function("PROD", -1, "Product of real values") { @Override public MyComplex eval(List<MyComplex> parameters) { Product p = new Product(); double[] d = MyComplex.getRealArray(parameters); return new MyComplex(p.evaluate(d)); } }); addFunction(new Function("SUM", -1, "Sum of values") { @Override public MyComplex eval(List<MyComplex> parameters) { Sum p = new Sum(); double[] d = MyComplex.getRealArray(parameters); return new MyComplex(p.evaluate(d)); } }); addFunction(new Function("ANG", 1, "Angle phi of complex number in radians") { @Override public MyComplex eval(List<MyComplex> parameters) { double b = parameters.get(0).angle(); return new MyComplex(b); } }); addFunction(new Function("IM", 1, "Get imaginary part") { @Override public MyComplex eval(List<MyComplex> parameters) { return new MyComplex(parameters.get(0).imaginary); } }); addFunction(new Function("RE", 1, "Get real part") { @Override public MyComplex eval(List<MyComplex> parameters) { return new MyComplex(parameters.get(0).real); } }); addFunction(new Function("POL", 2, "Make complex number from polar coords. angle is first arg") { @Override public MyComplex eval(List<MyComplex> parameters) { double angle = parameters.get(0).real; double len = parameters.get(1).real; Complex c = ComplexUtils.polar2Complex(len, angle); return new MyComplex(c); } }); addFunction(new Function("GMEAN", -1, "Geometric mean of a set of values") { @Override public MyComplex eval(List<MyComplex> parameters) { if (parameters.size() == 0) { throw new ExpressionException("MEAN requires at least one parameter"); } GeometricMean m = new GeometricMean(); double[] d = MyComplex.getRealArray(parameters); double d2 = m.evaluate(d); return new MyComplex(d2); } }); addFunction(new Function("HMEAN", -1, "Harmonic mean of a set of values") { @Override public MyComplex eval(List<MyComplex> parameters) { if (parameters.size() == 0) { throw new ExpressionException("MEAN requires at least one parameter"); } MyComplex res = new MyComplex(0); int num = 0; for (MyComplex parameter : parameters) { res = res.add(new MyComplex(1).divide(parameter)); num++; } res = new MyComplex(res.abs()); return new MyComplex(num).divide(res); } }); addFunction(new Function("VAR", -1, "Variance of a set of values") { @Override public MyComplex eval(List<MyComplex> parameters) { if (parameters.size() == 0) { throw new ExpressionException("MEAN requires at least one parameter"); } double[] arr = new double[parameters.size()]; for (int s = 0; s < parameters.size(); s++) { arr[s] = parameters.get(s).real; } return new MyComplex(variance(arr)); } }); addFunction(new Function("NPR", 1, "Next prime number greater or equal the argument") { @Override public MyComplex eval(List<MyComplex> parameters) { return new MyComplex(nextPrime((int) parameters.get(0).real)); } }); addFunction(new Function("NSWP", 1, "Swap nibbles") { @Override public MyComplex eval(List<MyComplex> parameters) { BigInteger bi = parameters.get(0).toBigIntegerReal(); String s = bi.toString(16); s = new StringBuilder(s).reverse().toString(); return new MyComplex(new BigInteger(s, 16), BigInteger.ZERO); } }); addFunction(new Function("BSWP", 1, "Swap bytes") { @Override public MyComplex eval(List<MyComplex> parameters) { BigInteger bi = parameters.get(0).toBigIntegerReal(); String s = bi.toString(16); while (s.length() % 4 != 0) { s = s + "0"; } if (bi.intValue() < 256) { s = "00" + s; } s = Misc.reverseHex(s); return new MyComplex(new BigInteger(s, 16), BigInteger.ZERO); } }); addFunction(new Function("PYT", 2, "Pythagoras's result = sqrt(param1^2+param2^2) https://en.wikipedia.org/wiki/Pythagorean_theorem") { @Override public MyComplex eval(List<MyComplex> par) { double a = par.get(0).real; double b = par.get(1).real; return new MyComplex(Math.sqrt(a * a + b * b)); } }); addFunction(new Function("FIB", 1, "Fibonacci number") { // --Commented out by Inspection (2/19/2017 7:46 PM):private final Operator exp = operators.get("^"); @Override public MyComplex eval(List<MyComplex> par) { return Misc.iterativeFibonacci((int) par.get(0).real); } }); /////////////////////////////////////////////// addFunction(new Function("MIN", -1, "Find the smallest in a list of values") { @Override public MyComplex eval(List<MyComplex> parameters) { MyComplex save = new MyComplex(Double.MAX_VALUE); if (parameters.size() == 0) { throw new ExpressionException("MAX requires at least one parameter"); } if (parameters.get(0).type == ValueType.COMPLEX) { for (MyComplex parameter : parameters) { if (parameter.abs() < save.abs()) { save = parameter; } } save.type = ValueType.COMPLEX; } else { for (MyComplex parameter : parameters) { if (parameter.real < save.real) { save = parameter; } } save.type = ValueType.REAL; } return save; } }); addFunction(new Function("ABS", 1, "Get absolute value of a number") { @Override public MyComplex eval(List<MyComplex> parameters) { return new MyComplex(parameters.get(0).abs()); } }); addFunction(new Function("LN", 1, "Logarithm base e of the argument") { @Override public MyComplex eval(List<MyComplex> parameters) { double d = Math.log(parameters.get(0).real); return new MyComplex(d); } }); addFunction(new Function("LOG", 1, "Logarithm base 10 of the argument") { @Override public MyComplex eval(List<MyComplex> parameters) { double d = Math.log10(parameters.get(0).real); return new MyComplex(d); } }); addFunction(new Function("FLOOR", 1, "Rounds DOWN to nearest Integer") { @Override public MyComplex eval(List<MyComplex> parameters) { double d = Math.floor(parameters.get(0).real); return new MyComplex(d); } }); addFunction(new Function("CEIL", 1, "Rounds UP to nearest Integer") { @Override public MyComplex eval(List<MyComplex> parameters) { double d = Math.ceil(parameters.get(0).real); return new MyComplex(d); } }); addFunction(new Function("ROU", 1, "Rounds to nearest Integer") { @Override public MyComplex eval(List<MyComplex> parameters) { int d = (int) (parameters.get(0).real + 0.5); return new MyComplex(d); } }); addFunction(new Function("SQRT", 1, "Square root") { @Override public MyComplex eval(List<MyComplex> parameters) { MyComplex p = parameters.get(0); if (p.type == ValueType.REAL) { return new MyComplex(Math.sqrt(p.real)); } return p.sqrt(); } }); addFunction(new Function("ARR", -1, "Create array") { @Override public MyComplex eval(List<MyComplex> parameters) { return new MyComplex(parameters); } }); addFunction(new Function("POLY", -1, "Treat array as Polynom") { @Override public MyComplex eval(List<MyComplex> parameters) { double[] d = MyComplex.getRealArray(parameters); PolynomialFunction p = new PolynomialFunction(d); return new MyComplex(p); } }); addFunction(new Function("DRVE", -1, "Make derivative of polynomial") { @Override public MyComplex eval(List<MyComplex> parameters) { PolynomialFunction p; if (parameters.get(0).isPoly()) { p = new PolynomialFunction(parameters.get(0).getRealArray()); } else { double[] d = MyComplex.getRealArray(parameters); p = new PolynomialFunction(d); } return new MyComplex(p.polynomialDerivative()); } }); addFunction(new Function("ADRVE", -1, "Make antiderivative of polynomial. Constant is always zero") { @Override public MyComplex eval(List<MyComplex> parameters) { PolynomialFunction p; if (parameters.get(0).isPoly()) { p = new PolynomialFunction(parameters.get(0).getRealArray()); } else { double[] d = MyComplex.getRealArray(parameters); p = new PolynomialFunction(d); } return new MyComplex(Misc.antiDerive(p)); } }); addFunction(new Function("PVAL", 2, "Compute value of polynom for the given argument.") { @Override public MyComplex eval(List<MyComplex> parameters) { if (parameters.get(0).isPoly()) { PolynomialFunction p = new PolynomialFunction(parameters.get(0).getRealArray()); double v = p.value(parameters.get(1).real); return new MyComplex(v); } throw new ExpressionException("first arg must be polynomial"); } }); addFunction(new Function("INTGR", 3, "Numerical integration") { @Override public MyComplex eval(List<MyComplex> parameters) { if (parameters.get(0).isPoly()) { PolynomialFunction p = new PolynomialFunction(parameters.get(0).getRealArray()); double start = parameters.get(1).real; double end = parameters.get(2).real; SimpsonIntegrator si = new SimpsonIntegrator(); double d = si.integrate(1000, p, start, end); return new MyComplex(d); } throw new ExpressionException("first arg must be polynomial"); } }); }
From source file:org.broadinstitute.gatk.tools.walkers.genotyper.afcalc.AFCalculationUnitTest.java
@Test(enabled = true && !DEBUG_ONLY, dataProvider = "PNonRef") private void testPNonRef(final VariantContext vcRoot, AFCalculatorImplementation modelType, AFCalculatorTestBuilder.PriorType priorType, final List<Genotype> genotypes, final double expectedPNonRef, final double tolerance, final int nNonInformative) { final AFCalculatorTestBuilder testBuilder = new AFCalculatorTestBuilder(1, vcRoot.getNAlleles() - 1, modelType, priorType);//from w w w.j a v a 2 s . c o m final VariantContextBuilder vcb = new VariantContextBuilder(vcRoot); vcb.genotypes(genotypes); final AFCalculationResult resultTracker = testBuilder.makeModel().getLog10PNonRef(vcb.make(), PLOIDY, MAX_ALT_ALLELES, testBuilder.makePriors()); Assert.assertEquals(resultTracker.getLog10PosteriorOfAFGT0(), Math.log10(expectedPNonRef), tolerance, "Actual pNonRef not within tolerance " + tolerance + " of expected"); }
From source file:org.esa.s1tbx.insar.gpf.coregistration.CrossCorrelationOp.java
private void determiningImageOffset(final Band slaveBand1, final Band slaveBand2, int[] offset) { try {//from w w w. j av a2s.com // get master and slave imagettes final MetadataElement absRoot = AbstractMetadata.getAbstractedMetadata(sourceProduct); double groundRangeSpacing = absRoot.getAttributeDouble(AbstractMetadata.range_spacing, 1); final double azimuthSpacing = absRoot.getAttributeDouble(AbstractMetadata.azimuth_spacing, 1); final boolean srgrFlag = AbstractMetadata.getAttributeBoolean(absRoot, AbstractMetadata.srgr_flag); if (!srgrFlag) { final TiePointGrid incidenceAngle = OperatorUtils.getIncidenceAngle(sourceProduct); final double incidenceAngleAtCentreRangePixel = incidenceAngle.getPixelDouble(sourceImageWidth / 2f, sourceImageHeight / 2f); groundRangeSpacing /= FastMath.sin(incidenceAngleAtCentreRangePixel * Constants.DTOR); } final int nRgLooks = Math.max(1, sourceImageWidth / 2048); final int nAzLooks = Math.max(1, (int) ((double) nRgLooks * groundRangeSpacing / azimuthSpacing + 0.5)); final int targetImageWidth = sourceImageWidth / nRgLooks; final int targetImageHeight = sourceImageHeight / nAzLooks; final int windowWidth = (int) FastMath.pow(2, (int) (Math.log10(targetImageWidth) / Math.log10(2))); final int windowHeight = (int) FastMath.pow(2, (int) (Math.log10(targetImageHeight) / Math.log10(2))); final double[] mI = new double[windowWidth * windowHeight]; final double[] sI = new double[windowWidth * windowHeight]; final int tileCountX = 4; final int tileCountY = 4; final int tileWidth = windowWidth / tileCountX; final int tileHeight = windowHeight / tileCountY; final Rectangle[] tileRectangles = new Rectangle[tileCountX * tileCountY]; int index = 0; for (int tileY = 0; tileY < tileCountY; tileY++) { final int ypos = tileY * tileHeight; for (int tileX = 0; tileX < tileCountX; tileX++) { final Rectangle tileRectangle = new Rectangle(tileX * tileWidth, ypos, tileWidth, tileHeight); tileRectangles[index++] = tileRectangle; } } final StatusProgressMonitor status = new StatusProgressMonitor(StatusProgressMonitor.TYPE.SUBTASK); status.beginTask("Computing offset... ", tileRectangles.length); final ThreadManager threadManager = new ThreadManager(); try { for (final Rectangle rectangle : tileRectangles) { checkForCancellation(); final Thread worker = new Thread() { @Override public void run() { final int x0 = rectangle.x; final int y0 = rectangle.y; final int w = rectangle.width; final int h = rectangle.height; final int xMax = x0 + w; final int yMax = y0 + h; final int xStart = x0 * nRgLooks; final int yStart = y0 * nAzLooks; final int xEnd = xMax * nRgLooks; final int yEnd = yMax * nAzLooks; final Rectangle srcRect = new Rectangle(xStart, yStart, xEnd - xStart, yEnd - yStart); final Tile mstTile1 = getSourceTile(masterBand1, srcRect); final ProductData mstData1 = mstTile1.getDataBuffer(); final TileIndex mstIndex = new TileIndex(mstTile1); final Tile slvTile1 = getSourceTile(slaveBand1, srcRect); final ProductData slvData1 = slvTile1.getDataBuffer(); final TileIndex slvIndex = new TileIndex(slvTile1); ProductData mstData2 = null; ProductData slvData2 = null; if (complexCoregistration) { mstData2 = getSourceTile(masterBand2, srcRect).getDataBuffer(); slvData2 = getSourceTile(slaveBand2, srcRect).getDataBuffer(); } final double rgAzLooks = nRgLooks * nAzLooks; for (int y = y0; y < yMax; y++) { final int yByWidth = y * windowWidth; final int y1 = y * nAzLooks; final int y2 = y1 + nAzLooks; for (int x = x0; x < xMax; x++) { final int x1 = x * nRgLooks; final int x2 = x1 + nRgLooks; mI[yByWidth + x] = getMeanValue(x1, x2, y1, y2, mstData1, mstData2, mstIndex, rgAzLooks); sI[yByWidth + x] = getMeanValue(x1, x2, y1, y2, slvData1, slvData2, slvIndex, rgAzLooks); } } status.worked(1); } }; threadManager.add(worker); } threadManager.finish(); } catch (Throwable e) { OperatorUtils.catchOperatorException("GCPSelectionOp", e); } finally { status.done(); } // correlate master and slave imagettes final RenderedImage masterImage = createRenderedImage(mI, windowWidth, windowHeight); final PlanarImage masterSpectrum = JAIFunctions.dft(masterImage); final RenderedImage slaveImage = createRenderedImage(sI, windowWidth, windowHeight); final PlanarImage slaveSpectrum = JAIFunctions.dft(slaveImage); final PlanarImage conjugateSlaveSpectrum = JAIFunctions.conjugate(slaveSpectrum); final PlanarImage crossSpectrum = JAIFunctions.multiplyComplex(masterSpectrum, conjugateSlaveSpectrum); final PlanarImage correlatedImage = JAIFunctions.idft(crossSpectrum); final PlanarImage crossCorrelatedImage = JAIFunctions.magnitude(correlatedImage); // compute offset final int w = crossCorrelatedImage.getWidth(); final int h = crossCorrelatedImage.getHeight(); final Raster idftData = crossCorrelatedImage.getData(); final double[] real = idftData.getSamples(0, 0, w, h, 0, (double[]) null); int peakRow = 0; int peakCol = 0; double peak = 0; for (int r = 0; r < h; r++) { for (int c = 0; c < w; c++) { if (r >= h / 4 && r <= h * 3 / 4 || c >= w / 4 && c <= w * 3 / 4) { continue; } final int s = r * w + c; if (peak < real[s]) { peak = real[s]; peakRow = r; peakCol = c; } } } // System.out.println("peakRow = " + peakRow + ", peakCol = " + peakCol); if (peakRow <= h / 2) { offset[1] = -peakRow * nAzLooks; } else { offset[1] = (h - peakRow) * nAzLooks; } if (peakCol <= w / 2) { offset[0] = -peakCol * nRgLooks; } else { offset[0] = (w - peakCol) * nRgLooks; } // System.out.println("offsetX = " + offset[0] + ", offsetY = " + offset[1]); } catch (Throwable e) { OperatorUtils.catchOperatorException(getId() + " getCoarseSlaveGCPPosition ", e); } }
From source file:org.broadinstitute.gatk.utils.MathUtils.java
/** * See #normalizeFromLog10 but with the additional option to use an approximation that keeps the calculation always in log-space * * @param array/*from w ww.j av a 2s . c o m*/ * @param takeLog10OfOutput * @param keepInLogSpace * * @return */ public static double[] normalizeFromLog10(final double[] array, final boolean takeLog10OfOutput, final boolean keepInLogSpace) { // for precision purposes, we need to add (or really subtract, since they're // all negative) the largest value; also, we need to convert to normal-space. double maxValue = arrayMax(array); // we may decide to just normalize in log space without converting to linear space if (keepInLogSpace) { for (int i = 0; i < array.length; i++) { array[i] -= maxValue; } return array; } // default case: go to linear space double[] normalized = new double[array.length]; for (int i = 0; i < array.length; i++) normalized[i] = Math.pow(10, array[i] - maxValue); // normalize double sum = 0.0; for (int i = 0; i < array.length; i++) sum += normalized[i]; for (int i = 0; i < array.length; i++) { double x = normalized[i] / sum; if (takeLog10OfOutput) { x = Math.log10(x); if (x < LOG10_P_OF_ZERO || Double.isInfinite(x)) x = array[i] - maxValue; } normalized[i] = x; } return normalized; }
From source file:Model.MultiPlatformLDA.java
private double getPostContentLikelihood(int u, int j) { // compute likelihood of content of post number j of user number u // content//from ww w.j a va 2 s. c om double content_LogLikelihood = 0; for (int i = 0; i < users[u].posts[j].words.length; i++) { int w = users[u].posts[j].words[i]; // probability that word i is generated by background topic double p_0 = backgroundTopic[w] * coinBias[0]; // probability that word i is generated by other topics double p_1 = 0; for (int z = 0; z < nTopics; z++) { double p_z = topics[z][w] * users[u].topicDistribution[z]; p_1 = p_1 + p_z; } p_1 = p_1 * coinBias[1]; content_LogLikelihood += Math.log10(p_0 + p_1); } return content_LogLikelihood; }
From source file:br.fapesp.myutils.MyUtils.java
/** * Creates an exponentially increasing sequence * @param start//from w ww . jav a 2s . co m * @param end * @param n number of elements * @return An n-element sequence of exponentially increasing elements */ public static double[] expspace(double start, double end, int n) { double[] ar = new double[n]; double step = (Math.log10(end) - Math.log10(start)) / (n - 1); if (n < 3) { throw new RuntimeException("n must be >= 3"); } ar[0] = start; ar[n - 1] = end; for (int i = 1; i < n - 1; i++) ar[i] = Math.exp(Math.log(10) * (Math.log10(ar[i - 1]) + step)); return ar; }
From source file:no.sintef.ict.splcatool.CoveringArrayChvatal.java
private void generate3(int coverLimit, Integer sizelimit) { // Get a list of vars List<BooleanVariableInterface> vars = new ArrayList<BooleanVariableInterface>(cnf.getVariables()); List<List<Integer>> solutions = new ArrayList<List<Integer>>(initial); // Calculate uncovered tuples List<Pair3> uncovered = new ArrayList<Pair3>(); List<BooleanVariableInterface> vars2 = new ArrayList<BooleanVariableInterface>(vars); List<BooleanVariableInterface> vars3 = new ArrayList<BooleanVariableInterface>(vars); long ignored = 0; long alreadyCovered = 0; for (int i = 0; i < vars.size(); i++) { BooleanVariableInterface var1 = vars.get(i); for (int j = i + 1; j < vars2.size(); j++) { BooleanVariableInterface var2 = vars2.get(j); for (int k = j + 1; k < vars3.size(); k++) { BooleanVariableInterface var3 = vars3.get(k); // Set triples Pair3 unc;/*w ww . j ava2 s . c om*/ if (!coverOnlyOnes) { if (coverZerosOnly) { unc = new Pair3(idnr); unc.v1 = var1; unc.b1 = false; unc.v2 = var2; unc.b2 = false; unc.v3 = var3; unc.b3 = false; if (!CALib.isCovered3(idnr, unc, solutions)) uncovered.add(unc); else alreadyCovered++; } unc = new Pair3(idnr); unc.v1 = var1; unc.b1 = false; unc.v2 = var2; unc.b2 = false; unc.v3 = var3; unc.b3 = true; if (!CALib.isCovered3(idnr, unc, solutions)) uncovered.add(unc); else alreadyCovered++; unc = new Pair3(idnr); unc.v1 = var1; unc.b1 = false; unc.v2 = var2; unc.b2 = true; unc.v3 = var3; unc.b3 = false; if (!CALib.isCovered3(idnr, unc, solutions)) uncovered.add(unc); else alreadyCovered++; unc = new Pair3(idnr); unc.v1 = var1; unc.b1 = false; unc.v2 = var2; unc.b2 = true; unc.v3 = var3; unc.b3 = true; if (!CALib.isCovered3(idnr, unc, solutions)) uncovered.add(unc); else alreadyCovered++; unc = new Pair3(idnr); unc.v1 = var1; unc.b1 = true; unc.v2 = var2; unc.b2 = false; unc.v3 = var3; unc.b3 = false; if (!CALib.isCovered3(idnr, unc, solutions)) uncovered.add(unc); else alreadyCovered++; unc = new Pair3(idnr); unc.v1 = var1; unc.b1 = true; unc.v2 = var2; unc.b2 = false; unc.v3 = var3; unc.b3 = true; if (!CALib.isCovered3(idnr, unc, solutions)) uncovered.add(unc); else alreadyCovered++; unc = new Pair3(idnr); unc.v1 = var1; unc.b1 = true; unc.v2 = var2; unc.b2 = true; unc.v3 = var3; unc.b3 = false; if (!CALib.isCovered3(idnr, unc, solutions)) uncovered.add(unc); else alreadyCovered++; } unc = new Pair3(idnr); unc.v1 = var1; unc.b1 = true; unc.v2 = var2; unc.b2 = true; unc.v3 = var3; unc.b3 = true; if (!CALib.isCovered3(idnr, unc, solutions)) uncovered.add(unc); else alreadyCovered++; } } } System.out.println("Uncovered triples left: " + uncovered.size()); System.out.println("alreadyCovered: " + alreadyCovered); System.out.println("expected: " + 8 * MathUtils.binomialCoefficient(vars.size(), 3)); // Check if (coverOnlyOnes) { if (uncovered.size() + alreadyCovered != MathUtils.binomialCoefficient(vars.size(), 3)) { System.out.println("Internal error: Wrong number of tuples"); System.exit(-1); } } else if (!coverZerosOnly) { if (uncovered.size() + alreadyCovered != 7 * MathUtils.binomialCoefficient(vars.size(), 3)) { System.out.println("Internal error: Wrong number of tuples"); System.exit(-1); } } else { if (uncovered.size() + alreadyCovered != 8 * MathUtils.binomialCoefficient(vars.size(), 3)) { System.out.println("Internal error: Wrong number of tuples"); System.exit(-1); } } // If starting from a covering array or doing a cover limit, start by finding invalids boolean invalidRemoved = false; long invalids = 0; if (coverLimit != 100 || initial.size() > 0) { System.out.println( "Removing invalid first when given a cover limit or a size limit or an initial covering array"); int diff = uncovered.size(); uncovered = getInvalid3(0, uncovered); diff -= uncovered.size(); System.out.println("Invalid: " + diff); invalids = diff; invalidRemoved = true; } // Get solver SAT4JSolver satSolver = null; try { satSolver = cnf.getSAT4JSolver(); } catch (ContradictionException e) { } // Cover long grandTotal = uncovered.size() + alreadyCovered; while (true) { // Calculate coverage coverage = (grandTotal - uncovered.size()) * 100 / grandTotal; // Stop at limit if (invalidRemoved && coverLimit <= coverage) break; // Check for limit if (solutions.size() >= sizelimit) break; // Mix Set<Pair3> mix = new HashSet<Pair3>(uncovered); List<Pair3> canBeSet = new ArrayList<Pair3>(); List<Pair3> x = new ArrayList<Pair3>(uncovered); // assumptions Set<Integer> sol = new HashSet<Integer>(); //System.out.println("Uncovered: " +uncovered.size()); for (int i = 0; i < x.size(); i++) { if (i % 1000 == 0) System.out.println(i + "/" + x.size()); // Get the two pairs boolean b1 = x.get(i).b1; boolean b2 = x.get(i).b2; boolean b3 = x.get(i).b3; BooleanVariableInterface v1 = x.get(i).v1; BooleanVariableInterface v2 = x.get(i).v2; BooleanVariableInterface v3 = x.get(i).v3; Pair p1 = new Pair(); p1.v = v1; p1.b = b1; Pair p2 = new Pair(); p2.v = v2; p2.b = b2; Pair p3 = new Pair(); p3.v = v3; p3.b = b3; // Set it int var1nr, var2nr, var3nr; var1nr = (b1 ? 1 : -1) * idnr.get(v1.getID()); var2nr = (b2 ? 1 : -1) * idnr.get(v2.getID()); var3nr = (b3 ? 1 : -1) * idnr.get(v3.getID()); // Check try { // List List<Integer> assumpsList = new ArrayList<Integer>(); for (int a : sol) { assumpsList.add(a); } if (assumpsList.contains(-var1nr)) continue; if (assumpsList.contains(-var2nr)) continue; if (assumpsList.contains(-var3nr)) continue; assumpsList.add(var1nr); assumpsList.add(var2nr); assumpsList.add(var3nr); // Convert int assumpsArray[] = new int[assumpsList.size()]; int c = 0; for (int a : assumpsList) { assumpsArray[c] = a; c++; } IVecInt assumps = new VecInt(assumpsArray); // Check if (satSolver.solver.isSatisfiable(assumps)) { sol.add(var1nr); sol.add(var2nr); sol.add(var3nr); canBeSet.add(x.get(i)); mix.remove(x.get(i)); } } catch (org.sat4j.specs.TimeoutException e1) { } } uncovered = new ArrayList<Pair3>(mix); // Convert int asssumpsArray[] = new int[sol.size()]; int c = 0; for (int a : sol) { asssumpsArray[c] = a; c++; } IVecInt assumps = new VecInt(asssumpsArray); try { satSolver.solver.isSatisfiable(assumps); } catch (org.sat4j.specs.TimeoutException e1) { } int[] s = satSolver.solver.model(); List<Integer> solution = new ArrayList<Integer>(); for (int z : s) solution.add(z); // Remove invalid at some round if (!invalidRemoved) { if ((int) Math.log10(canBeSet.size()) <= (int) Math.log10(cnf.getVariables().size())) { System.out.println("Removing invalid"); int diff = uncovered.size(); uncovered = getInvalid3(0, uncovered); diff -= uncovered.size(); System.out.println("Invalid: " + diff); invalidRemoved = true; } } // Check if done if (canBeSet.size() == 0) { System.out.println("Breaking at " + uncovered.size() + " invalids"); break; } else { System.out.println("Covered at " + (uncovered.size() + canBeSet.size()) + ", " + canBeSet.size()); //System.out.println(canBeSet); } // Return solutions.add(solution); } // Done result = solutions; }
From source file:spectrogram.Spectrogram.java
private int getYpos(double mxexp, double val) { int ypos;/*from ww w . java 2s .co m*/ double logMin = fmin == 0 ? 1 : Math.log10(fmin); double yfrac = (Math.log10(val) - logMin) / (Math.log10(fmax) - logMin); ypos = dimY - (int) (yfrac * dimY) + imgY0; return ypos; }