Example usage for org.apache.commons.math3.complex Complex pow

List of usage examples for org.apache.commons.math3.complex Complex pow

Introduction

In this page you can find the example usage for org.apache.commons.math3.complex Complex pow.

Prototype

public Complex pow(double x) 

Source Link

Document

Returns of value of this complex number raised to the power of x .

Usage

From source file:org.eclipse.january.dataset.CompoundShortDataset.java

@Override
public CompoundShortDataset ipower(final Object b) {
    setDirty();// ww w .  j a v  a 2  s  .  c o  m
    Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    final int is = bds.getElementsPerItem();
    if (bds.getSize() == 1) {
        final double vr = bds.getElementDoubleAbs(0);
        final IndexIterator it = getIterator();
        if (bds.isComplex()) {
            final double vi = bds.getElementDoubleAbs(1);
            if (vi == 0) {
                while (it.hasNext()) {
                    for (int i = 0; i < isize; i++) {
                        final double v = Math.pow(data[it.index + i], vr);
                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                            data[it.index + i] = 0; // INT_USE
                        } else { // INT_USE
                            data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                        } // INT_USE
                    }
                }
            } else {
                final Complex zv = new Complex(vr, vi);
                while (it.hasNext()) {
                    for (int i = 0; i < isize; i++) {
                        Complex zd = new Complex(data[it.index + i], 0);
                        final double v = zd.pow(zv).getReal();
                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                            data[it.index + i] = 0; // INT_USE
                        } else { // INT_USE
                            data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                        } // INT_USE
                    }
                }
            }
        } else if (is == 1) {
            while (it.hasNext()) {
                for (int i = 0; i < isize; i++) {
                    final double v = Math.pow(data[it.index + i], vr);
                    if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                        data[it.index + i] = 0; // INT_USE
                    } else { // INT_USE
                        data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }
            }
        } else if (is == isize) {
            while (it.hasNext()) {
                for (int i = 0; i < isize; i++) {
                    final double v = Math.pow(data[it.index + i], bds.getElementDoubleAbs(i));
                    if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                        data[it.index + i] = 0; // INT_USE
                    } else { // INT_USE
                        data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }
            }
        }
    } else {
        final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
        it.setOutputDouble(true);
        if (bds.isComplex()) {
            while (it.hasNext()) {
                final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1));
                double v = new Complex(it.aDouble, 0).pow(zv).getReal();
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.aIndex] = 0; // INT_USE
                } else { // INT_USE
                    data[it.aIndex] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
                for (int i = 1; i < isize; i++) {
                    v = new Complex(data[it.aIndex + i], 0).pow(zv).getReal();
                    if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                        data[it.aIndex + i] = 0; // INT_USE
                    } else { // INT_USE
                        data[it.aIndex + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }
            }
        } else {
            while (it.hasNext()) {
                double v = Math.pow(it.aDouble, it.bDouble);
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.aIndex] = 0; // INT_USE
                } else { // INT_USE
                    data[it.aIndex] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
                for (int i = 1; i < isize; i++) {
                    v = Math.pow(data[it.aIndex + i], bds.getElementDoubleAbs(it.bIndex + i));
                    if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                        data[it.aIndex + i] = 0; // INT_USE
                    } else { // INT_USE
                        data[it.aIndex + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }
            }
        }
    }
    return this;
}

From source file:org.eclipse.january.dataset.DoubleDataset.java

@Override
public DoubleDataset ipower(final Object b) {
    setDirty(); // NAN_OMIT
    Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b); // NAN_OMIT
    if (bds.getSize() == 1) { // NAN_OMIT
        final double vr = bds.getElementDoubleAbs(0); // NAN_OMIT
        final IndexIterator it = getIterator(); // NAN_OMIT
        if (bds.isComplex()) { // NAN_OMIT
            final double vi = bds.getElementDoubleAbs(1); // NAN_OMIT
            if (vi == 0) { // NAN_OMIT
                while (it.hasNext()) { // NAN_OMIT
                    final double v = Math.pow(data[it.index], vr); // NAN_OMIT
                    // if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE // NAN_OMIT
                    //    data[it.index] = 0; // INT_USE // NAN_OMIT
                    // } else { // INT_USE // NAN_OMIT
                    data[it.index] = v; // PRIM_TYPE_LONG // NAN_OMIT // ADD_CAST
                    // } // INT_USE // NAN_OMIT
                } // NAN_OMIT
            } else { // NAN_OMIT
                final Complex zv = new Complex(vr, vi); // NAN_OMIT
                while (it.hasNext()) { // NAN_OMIT
                    Complex zd = new Complex(data[it.index], 0); // NAN_OMIT
                    final double v = zd.pow(zv).getReal(); // NAN_OMIT
                    // if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE // NAN_OMIT
                    //    data[it.index] = 0; // INT_USE // NAN_OMIT
                    // } else { // INT_USE // NAN_OMIT
                    data[it.index] = v; // PRIM_TYPE_LONG // NAN_OMIT // ADD_CAST
                    // } // INT_USE // NAN_OMIT
                } // NAN_OMIT
            } // NAN_OMIT
        } else {// NAN_OMIT
            while (it.hasNext()) { // NAN_OMIT
                final double v = Math.pow(data[it.index], vr); // NAN_OMIT
                // if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE // NAN_OMIT
                //    data[it.index] = 0; // INT_USE // NAN_OMIT
                // } else { // INT_USE // NAN_OMIT
                data[it.index] = v; // PRIM_TYPE_LONG // NAN_OMIT // ADD_CAST
                // } // INT_USE // NAN_OMIT
            } // NAN_OMIT
        } // NAN_OMIT
    } else { // NAN_OMIT
        final BroadcastIterator it = BroadcastIterator.createIterator(this, bds); // NAN_OMIT
        it.setOutputDouble(true); // NAN_OMIT
        if (bds.isComplex()) { // NAN_OMIT
            while (it.hasNext()) { // NAN_OMIT
                final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1)); // NAN_OMIT
                final double v = new Complex(it.aDouble, 0).pow(zv).getReal(); // NAN_OMIT
                // if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE // NAN_OMIT
                //    data[it.aIndex] = 0; // INT_USE // NAN_OMIT
                // } else { // INT_USE // NAN_OMIT
                data[it.aIndex] = v; // PRIM_TYPE_LONG // NAN_OMIT // ADD_CAST
                // } // INT_USE // NAN_OMIT
            } // NAN_OMIT
        } else {// NAN_OMIT
            while (it.hasNext()) { // NAN_OMIT
                final double v = Math.pow(it.aDouble, it.bDouble); // NAN_OMIT
                // if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE // NAN_OMIT
                //    data[it.aIndex] = 0; // INT_USE // NAN_OMIT
                // } else { // INT_USE // NAN_OMIT
                data[it.aIndex] = v; // PRIM_TYPE_LONG // NAN_OMIT // ADD_CAST
                // } // INT_USE // NAN_OMIT
            } // NAN_OMIT
        } // NAN_OMIT
    } // NAN_OMIT
    return this;
}

From source file:org.eclipse.january.dataset.FloatDataset.java

@Override
public FloatDataset ipower(final Object b) {
    setDirty();//w  ww  .ja  va 2 s  .co  m
    Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    if (bds.getSize() == 1) {
        final double vr = bds.getElementDoubleAbs(0);
        final IndexIterator it = getIterator();
        if (bds.isComplex()) {
            final double vi = bds.getElementDoubleAbs(1);
            if (vi == 0) {
                while (it.hasNext()) {
                    final double v = Math.pow(data[it.index], vr);
                    // if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    //    data[it.index] = 0; // INT_USE
                    // } else { // INT_USE
                    data[it.index] = (float) v; // PRIM_TYPE_LONG // ADD_CAST
                    // } // INT_USE
                }
            } else {
                final Complex zv = new Complex(vr, vi);
                while (it.hasNext()) {
                    Complex zd = new Complex(data[it.index], 0);
                    final double v = zd.pow(zv).getReal();
                    // if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    //    data[it.index] = 0; // INT_USE
                    // } else { // INT_USE
                    data[it.index] = (float) v; // PRIM_TYPE_LONG // ADD_CAST
                    // } // INT_USE
                }
            }
        } else {// NAN_OMIT
            while (it.hasNext()) {
                final double v = Math.pow(data[it.index], vr);
                // if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                //    data[it.index] = 0; // INT_USE
                // } else { // INT_USE
                data[it.index] = (float) v; // PRIM_TYPE_LONG // ADD_CAST
                // } // INT_USE
            }
        }
    } else {
        final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
        it.setOutputDouble(true);
        if (bds.isComplex()) {
            while (it.hasNext()) {
                final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1));
                final double v = new Complex(it.aDouble, 0).pow(zv).getReal();
                // if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                //    data[it.aIndex] = 0; // INT_USE
                // } else { // INT_USE
                data[it.aIndex] = (float) v; // PRIM_TYPE_LONG // ADD_CAST
                // } // INT_USE
            }
        } else {// NAN_OMIT
            while (it.hasNext()) {
                final double v = Math.pow(it.aDouble, it.bDouble);
                // if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                //    data[it.aIndex] = 0; // INT_USE
                // } else { // INT_USE
                data[it.aIndex] = (float) v; // PRIM_TYPE_LONG // ADD_CAST
                // } // INT_USE
            }
        }
    }
    return this;
}

From source file:org.eclipse.january.dataset.IntegerDataset.java

@Override
public IntegerDataset ipower(final Object b) {
    setDirty();/*from  w  w  w .  j a  v a2s.c  o m*/
    Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    if (bds.getSize() == 1) {
        final double vr = bds.getElementDoubleAbs(0);
        final IndexIterator it = getIterator();
        if (bds.isComplex()) {
            final double vi = bds.getElementDoubleAbs(1);
            if (vi == 0) {
                while (it.hasNext()) {
                    final double v = Math.pow(data[it.index], vr);
                    if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                        data[it.index] = 0; // INT_USE
                    } else { // INT_USE
                        data[it.index] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }
            } else {
                final Complex zv = new Complex(vr, vi);
                while (it.hasNext()) {
                    Complex zd = new Complex(data[it.index], 0);
                    final double v = zd.pow(zv).getReal();
                    if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                        data[it.index] = 0; // INT_USE
                    } else { // INT_USE
                        data[it.index] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }
            }
        } else {// NAN_OMIT
            while (it.hasNext()) {
                final double v = Math.pow(data[it.index], vr);
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.index] = 0; // INT_USE
                } else { // INT_USE
                    data[it.index] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
            }
        }
    } else {
        final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
        it.setOutputDouble(true);
        if (bds.isComplex()) {
            while (it.hasNext()) {
                final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1));
                final double v = new Complex(it.aDouble, 0).pow(zv).getReal();
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.aIndex] = 0; // INT_USE
                } else { // INT_USE
                    data[it.aIndex] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
            }
        } else {// NAN_OMIT
            while (it.hasNext()) {
                final double v = Math.pow(it.aDouble, it.bDouble);
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.aIndex] = 0; // INT_USE
                } else { // INT_USE
                    data[it.aIndex] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
            }
        }
    }
    return this;
}

From source file:org.eclipse.january.dataset.LongDataset.java

@Override
public LongDataset ipower(final Object b) {
    setDirty();/*from w  w  w .ja  v a  2s.c om*/
    Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    if (bds.getSize() == 1) {
        final double vr = bds.getElementDoubleAbs(0);
        final IndexIterator it = getIterator();
        if (bds.isComplex()) {
            final double vi = bds.getElementDoubleAbs(1);
            if (vi == 0) {
                while (it.hasNext()) {
                    final double v = Math.pow(data[it.index], vr);
                    if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                        data[it.index] = 0; // INT_USE
                    } else { // INT_USE
                        data[it.index] = (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }
            } else {
                final Complex zv = new Complex(vr, vi);
                while (it.hasNext()) {
                    Complex zd = new Complex(data[it.index], 0);
                    final double v = zd.pow(zv).getReal();
                    if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                        data[it.index] = 0; // INT_USE
                    } else { // INT_USE
                        data[it.index] = (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }
            }
        } else {// NAN_OMIT
            while (it.hasNext()) {
                final double v = Math.pow(data[it.index], vr);
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.index] = 0; // INT_USE
                } else { // INT_USE
                    data[it.index] = (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
            }
        }
    } else {
        final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
        it.setOutputDouble(true);
        if (bds.isComplex()) {
            while (it.hasNext()) {
                final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1));
                final double v = new Complex(it.aDouble, 0).pow(zv).getReal();
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.aIndex] = 0; // INT_USE
                } else { // INT_USE
                    data[it.aIndex] = (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
            }
        } else {// NAN_OMIT
            while (it.hasNext()) {
                final double v = Math.pow(it.aDouble, it.bDouble);
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.aIndex] = 0; // INT_USE
                } else { // INT_USE
                    data[it.aIndex] = (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
            }
        }
    }
    return this;
}

From source file:org.eclipse.january.dataset.MathsTest.java

@Test
public void testPower() {
    Dataset a, b, c = null, d = null;/* w w  w.  j  a  v a 2s  .co m*/
    Complex zv = new Complex(-3.5, 0);
    final double dv = zv.getReal();
    long start;
    int n;
    int eCount = 0;

    for (String dn : classes.keySet()) {
        final int dtype = classes.get(dn);
        Random.seed(12735L);

        for (String en : classes.keySet()) {
            final int etype = classes.get(en);

            TestUtils.verbosePrintf("%s by %s, ", dn, en);

            n = 32;
            for (int i = 0; i < SITER; i++) {
                if (dtype < Dataset.ARRAYINT8) {
                    a = Random.randn(n).imultiply(100);
                    a = a.cast(dtype);
                } else {
                    Dataset[] aa = new Dataset[ISIZEA];
                    for (int j = 0; j < ISIZEA; j++) {
                        aa[j] = Random.randn(n).imultiply(100);
                    }
                    a = DatasetUtils.cast(aa, dtype);
                }
                if (etype < Dataset.ARRAYINT8) {
                    b = Random.randn(n).imultiply(100);
                    b = b.cast(etype);
                } else {
                    Dataset[] ab = new Dataset[ISIZEB];
                    for (int j = 0; j < ISIZEB; j++) {
                        ab[j] = Random.randn(n).imultiply(100);
                    }
                    b = DatasetUtils.cast(ab, etype);
                }

                start = -System.nanoTime();
                try {
                    c = Maths.power(a, b);
                } catch (IllegalArgumentException e) {
                    TestUtils.verbosePrintf("Could not perform this operation: ", e.getMessage());
                    eCount++;
                    continue;
                }
                start += System.nanoTime();
                double ntime = ((double) start) / c.getSize();

                d = DatasetFactory.zeros(c);
                start = -System.nanoTime();
                IndexIterator ita = a.getIterator();
                IndexIterator itb = b.getIterator();
                int j = 0;
                if ((dtype == Dataset.COMPLEX64 || dtype == Dataset.COMPLEX128)
                        && (etype == Dataset.COMPLEX64 || etype == Dataset.COMPLEX128)) {
                    final int is = d.getElementsPerItem();
                    while (ita.hasNext() && itb.hasNext()) {
                        d.setObjectAbs(j,
                                ((Complex) a.getObjectAbs(ita.index)).pow((Complex) b.getObjectAbs(itb.index)));
                        j += is;
                    }
                } else if ((dtype == Dataset.COMPLEX64 || dtype == Dataset.COMPLEX128)
                        && !(etype == Dataset.COMPLEX64 || etype == Dataset.COMPLEX128)) {
                    final int is = d.getElementsPerItem();
                    while (ita.hasNext() && itb.hasNext()) {
                        d.setObjectAbs(j, ((Complex) a.getObjectAbs(ita.index))
                                .pow(new Complex(b.getElementDoubleAbs(itb.index), 0)));
                        j += is;
                    }
                } else if (!(dtype == Dataset.COMPLEX64 || dtype == Dataset.COMPLEX128)
                        && (etype == Dataset.COMPLEX64 || etype == Dataset.COMPLEX128)) {
                    final int is = d.getElementsPerItem();
                    while (ita.hasNext() && itb.hasNext()) {
                        d.setObjectAbs(j, new Complex(a.getElementDoubleAbs(ita.index), 0)
                                .pow((Complex) b.getObjectAbs(itb.index)));
                        j += is;
                    }
                } else {
                    if (dtype < Dataset.ARRAYINT8 && etype < Dataset.ARRAYINT8) {
                        while (ita.hasNext() && itb.hasNext()) {
                            d.setObjectAbs(j++, Math.pow(a.getElementDoubleAbs(ita.index),
                                    b.getElementDoubleAbs(itb.index)));
                        }
                    } else {
                        final double[] answer = new double[MAXISIZE];
                        final int is = d.getElementsPerItem();

                        if (a.getElementsPerItem() < is) {
                            while (ita.hasNext() && itb.hasNext()) {
                                final double xa = a.getElementDoubleAbs(ita.index);
                                for (int k = 0; k < ISIZEB; k++) {
                                    answer[k] = Math.pow(xa, b.getElementDoubleAbs(itb.index + k));
                                }
                                d.setObjectAbs(j, answer);
                                j += is;
                            }
                        } else if (b.getElementsPerItem() < is) {
                            while (ita.hasNext() && itb.hasNext()) {
                                final double xb = b.getElementDoubleAbs(itb.index);
                                for (int k = 0; k < ISIZEA; k++) {
                                    answer[k] = Math.pow(a.getElementDoubleAbs(ita.index + k), xb);
                                }
                                d.setObjectAbs(j, answer);
                                j += is;
                            }
                        } else {
                            while (ita.hasNext() && itb.hasNext()) {
                                for (int k = 0; k < is; k++) {
                                    answer[k] = Math.pow(a.getElementDoubleAbs(ita.index + k),
                                            b.getElementDoubleAbs(itb.index + k));
                                }
                                d.setObjectAbs(j, answer);
                                j += is;
                            }
                        }
                    }
                }
                if (d == null)
                    break;
                start += System.nanoTime();
                double otime = ((double) start) / d.getSize();

                TestUtils.verbosePrintf("Time taken by pow for %s: %s; %s (%.1f%%)\n", n, otime, ntime,
                        100. * (otime - ntime) / otime);
                checkDatasets(a, b, c, d);

                n *= SSTEP;
            }
        }

        Random.seed(12735L);
        n = 32;
        TestUtils.verbosePrintf("%s by constant, ", dn);
        for (int i = 0; i < SITER; i++) {
            if (dtype < Dataset.ARRAYINT8) {
                a = Random.randn(n);
                a.imultiply(100);
                a = a.cast(dtype);
            } else {
                Dataset[] aa = new Dataset[ISIZEA];
                for (int j = 0; j < ISIZEA; j++) {
                    aa[j] = Random.randn(n).imultiply(100);
                }
                a = DatasetUtils.cast(aa, dtype);
            }

            start = -System.nanoTime();
            try {
                c = Maths.power(a, dv);
            } catch (IllegalArgumentException e) {
                TestUtils.verbosePrintf("Could not perform this operation: ", e.getMessage());
                eCount++;
                continue;
            }
            start += System.nanoTime();
            double ntime = ((double) start) / c.getSize();

            d = DatasetFactory.zeros(c);
            start = -System.nanoTime();
            IndexIterator ita = a.getIterator();
            int j = 0;
            if (dtype == Dataset.COMPLEX64 || dtype == Dataset.COMPLEX128) {
                final int is = d.getElementsPerItem();
                while (ita.hasNext()) {
                    d.setObjectAbs(j, ((Complex) a.getObjectAbs(ita.index)).pow(zv));
                    j += is;
                }
            } else {
                if (dtype < Dataset.ARRAYINT8) {
                    while (ita.hasNext()) {
                        d.setObjectAbs(j++, Math.pow(((Number) a.getObjectAbs(ita.index)).doubleValue(), dv));
                    }
                } else {
                    final double[] answer = new double[ISIZEA];
                    while (ita.hasNext()) {
                        for (int k = 0; k < ISIZEA; k++) {
                            answer[k] = Math.pow(a.getElementDoubleAbs(ita.index + k), dv);
                        }
                        d.setObjectAbs(j, answer);
                        j += ISIZEA;
                    }
                }
            }
            if (d == null)
                break;
            start += System.nanoTime();
            double otime = ((double) start) / d.getSize();

            TestUtils.verbosePrintf("Time taken by pow for %s: %s; %s (%.1f%%)\n", n, otime, ntime,
                    100. * (otime - ntime) / otime);
            checkDatasets(a, dv, c, d);

            n *= SSTEP;
        }

        Random.seed(12735L);
        n = 32;
        TestUtils.verbosePrintf("constant by %s, ", dn);
        for (int i = 0; i < SITER; i++) {
            if (dtype < Dataset.ARRAYINT8) {
                a = Random.randn(n);
                a.imultiply(100);
                a = a.cast(dtype);
            } else {
                Dataset[] aa = new Dataset[ISIZEA];
                for (int j = 0; j < ISIZEA; j++) {
                    aa[j] = Random.randn(n).imultiply(100);
                }
                a = DatasetUtils.cast(aa, dtype);
            }

            start = -System.nanoTime();
            try {
                c = Maths.power(dv, a);
            } catch (IllegalArgumentException e) {
                TestUtils.verbosePrintf("Could not perform this operation: ", e.getMessage());
                eCount++;
                continue;
            }
            start += System.nanoTime();
            double ntime = ((double) start) / c.getSize();

            d = DatasetFactory.zeros(c);
            start = -System.nanoTime();
            IndexIterator ita = a.getIterator();
            int j = 0;
            if (dtype == Dataset.COMPLEX64 || dtype == Dataset.COMPLEX128) {
                final int is = d.getElementsPerItem();
                while (ita.hasNext()) {
                    d.setObjectAbs(j, zv.pow((Complex) a.getObjectAbs(ita.index)));
                    j += is;
                }
            } else {
                if (dtype < Dataset.ARRAYINT8) {
                    while (ita.hasNext()) {
                        d.setObjectAbs(j++, Math.pow(dv, ((Number) a.getObjectAbs(ita.index)).doubleValue()));
                    }
                } else {
                    final double[] answer = new double[ISIZEA];
                    while (ita.hasNext()) {
                        for (int k = 0; k < ISIZEA; k++) {
                            answer[k] = Math.pow(dv, a.getElementDoubleAbs(ita.index + k));
                        }
                        d.setObjectAbs(j, answer);
                        j += ISIZEA;
                    }
                }
            }
            if (d == null)
                break;
            start += System.nanoTime();
            double otime = ((double) start) / d.getSize();

            TestUtils.verbosePrintf("Time taken by pow for %s: %s; %s (%.1f%%)\n", n, otime, ntime,
                    100. * (otime - ntime) / otime);
            checkDatasets(dv, a, c, d);

            n *= SSTEP;
        }
    }

    if (eCount > 0) {
        TestUtils.verbosePrintf("Number of exceptions caught: %d\n", eCount);
    }
}

From source file:org.eclipse.january.dataset.ShortDataset.java

@Override
public ShortDataset ipower(final Object b) {
    setDirty();/*from  w  ww. j  av  a 2s. c om*/
    Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    if (bds.getSize() == 1) {
        final double vr = bds.getElementDoubleAbs(0);
        final IndexIterator it = getIterator();
        if (bds.isComplex()) {
            final double vi = bds.getElementDoubleAbs(1);
            if (vi == 0) {
                while (it.hasNext()) {
                    final double v = Math.pow(data[it.index], vr);
                    if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                        data[it.index] = 0; // INT_USE
                    } else { // INT_USE
                        data[it.index] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }
            } else {
                final Complex zv = new Complex(vr, vi);
                while (it.hasNext()) {
                    Complex zd = new Complex(data[it.index], 0);
                    final double v = zd.pow(zv).getReal();
                    if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                        data[it.index] = 0; // INT_USE
                    } else { // INT_USE
                        data[it.index] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }
            }
        } else {// NAN_OMIT
            while (it.hasNext()) {
                final double v = Math.pow(data[it.index], vr);
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.index] = 0; // INT_USE
                } else { // INT_USE
                    data[it.index] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
            }
        }
    } else {
        final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
        it.setOutputDouble(true);
        if (bds.isComplex()) {
            while (it.hasNext()) {
                final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1));
                final double v = new Complex(it.aDouble, 0).pow(zv).getReal();
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.aIndex] = 0; // INT_USE
                } else { // INT_USE
                    data[it.aIndex] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
            }
        } else {// NAN_OMIT
            while (it.hasNext()) {
                final double v = Math.pow(it.aDouble, it.bDouble);
                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
                    data[it.aIndex] = 0; // INT_USE
                } else { // INT_USE
                    data[it.aIndex] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
            }
        }
    }
    return this;
}

From source file:org.xlrnet.tibaija.util.TIMathUtils.java

/**
 * Calculates the n-th root of two complex values. Calculation is based on x = d(r) = r^(1/d).
 *
 * @param degree//from www  .j av  a  2 s .c  o m
 *         The degree i.e. left side of the root expression
 * @param radicand
 *         The radicand i.e. right side of the root expression
 * @return The n-th complex root.
 */
@NotNull
public static Complex complexNthRoot(@NotNull Complex degree, @NotNull Complex radicand)
        throws TIArgumentException {
    return radicand.pow(Complex.ONE.divide(degree));
}

From source file:tools.help.java

/**
 *  /*from   w  w w  . j a  v a  2 s. c o m*/
 * @param c complex number
 * @param x double number
 * @return complex c^x
 */
public static Complex cpow(Complex c, double x) {
    return c.pow(x);
}

From source file:tools.help.java

public static Complex cpow(Complex c, int x) {
    return c.pow((double) x);
}