List of usage examples for java.lang Math max
@HotSpotIntrinsicCandidate public static double max(double a, double b)
From source file:RadialLayout.java
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { Point maxDimensions = calculateMaxDimensions(composite.getChildren()); int stepsPerHemisphere = stepsPerHemisphere(composite.getChildren().length); int maxWidth = maxDimensions.x; int maxHeight = maxDimensions.y; int dimensionMultiplier = (stepsPerHemisphere + 1); int controlWidth = maxWidth * dimensionMultiplier; int controlHeight = maxHeight * dimensionMultiplier; int diameter = Math.max(controlWidth, controlHeight); Point preferredSize = new Point(diameter, diameter); if (wHint != SWT.DEFAULT) { if (preferredSize.x > wHint) { preferredSize.x = wHint;/*www .j a v a 2s . c o m*/ } } if (hHint != SWT.DEFAULT) { if (preferredSize.y > hHint) { preferredSize.y = hHint; } } return preferredSize; }
From source file:bots.mctsbot.ai.bots.util.Gaussian.java
public final static Gaussian maxOf(Gaussian g1, Gaussian g2) { double a = Math.sqrt(g1.variance + g2.variance); if (a == 0) { return new Gaussian(Math.max(g1.mean, g2.mean), 0); }//from w w w . j a v a 2 s .c om double alpha = (g1.mean - g2.mean) / a; double bigPhiAlpha = bigPhi(alpha); double bigPhiMinAlpha = 1 - bigPhiAlpha; double smallPhiAlpha = smallPhi(alpha); double aSmallPhiAlpha = a * smallPhiAlpha; double mean = g1.mean * bigPhiAlpha + g2.mean * bigPhiMinAlpha + aSmallPhiAlpha; double stddev = (g1.mean * g1.mean + g1.variance) * bigPhiAlpha + (g2.mean * g2.mean + g2.variance) * bigPhiMinAlpha + (g1.mean + g2.mean) * aSmallPhiAlpha - mean * mean; return new Gaussian(mean, Math.max(0, stddev)); }
From source file:BloomFilter.java
/** * Builds a Bloom filter with the optimum length * /*from w w w. j a v a2 s . c o m*/ * @param members * The members to enter into the filter * @param hashCount * The number of hashes to use * @return An optimally-sized filter that contains the specified * elements */ public static BloomFilter buildFilter(int[] members, int hashCount) { int filterLength = (int) (members.length * hashCount / BloomFilter.bloomMagic); filterLength = Math.max(filterLength, 1); BloomFilter filter = new BloomFilter(filterLength, hashCount); for (int i : members) { filter.insert(i); } return filter; }
From source file:Main.java
public static int availableProcessors() { return Math.max(1, Runtime.getRuntime().availableProcessors()); }
From source file:endrov.nucAutoJH.FitGaussian.java
private static double[] fitGaussian2D_(EvPixels p, double sigmaInit, final double midxInit, final double midyInit) { //sigma00, sigma01, sigma11, mu_x, mu_y, c p = p.getReadOnly(EvPixelsType.DOUBLE); final double[] arrPixels = p.getArrayDouble(); final int w = p.getWidth(); final int h = p.getHeight(); int extent = (int) Math.round(3 * sigmaInit); extent = Math.max(extent, 2); final int sx = Math.max(0, (int) (midxInit - extent)); final int ex = Math.min(w, (int) (midxInit + extent + 1)); //+1 to the right? final int sy = Math.max(0, (int) (midyInit - extent)); final int ey = Math.min(h, (int) (midyInit + extent + 1)); double minIntensity = Double.MAX_VALUE; double maxIntensity = Double.MIN_VALUE; for (int y = sy; y < ey; y++) { int base = y * w; double dy2 = y - midyInit; dy2 = dy2 * dy2;/*from www . j a va 2s . c o m*/ for (int x = sx; x < ex; x++) { double dx2 = x - midxInit; dx2 = dx2 * dx2; double t = arrPixels[base + x]; //if(dx2+dy2<=extent*extent) { if (t < minIntensity) minIntensity = t; if (t > maxIntensity) maxIntensity = t; } } } //double[] weights=new double[]{1}; double[] startPoint = new double[] { sigmaInit, 0, sigmaInit, midxInit, midyInit, minIntensity, maxIntensity - minIntensity }; //double[] output=new double[startPoint.length]; try { MultivariateRealFunction func = new MultivariateRealFunction() { // opt.optimize( public double value(double[] arg) throws FunctionEvaluationException, IllegalArgumentException { double sigma00 = arg[0]; double sigma01 = arg[1]; double sigma11 = arg[2]; double mu0 = arg[3]; double mu1 = arg[4]; double C = arg[5]; double D = arg[6]; double sumError = 0; Matrix2d sigma = new Matrix2d(sigma00, sigma01, sigma01, sigma11); Matrix2d sigmaInv = new Matrix2d(); sigma.invert(sigmaInv); double sigmaDet = sigma.determinant(); double front = 1.0 / (2 * Math.PI * Math.sqrt(sigmaDet)); //System.out.println("front: "+front); //System.out.println("sigma inv "+sigmaInv); if (mu0 < sx || mu0 > ex) sumError += 1000000; if (mu1 < sy || mu1 > ey) sumError += 1000000; if (sigma00 < 1) sumError += 1000000; //if(sigma01<0) sumError+=1000000; if (sigma11 < 1) sumError += 1000000; if (D <= 0) sumError += 1000000; for (int y = sy; y < ey; y++) { int base = y * w; double dy2 = y - midyInit; dy2 = dy2 * dy2; for (int x = sx; x < ex; x++) { double dx2 = x - midxInit; dx2 = dx2 * dx2; double thisReal = arrPixels[base + x]; // if(dx2+dy2<=extent*extent) { // DoubleMatrix2D sigma=new DenseDoubleMatrix2D(new double[][]{{sigma00,sigma01},{sigma01,sigma11}}); //double sigmaDet=sigma00*sigma11-sigma01*sigma01; double dx0 = x - mu0; double dx1 = y - mu1; //http://en.wikipedia.org/wiki/Multivariate_normal_distribution Vector2d vX = new Vector2d(dx0, dx1); Vector2d op = new Vector2d(vX); sigmaInv.transform(op); double upper = -0.5 * op.dot(vX); double exp = Math.exp(upper); //System.out.println("front "+front+" "+exp+" C "+C+" thisreal"+thisReal+" upper "+upper); if (upper > -0.4) exp = 1; else exp = Math.max(0, 1 + upper + 0.4); /* if(exp<0.7) exp=0; else exp=1; */ double thisExpected = D * front * exp + C; double diff = thisExpected - thisReal; sumError += diff * diff; } } } //System.out.println(sigma00+"\t"+sigma01+"\t"+sigma11+"\tC"+C+"\tmu "+mu0+","+mu1+"\terr "+sumError); return sumError; // return new double[]{sumError}; } }; NelderMead opt = new NelderMead(); //LevenbergMarquardtOptimizer opt=new LevenbergMarquardtOptimizer(); opt.setMaxIterations(10000); RealPointValuePair pair = opt.optimize(func, GoalType.MINIMIZE, startPoint); int numit = opt.getIterations(); System.out.println("#it " + numit); System.out.println("err " + func.value(pair.getPointRef())); return pair.getPointRef(); // for(int i=0;i<startPoint.length;i++) // System.out.println("i: "+i+" "+output[i]); //, output, weights, startPoint); } /* catch (MaxIterationsExceededException e) { System.out.println("max it reached"); }*/ catch (Exception e) { e.printStackTrace(); } //Maybe this is a bad point? System.out.println("max it reached"); return startPoint; // return output; }
From source file:Win.java
public static void position(Window frame, Component ref, double xfrac, double yfrac) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension size = frame.getSize(); Dimension refSize = (ref != null) ? ref.getSize() : screenSize; Point origin = (ref != null) ? ref.getLocationOnScreen() : new Point(0, 0); int x = origin.x + relativePoint(xfrac, refSize.width, size.width); int y = origin.y + relativePoint(yfrac, refSize.height, size.height); // make sure frame is entirely on screen x = Math.max(0, Math.min(screenSize.width - size.width, x)); y = Math.max(0, Math.min(screenSize.height - size.height, y)); frame.setLocation(x, y);/*from w ww . j a v a 2 s . c o m*/ }
From source file:Main.java
private static int getDoubleChars(double number, char[] buf, int charPos, int significantDigits) { if (number != number) { STR_NAN.getChars(0, STR_NAN_LEN, buf, charPos); return charPos + STR_NAN_LEN; }//from w w w.ja va 2s. c om //we need to detect -0.0 to be compatible with JDK boolean negative = (Double.doubleToRawLongBits(number) & 0x8000000000000000L) != 0; if (negative) { buf[charPos++] = '-'; number = -number; } if (number == Double.POSITIVE_INFINITY) { STR_INFINITY.getChars(0, STR_INFINITY_LEN, buf, charPos); return charPos + STR_INFINITY_LEN; } if (number == 0) { buf[charPos++] = '0'; buf[charPos++] = '.'; buf[charPos++] = '0'; } else { int exponent = 0; // calc. the power (base 10) for the given number: int pow = (int) Math.floor(Math.log(number) / LN10); // use exponential formatting if number too big or too small if (pow < -3 || pow > 6) { exponent = pow; number /= Math.exp(LN10 * exponent); } // if // Recalc. the pow if exponent removed and d has changed pow = (int) Math.floor(Math.log(number) / LN10); // Decide how many insignificant zeros there will be in the // lead of the number. int insignificantDigits = -Math.min(0, pow); // Force it to start with at least "0." if necessarry pow = Math.max(0, pow); double divisor = Math.pow(10, pow); // Loop over the significant digits (17 for double, 8 for float) for (int i = 0, end = significantDigits + insignificantDigits, div; i < end; i++) { // Add the '.' when passing from 10^0 to 10^-1 if (pow == -1) { buf[charPos++] = '.'; } // if // Find the divisor div = (int) (number / divisor); // This might happen with 1e6: pow = 5 ( instead of 6 ) if (div == 10) { buf[charPos++] = '1'; buf[charPos++] = '0'; } // if else { buf[charPos] = (char) (div + '0'); charPos++; } // else number -= div * divisor; divisor /= 10.0; pow--; // Break the loop if we have passed the '.' if (number == 0 && divisor < 0.1) break; } // for // Remove trailing zeros while (buf[charPos - 1] == '0') charPos--; // Avoid "4." instead of "4.0" if (buf[charPos - 1] == '.') charPos++; if (exponent != 0) { buf[charPos++] = 'E'; if (exponent < 0) { buf[charPos++] = '-'; exponent = -exponent; } if (exponent >= 100) buf[charPos++] = (char) (exponent / 100 + '0'); if (exponent >= 10) buf[charPos++] = (char) (exponent / 10 % 10 + '0'); buf[charPos++] = (char) (exponent % 10 + '0'); } // if } return charPos; }
From source file:Main.java
/** * Create a custom cursor out of the specified image, with the specified hotspot. *//*from www.j ava 2 s . c o m*/ public static Cursor createImageCursor(Image img, Point hotspot) { Toolkit tk = Toolkit.getDefaultToolkit(); // for now, just report the cursor restrictions, then blindly create int w = img.getWidth(null); int h = img.getHeight(null); Dimension d = tk.getBestCursorSize(w, h); // int colors = tk.getMaximumCursorColors(); // Log.debug("Creating custom cursor [desiredSize=" + w + "x" + h + // ", bestSize=" + d.width + "x" + d.height + // ", maxcolors=" + colors + "]."); // if the passed-in image is smaller, pad it with transparent pixels and use it anyway. if (((w < d.width) && (h <= d.height)) || ((w <= d.width) && (h < d.height))) { Image padder = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration().createCompatibleImage(d.width, d.height, Transparency.BITMASK); Graphics g = padder.getGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); // and reassign the image to the padded image img = padder; // and adjust the 'best' to cheat the hotspot checking code d.width = w; d.height = h; } // make sure the hotspot is valid if (hotspot == null) { hotspot = new Point(d.width / 2, d.height / 2); } else { hotspot.x = Math.min(d.width - 1, Math.max(0, hotspot.x)); hotspot.y = Math.min(d.height - 1, Math.max(0, hotspot.y)); } // and create the cursor return tk.createCustomCursor(img, hotspot, "samskivertDnDCursor"); }
From source file:de.sanandrew.mods.claysoldiers.util.soldier.upgrade.misc.enchantment.UpgradeWool.java
@Override public boolean onSoldierHurt(EntityClayMan clayMan, SoldierUpgradeInst upgradeInst, DamageSource source, MutableFloat damage) {//from w w w . j ava 2s. co m if (!source.isUnblockable()) { damage.setValue(Math.max(0.25F, damage.getValue() - 1.0F)); } return false; }
From source file:Main.java
/** * Returns a {@link Collection} containing the union * of the given {@link Collection}s./* ww w . java2 s . com*/ * <p/> * The cardinality of each element in the returned {@link Collection} * will be equal to the maximum of the cardinality of that element * in the two given {@link Collection}s. * * @param a the first collection, must not be null * @param b the second collection, must not be null * @return the union of the two collections15 * @see Collection#addAll */ public static <E> Collection<E> union(final Collection<? extends E> a, final Collection<? extends E> b) { ArrayList<E> list = new ArrayList<E>(); Map mapa = getCardinalityMap(a); Map mapb = getCardinalityMap(b); Set<E> elts = new HashSet<E>(a); elts.addAll(b); Iterator<E> it = elts.iterator(); while (it.hasNext()) { E obj = it.next(); for (int i = 0, m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) { list.add(obj); } } return list; }