List of usage examples for java.lang Math signum
public static float signum(float f)
From source file:org.jcurl.demo.tactics.sg.BroomPromptScenario.java
public BroomPromptScenario() { // create the scene final boolean stickUp = false; final boolean bothSides = true; final int pieAngle = 150; final Color sp = Color.BLACK; final Color bgc = new Color(1, 1, 1, 0.5f); final Stroke fine = new BasicStroke(0.01f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); final Stroke bold = new BasicStroke(0.03f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); // final Font fo = new Font("SansSerif", Font.BOLD, 1); final float halo = RockProps.DEFAULT.getRadius(); final float outer = 0.8f * RockProps.DEFAULT.getRadius(); stickLength = (stickUp ? 1 : -1) * 5 * outer; final float inner = 0.5F * outer; final SGGroup me = new SGGroup(); // the transparent background {// w w w .ja v a2s .c o m final SGShape bg = node(new Arc2D.Float(-halo, -halo, 2 * halo, 2 * halo, 0, 360, Arc2D.OPEN), null, null, scale0); bg.setFillPaint(bgc); bg.addMouseListener(new MoveHandler()); bg.setMouseBlocker(true); bg.setCursor(moveC); me.add(bg); } // the cross-hair and stick { final int off = 90; final int pieOff = 180; final int arrowLengthDegrees = 7; // colored pie: pie = node(new Arc2D.Float(-outer, -outer, 2 * outer, 2 * outer, off - pieOff, pieAngle, Arc2D.PIE), null, null, scale0); me.add(pie); // inner circle: me.add(node(new Arc2D.Float(-inner, -inner, 2 * inner, 2 * inner, off, pieOff + pieAngle, Arc2D.OPEN), fine, sp, scale50)); // outer circle: me.add(node(new Arc2D.Float(-outer, -outer, 2 * outer, 2 * outer, off, pieOff + pieAngle - (14 + arrowLengthDegrees), Arc2D.OPEN), fine, sp, scale50)); // Semantic zooming: me.add(node(new Arc2D.Float(-outer, -outer, 2 * // outer, 2 * outer, // off, pieOff + pieAngle, Arc2D.OPEN), fine, sp, -scale50)); final double ar = Math.PI * (off + pieAngle) / 180.0; // radius // if (pieAngle % 90 != 0) me.add(node(new Line2D.Double(0, 0, -outer * Math.cos(ar), outer * Math.sin(ar)), bold, sp, scale0)); // arrow: final float f = outer / 10; final SGShape s = node(IceShapes.createArrowHead(f, 3 * f, 0.5f * f), null, null, scale50); s.setFillPaint(sp); final double a = Math.PI * (off + pieAngle - arrowLengthDegrees) / 180.0; final AffineTransform a_ = new AffineTransform(); a_.translate(-outer * Math.cos(a), outer * Math.sin(a)); a_.rotate(Math.PI * (90 - (off + pieAngle) + 8 + arrowLengthDegrees) / 180.0); final Affine s_ = SGTransform.createAffine(a_, s); me.add(s_); } { // y-axis: me.add(node(new Line2D.Float(0, -Math.signum(stickLength) * halo, 0, stickLength), fine, sp, scale0)); // x-axis: me.add(node(new Line2D.Float(-halo, 0, halo, 0), fine, sp, scale0)); } { // slider sli = new SGShape(); sli.setShape(IceShapes.createSlider(0.4f * outer, bothSides)); // sli.setFillPaint(sp); sli.setDrawStroke(fine); sli.setDrawPaint(sp); sli.setMode(Mode.STROKE_FILL); sli.setAntialiasingHint(RenderingHints.VALUE_ANTIALIAS_ON); me.add(slider = SGTransform.createAffine(new AffineTransform(), sli)); slider.setCursor(moveC); slider.addMouseListener(new SpeedHandler()); slider.setMouseBlocker(true); } handle = SGTransform.createAffine(new AffineTransform(), me); scene = SGTransform.createAffine(new AffineTransform(), handle); scene.setVisible(false); }
From source file:com.crazymin2.retailstore.ui.BaseActivity.java
/** * Indicates that the main content has scrolled (for the purposes of showing/hiding * the action bar for the "action bar auto hide" effect). currentY and deltaY may be exact * (if the underlying view supports it) or may be approximate indications: * deltaY may be INT_MAX to mean "scrolled forward indeterminately" and INT_MIN to mean * "scrolled backward indeterminately". currentY may be 0 to mean "somewhere close to the * start of the list" and INT_MAX to mean "we don't know, but not at the start of the list" *///from w w w . j av a 2s.c om private void onMainContentScrolled(int currentY, int deltaY) { if (deltaY > mActionBarAutoHideSensivity) { deltaY = mActionBarAutoHideSensivity; } else if (deltaY < -mActionBarAutoHideSensivity) { deltaY = -mActionBarAutoHideSensivity; } if (Math.signum(deltaY) * Math.signum(mActionBarAutoHideSignal) < 0) { // deltaY is a motion opposite to the accumulated signal, so reset signal mActionBarAutoHideSignal = deltaY; } else { // add to accumulated signal mActionBarAutoHideSignal += deltaY; } boolean shouldShow = currentY < mActionBarAutoHideMinY || (mActionBarAutoHideSignal <= -mActionBarAutoHideSensivity); autoShowOrHideActionBar(shouldShow); }
From source file:org.shaman.terrain.sketch.SketchTerrain_old.java
/** * Checks if the given sketch is a valid stroke * @param points/* w w w .ja v a 2 s. c o m*/ * @return {@code true} if it is valid */ private boolean isSketchValid(List<Vector2f> points) { if (points.size() < 2) { return false; } int dir = 0; for (int i = 1; i < points.size(); ++i) { Vector2f p1 = points.get(i - 1); Vector2f p2 = points.get(i); float dx = p2.x - p1.x; int d = (int) Math.signum(dx); if (d * dir == -1) { return false; //changed direction } dir = d != 0 ? d : dir; } if (dir < 0) { Collections.reverse(points); } return true; }
From source file:cn2.CN2.java
public static double computeK(double[][] a, int r, double theta) { double k = -Math.signum(a[r][r]) * Math.sqrt(theta); return k;// w ww . j av a 2s . c o m }
From source file:keel.Algorithms.Neural_Networks.IRPropPlus_Clas.IRPropPlus.java
/** * <p>/* ww w.j ava 2s. c o m*/ * Apply the iRprop+ over a function that implements the IOptimizableFunc * interface. * * @param f function to apply the iRprop+ method * @return double array with the coefficients optimized * </p> */ protected double[] solve(IOptimizableFunc f) { // Stop forced int stop = 0; // Number of epochs without improving error int epochsWithoutImproving = 0; // Actual gradients double[] actualGradient = null; // Last gradients double[] lastGradient = new double[this.a.length]; // Step-size double[] increaseStepSize = new double[this.a.length]; // Increases double[] increaseCoefficients = new double[this.a.length]; // Weights or Coefficients double[] coefficients = this.a; // Actual Error. One error by each input double actualError = 0.0; // Last Error double lastError = 0.0; // Initialization of arrays for (int i = 0; i < this.a.length; i++) { lastGradient[i] = increaseCoefficients[i] = 0.0; increaseStepSize[i] = this.initialStepSize; } while (stop < this.epochs) { // Actual gradient and error for each weight or coefficient actualGradient = f.gradient(this.x, this.y); actualError = f.getLastError(); //System.out.println("Epoch: " + stop + " Error: " + actualError); // Actualization of coefficients // All coefficients treated equally if (reducedStepSize == null) { for (int i = 0; i < this.a.length; i++) { if (lastGradient[i] * actualGradient[i] > 0) { increaseStepSize[i] = Math.min(increaseStepSize[i] * this.positiveEta, maximumDelta); increaseCoefficients[i] = -(Math.signum(actualGradient[i]) * increaseStepSize[i]); coefficients[i] = coefficients[i] + increaseCoefficients[i]; } else if (lastGradient[i] * actualGradient[i] == 0) { increaseCoefficients[i] = -(Math.signum(actualGradient[i]) * increaseStepSize[i]); coefficients[i] = coefficients[i] + increaseCoefficients[i]; } else // (lastGradient[i]*actualGradient[i] < 0) OR Infinite problems { increaseStepSize[i] = Math.max(increaseStepSize[i] * this.negativeEta, minimumDelta); if (actualError > lastError) coefficients[i] = coefficients[i] - increaseCoefficients[i]; actualGradient[i] = 0.0; } } //for } // Actualization of coefficients // Some coefficients treated as more sensible else { for (int i = 0; i < this.a.length; i++) { // Reduced step size double finalPositiveEta = this.positiveEta; double finalNegativeEta = this.negativeEta; if (reducedStepSize[i]) { finalPositiveEta = 1 + ((finalPositiveEta - 1) * 0.5); finalNegativeEta *= 0.5; } if (lastGradient[i] * actualGradient[i] > 0) { increaseStepSize[i] = Math.min(increaseStepSize[i] * finalPositiveEta, maximumDelta); increaseCoefficients[i] = -(Math.signum(actualGradient[i]) * increaseStepSize[i]); coefficients[i] = coefficients[i] + increaseCoefficients[i]; } else if (lastGradient[i] * actualGradient[i] == 0) { increaseCoefficients[i] = -(Math.signum(actualGradient[i]) * increaseStepSize[i]); coefficients[i] = coefficients[i] + increaseCoefficients[i]; } else // (lastGradient[i]*actualGradient[i] < 0) OR Infinite problems { increaseStepSize[i] = Math.max(increaseStepSize[i] * finalNegativeEta, minimumDelta); if (actualError > lastError) coefficients[i] = coefficients[i] - increaseCoefficients[i]; actualGradient[i] = 0.0; } } //for } if (lastError == actualError) { epochsWithoutImproving++; if (epochsWithoutImproving >= 20) stop = this.epochs; } else epochsWithoutImproving = 0; // Updating gradient and error for the next step for (int i = 0; i < this.a.length; i++) lastGradient[i] = actualGradient[i]; lastError = actualError; f.setCoefficients(coefficients); stop++; } //While // Return the result return a; }
From source file:org.dawnsci.plotting.tools.powdercheck.PowderCheckTool.java
private void update() { if (!onDialog) { if (getViewPart() == null) return; IWorkbenchPartSite site = getViewPart().getSite(); if (site == null || !site.getPage().isPartVisible(getViewPart())) return; }//from w w w .ja v a 2s . c o m IImageTrace im = getImageTrace(); logger.debug("Update"); if (im == null) { //cleanPlottingSystem(); return; } final Dataset ds = DatasetUtils.convertToDataset(im.getData()); if (ds == null) return; IDiffractionMetadata m = ds.getFirstMetadata(IDiffractionMetadata.class); if (m == null) { //TODO nicer error logger.error("No Diffraction Metadata"); return; } if (updatePlotJob == null) { updatePlotJob = new PowderCheckJob(system); updatePlotJob.addJobChangeListener(new JobChangeAdapter() { @Override public void done(IJobChangeEvent event) { Display.getDefault().syncExec(new Runnable() { @Override public void run() { List<PowderCheckResult> resultsList = updatePlotJob.getResultsList(); Collections.sort(resultsList, new Comparator<PowderCheckResult>() { @Override public int compare(PowderCheckResult o1, PowderCheckResult o2) { return (int) Math.signum(o1.getCalibrantQValue() - o2.getCalibrantQValue()); } }); if (viewer != null && !viewer.getTable().isDisposed()) viewer.setInput(resultsList); } }); } }); } updatePlotJob.cancel(); updatePlotJob.setAxisMode(xAxis); updatePlotJob.setCheckMode(PowderCheckMode.FullImage); updatePlotJob.setData(ds, (IDiffractionMetadata) m); if (fullImage != null) fullImage.run(); }
From source file:com.opengamma.analytics.math.interpolation.PiecewiseCubicHermiteSplineInterpolatorWithSensitivity.java
/** * First derivative at end point and its sensitivity to delta * @param h1// w w w . java2 s . c o m * @param h2 * @param y1 * @param y2 * @param y3 * @return array of length 4 - the first element contains d, while the other three are sensitivities to the ys */ private double[] endpointSlope(final double h1, final double h2, final double del1, final double del2, final boolean rightSide) { final double[] res = new double[4]; if (del1 == 0.0) { // quick exist for particular edge case // d and dDy3 are both zero - no need to explicitly set if (del2 == 0) { res[1] = -(2 * h1 + h2) / h1 / (h1 + h2); if (h1 > 2 * h2) { res[2] = 3 / h1; } else { res[2] = (h1 + h2) / h1 / h2; } } else { res[1] = -1.5 / h1; res[2] = -res[1]; } if (rightSide) { res[1] = -res[1]; res[2] = -res[2]; } return res; } // This value is used in the clauses - may not be the returned value final double d = ((2. * h1 + h2) * del1 - h1 * del2) / (h1 + h2); if (Math.signum(d) != Math.signum(del1)) { // again d is now set to zero if (Math.abs(d) < 1e-15) { res[1] = -(2 * h1 + h2) / h1 / (h1 + h2); res[2] = (h1 + h2) / h1 / h2; res[3] = -h1 / h2 / (h1 + h2); } } else if (Math.signum(del1) != Math.signum(del2) && Math.abs(d) > 3. * Math.abs(del1)) { res[0] = 3 * del1; res[1] = -3 / h1; res[2] = -res[1]; } else { res[0] = d; res[1] = -(2 * h1 + h2) / h1 / (h1 + h2); res[2] = (h1 + h2) / h1 / h2; res[3] = -h1 / h2 / (h1 + h2); } if (rightSide) { for (int i = 1; i < 4; i++) { res[i] = -res[i]; } } return res; }
From source file:org.powertac.samplebroker.MarketManagerService.java
/** * Computes a limit price with a random element. *///from w w w .j ava2 s. c om private Double computeLimitPrice(int timeslot, double amountNeeded) { log.debug("Compute limit for " + amountNeeded + ", timeslot " + timeslot); // start with default limits Double oldLimitPrice; double minPrice; if (amountNeeded > 0.0) { // buying oldLimitPrice = buyLimitPriceMax; minPrice = buyLimitPriceMin; } else { // selling oldLimitPrice = sellLimitPriceMax; minPrice = sellLimitPriceMin; } // check for escalation Order lastTry = lastOrder.get(timeslot); if (lastTry != null) log.debug("lastTry: " + lastTry.getMWh() + " at " + lastTry.getLimitPrice()); if (lastTry != null && Math.signum(amountNeeded) == Math.signum(lastTry.getMWh())) { oldLimitPrice = lastTry.getLimitPrice(); log.debug("old limit price: " + oldLimitPrice); } // set price between oldLimitPrice and maxPrice, according to number of // remaining chances we have to get what we need. double newLimitPrice = minPrice; // default value int current = timeslotRepo.currentSerialNumber(); int remainingTries = (timeslot - current - Competition.currentCompetition().getDeactivateTimeslotsAhead()); log.debug("remainingTries: " + remainingTries); if (remainingTries > 0) { double range = (minPrice - oldLimitPrice) * 2.0 / (double) remainingTries; log.debug("oldLimitPrice=" + oldLimitPrice + ", range=" + range); double computedPrice = oldLimitPrice + randomGen.nextDouble() * range; return Math.max(newLimitPrice, computedPrice); } else return null; // market order }
From source file:com.almende.pi5.common.PowerTimeLine.java
private PowerTimeLine operation(operator op, PowerTimeLine other) { if (other.series.size() == 0) { return this; }/* w w w. j a va 2 s . c om*/ final long offset = new Duration(this.timestamp, other.timestamp).getMillis(); final ArrayList<PowerTime> result = new ArrayList<PowerTime>(); if (this.series.size() == 0) { for (PowerTime pt : other.series) { result.add(new PowerTime(pt.getOffset() + offset, op.doOp(0, pt.getValue()))); } this.series = result; return this; } PowerTime val_mine = null; PowerTime val_other = null; int index_mine = 0; int index_other = 0; double value_mine = 0; double value_other = 0; while (index_mine < this.series.size() && index_other < other.series.size()) { val_mine = this.series.get(index_mine); val_other = other.series.get(index_other); switch ((int) Math.signum(val_other.getOffset() + offset - val_mine.getOffset())) { case 1: value_mine = val_mine.getValue(); result.add(new PowerTime(val_mine.getOffset(), op.doOp(value_mine, value_other))); index_mine++; break; case 0: value_mine = val_mine.getValue(); value_other = val_other.getValue(); result.add(new PowerTime(val_mine.getOffset(), op.doOp(value_mine, value_other))); index_mine++; index_other++; break; case -1: value_other = val_other.getValue(); result.add(new PowerTime(val_other.getOffset() + offset, op.doOp(value_mine, value_other))); index_other++; break; } } for (int p = index_mine; p < this.series.size(); p++) { PowerTime pt = this.series.get(p); result.add(new PowerTime(pt.getOffset(), op.doOp(pt.getValue(), value_other))); } for (int p = index_other; p < other.series.size(); p++) { PowerTime pt = other.series.get(p); result.add(new PowerTime(pt.getOffset() + offset, op.doOp(value_mine, pt.getValue()))); } this.series = result; return this; }
From source file:com.facebook.presto.operator.scalar.MathFunctions.java
@Description("round to integer by dropping digits after decimal point") @ScalarFunction/*from w w w . j a va2s . c om*/ @SqlType(StandardTypes.DOUBLE) public static double truncate(@SqlType(StandardTypes.DOUBLE) double num) { return Math.signum(num) * Math.floor(Math.abs(num)); }