List of usage examples for java.lang Math max
@HotSpotIntrinsicCandidate public static double max(double a, double b)
From source file:com.gordoni.opal.BiInterpolator.java
public double value(double[] p) { double x = p[0]; x = Math.max(x, mp.floor[0]); x = Math.min(x, mp.ceiling[0]); double y = p[1]; y = Math.max(y, mp.floor[1]); y = Math.min(y, mp.ceiling[1]); double v = f.value(x, y); int xindex = Arrays.binarySearch(xval, x); if (xindex < 0) xindex = -xindex - 2;// ww w . j a v a 2 s .c o m int yindex = Arrays.binarySearch(yval, y); if (yindex < 0) yindex = -yindex - 2; double fmin = fval[xindex][yindex]; double fmax = fval[xindex][yindex]; if (xindex + 1 < xval.length) { fmin = Math.min(fmin, fval[xindex + 1][yindex]); fmax = Math.max(fmax, fval[xindex + 1][yindex]); } if (yindex + 1 < yval.length) { fmin = Math.min(fmin, fval[xindex][yindex + 1]); fmax = Math.max(fmax, fval[xindex][yindex + 1]); } if (xindex + 1 < xval.length && yindex + 1 < yval.length) { fmin = Math.min(fmin, fval[xindex + 1][yindex + 1]); fmax = Math.max(fmax, fval[xindex + 1][yindex + 1]); } v = Math.max(v, fmin); v = Math.min(v, fmax); return v; }
From source file:com.opengamma.analytics.financial.model.option.pricing.analytic.formula.AmericanVanillaOption.java
/** * Computes the pay-off for a spot price at expiry. * @param spot The spot price.//from ww w. jav a 2 s . c om * @return The pay-off. */ public double getPayoff(final double spot) { return isCall() ? Math.max(0, spot - _strike) : Math.max(0, _strike - spot); }
From source file:com.gordoni.opal.UniInterpolator.java
public double value(double[] p) { double x = p[0]; x = Math.max(x, mp.floor[0]); x = Math.min(x, mp.ceiling[0]); double v = f.value(x); // Bound value by surrounding knot values. Otherwise get bad results if metric_sm is non-monotone in p. int xindex = Arrays.binarySearch(xval, x); if (xindex < 0) xindex = -xindex - 2;// w w w . ja v a 2 s .c o m double fmin = fval[xindex]; double fmax = fval[xindex]; if (xindex + 1 < xval.length) { fmin = Math.min(fmin, fval[xindex + 1]); fmax = Math.max(fmax, fval[xindex + 1]); } v = Math.max(v, fmin); v = Math.min(v, fmax); return v; }
From source file:org.matsim.contrib.drt.analysis.DensityScatterPlots.java
public static JFreeChart createPlot(String title, String xAxisLabel, String yAxisLabel, XYSeries series, Pair<Double, Double> lineCoeffs) { XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(series);//from w w w.j a va 2 s . c om double maxValue = Math.max(series.getMaxX(), series.getMaxY()); // y=x XYSeries lineXY = new XYSeries("y = x"); lineXY.add(0, 0); lineXY.add(maxValue, maxValue); dataset.addSeries(lineXY); if (lineCoeffs != null) { // a*y+b=x double a = lineCoeffs.getLeft(); double b = lineCoeffs.getRight(); String namePrefix = a == 0 ? "" : (a + " * y + "); XYSeries lineABXY = new XYSeries(namePrefix + b + " = x"); lineABXY.add(b, 0); if (a == 0) { lineABXY.add(b, maxValue); } else { lineABXY.add(maxValue, (maxValue - b) / a); } dataset.addSeries(lineABXY); } final JFreeChart chart = ChartFactory.createScatterPlot(title, xAxisLabel, yAxisLabel, dataset); XYPlot xyPlot = (XYPlot) chart.getPlot(); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyPlot.getRenderer(0); renderer.setSeriesPaint(0, new Color(255, 0, 0, 50)); renderer.setSeriesShape(0, CIRCLE); renderer.setSeriesLinesVisible(0, false); renderer.setSeriesShapesVisible(0, true); renderer.setSeriesVisibleInLegend(0, false); for (int i = 1; i < dataset.getSeriesCount(); i++) { renderer.setSeriesPaint(i, new Color(0, 0, 0)); renderer.setSeriesLinesVisible(i, true); renderer.setSeriesShapesVisible(i, false); renderer.setSeriesVisibleInLegend(i, false); } xyPlot.getDomainAxis().setUpperBound(maxValue); xyPlot.getRangeAxis().setUpperBound(maxValue); xyPlot.getDomainAxis().setLowerBound(0); xyPlot.getRangeAxis().setLowerBound(0); return chart; }
From source file:Main.java
/** * Convert HSL values to a RGB Color.//from ww w. java 2 s . c o m * * @param h Hue is specified as degrees in the range 0 - 360. * @param s Saturation is specified as a percentage in the range 1 - 100. * @param l Lumanance is specified as a percentage in the range 1 - 100. * @paran alpha the alpha value between 0 - 1 * adapted from https://svn.codehaus.org/griffon/builders/gfxbuilder/tags/GFXBUILDER_0.2/ * gfxbuilder-core/src/main/com/camick/awt/HSLColor.java */ @SuppressWarnings("PMD.MethodNamingConventions") public static int[] HSLtoRGB(float h, float s, float l, float alpha) { if (s < 0.0f || s > 100.0f) { String message = "Color parameter outside of expected range - Saturation"; throw new IllegalArgumentException(message); } if (l < 0.0f || l > 100.0f) { String message = "Color parameter outside of expected range - Luminance"; throw new IllegalArgumentException(message); } if (alpha < 0.0f || alpha > 1.0f) { String message = "Color parameter outside of expected range - Alpha"; throw new IllegalArgumentException(message); } // Formula needs all values between 0 - 1. h = h % 360.0f; h /= 360f; s /= 100f; l /= 100f; float q = 0; if (l < 0.5) { q = l * (1 + s); } else { q = (l + s) - (s * l); } float p = 2 * l - q; int r = Math.round(Math.max(0, HueToRGB(p, q, h + (1.0f / 3.0f)) * 256)); int g = Math.round(Math.max(0, HueToRGB(p, q, h) * 256)); int b = Math.round(Math.max(0, HueToRGB(p, q, h - (1.0f / 3.0f)) * 256)); return new int[] { r, g, b }; }
From source file:it.univaq.incipict.profilemanager.common.utility.Utility.java
public static HashMap<Profile, Double> getEuclideanDistances(List<Profile> profilesList, User user) { Map<Profile, Double> result = new HashMap<Profile, Double>(); Set<ProfileInformation> profileInformationSet; Set<Information> userInformationSet; // Retrieve user information set userInformationSet = user.getInformationSet(); if (userInformationSet.isEmpty()) { return (HashMap<Profile, Double>) result; }/*from w w w. ja v a2 s . c om*/ // For each Profile for (Profile profile : profilesList) { profileInformationSet = profile.getProfileInformationSet(); int vectorsLenght = Math.max(profileInformationSet.size(), userInformationSet.size()); RealVector ranksRealVector = new ArrayRealVector(new double[] {}); RealVector userInformationVector = new ArrayRealVector(new double[] {}); // Loop userInformationSet and // profileInformationSet (i.e. one specific column vector in the // knowledge base representation) for (Information information : userInformationSet) { Long x = information.getId(); for (ProfileInformation profileInformation : profileInformationSet) { Long y = profileInformation.getInformation().getId(); // User selected information was stored in a RealVector at same // position of relative ranksVector // This permit to calculate Euclidean distance right. if (x == y) { userInformationVector = userInformationVector.append(1d); // Associated:1, Else:0 ranksRealVector = ranksRealVector.append(profileInformation.getRank()); profileInformationSet.remove(profileInformation); break; } } } // At this point we aren't interested to elements position // because every other information worth zero. // Euclidean distance are not influenced from position of 0-elements in // a "sub-vector". // if they are all zeros. // => Append the zeros until completion of the length of the vectors userInformationVector = userInformationVector .append(new double[vectorsLenght - userInformationSet.size()]); for (ProfileInformation profileInformation : profileInformationSet) { // Append the remaining elements of this set (profileInformationSet) ranksRealVector = ranksRealVector.append(profileInformation.getRank()); } // Calculate Euclidean Distance double distance = userInformationVector.getDistance(ranksRealVector); // add the distance to Distance's Map result.put(profile, distance); } // END, goto Next Profile // return the HashMap sorted by value (ASC) return (HashMap<Profile, Double>) MapUtil.sortByValueASC(result); }
From source file:Main.java
private void initComponents() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextPane textPane = new JTextPane(); ((AbstractDocument) textPane.getDocument()).addDocumentListener(new DocumentListener() { @Override/*from w w w . j av a 2 s. c o m*/ public void insertUpdate(final DocumentEvent de) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { StyledDocument doc = (StyledDocument) de.getDocument(); int start = Utilities.getRowStart(textPane, Math.max(0, de.getOffset() - 1)); int end = Utilities.getWordStart(textPane, de.getOffset() + de.getLength()); String text = doc.getText(start, end - start); for (String emoticon : imageTokens) { int i = text.indexOf(emoticon); while (i >= 0) { final SimpleAttributeSet attrs = new SimpleAttributeSet( doc.getCharacterElement(start + i).getAttributes()); if (StyleConstants.getIcon(attrs) == null) { switch (emoticon) { case imageToken: StyleConstants.setIcon(attrs, anImage); break; } doc.remove(start + i, emoticon.length()); doc.insertString(start + i, emoticon, attrs); } i = text.indexOf(emoticon, i + emoticon.length()); } } } catch (BadLocationException ex) { ex.printStackTrace(); } } }); } @Override public void removeUpdate(DocumentEvent e) { } @Override public void changedUpdate(DocumentEvent e) { } }); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setPreferredSize(new Dimension(300, 300)); frame.add(scrollPane); frame.pack(); frame.setVisible(true); }
From source file:com.itemanalysis.psychometrics.statistics.NormalDensity.java
public double[][] value(double min, double max, int numPoints) { numPoints = Math.max(1, numPoints); double[][] density = new double[numPoints][numPoints]; double increment = (max - min) / (numPoints - 1); double val = min; double densitySum = 0.0; for (int i = 0; i < numPoints; i++) { density[i][0] = val; density[i][1] = normal.density(val); densitySum += density[i][1];// ww w.j a v a 2 s. co m val += increment; } //make sure probabilities sum to unity for (int i = 0; i < numPoints; i++) { density[i][1] = density[i][1] / densitySum; } return density; }
From source file:com.gordoni.opal.LSInterpolator.java
public double value(double[] p) { double x = p[0]; x = Math.max(x, mp.floor[0]); x = Math.min(x, mp.ceiling[0]); double y = p[1]; y = Math.max(y, mp.floor[1]); y = Math.min(y, mp.ceiling[1]); if (!linear_spline) { // spline-linear. double tmp = x; x = y;/*w w w . j ava2 s . co m*/ y = tmp; } int xindex = Arrays.binarySearch(xval, x); if (xindex < 0) xindex = -xindex - 2; int x1; int x2; if (xindex + 1 > xval.length - 1) { x1 = xindex; x2 = xindex - 1; } else { x1 = xindex; x2 = xindex + 1; } double v1 = f[x1].value(y); double v2 = f[x2].value(y); double v = v1 + (x - xval[x1]) / (xval[x2] - xval[x1]) * (v2 - v1); int yindex = Arrays.binarySearch(yval, y); if (yindex < 0) yindex = -yindex - 2; double fmin = Math.min(fval[x1][yindex], fval[x2][yindex]); double fmax = Math.max(fval[x1][yindex], fval[x2][yindex]); if (yindex + 1 < yval.length) { fmin = Math.min(fmin, fval[x1][yindex + 1]); fmin = Math.min(fmin, fval[x2][yindex + 1]); fmax = Math.max(fmax, fval[x1][yindex + 1]); fmax = Math.max(fmax, fval[x2][yindex + 1]); } v = Math.max(v, fmin); v = Math.min(v, fmax); return v; }
From source file:Main.java
/** * Calculates the optimal width for the column of the given table. The * calculation is based on the preferred width of the header and cell * renderer./*from w w w . j a v a 2s . c om*/ * <br> * Taken from the newsgoup de.comp.lang.java with some modifications.<br> * Taken from FOPPS/EnhancedTable - http://fopps.sourceforge.net/<br> * * @param table the table to calculate the column width * @param col the column to calculate the widths * @return the width, -1 if error */ public static int calcColumnWidth(JTable table, int col) { int width = calcHeaderWidth(table, col); if (width == -1) return width; TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int rowCount = data.getRowCount(); TableColumn column = columns.getColumn(col); try { for (int row = rowCount - 1; row >= 0; --row) { Component c = table.prepareRenderer(table.getCellRenderer(row, col), row, col); width = Math.max(width, c.getPreferredSize().width + 10); } } catch (Exception e) { e.printStackTrace(); } return width; }