Example usage for java.lang Math signum

List of usage examples for java.lang Math signum

Introduction

In this page you can find the example usage for java.lang Math signum.

Prototype

public static float signum(float f) 

Source Link

Document

Returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.

Usage

From source file:com.fluidops.iwb.widget.GMapWidget.java

public static String[] parseGeoCoords(String coords) {
    double lat = Double.NaN, lng = Double.NaN;

    String toParse;//from  w  w  w  . j a  va2s.  com
    if (coords.startsWith("{{coord") && coords.endsWith("}}")) {
        toParse = coords.substring(coords.indexOf('|') + 1, coords.indexOf('}'));
        // single components
        String[] cs = toParse.split("\\|");

        switch (cs.length) {
        case 0:
        case 1:
            throw new IllegalArgumentException(coords + " is not a valid GeoCoord!!");

        case 2:
        case 3: /* decimal with sign */
            lat = valueOf(cs[0]);
            lng = valueOf(cs[1]);
            break;

        case 4:
        case 5: /* have decimal coords with [NS][WE] specs */
            if (Character.isLetter(cs[1].charAt(0)) && Character.isLetter(cs[3].charAt(0))) {
                lat = valueOf(cs[0]) * convertLatSpecToSign(cs[1]);
                lng = valueOf(cs[2]) * convertLngSpecToSign(cs[3]);
            } else {
                double hrslat = valueOf(cs[0]);
                double hrslng = valueOf(cs[2]);
                lat = convertDegToDec(Math.abs(hrslat), valueOf(cs[1]), 0, (int) Math.signum(hrslat));
                lng = convertDegToDec(Math.abs(hrslng), valueOf(cs[3]), 0, (int) Math.signum(hrslng));
            }
            break;

        case 6:
        case 7: /* now degree coords with sign or in DM format (degree, min) */
            if (Character.isLetter(cs[2].charAt(0)) && Character.isLetter(cs[5].charAt(0))) {
                /* is in DM format */
                lat = convertDegToDec(valueOf(cs[0]), valueOf(cs[1]), 0.0, convertLatSpecToSign(cs[2]));
                lng = convertDegToDec(valueOf(cs[3]), valueOf(cs[4]), 0.0, convertLngSpecToSign(cs[5]));
            } else {
                /* is degree coords with sign */
                double hrslat = valueOf(cs[0]);
                double hrslng = valueOf(cs[3]);
                lat = convertDegToDec(Math.abs(hrslat), valueOf(cs[1]), valueOf(cs[2]),
                        (int) Math.signum(hrslat));
                lng = convertDegToDec(Math.abs(hrslng), valueOf(cs[4]), valueOf(cs[5]),
                        (int) Math.signum(hrslng));
                break;
            }
            break;

        default: /* have degree coords with [NS][WE] specs */
            lat = convertDegToDec(valueOf(cs[0]), valueOf(cs[1]), valueOf(cs[2]), convertLatSpecToSign(cs[3]));
            lng = convertDegToDec(valueOf(cs[4]), valueOf(cs[5]), valueOf(cs[6]), convertLngSpecToSign(cs[7]));
            break;

        }
    } else {
        String[] sep = coords.split("\\s+");
        if (sep.length == 2) {
            return sep;
        }

        sep = coords.split(",");
        if (sep.length == 2) {
            return sep;
        }
    }

    return new String[] { String.format(Locale.US, "%3.6f", lat), String.format(Locale.US, "%3.6f", lng) };
}

From source file:be.ugent.maf.cellmissy.analysis.singlecell.processing.impl.interpolation.TrackHermiteInterpolator.java

private double getSlope(double h1, double h2, double del1, double del2) {
    // noncentered, shape-preserving, three-point formula
    double d = ((2 * h1 + h2) * del1 - h1 * del2) / (h1 + h2);
    if (Math.signum(d) != Math.signum(del1)) {
        d = 0;/*from  w w w  . ja  va  2  s.  c  o  m*/
    } else if ((Math.signum(del1) != Math.signum(del2)) & (Math.abs(d) > Math.abs(3 * del1))) {
        d = 3 * del1;
    }
    return d;
}

From source file:org.j2free.http.HttpCallTask.java

/**
 * This implementation of <tt>compareTo</tt> compares <tt>HttpCallTask</tt>
 * instances first on priority of the task, then using the creation time of
 * so that tasks of equal priority will run in FIFO order.
 *
 * This method does not need to be synchronized because <tt>priority</tt>
 * is final.//  ww w. ja  v  a 2 s  . c  o m
 * @param other
 * @return
 */
public int compareTo(HttpCallTask other) {
    if (other == null)
        return 1;

    int c = this.priority.compareTo(other.priority);

    if (c != 0)
        return c;

    return Float.valueOf(Math.signum(other.created - this.created)).intValue();
}

From source file:org.kalypso.model.wspm.tuhh.core.wspwin.LengthSectionExporter.java

private double guessStationDirectionFactor(final TupleResult result, final int stationIndex) {
    /* Ignore if too short */
    final int recordCount = result.size();
    if (recordCount < 2)
        return 1;

    final Number firstStation = (Number) result.get(0).getValue(stationIndex);
    final Number lastStation = (Number) result.get(recordCount - 1).getValue(stationIndex);

    final int signum = (int) Math.signum(firstStation.doubleValue() - lastStation.doubleValue());
    // 0 is forbidden, else the ls will be broken.
    if (signum == 0)
        return 1;

    return signum;
}

From source file:org.opennms.features.topology.app.internal.jung.TopoFRLayout.java

private double epsilon() {
    double e = 2 * EPSILON * Math.signum(Math.random() - 0.5);
    return e == 0 ? EPSILON : e;
}

From source file:org.eclipse.dataset.InterpolatedPoint.java

private static double getInterpolatedResultFromNinePoints(Dataset val, Dataset x, Dataset y, double xPos,
        double yPos) throws Exception {

    // First build the nine points
    InterpolatedPoint p00 = makePoint(x, y, 0, 0);
    InterpolatedPoint p01 = makePoint(x, y, 0, 1);
    InterpolatedPoint p02 = makePoint(x, y, 0, 2);
    InterpolatedPoint p10 = makePoint(x, y, 1, 0);
    InterpolatedPoint p11 = makePoint(x, y, 1, 1);
    InterpolatedPoint p12 = makePoint(x, y, 1, 2);
    InterpolatedPoint p20 = makePoint(x, y, 2, 0);
    InterpolatedPoint p21 = makePoint(x, y, 2, 1);
    InterpolatedPoint p22 = makePoint(x, y, 2, 2);

    // now try every connection and find points that intersect with the interpolated value
    ArrayList<InterpolatedPoint> points = new ArrayList<InterpolatedPoint>();

    InterpolatedPoint A = get1DInterpolatedPoint(p00, p10, 0, xPos);
    InterpolatedPoint B = get1DInterpolatedPoint(p10, p20, 0, xPos);
    InterpolatedPoint C = get1DInterpolatedPoint(p00, p01, 0, xPos);
    InterpolatedPoint D = get1DInterpolatedPoint(p10, p11, 0, xPos);
    InterpolatedPoint E = get1DInterpolatedPoint(p20, p21, 0, xPos);
    InterpolatedPoint F = get1DInterpolatedPoint(p01, p11, 0, xPos);
    InterpolatedPoint G = get1DInterpolatedPoint(p11, p21, 0, xPos);
    InterpolatedPoint H = get1DInterpolatedPoint(p01, p02, 0, xPos);
    InterpolatedPoint I = get1DInterpolatedPoint(p11, p12, 0, xPos);
    InterpolatedPoint J = get1DInterpolatedPoint(p21, p22, 0, xPos);
    InterpolatedPoint K = get1DInterpolatedPoint(p02, p12, 0, xPos);
    InterpolatedPoint L = get1DInterpolatedPoint(p12, p22, 0, xPos);

    // Now add any to the list which are not null
    if (A != null)
        points.add(A);/*from w ww. ja  v  a 2 s. c om*/
    if (B != null)
        points.add(B);
    if (C != null)
        points.add(C);
    if (D != null)
        points.add(D);
    if (E != null)
        points.add(E);
    if (F != null)
        points.add(F);
    if (G != null)
        points.add(G);
    if (H != null)
        points.add(H);
    if (I != null)
        points.add(I);
    if (J != null)
        points.add(J);
    if (K != null)
        points.add(K);
    if (L != null)
        points.add(L);

    // if no intercepts, then retun NaN;
    if (points.size() == 0)
        return Double.NaN;

    InterpolatedPoint bestPoint = null;

    // sort the points by y
    Collections.sort(points, new Comparator<InterpolatedPoint>() {

        @Override
        public int compare(InterpolatedPoint o1, InterpolatedPoint o2) {
            return (int) Math.signum(o1.realPoint.getDouble(1) - o2.realPoint.getDouble(1));
        }
    });

    // now we have all the points which fit the x criteria, Find the points which fit the y
    for (int a = 1; a < points.size(); a++) {
        InterpolatedPoint testPoint = get1DInterpolatedPoint(points.get(a - 1), points.get(a), 1, yPos);
        if (testPoint != null) {
            bestPoint = testPoint;
            break;
        }
    }

    if (bestPoint == null) {
        return Double.NaN;
    }

    // now we have the best point, we can calculate the weights, and positions
    int xs = (int) Math.floor(bestPoint.getCoordPoint().getDouble(0));
    int ys = (int) Math.floor(bestPoint.getCoordPoint().getDouble(1));

    double xoff = bestPoint.getCoordPoint().getDouble(0) - xs;
    double yoff = bestPoint.getCoordPoint().getDouble(1) - ys;

    // check corner cases
    if (xs == 2) {
        xs = 1;
        xoff = 1.0;
    }

    if (ys == 2) {
        ys = 1;
        yoff = 1.0;
    }

    double w00 = (1 - xoff) * (1 - yoff);
    double w10 = (xoff) * (1 - yoff);
    double w01 = (1 - xoff) * (yoff);
    double w11 = (xoff) * (yoff);

    // now using the weights, we can get the final interpolated value
    double result = val.getDouble(xs, ys) * w00;
    result += val.getDouble(xs + 1, ys) * w10;
    result += val.getDouble(xs, ys + 1) * w01;
    result += val.getDouble(xs + 1, ys + 1) * w11;

    return result;
}

From source file:com.opengamma.analytics.math.interpolation.ShapePreservingCubicSplineInterpolator.java

/**
 * @param intervals// w  w  w. j  a va 2 s .  co m
 * @param slopes
 * @return First derivative at knots
 */
private double[] firstDiffFinder(final double[] intervals, final double[] slopes) {
    final int nInts = intervals.length;
    final double[] res = new double[nInts + 1];

    res[0] = endpointFirst(intervals[0], intervals[1], slopes[0], slopes[1]);
    res[nInts] = endpointFirst(intervals[nInts - 1], intervals[nInts - 2], slopes[nInts - 1],
            slopes[nInts - 2]);

    for (int i = 1; i < nInts; ++i) {
        if (Math.signum(slopes[i]) != Math.signum(slopes[i - 1]) | (slopes[i] == 0 | slopes[i - 1] == 0)) {
            res[i] = 0.;
        } else {
            final double den1 = 2. * intervals[i] + intervals[i - 1];
            final double den2 = intervals[i] + 2. * intervals[i - 1];
            res[i] = 3. * (intervals[i] + intervals[i - 1]) / (den1 / slopes[i - 1] + den2 / slopes[i]);
        }
    }

    return res;
}

From source file:com.aionemu.gameserver.spawnengine.WalkerGroup.java

/**
 * Returns coordinates of NPC in 2D from the initial spawn location
 *
 * @param origin      - initial spawn location
 * @param destination - point of next move
 * @param shift       - distance from origin located in lines perpendicular to
 *                    destination; for SagittalShift if negative then located to the left from
 *                    origin, otherwise, to the right for CoronalShift if negative then located
 *                    to back, otherwise to the front
 * @category TODO: move to MathUtil when all kinds of WalkerGroupType are
 * implemented.//from   w  w  w .  j a va  2 s  . co  m
 */
public static Point2D getLinePoint(Point2D origin, Point2D destination, WalkerGroupShift shift) {
    // TODO: implement angle shift
    WalkerGroupShift dir = getShiftSigns(origin, destination);
    Point2D result = null;
    if (origin.getY() - destination.getY() == 0) {
        return new Point2D(origin.getX() + dir.getCoronalShift() * shift.getCoronalShift(),
                origin.getY() - dir.getSagittalShift() * shift.getSagittalShift());
    } else if (origin.getX() - destination.getX() == 0) {
        return new Point2D(origin.getX() + dir.getCoronalShift() * shift.getSagittalShift(),
                origin.getY() + dir.getCoronalShift() * shift.getCoronalShift());
    } else {
        double slope = (origin.getX() - destination.getX()) / (origin.getY() - destination.getY());
        double dx = Math.abs(shift.getSagittalShift()) / Math.sqrt(1 + slope * slope);
        if (shift.getSagittalShift() * dir.getCoronalShift() < 0) {
            result = new Point2D((float) (origin.getX() - dx), (float) (origin.getY() + dx * slope));
        } else {
            result = new Point2D((float) (origin.getX() + dx), (float) (origin.getY() - dx * slope));
        }
    }
    if (shift.getCoronalShift() != 0) {
        Point2D rotatedShift = null;
        if (shift.getSagittalShift() != 0) {
            rotatedShift = getLinePoint(origin, destination, new WalkerGroupShift(
                    Math.signum(shift.getSagittalShift()) * Math.abs(shift.getCoronalShift()), 0));
        } else {
            rotatedShift = getLinePoint(origin, destination,
                    new WalkerGroupShift(Math.abs(shift.getCoronalShift()), 0));
        }

        // since it's rotated, and perpendicular, dx and dy are reciprocal when not rotated
        float dx = Math.abs(origin.getX() - rotatedShift.getX());
        float dy = Math.abs(origin.getY() - rotatedShift.getY());
        if (shift.getCoronalShift() < 0) {
            if (dir.getSagittalShift() < 0 && dir.getCoronalShift() < 0) {
                result = new Point2D(result.getX() + dy, result.getY() + dx);
            } else if (dir.getSagittalShift() > 0 && dir.getCoronalShift() > 0) {
                result = new Point2D(result.getX() - dy, result.getY() - dx);
            } else if (dir.getSagittalShift() < 0 && dir.getCoronalShift() > 0) {
                result = new Point2D(result.getX() + dy, result.getY() - dx);
            } else if (dir.getSagittalShift() > 0 && dir.getCoronalShift() < 0) {
                result = new Point2D(result.getX() - dy, result.getY() + dx);
            }
        } else {
            if (dir.getSagittalShift() < 0 && dir.getCoronalShift() < 0) {
                result = new Point2D(result.getX() - dy, result.getY() - dx);
            } else if (dir.getSagittalShift() > 0 && dir.getCoronalShift() > 0) {
                result = new Point2D(result.getX() + dy, result.getY() + dx);
            } else if (dir.getSagittalShift() < 0 && dir.getCoronalShift() > 0) {
                result = new Point2D(result.getX() - dy, result.getY() + dx);
            } else if (dir.getSagittalShift() > 0 && dir.getCoronalShift() < 0) {
                result = new Point2D(result.getX() + dy, result.getY() - dx);
            }
        }
    }
    return result;
}

From source file:org.opennms.features.topology.app.internal.jung.TopoFRLayout.java

protected void calcAttraction(E e) {
    Pair<V> endpoints = getGraph().getEndpoints(e);
    V v1 = endpoints.getFirst();//from   w w  w  .ja  v a2s . c  o  m
    V v2 = endpoints.getSecond();
    boolean v1_locked = isLocked(v1);
    boolean v2_locked = isLocked(v2);

    if (v1_locked && v2_locked) {
        // both locked, do nothing
        return;
    }
    Point2D p1 = transform(v1);
    Point2D p2 = transform(v2);
    if (p1 == null || p2 == null)
        return;
    double xDelta = p1.getX() - p2.getX();
    double yDelta = p1.getY() - p2.getY();

    xDelta = Math.abs(xDelta) > EPSILON ? xDelta : xDelta == 0 ? epsilon() : Math.signum(xDelta) * EPSILON;
    yDelta = Math.abs(yDelta) > EPSILON ? yDelta : yDelta == 0 ? epsilon() : Math.signum(yDelta) * EPSILON;

    double deltaLength = Math.sqrt((xDelta * xDelta) + (yDelta * yDelta));

    double force = (deltaLength * deltaLength) / attraction_constant;

    if (Double.isNaN(force)) {
        throw new IllegalArgumentException("Unexpected mathematical result in FRLayout:calcPositions [force]");
    }

    double dx = (xDelta / deltaLength) * force;
    double dy = (yDelta / deltaLength) * force;
    if (v1_locked == false) {
        FRVertexData fvd1 = getFRData(v1);
        fvd1.offset(-dx, -dy);
    }
    if (v2_locked == false) {
        FRVertexData fvd2 = getFRData(v2);
        fvd2.offset(dx, dy);
    }
}

From source file:ml.shifu.shifu.core.dtrain.Weight.java

public double[] calculateWeights(double[] weights, double[] gradients, int iteration) {
    if (this.updateRule != null) {
        this.updateRule.update(gradients, weights, iteration, this.fixedWeights);
        return weights;
    } else {/*from w ww.ja v a 2 s  .co  m*/
        for (int i = 0; i < gradients.length; i++) {
            switch (this.rl) {
            case NONE:
                weights[i] += updateWeight(i, weights, gradients);
                break;
            case L1:
                if (Double.compare(this.reg, 0d) == 0) {
                    weights[i] += updateWeight(i, weights, gradients);
                } else {
                    double shrinkValue = this.reg / getNumTrainSize();
                    double delta = updateWeight(i, weights, gradients);
                    weights[i] = Math.signum(delta) * Math.max(0.0, Math.abs(delta) - shrinkValue);
                }
                break;
            case L2:
            default:
                weights[i] += (updateWeight(i, weights, gradients) - this.reg * weights[i] / getNumTrainSize());
                break;
            }
        }
        return weights;
    }
}