Example usage for java.lang Math copySign

List of usage examples for java.lang Math copySign

Introduction

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

Prototype

public static float copySign(float magnitude, float sign) 

Source Link

Document

Returns the first floating-point argument with the sign of the second floating-point argument.

Usage

From source file:FloastPointDemo.java

public static void main(String[] args) {

    // Returns a copySign of the first argument
    double d = Math.copySign(1234.56, -1);
    System.out.println("Math.copySign (1234.56, -1) = " + d);
}

From source file:Main.java

public static void main(String[] args) {

    double x = 125.9;
    double y = -0.4873;

    System.out.println(Math.copySign(x, y));
    System.out.println(Math.copySign(y, x));

}

From source file:Main.java

public static void main(String[] args) {

    float x = 123.4f;
    float y = -0.1234F;

    System.out.println(Math.copySign(x, y));
    System.out.println(Math.copySign(y, x));

}

From source file:eremeykin.pete.plotter.PolarPlotterTopComponent.java

protected void fillData(List<Map.Entry<Double, Double>> tmpList) {
    XYSeries toleranceSeries1 = new XYSeries("ei");
    XYSeries toleranceSeries2 = new XYSeries("es");
    XYSeries dataSeries = new XYSeries("U");
    XYSeries diameter = new XYSeries("d");
    Double x1 = tmpList.get(0).getKey();
    Double x2 = tmpList.get(0).getKey();
    double delta = x2 - x1;

    Double y1 = tmpList.get(0).getValue();
    Double xLast = tmpList.get(tmpList.size() - 1).getKey();

    tmpList.add(new AbstractMap.SimpleEntry<>(x1, y1));
    double min = Double.MAX_VALUE;
    for (Map.Entry<Double, Double> point : tmpList) {
        Double xv = point.getKey();
        Double yv = point.getValue();
        if (yv < min) {
            min = yv;//from  w w w  . jav  a 2 s .com
        }
    }
    diameter.clear();
    dataSeries.clear();
    toleranceSeries1.clear();
    toleranceSeries2.clear();
    double d = Double.valueOf(model.getParameterByID(model.getRoot(), 3).getValue());
    for (Map.Entry<Double, Double> point : tmpList) {
        Double xv = point.getKey();
        Double yv = point.getValue();
        if (xv == null || yv == null) {
            continue;
        }
        dataSeries.add(xv * 360 / (xLast + delta), d + scale * yv);
        toleranceSeries1.add(xv * 360 / (xLast + delta),
                d + scale * Double.valueOf(model.getParameterByID(model.getRoot(), 5).getValue()));
        toleranceSeries2.add(xv * 360 / (xLast + delta), d + scale
                * Math.copySign(Double.valueOf(model.getParameterByID(model.getRoot(), 5).getValue()), -1));
        diameter.add(xv * 360 / (xLast + delta), d);
        XYSeriesCollection ds = new XYSeriesCollection();
        ds.addSeries(dataSeries);
        ds.addSeries(toleranceSeries1);
        ds.addSeries(toleranceSeries2);
        ds.addSeries(diameter);
        plot.setDataset(ds);
    }
}

From source file:automenta.vivisect.dimensionalize.HyperassociativeMap.java

/** vertices is passed as a list because the Set iterator from JGraphT is slow */
public ArrayRealVector align(V nodeToAlign, ObjectDoubleHashMap<V> neighbors, V[] vertices) {

    double nodeSpeed = getSpeedFactor(nodeToAlign);

    ArrayRealVector originalPosition = new ArrayRealVector(new double[2], true);
    //getPosition(nodeToAlign);
    //getCurrentPosition(nodeToAlign);
    getPosition(nodeToAlign, originalPosition.getDataRef());

    if (nodeSpeed == 0)
        return originalPosition;

    // calculate equilibrium with neighbors
    ArrayRealVector position = (ArrayRealVector) originalPosition.mapMultiplyToSelf(1.0 / scale);

    getNeighbors(nodeToAlign, neighbors);

    ArrayRealVector delta = newVector();

    double radius = getRadius(nodeToAlign);
    double targetDistance = radius + equilibriumDistance;
    double learningRate = this.learningRate;

    ArrayRealVector tmpAttractVector = newVector();

    // align with neighbours
    neighbors.forEachKeyValue((neighbor, distToNeighbor) -> {

        ArrayRealVector attractVector = getThePosition(neighbor, tmpAttractVector).combineToSelf(1, -1,
                position);//from   ww  w  .j a va2  s.  c om

        double oldDistance = magnitude(attractVector);

        double newDistance;
        double factor = 0;
        double deltaDist = oldDistance - distToNeighbor;
        if (oldDistance > distToNeighbor) {
            newDistance = Math.pow(deltaDist, attractionStrength);

        } else {

            newDistance = -targetDistance * atanh((-deltaDist) / distToNeighbor);

            if (Math.abs(newDistance) > (Math.abs(deltaDist))) {
                newDistance = -targetDistance * (-deltaDist);
            }

        }

        newDistance *= learningRate;
        if (oldDistance != 0) {
            factor = newDistance / oldDistance;
        }

        add(delta, attractVector, factor);
    });

    ArrayRealVector repelVector = newVector();
    double maxEffectiveDistance = targetDistance * maxRepulsionDistance;

    ArrayRealVector nodePos = newVector();

    // calculate repulsion with all non-neighbors

    DistanceMetric distanceFunction = this.distanceFunction;
    double minDistance = this.minDistance;
    double repulsiveWeakness = this.repulsiveWeakness;

    for (V node : vertices) {

        if (node == null)
            continue;

        //vertices.forEach((Consumer<N>)node -> {
        //for (final N node : vertices) {
        if ((node == nodeToAlign) || (neighbors.containsKey(node)))
            continue;

        double oldDistance = distanceFunction.subtractIfLessThan(getThePosition(node, nodePos), position,
                repelVector, maxEffectiveDistance);

        if (oldDistance == Double.POSITIVE_INFINITY)
            continue; //too far to matter

        if (oldDistance < minDistance)
            oldDistance = minDistance; //continue;
        //throw new RuntimeException("invalid oldDistance");

        double newDistance = -targetDistance * Math.pow(oldDistance, -repulsiveWeakness);

        if (Math.abs(newDistance) > targetDistance) {
            newDistance = Math.copySign(targetDistance, newDistance);
        }
        newDistance *= learningRate;

        add(delta, repelVector, newDistance / oldDistance);
    }

    /*if (normalizeRepulsion)
    nodeSpeed/=delta.getNorm(); //TODO check when norm = 0*/

    if (nodeSpeed != 1.0) {
        delta.mapMultiplyToSelf(nodeSpeed);
    }

    double moveDistance = magnitude(delta);
    if (!Double.isFinite(moveDistance))
        throw new RuntimeException("invalid magnitude");

    if (moveDistance > targetDistance * acceptableMaxDistanceFactor) {
        double newLearningRate = ((targetDistance * acceptableMaxDistanceFactor) / moveDistance);
        if (newLearningRate < learningRate) {
            this.learningRate = newLearningRate;
        } else {
            this.learningRate *= LEARNING_RATE_INCREASE_FACTOR / vertices.length;
        }

        moveDistance = DEFAULT_TOTAL_MOVEMENT;
    } else {
        add(position, delta);
    }

    if (moveDistance > maxMovement) {
        maxMovement = moveDistance;
    }
    totalMovement += moveDistance;

    originalPosition.mapMultiplyToSelf(scale);
    move(nodeToAlign, originalPosition.getEntry(0), originalPosition.getEntry(1));
    return originalPosition;
}

From source file:org.renjin.primitives.Ops.java

private static double convertNaN(double d) {
    if (Double.isNaN(d)) {
        return Math.copySign(0, d);
    } else {//from   w  ww.j  a  v  a  2  s .  co  m
        return d;
    }
}

From source file:uk.ac.diamond.scisoft.analysis.utils.Faddeeva.java

private static final double copysign(double x, double y) {
    return Math.copySign(x, y);
}