Example usage for java.lang Math atan2

List of usage examples for java.lang Math atan2

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double atan2(double y, double x) 

Source Link

Document

Returns the angle theta from the conversion of rectangular coordinates ( x ,  y ) to polar coordinates (r, theta).

Usage

From source file:uk.ac.diamond.scisoft.analysis.dataset.Maths.java

/**
 * @param a opposite side of right-angled triangle
 * @param b adjacent side of right-angled triangle
 * @return angle of triangle: atan(a/b)/*from  www . j  a  v  a  2s. c  o m*/
 */
public static AbstractDataset arctan2(final AbstractDataset a, final AbstractDataset b) {
    a.checkCompatibility(b);

    final int is = Math.max(a.getElementsPerItem(), b.getElementsPerItem());

    if (is > 1)
        throw new UnsupportedOperationException("hypot does not support multiple-element dataset");

    final int rt = AbstractDataset.getBestDType(a.getDtype(), b.getDtype());

    final AbstractDataset result = a.clone().cast(rt);

    IndexIterator it1 = a.getIterator();
    IndexIterator it2 = b.getIterator();

    switch (rt) {
    case AbstractDataset.BOOL:
        boolean[] bdata = ((BooleanDataset) result).getData();

        for (int i = 0; it1.hasNext() && it2.hasNext();) {
            bdata[i++] = Math.atan2(a.getElementDoubleAbs(it1.index), b.getElementDoubleAbs(it2.index)) != 0;
        }
        break;
    case AbstractDataset.INT8:
        byte[] i8data = ((ByteDataset) result).getData();

        for (int i = 0; it1.hasNext() && it2.hasNext();) {
            i8data[i++] = (byte) Math.atan2(a.getElementDoubleAbs(it1.index), b.getElementDoubleAbs(it2.index));
        }
        break;
    case AbstractDataset.INT16:
        short[] i16data = ((ShortDataset) result).getData();

        for (int i = 0; it1.hasNext() && it2.hasNext();) {
            i16data[i++] = (short) Math.atan2(a.getElementDoubleAbs(it1.index),
                    b.getElementDoubleAbs(it2.index));
        }
        break;
    case AbstractDataset.INT32:
        int[] i32data = ((IntegerDataset) result).getData();

        for (int i = 0; it1.hasNext() && it2.hasNext();) {
            i32data[i++] = (int) Math.atan2(a.getElementDoubleAbs(it1.index), b.getElementDoubleAbs(it2.index));
        }
        break;
    case AbstractDataset.INT64:
        long[] i64data = ((LongDataset) result).getData();

        for (int i = 0; it1.hasNext() && it2.hasNext();) {
            i64data[i++] = (long) Math.atan2(a.getElementDoubleAbs(it1.index),
                    b.getElementDoubleAbs(it2.index));
        }
        break;
    case AbstractDataset.FLOAT32:
        float[] f32data = ((FloatDataset) result).getData();

        for (int i = 0; it1.hasNext() && it2.hasNext();) {
            f32data[i++] = (float) Math.atan2(a.getElementDoubleAbs(it1.index),
                    b.getElementDoubleAbs(it2.index));
        }
        break;
    case AbstractDataset.FLOAT64:
        double[] f64data = ((DoubleDataset) result).getData();

        for (int i = 0; it1.hasNext() && it2.hasNext();) {
            f64data[i++] = Math.atan2(a.getElementDoubleAbs(it1.index), b.getElementDoubleAbs(it2.index));
        }
        break;
    default:
        throw new UnsupportedOperationException("atan2 does not support multiple-element dataset");
    }

    // set the name based on the changes made
    result.setName(new StringBuilder("atan2(").append(a.getName()).append(", ").append(b.getName()).append(")")
            .toString());

    return result;
}

From source file:com.processing.core.PApplet.java

static public final float atan2(float a, float b) {
    return (float) Math.atan2(a, b);
}

From source file:com.l2jfree.gameserver.model.skills.L2Skill.java

private int getNewHeadingToTarget(L2Creature caster, L2Creature target) {
    if (caster == null || target == null)
        return 0;

    double befHeading = Util.convertHeadingToDegree(caster.getHeading());
    if (befHeading > 360)
        befHeading -= 360;//from  ww w.  j  a va 2s .  c  o m

    int dx = caster.getX() - target.getX();
    int dy = caster.getY() - target.getY();

    double dist = Math.sqrt(dx * dx + dy * dy);

    if (dist == 0)
        dist = 0.01;

    double sin = dy / dist;
    double cos = dx / dist;
    int heading = (int) (Math.atan2(-sin, -cos) * 10430.378350470452724949566316381);

    return heading;

}

From source file:com.l2jfree.gameserver.model.skills.L2Skill.java

public final boolean isBehindFromCaster(int heading, L2Creature caster, L2Creature target) {
    if (caster == null || target == null)
        return true;

    double befHeading = Util.convertHeadingToDegree(heading);
    if (befHeading > 360)
        befHeading -= 360;//from   ww  w. j  a  va2s .c  o m
    else if (befHeading < 0)
        befHeading += 360;

    int dx = caster.getX() - target.getX();
    int dy = caster.getY() - target.getY();

    double dist = Math.sqrt(dx * dx + dy * dy);

    if (dist == 0)
        dist = 0.01;

    double sin = dy / dist;
    double cos = dx / dist;
    int newheading = (int) (Math.atan2(-sin, -cos) * 10430.378350470452724949566316381);

    double aftHeading = Util.convertHeadingToDegree(newheading);
    if (aftHeading > 360)
        aftHeading -= 360;
    else if (aftHeading < 0)
        aftHeading += 360;

    double diffHeading = Math.abs(aftHeading - befHeading);
    if (diffHeading > 360)
        diffHeading -= 360;
    else if (diffHeading < 0)
        diffHeading += 360;

    return (diffHeading > 90) && (diffHeading < 270);
}

From source file:carnero.cgeo.cgBase.java

public HashMap<String, Double> getRadialDistance(Double latitude, Double longitude, Double bearing,
        Double distance) {/*from ww  w  .ja  va2  s  . co m*/
    final Double rlat1 = latitude * deg2rad;
    final Double rlon1 = longitude * deg2rad;
    final Double rbearing = bearing * deg2rad;
    final Double rdistance = distance / erad;

    final Double rlat = Math.asin(
            Math.sin(rlat1) * Math.cos(rdistance) + Math.cos(rlat1) * Math.sin(rdistance) * Math.cos(rbearing));
    final Double rlon = rlon1 + Math.atan2(Math.sin(rbearing) * Math.sin(rdistance) * Math.cos(rlat1),
            Math.cos(rdistance) - Math.sin(rlat1) * Math.sin(rlat));

    HashMap<String, Double> result = new HashMap<String, Double>();
    result.put("latitude", rlat * rad2deg);
    result.put("longitude", rlon * rad2deg);

    return result;
}

From source file:uk.ac.diamond.scisoft.analysis.dataset.Maths.java

/**
 * Create a dataset of the arguments from a complex dataset
 * @param a dataset//from  w  ww. ja v  a 2s.c o m
 * @return dataset of angles
 */
public static AbstractDataset angle(final AbstractDataset a) {
    AbstractDataset ds = null;
    int dt = a.getDtype();
    IndexIterator it = a.getIterator();

    switch (dt) {
    case AbstractDataset.COMPLEX64:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT32);
        float[] f32data = ((FloatDataset) ds).getData();
        float[] c64data = ((ComplexFloatDataset) a).data;

        for (int i = 0; it.hasNext();) {
            f32data[i++] = (float) Math.atan2(c64data[it.index + 1], c64data[it.index]);
        }
        break;
    case AbstractDataset.COMPLEX128:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT64);
        double[] f64data = ((DoubleDataset) ds).getData();
        double[] c128data = ((ComplexDoubleDataset) a).data;

        for (int i = 0; it.hasNext();) {
            f64data[i++] = Math.atan2(c128data[it.index + 1], c128data[it.index]);
        }
        break;
    default:
        throw new UnsupportedOperationException("angle does not support this dataset type");
    }

    return ds;
}

From source file:com.l2jfree.gameserver.gameobjects.L2Creature.java

public boolean moveToNextRoutePoint() {
    if (!isOnGeodataPath()) {
        // Cancel the move action
        _move = null;/*ww  w . j  a v  a  2 s.c  o m*/
        return false;
    }

    // Get the Move Speed of the L2Charcater
    float speed = getStat().getMoveSpeed();
    if (speed <= 0 || isMovementDisabled()) {
        // Cancel the move action
        _move = null;
        return false;
    }

    // Create and Init a MoveData object
    MoveData m = new MoveData();
    MoveData md = _move;
    if (md == null)
        return false;

    // Update MoveData object
    m.onGeodataPathIndex = md.onGeodataPathIndex + 1; // next segment
    m.geoPath = md.geoPath;
    m.geoPathGtx = md.geoPathGtx;
    m.geoPathGty = md.geoPathGty;
    m.geoPathAccurateTx = md.geoPathAccurateTx;
    m.geoPathAccurateTy = md.geoPathAccurateTy;

    if (md.onGeodataPathIndex == md.geoPath.length - 2) {
        m._xDestination = md.geoPathAccurateTx;
        m._yDestination = md.geoPathAccurateTy;
        m._zDestination = md.geoPath[m.onGeodataPathIndex].getZ();
    } else {
        m._xDestination = md.geoPath[m.onGeodataPathIndex].getX();
        m._yDestination = md.geoPath[m.onGeodataPathIndex].getY();
        m._zDestination = md.geoPath[m.onGeodataPathIndex].getZ();
    }
    double dx = (m._xDestination - super.getX());
    double dy = (m._yDestination - super.getY());

    double distance = Math.sqrt(dx * dx + dy * dy);
    double sin = dy / distance;
    double cos = dx / distance;

    // Caclulate the Nb of ticks between the current position and the destination
    // One tick added for rounding reasons
    int ticksToMove = 1 + (int) (GameTimeManager.TICKS_PER_SECOND * distance / speed);

    // Calculate and set the heading of the L2Creature
    int heading = (int) (Math.atan2(-sin, -cos) * 10430.378);
    heading += 32768;
    setHeading(heading);
    m._heading = 0; // initial value for coordinate sync

    m._moveStartTime = GameTimeManager.getGameTicks();

    if (_log.isDebugEnabled())
        _log.info("time to target:" + ticksToMove);

    // Set the L2Creature _move object to MoveData object
    _move = m;

    MovementController.getInstance().add(this, ticksToMove);

    // Send a Server->Client packet MoveToLocation to the actor and all L2Player in its _knownPlayers
    MoveToLocation msg = new MoveToLocation(this);
    broadcastPacket(msg);

    return true;
}

From source file:uk.ac.diamond.scisoft.analysis.dataset.Maths.java

/**
 * log - evaluate the logarithm function on each element of the dataset
 * @param a/*  w  w w. j  a  v  a 2s.  c om*/
 * @return dataset
 */
@SuppressWarnings("cast")
public static AbstractDataset log(final AbstractDataset a) {
    final int isize;
    final IndexIterator it = a.getIterator();
    AbstractDataset ds;
    final int dt = a.getDtype();

    switch (dt) {
    case AbstractDataset.INT8:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT32);
        final byte[] i8data = ((ByteDataset) a).data;
        final float[] oi8data = ((FloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final byte ix = i8data[it.index];
            float ox;
            ox = (float) (Math.log(ix));
            oi8data[i++] = ox;
        }
        break;
    case AbstractDataset.INT16:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT32);
        final short[] i16data = ((ShortDataset) a).data;
        final float[] oi16data = ((FloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final short ix = i16data[it.index];
            float ox;
            ox = (float) (Math.log(ix));
            oi16data[i++] = ox;
        }
        break;
    case AbstractDataset.INT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT64);
        final int[] i32data = ((IntegerDataset) a).data;
        final double[] oi32data = ((DoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final int ix = i32data[it.index];
            double ox;
            ox = (double) (Math.log(ix));
            oi32data[i++] = ox;
        }
        break;
    case AbstractDataset.INT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT64);
        final long[] i64data = ((LongDataset) a).data;
        final double[] oi64data = ((DoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final long ix = i64data[it.index];
            double ox;
            ox = (double) (Math.log(ix));
            oi64data[i++] = ox;
        }
        break;
    case AbstractDataset.ARRAYINT8:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT32);
        isize = a.getElementsPerItem();
        final byte[] ai8data = ((CompoundByteDataset) a).data;
        final float[] oai8data = ((CompoundFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final byte ix = ai8data[it.index + j];
                float ox;
                ox = (float) (Math.log(ix));
                oai8data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYINT16:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT32);
        isize = a.getElementsPerItem();
        final short[] ai16data = ((CompoundShortDataset) a).data;
        final float[] oai16data = ((CompoundFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final short ix = ai16data[it.index + j];
                float ox;
                ox = (float) (Math.log(ix));
                oai16data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYINT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT64);
        isize = a.getElementsPerItem();
        final int[] ai32data = ((CompoundIntegerDataset) a).data;
        final double[] oai32data = ((CompoundDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final int ix = ai32data[it.index + j];
                double ox;
                ox = (double) (Math.log(ix));
                oai32data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYINT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT64);
        isize = a.getElementsPerItem();
        final long[] ai64data = ((CompoundLongDataset) a).data;
        final double[] oai64data = ((CompoundDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final long ix = ai64data[it.index + j];
                double ox;
                ox = (double) (Math.log(ix));
                oai64data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.FLOAT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT32);
        final float[] f32data = ((FloatDataset) a).data;
        final float[] of32data = ((FloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final float ix = f32data[it.index];
            float ox;
            ox = (float) (Math.log(ix));
            of32data[i++] = ox;
        }
        break;
    case AbstractDataset.FLOAT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT64);
        final double[] f64data = ((DoubleDataset) a).data;
        final double[] of64data = ((DoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final double ix = f64data[it.index];
            double ox;
            ox = (double) (Math.log(ix));
            of64data[i++] = ox;
        }
        break;
    case AbstractDataset.ARRAYFLOAT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT32);
        isize = a.getElementsPerItem();
        final float[] af32data = ((CompoundFloatDataset) a).data;
        final float[] oaf32data = ((CompoundFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final float ix = af32data[it.index + j];
                float ox;
                ox = (float) (Math.log(ix));
                oaf32data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYFLOAT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT64);
        isize = a.getElementsPerItem();
        final double[] af64data = ((CompoundDoubleDataset) a).data;
        final double[] oaf64data = ((CompoundDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final double ix = af64data[it.index + j];
                double ox;
                ox = (double) (Math.log(ix));
                oaf64data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.COMPLEX64:
        ds = AbstractDataset.zeros(a, AbstractDataset.COMPLEX64);
        final float[] c64data = ((ComplexFloatDataset) a).data;
        final float[] oc64data = ((ComplexFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final float ix = c64data[it.index];
            final float iy = c64data[it.index + 1];
            float ox;
            float oy;
            ox = (float) (Math.log(Math.hypot(ix, iy)));
            oy = (float) (Math.atan2(iy, ix));
            oc64data[i++] = ox;
            oc64data[i++] = oy;
        }
        break;
    case AbstractDataset.COMPLEX128:
        ds = AbstractDataset.zeros(a, AbstractDataset.COMPLEX128);
        final double[] c128data = ((ComplexDoubleDataset) a).data;
        final double[] oc128data = ((ComplexDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final double ix = c128data[it.index];
            final double iy = c128data[it.index + 1];
            double ox;
            double oy;
            ox = (double) (Math.log(Math.hypot(ix, iy)));
            oy = (double) (Math.atan2(iy, ix));
            oc128data[i++] = ox;
            oc128data[i++] = oy;
        }
        break;
    default:
        throw new IllegalArgumentException(
                "log supports integer, compound integer, real, compound real, complex datasets only");
    }

    ds.setName(a.getName());
    addFunctionName(ds, "log");
    return ds;
}

From source file:uk.ac.diamond.scisoft.analysis.dataset.Maths.java

/**
 * log2 - evaluate the logarithm function on each element of the dataset
 * @param a/*from  www .j  ava  2 s .com*/
 * @return dataset
 */
@SuppressWarnings("cast")
public static AbstractDataset log2(final AbstractDataset a) {
    final int isize;
    final IndexIterator it = a.getIterator();
    AbstractDataset ds;
    final int dt = a.getDtype();

    switch (dt) {
    case AbstractDataset.INT8:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT32);
        final byte[] i8data = ((ByteDataset) a).data;
        final float[] oi8data = ((FloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final byte ix = i8data[it.index];
            float ox;
            ox = (float) (Math.log(ix) / Math.log(2.));
            oi8data[i++] = ox;
        }
        break;
    case AbstractDataset.INT16:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT32);
        final short[] i16data = ((ShortDataset) a).data;
        final float[] oi16data = ((FloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final short ix = i16data[it.index];
            float ox;
            ox = (float) (Math.log(ix) / Math.log(2.));
            oi16data[i++] = ox;
        }
        break;
    case AbstractDataset.INT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT64);
        final int[] i32data = ((IntegerDataset) a).data;
        final double[] oi32data = ((DoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final int ix = i32data[it.index];
            double ox;
            ox = (double) (Math.log(ix) / Math.log(2.));
            oi32data[i++] = ox;
        }
        break;
    case AbstractDataset.INT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT64);
        final long[] i64data = ((LongDataset) a).data;
        final double[] oi64data = ((DoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final long ix = i64data[it.index];
            double ox;
            ox = (double) (Math.log(ix) / Math.log(2.));
            oi64data[i++] = ox;
        }
        break;
    case AbstractDataset.ARRAYINT8:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT32);
        isize = a.getElementsPerItem();
        final byte[] ai8data = ((CompoundByteDataset) a).data;
        final float[] oai8data = ((CompoundFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final byte ix = ai8data[it.index + j];
                float ox;
                ox = (float) (Math.log(ix) / Math.log(2.));
                oai8data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYINT16:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT32);
        isize = a.getElementsPerItem();
        final short[] ai16data = ((CompoundShortDataset) a).data;
        final float[] oai16data = ((CompoundFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final short ix = ai16data[it.index + j];
                float ox;
                ox = (float) (Math.log(ix) / Math.log(2.));
                oai16data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYINT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT64);
        isize = a.getElementsPerItem();
        final int[] ai32data = ((CompoundIntegerDataset) a).data;
        final double[] oai32data = ((CompoundDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final int ix = ai32data[it.index + j];
                double ox;
                ox = (double) (Math.log(ix) / Math.log(2.));
                oai32data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYINT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT64);
        isize = a.getElementsPerItem();
        final long[] ai64data = ((CompoundLongDataset) a).data;
        final double[] oai64data = ((CompoundDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final long ix = ai64data[it.index + j];
                double ox;
                ox = (double) (Math.log(ix) / Math.log(2.));
                oai64data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.FLOAT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT32);
        final float[] f32data = ((FloatDataset) a).data;
        final float[] of32data = ((FloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final float ix = f32data[it.index];
            float ox;
            ox = (float) (Math.log(ix) / Math.log(2.));
            of32data[i++] = ox;
        }
        break;
    case AbstractDataset.FLOAT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT64);
        final double[] f64data = ((DoubleDataset) a).data;
        final double[] of64data = ((DoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final double ix = f64data[it.index];
            double ox;
            ox = (double) (Math.log(ix) / Math.log(2.));
            of64data[i++] = ox;
        }
        break;
    case AbstractDataset.ARRAYFLOAT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT32);
        isize = a.getElementsPerItem();
        final float[] af32data = ((CompoundFloatDataset) a).data;
        final float[] oaf32data = ((CompoundFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final float ix = af32data[it.index + j];
                float ox;
                ox = (float) (Math.log(ix) / Math.log(2.));
                oaf32data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYFLOAT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT64);
        isize = a.getElementsPerItem();
        final double[] af64data = ((CompoundDoubleDataset) a).data;
        final double[] oaf64data = ((CompoundDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final double ix = af64data[it.index + j];
                double ox;
                ox = (double) (Math.log(ix) / Math.log(2.));
                oaf64data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.COMPLEX64:
        ds = AbstractDataset.zeros(a, AbstractDataset.COMPLEX64);
        final float[] c64data = ((ComplexFloatDataset) a).data;
        final float[] oc64data = ((ComplexFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final float ix = c64data[it.index];
            final float iy = c64data[it.index + 1];
            float ox;
            float oy;
            ox = (float) (Math.log(Math.hypot(ix, iy)) / Math.log(2.));
            oy = (float) (Math.atan2(iy, ix));
            oc64data[i++] = ox;
            oc64data[i++] = oy;
        }
        break;
    case AbstractDataset.COMPLEX128:
        ds = AbstractDataset.zeros(a, AbstractDataset.COMPLEX128);
        final double[] c128data = ((ComplexDoubleDataset) a).data;
        final double[] oc128data = ((ComplexDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final double ix = c128data[it.index];
            final double iy = c128data[it.index + 1];
            double ox;
            double oy;
            ox = (double) (Math.log(Math.hypot(ix, iy)) / Math.log(2.));
            oy = (double) (Math.atan2(iy, ix));
            oc128data[i++] = ox;
            oc128data[i++] = oy;
        }
        break;
    default:
        throw new IllegalArgumentException(
                "log2 supports integer, compound integer, real, compound real, complex datasets only");
    }

    ds.setName(a.getName());
    addFunctionName(ds, "log2");
    return ds;
}

From source file:uk.ac.diamond.scisoft.analysis.dataset.Maths.java

/**
 * log10 - evaluate the logarithm function on each element of the dataset
 * @param a//from w w  w .j a  va  2  s.co m
 * @return dataset
 */
@SuppressWarnings("cast")
public static AbstractDataset log10(final AbstractDataset a) {
    final int isize;
    final IndexIterator it = a.getIterator();
    AbstractDataset ds;
    final int dt = a.getDtype();

    switch (dt) {
    case AbstractDataset.INT8:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT32);
        final byte[] i8data = ((ByteDataset) a).data;
        final float[] oi8data = ((FloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final byte ix = i8data[it.index];
            float ox;
            ox = (float) (Math.log10(ix));
            oi8data[i++] = ox;
        }
        break;
    case AbstractDataset.INT16:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT32);
        final short[] i16data = ((ShortDataset) a).data;
        final float[] oi16data = ((FloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final short ix = i16data[it.index];
            float ox;
            ox = (float) (Math.log10(ix));
            oi16data[i++] = ox;
        }
        break;
    case AbstractDataset.INT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT64);
        final int[] i32data = ((IntegerDataset) a).data;
        final double[] oi32data = ((DoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final int ix = i32data[it.index];
            double ox;
            ox = (double) (Math.log10(ix));
            oi32data[i++] = ox;
        }
        break;
    case AbstractDataset.INT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT64);
        final long[] i64data = ((LongDataset) a).data;
        final double[] oi64data = ((DoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final long ix = i64data[it.index];
            double ox;
            ox = (double) (Math.log10(ix));
            oi64data[i++] = ox;
        }
        break;
    case AbstractDataset.ARRAYINT8:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT32);
        isize = a.getElementsPerItem();
        final byte[] ai8data = ((CompoundByteDataset) a).data;
        final float[] oai8data = ((CompoundFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final byte ix = ai8data[it.index + j];
                float ox;
                ox = (float) (Math.log10(ix));
                oai8data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYINT16:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT32);
        isize = a.getElementsPerItem();
        final short[] ai16data = ((CompoundShortDataset) a).data;
        final float[] oai16data = ((CompoundFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final short ix = ai16data[it.index + j];
                float ox;
                ox = (float) (Math.log10(ix));
                oai16data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYINT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT64);
        isize = a.getElementsPerItem();
        final int[] ai32data = ((CompoundIntegerDataset) a).data;
        final double[] oai32data = ((CompoundDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final int ix = ai32data[it.index + j];
                double ox;
                ox = (double) (Math.log10(ix));
                oai32data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYINT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT64);
        isize = a.getElementsPerItem();
        final long[] ai64data = ((CompoundLongDataset) a).data;
        final double[] oai64data = ((CompoundDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final long ix = ai64data[it.index + j];
                double ox;
                ox = (double) (Math.log10(ix));
                oai64data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.FLOAT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT32);
        final float[] f32data = ((FloatDataset) a).data;
        final float[] of32data = ((FloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final float ix = f32data[it.index];
            float ox;
            ox = (float) (Math.log10(ix));
            of32data[i++] = ox;
        }
        break;
    case AbstractDataset.FLOAT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.FLOAT64);
        final double[] f64data = ((DoubleDataset) a).data;
        final double[] of64data = ((DoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final double ix = f64data[it.index];
            double ox;
            ox = (double) (Math.log10(ix));
            of64data[i++] = ox;
        }
        break;
    case AbstractDataset.ARRAYFLOAT32:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT32);
        isize = a.getElementsPerItem();
        final float[] af32data = ((CompoundFloatDataset) a).data;
        final float[] oaf32data = ((CompoundFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final float ix = af32data[it.index + j];
                float ox;
                ox = (float) (Math.log10(ix));
                oaf32data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.ARRAYFLOAT64:
        ds = AbstractDataset.zeros(a, AbstractDataset.ARRAYFLOAT64);
        isize = a.getElementsPerItem();
        final double[] af64data = ((CompoundDoubleDataset) a).data;
        final double[] oaf64data = ((CompoundDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            for (int j = 0; j < isize; j++) {
                final double ix = af64data[it.index + j];
                double ox;
                ox = (double) (Math.log10(ix));
                oaf64data[i++] = ox;
            }
        }
        break;
    case AbstractDataset.COMPLEX64:
        ds = AbstractDataset.zeros(a, AbstractDataset.COMPLEX64);
        final float[] c64data = ((ComplexFloatDataset) a).data;
        final float[] oc64data = ((ComplexFloatDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final float ix = c64data[it.index];
            final float iy = c64data[it.index + 1];
            float ox;
            float oy;
            ox = (float) (Math.log10(Math.hypot(ix, iy)));
            oy = (float) (Math.atan2(iy, ix));
            oc64data[i++] = ox;
            oc64data[i++] = oy;
        }
        break;
    case AbstractDataset.COMPLEX128:
        ds = AbstractDataset.zeros(a, AbstractDataset.COMPLEX128);
        final double[] c128data = ((ComplexDoubleDataset) a).data;
        final double[] oc128data = ((ComplexDoubleDataset) ds).getData();
        for (int i = 0; it.hasNext();) {
            final double ix = c128data[it.index];
            final double iy = c128data[it.index + 1];
            double ox;
            double oy;
            ox = (double) (Math.log10(Math.hypot(ix, iy)));
            oy = (double) (Math.atan2(iy, ix));
            oc128data[i++] = ox;
            oc128data[i++] = oy;
        }
        break;
    default:
        throw new IllegalArgumentException(
                "log10 supports integer, compound integer, real, compound real, complex datasets only");
    }

    ds.setName(a.getName());
    addFunctionName(ds, "log10");
    return ds;
}