List of usage examples for java.lang Number doubleValue
public abstract double doubleValue();
From source file:com.anhth12.optimize.solvers.BackTrackLineSearch.java
public double optimize(INDArray line, int lineSearchIteration, double initialStep, INDArray x, INDArray g) throws InvalidStepException { INDArray oldParameters;// www. ja v a2 s . c om double slope, test, alamin, alam, alam2, tmplam; double rhs1, rhs2, a, b, disc, oldAlam; double f, fold, f2; oldParameters = x.dup(); alam2 = 0.0; f2 = fold = optimizer.score(); if (logger.isDebugEnabled()) { logger.trace("ENTERING BACKTRACK\n"); logger.trace("Entering BackTrackLinnSearch, value = " + fold + ",\ndirection.oneNorm:" + line.norm1(Integer.MAX_VALUE) + " direction.infNorm:" + FastMath.max(Float.NEGATIVE_INFINITY, Transforms.abs(line).max(Integer.MAX_VALUE).getDouble(0))); } BooleanIndexing.applyWhere(g, new Or(Conditions.isNan(), Conditions.isInfinite()), new Value(Nd4j.EPS_THRESHOLD)); LinAlgExceptions.assertValidNum(g); double sum = line.norm2(Integer.MAX_VALUE).getDouble(0); if (sum > stpmax) { logger.warn("attempted step too big. scaling: sum= " + sum + ", stpmax= " + stpmax); line.muli(stpmax / sum); } //dot product slope = Nd4j.getBlasWrapper().dot(g, line); logger.debug("slope = " + slope); if (slope < 0) throw new InvalidStepException("Slope = " + slope + " is negative"); if (slope == 0) throw new InvalidStepException("Slope = " + slope + " is zero"); // find maximum lambda // converge when (delta x) / x < REL_TOLX for all coordinates. // the largest step size that triggers this threshold is // precomputed and saved in alamin INDArray maxOldParams = Transforms.abs(oldParameters); BooleanIndexing.applyWhere(maxOldParams, new Condition() { @Override public Boolean apply(Number input) { return input.doubleValue() < 1.0; } @Override public Boolean apply(IComplexNumber input) { return false; } }, new Value(1.0)); INDArray testMatrix = Transforms.abs(line).divi(maxOldParams); test = testMatrix.max(Integer.MAX_VALUE).getDouble(0); //no longer needed testMatrix = null; alamin = relTolx / test; alam = 1.0f; oldAlam = 0.0f; int iteration; // look for step size in direction given by "line" for (iteration = 0; iteration < maxIterations; iteration++) { // x = oldParameters + alam*line // initially, alam = 1.0, i.e. take full Newton step logger.trace("BackTrack loop iteration " + iteration + " : alam=" + alam + " oldAlam=" + oldAlam); logger.trace("before step, x.1norm: " + x.norm1(Integer.MAX_VALUE) + "\nalam: " + alam + "\noldAlam: " + oldAlam); assert (alam != oldAlam) : "alam == oldAlam"; if (stepFunction == null) stepFunction = new DefaultStepFunction(); stepFunction.step(x, line, new Object[] { alam, oldAlam }); //step double norm1 = x.norm1(Integer.MAX_VALUE).getDouble(0); logger.debug("after step, x.1norm: " + norm1); // check for convergence //convergence on delta x if ((alam < alamin) || smallAbsDiff(oldParameters, x)) { function.setParams(oldParameters); f = function.score(); logger.trace("EXITING BACKTRACK: Jump too small (alamin = " + alamin + "). Exiting and using xold. Value = " + f); return 0.0f; } function.setParams(x); oldAlam = alam; f = function.score(); logger.debug("value = " + f); // sufficient function increase (Wolf condition) if (f >= fold + ALF * alam * slope) { logger.debug("EXITING BACKTRACK: value=" + f); if (f < fold) throw new IllegalStateException( "Function did not increase: f = " + f + " < " + fold + " = fold"); return alam; } // if value is infinite, i.e. we've // jumped to unstable territory, then scale down jump else if (Double.isInfinite(f) || Double.isInfinite(f2)) { logger.warn("Value is infinite after jump " + oldAlam + ". f=" + f + ", f2=" + f2 + ". Scaling back step size..."); tmplam = .2f * alam; if (alam < alamin) { //convergence on delta x function.setParams(oldParameters); f = function.score(); logger.warn("EXITING BACKTRACK: Jump too small. Exiting and using xold. Value=" + f); return 0.0f; } } else { // backtrack if (alam == 1.0) // first time through tmplam = -slope / (2.0f * (f - fold - slope)); else { rhs1 = f - fold - alam * slope; rhs2 = f2 - fold - alam2 * slope; if ((alam - alam2) == 0) throw new IllegalStateException("FAILURE: dividing by alam-alam2. alam=" + alam); a = (rhs1 / (FastMath.pow(alam, 2)) - rhs2 / (FastMath.pow(alam2, 2))) / (alam - alam2); b = (-alam2 * rhs1 / (alam * alam) + alam * rhs2 / (alam2 * alam2)) / (alam - alam2); if (a == 0.0) tmplam = -slope / (2.0f * b); else { disc = b * b - 3.0f * a * slope; if (disc < 0.0) { tmplam = .5f * alam; } else if (b <= 0.0) tmplam = (-b + FastMath.sqrt(disc)) / (3.0f * a); else tmplam = -slope / (b + FastMath.sqrt(disc)); } if (tmplam > .5f * alam) tmplam = .5f * alam; // lambda <= .5 lambda_1 } } alam2 = alam; f2 = f; logger.debug("tmplam:" + tmplam); alam = Math.max(tmplam, .1f * alam); // lambda >= .1*Lambda_1 } return 0.0f; }
From source file:org.zenoss.metrics.reporter.ZenossMetricsReporter.java
@Override public void processGauge(MetricName name, Gauge<?> gauge, MetricBatch context) throws Exception { Object gaugeValue = gauge.value(); if (gaugeValue instanceof Number) { Number value = (Number) gaugeValue; addMetric(name, "value", value.doubleValue(), context); } else {// w ww. j a v a2 s. c o m LOG.debug("Un-reportable gauge %s; type: %s", name.getName(), gaugeValue.getClass()); } }
From source file:MutableDouble.java
/** * Constructs a new MutableDouble with the specified value. * //www. j a v a 2 s . c o m * @param value * a value. * @throws NullPointerException * if the object is null */ public MutableDouble(Number value) { super(); this.value = value.doubleValue(); }
From source file:com.bdaum.zoom.report.internal.jfree.custom.CylinderRenderer.java
/** * Draws a cylinder to represent one data item. * * @param g2 the graphics device.//from w w w .j a va2s. co m * @param state the renderer state. * @param dataArea the area for plotting the data. * @param plot the plot. * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param dataset the dataset. * @param row the row index (zero-based). * @param column the column index (zero-based). * @param pass the pass index. */ public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) { // check the value we are plotting... Number dataValue = dataset.getValue(row, column); if (dataValue == null) { return; } double value = dataValue.doubleValue(); Rectangle2D adjusted = new Rectangle2D.Double(dataArea.getX(), dataArea.getY() + getYOffset(), dataArea.getWidth() - getXOffset(), dataArea.getHeight() - getYOffset()); PlotOrientation orientation = plot.getOrientation(); double barW0 = calculateBarW0(plot, orientation, adjusted, domainAxis, state, row, column); double[] barL0L1 = calculateBarL0L1(value); if (barL0L1 == null) { return; // the bar is not visible } RectangleEdge edge = plot.getRangeAxisEdge(); float transL0 = (float) rangeAxis.valueToJava2D(barL0L1[0], adjusted, edge); float transL1 = (float) rangeAxis.valueToJava2D(barL0L1[1], adjusted, edge); float barL0 = Math.min(transL0, transL1); float barLength = Math.abs(transL1 - transL0); // draw the bar... GeneralPath bar = new GeneralPath(); Shape top = null; if (orientation == PlotOrientation.HORIZONTAL) { bar.moveTo((float) (barL0 + getXOffset() / 2), (float) barW0); bar.lineTo((float) (barL0 + barLength + getXOffset() / 2), (float) barW0); Arc2D arc = new Arc2D.Double(barL0 + barLength, barW0, getXOffset(), state.getBarWidth(), 90, 180, Arc2D.OPEN); bar.append(arc, true); bar.lineTo((float) (barL0 + getXOffset() / 2), (float) (barW0 + state.getBarWidth())); arc = new Arc2D.Double(barL0, barW0, getXOffset(), state.getBarWidth(), 270, -180, Arc2D.OPEN); bar.append(arc, true); bar.closePath(); top = new Ellipse2D.Double(barL0 + barLength, barW0, getXOffset(), state.getBarWidth()); } else { bar.moveTo((float) barW0, (float) (barL0 - getYOffset() / 2)); bar.lineTo((float) barW0, (float) (barL0 + barLength - getYOffset() / 2)); Arc2D arc = new Arc2D.Double(barW0, (barL0 + barLength - getYOffset()), state.getBarWidth(), getYOffset(), 180, 180, Arc2D.OPEN); bar.append(arc, true); bar.lineTo((float) (barW0 + state.getBarWidth()), (float) (barL0 - getYOffset() / 2)); arc = new Arc2D.Double(barW0, (barL0 - getYOffset()), state.getBarWidth(), getYOffset(), 0, -180, Arc2D.OPEN); bar.append(arc, true); bar.closePath(); top = new Ellipse2D.Double(barW0, barL0 - getYOffset(), state.getBarWidth(), getYOffset()); } Paint itemPaint = getItemPaint(row, column); if (getGradientPaintTransformer() != null && itemPaint instanceof GradientPaint) { GradientPaint gp = (GradientPaint) itemPaint; itemPaint = getGradientPaintTransformer().transform(gp, bar); } g2.setPaint(itemPaint); g2.fill(bar); if (itemPaint instanceof GradientPaint) { g2.setPaint(((GradientPaint) itemPaint).getColor2()); } else { g2.setPaint(PaintAlpha.darker(itemPaint)); // bd } if (top != null) { g2.fill(top); } if (isDrawBarOutline() && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) { g2.setStroke(getItemOutlineStroke(row, column)); g2.setPaint(getItemOutlinePaint(row, column)); g2.draw(bar); if (top != null) { g2.draw(top); } } CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column); if (generator != null && isItemLabelVisible(row, column)) { drawItemLabel(g2, dataset, row, column, plot, generator, bar.getBounds2D(), (value < 0.0)); } // collect entity and tool tip information... if (state.getInfo() != null) { EntityCollection entities = state.getEntityCollection(); if (entities != null) { String tip = null; CategoryToolTipGenerator tipster = getToolTipGenerator(row, column); if (tipster != null) { tip = tipster.generateToolTip(dataset, row, column); } String url = null; if (getItemURLGenerator(row, column) != null) { url = getItemURLGenerator(row, column).generateURL(dataset, row, column); } CategoryItemEntity entity = new CategoryItemEntity(bar.getBounds2D(), tip, url, dataset, dataset.getRowKey(row), dataset.getColumnKey(column)); entities.add(entity); } } }
From source file:org.jfree.data.xy.DefaultOHLCDataset.java
/** * Returns the low-value (as a double primitive) for an item within a * series.//from ww w .j ava 2 s . c om * * @param series the series (zero-based index). * @param item the item (zero-based index). * * @return The low-value. */ @Override public double getLowValue(int series, int item) { double result = Double.NaN; Number low = getLow(series, item); if (low != null) { result = low.doubleValue(); } return result; }
From source file:com.scit.sling.test.MockValueMap.java
@SuppressWarnings("unchecked") private <T> T convertType(Object o, Class<T> type) { if (o == null) { return null; }//from w w w . java2 s . c o m if (o.getClass().isArray() && type.isArray()) { if (type.getComponentType().isAssignableFrom(o.getClass().getComponentType())) { return (T) o; } else { // we need to convert the elements in the array Object array = Array.newInstance(type.getComponentType(), Array.getLength(o)); for (int i = 0; i < Array.getLength(o); i++) { Array.set(array, i, convertType(Array.get(o, i), type.getComponentType())); } return (T) array; } } if (o.getClass().isAssignableFrom(type)) { return (T) o; } if (String.class.isAssignableFrom(type)) { // Format dates if (o instanceof Calendar) { return (T) formatDate((Calendar) o); } else if (o instanceof Date) { return (T) formatDate((Date) o); } else if (o instanceof DateTime) { return (T) formatDate((DateTime) o); } return (T) String.valueOf(o); } else if (o instanceof DateTime) { DateTime dt = (DateTime) o; if (Calendar.class.isAssignableFrom(type)) { return (T) dt.toCalendar(Locale.getDefault()); } else if (Date.class.isAssignableFrom(type)) { return (T) dt.toDate(); } else if (Number.class.isAssignableFrom(type)) { return convertType(dt.getMillis(), type); } } else if (o instanceof Number && Number.class.isAssignableFrom(type)) { if (Byte.class.isAssignableFrom(type)) { return (T) (Byte) ((Number) o).byteValue(); } else if (Double.class.isAssignableFrom(type)) { return (T) (Double) ((Number) o).doubleValue(); } else if (Float.class.isAssignableFrom(type)) { return (T) (Float) ((Number) o).floatValue(); } else if (Integer.class.isAssignableFrom(type)) { return (T) (Integer) ((Number) o).intValue(); } else if (Long.class.isAssignableFrom(type)) { return (T) (Long) ((Number) o).longValue(); } else if (Short.class.isAssignableFrom(type)) { return (T) (Short) ((Number) o).shortValue(); } else if (BigDecimal.class.isAssignableFrom(type)) { return (T) new BigDecimal(o.toString()); } } else if (o instanceof Number && type.isPrimitive()) { final Number num = (Number) o; if (type == byte.class) { return (T) new Byte(num.byteValue()); } else if (type == double.class) { return (T) new Double(num.doubleValue()); } else if (type == float.class) { return (T) new Float(num.floatValue()); } else if (type == int.class) { return (T) new Integer(num.intValue()); } else if (type == long.class) { return (T) new Long(num.longValue()); } else if (type == short.class) { return (T) new Short(num.shortValue()); } } else if (o instanceof String && Number.class.isAssignableFrom(type)) { if (Byte.class.isAssignableFrom(type)) { return (T) new Byte((String) o); } else if (Double.class.isAssignableFrom(type)) { return (T) new Double((String) o); } else if (Float.class.isAssignableFrom(type)) { return (T) new Float((String) o); } else if (Integer.class.isAssignableFrom(type)) { return (T) new Integer((String) o); } else if (Long.class.isAssignableFrom(type)) { return (T) new Long((String) o); } else if (Short.class.isAssignableFrom(type)) { return (T) new Short((String) o); } else if (BigDecimal.class.isAssignableFrom(type)) { return (T) new BigDecimal((String) o); } } throw new NotImplementedException( "Can't handle conversion from " + o.getClass().getName() + " to " + type.getName()); }
From source file:org.jfree.data.xy.DefaultOHLCDataset.java
/** * Returns the high-value (as a double primitive) for an item within a * series.//from w w w . j a v a 2 s .c om * * @param series the series (zero-based index). * @param item the item (zero-based index). * * @return The high-value. */ @Override public double getHighValue(int series, int item) { double result = Double.NaN; Number high = getHigh(series, item); if (high != null) { result = high.doubleValue(); } return result; }
From source file:com.amazonaws.metrics.internal.cloudwatch.PredefinedMetricTransformer.java
protected List<MetricDatum> metricOfCount(Field metricType, Request<?> req, Object resp) { AWSRequestMetrics m = req.getAWSRequestMetrics(); TimingInfo ti = m.getTimingInfo();/*from w w w .j a v a 2 s. c om*/ Number counter = ti.getCounter(metricType.name()); if (counter == null) { return Collections.emptyList(); } final double count = counter.doubleValue(); if (count < 1) { return Collections.emptyList(); } else { return Collections.singletonList(new MetricDatum().withMetricName(req.getServiceName()) .withDimensions( new Dimension().withName(Dimensions.MetricType.name()).withValue(metricType.name())) .withUnit(StandardUnit.Count).withValue(Double.valueOf(count)).withTimestamp(endTimestamp(ti))); } }
From source file:org.jfree.eastwood.GValueAxis.java
/** * Creates and returns a list of ticks to display for the * axis./* w w w. j a v a 2 s . co m*/ * * @param g2 the graphics target. * @param state * @param dataArea * @param edge * * @return The list. */ public List refreshTicks(Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge) { if (this.tickLabels.isEmpty()) { // here we auto-generate the axis labels using a non-visible axis // first we must align the axis range boolean inverted = (this.labelAxisStart > this.labelAxisEnd); double range = this.labelAxisEnd - this.labelAxisStart; double v0 = this.labelAxisStart + getLowerBound() * range; double v1 = this.labelAxisStart + getUpperBound() * range; this.axisForAutoLabels.setRange(Math.min(v0, v1), Math.max(v0, v1)); this.axisForAutoLabels.setInverted(inverted); List ticks = this.axisForAutoLabels.refreshTicks(g2, state, dataArea, edge); // now we must 'normalise' the tick positions into the range // 0.0 to 1.0 List normalisedTicks = new java.util.ArrayList(); double min = Math.min(this.labelAxisStart, this.labelAxisEnd); double max = Math.max(this.labelAxisStart, this.labelAxisEnd); Iterator iterator = ticks.iterator(); while (iterator.hasNext()) { NumberTick tick = (NumberTick) iterator.next(); double v = tick.getValue(); double vv = (v - min) / (max - min); if (this.axisForAutoLabels.isInverted()) { vv = 1.0 - vv; } normalisedTicks.add(new NumberTick(new Double(vv), tick.getText(), tick.getTextAnchor(), tick.getRotationAnchor(), tick.getAngle())); } return normalisedTicks; } List result = new java.util.ArrayList(); int labelCount = this.tickLabels.size(); int positionCount = this.tickLabelPositions.size(); int tickCount = Math.max(labelCount, positionCount); // work out the label anchor points according to which side of the // chart the axis lies on... TextAnchor anchor = null; TextAnchor rotationAnchor = TextAnchor.CENTER; if (edge == RectangleEdge.LEFT) { anchor = TextAnchor.CENTER_RIGHT; } else if (edge == RectangleEdge.BOTTOM) { anchor = TextAnchor.TOP_CENTER; } else if (edge == RectangleEdge.TOP) { anchor = TextAnchor.BOTTOM_CENTER; } else if (edge == RectangleEdge.RIGHT) { anchor = TextAnchor.CENTER_LEFT; } for (int i = 0; i < tickCount; i++) { String tickLabel = null; if (i < labelCount) { tickLabel = this.tickLabels.get(i).toString(); } else { tickLabel = String.valueOf(this.tickLabelPositions.get(i)); } // is there a value specified double tickValue = Double.NaN; if (i < positionCount) { Number tv = (Number) this.tickLabelPositions.get(i); if (tv != null) { tickValue = (tv.doubleValue() - this.labelAxisStart) / (this.labelAxisEnd - this.labelAxisStart); } } if (Double.isNaN(tickValue)) { tickValue = (i * (1.0 / (Math.max(labelCount - 1, 1)))); } Tick tick = new NumberTick(new Double(tickValue), tickLabel, anchor, rotationAnchor, 0.0); result.add(tick); } return result; }
From source file:org.jfree.data.xy.DefaultOHLCDataset.java
/** * Returns the close-value (as a double primitive) for an item within a * series.//from ww w. j ava 2 s . c o m * * @param series the series (zero-based index). * @param item the item (zero-based index). * * @return The close-value. */ @Override public double getCloseValue(int series, int item) { double result = Double.NaN; Number close = getClose(series, item); if (close != null) { result = close.doubleValue(); } return result; }