List of usage examples for java.lang Math signum
public static float signum(float f)
From source file:org.renjin.MathExt.java
public static double sign(double x) { return Math.signum(x); }
From source file:org.jcurl.math.BisectionSolver.java
/** * Bisection root-finding algorithm. Ported from * http://en.wikipedia.org/wiki/Bisection_method * // w ww .j a v a 2s . c o m * @param f * the function to test * @param y * y value to find x for. 0 finds the root. * @param left * lower end of the interval to scan * @param right * upper end of the interval to scan * @param epsilon * iteration stops when * <code>Math.abs(right - left) <= epsilon</code> */ public static double findRoot(final R1R1Function f, final double y, double left, double right, final double epsilon) { double lefty = f.at(left, 0); if (Double.isNaN(lefty)) return Double.NaN; // check if left-y an right-y have opposite signs. Use signum to avoid // overflows. if (Math.signum(lefty) * f.at(right, 0) > 0) return Double.NaN; double midy = Double.NaN; while (Math.abs(right - left) > epsilon) { // Calculate midpoint of domain final double midpoint = (right + left) / 2; midy = f.at(midpoint, 0); if (log.isDebugEnabled()) log.debug("f(" + left + ")=" + lefty + " f(" + midpoint + ")=" + midy + " f(" + right + ")=unused"); if (Double.isNaN(midy)) return Double.NaN; // examine midy if ((lefty - y) * (midy - y) > 0) { // Throw away left half left = midpoint; lefty = midy; } else // Throw away right half right = midpoint; } return (right + left) / 2; }
From source file:org.renjin.primitives.MathExt.java
@Deferrable @Builtin @DataParallel public static double sign(double x) { return Math.signum(x); }
From source file:at.meikel.dmrl.webapp.rest.PlayerService.java
@RequestMapping(value = { "/players" }, method = RequestMethod.GET) @ResponseBody//from ww w.j av a 2s . c o m public List<Player> getAllPlayers() { List<Player> result = null; if (server != null) { result = server.getRankingList().getAllPlayers(); } if (result == null) { result = new Vector<Player>(); } Collections.sort(result, new Comparator<Player>() { @Override public int compare(Player p1, Player p2) { if (p1 == null) { return p2 == null ? 0 : 1; } else { if (p2 == null) { return -1; } else { return (int) Math.signum(p1.getRanglistenwert() - p2.getRanglistenwert()); } } } }); return result; }
From source file:com.opengamma.analytics.math.rootfinding.QuadraticRealRootFinder.java
/** * {@inheritDoc}/*w ww . ja v a 2 s . c om*/ * @throws IllegalArgumentException If the function is not a quadratic * @throws MathException If the roots are not real */ @Override public Double[] getRoots(final RealPolynomialFunction1D function) { Validate.notNull(function, "function"); final double[] coefficients = function.getCoefficients(); Validate.isTrue(coefficients.length == 3, "Function is not a quadratic"); final double c = coefficients[0]; final double b = coefficients[1]; final double a = coefficients[2]; final double discriminant = b * b - 4 * a * c; if (discriminant < 0) { throw new MathException("No real roots for quadratic"); } final double q = -0.5 * (b + Math.signum(b) * discriminant); return new Double[] { q / a, c / q }; }
From source file:net.dontdrinkandroot.utils.collections.CollectionUtils.java
public static <T extends Number> double getMean(final Collection<T> collection) { final int size = collection.size(); final List<T> sorted = new ArrayList<T>(collection); Collections.sort(sorted, new Comparator<T>() { @Override//ww w .j a va2s.c o m public int compare(final T n1, final T n2) { return (int) Math.signum(n2.doubleValue() - n1.doubleValue()); } }); if (size % 2 == 0) { return (sorted.get(size / 2).doubleValue() + sorted.get(size / 2 - 1).doubleValue()) / 2; } else { return sorted.get(size / 2).doubleValue(); } }
From source file:com.xpn.xwiki.objects.classes.PropertyClassTest.java
/** Test the {@link PropertyClass#compareTo(PropertyClass)} method. */ public void testCompareTo() { PropertyClass one = new PropertyClass(); PropertyClass two = new PropertyClass(); // Random numbers to be used as property indexes. int n1, n2;// w w w . java 2 s . c o m one.setName("first"); two.setName("second"); // Since the test might randomly succeed, run it several times to be safer. for (int i = 0; i < 20; ++i) { n1 = RandomUtils.nextInt(); n2 = RandomUtils.nextInt(); one.setNumber(n1); two.setNumber(n2); if (n1 == n2) { Assert.assertEquals(Math.signum(one.compareTo(two)), -1.0, 0); Assert.assertEquals(Math.signum(two.compareTo(one)), 1.0, 0); } else { Assert.assertEquals(Math.signum(one.compareTo(two)), Math.signum(n1 - n2)); Assert.assertEquals(Math.signum(two.compareTo(one)), Math.signum(n2 - n1)); } } // Also test that the comparison takes into account the name in case the two numbers are identical one.setNumber(42); two.setNumber(42); Assert.assertEquals(Math.signum(one.compareTo(two)), -1.0, 0); Assert.assertEquals(Math.signum(two.compareTo(one)), 1.0, 0); }
From source file:org.renjin.primitives.MathGroup.java
@Deferrable @Builtin/*from www . j a v a2 s . c o m*/ @DataParallel(value = PreserveAttributeStyle.ALL, passNA = true) public static double sign(double x) { return Math.signum(x); }
From source file:com.opengamma.analytics.math.rootfinding.CubicRootFinder.java
/** * {@inheritDoc}//from w w w . ja v a2 s . c om * @throws IllegalArgumentException If the function is not cubic */ @Override public ComplexNumber[] getRoots(final RealPolynomialFunction1D function) { Validate.notNull(function, "function"); final double[] coefficients = function.getCoefficients(); Validate.isTrue(coefficients.length == 4, "Function is not a cubic"); final double divisor = coefficients[3]; final double a = coefficients[2] / divisor; final double b = coefficients[1] / divisor; final double c = coefficients[0] / divisor; final double aSq = a * a; final double q = (aSq - 3 * b) / 9; final double r = (2 * a * aSq - 9 * a * b + 27 * c) / 54; final double rSq = r * r; final double qCb = q * q * q; final double constant = a / 3; if (rSq < qCb) { final double mult = -2 * Math.sqrt(q); final double theta = Math.acos(r / Math.sqrt(qCb)); return new ComplexNumber[] { new ComplexNumber(mult * Math.cos(theta / 3) - constant, 0), new ComplexNumber(mult * Math.cos((theta + TWO_PI) / 3) - constant, 0), new ComplexNumber(mult * Math.cos((theta - TWO_PI) / 3) - constant, 0) }; } final double s = -Math.signum(r) * Math.cbrt(Math.abs(r) + Math.sqrt(rSq - qCb)); final double t = CompareUtils.closeEquals(s, 0, 1e-16) ? 0 : q / s; final double sum = s + t; final double real = -0.5 * sum - constant; final double imaginary = Math.sqrt(3) * (s - t) / 2; return new ComplexNumber[] { new ComplexNumber(sum - constant, 0), new ComplexNumber(real, imaginary), new ComplexNumber(real, -imaginary) }; }
From source file:uk.bl.wa.analyser.text.SentimentJTextAnalyser.java
/** * /* w w w . j av a 2s .c o m*/ */ public void analyse(String text, SolrRecord solr) { // Sentiment Analysis: int sentilen = 10000; if (sentilen > text.length()) sentilen = text.length(); String sentitext = text.substring(0, sentilen); // metadata.get(HtmlFeatureParser.FIRST_PARAGRAPH); Sentiment senti = sentij.analyze(sentitext); double sentilog = Math.signum(senti.getComparative()) * (Math.log(1.0 + Math.abs(senti.getComparative())) / 40.0); int sentii = (int) (SolrFields.SENTIMENTS.length * (0.5 + sentilog)); if (sentii < 0) { log.debug("Caught a sentiment rating less than zero: " + sentii + " from " + sentilog); sentii = 0; } if (sentii >= SolrFields.SENTIMENTS.length) { log.debug("Caught a sentiment rating too large to be in range: " + sentii + " from " + sentilog); sentii = SolrFields.SENTIMENTS.length - 1; } // if( sentii != 3 ) // log.debug("Got sentiment: " + sentii+" "+sentilog+" "+ SolrFields.SENTIMENTS[sentii] ); // Map to sentiment scale: solr.addField(SolrFields.SENTIMENT, SolrFields.SENTIMENTS[sentii]); solr.addField(SolrFields.SENTIMENT_SCORE, "" + senti.getComparative()); }