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

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

Introduction

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

Prototype

public Complex(double real, double imaginary) 

Source Link

Document

Create a complex number given the real and imaginary parts.

Usage

From source file:org.briljantframework.array.base.BaseArrayRoutines.java

@Override
public ComplexArray ceil(ComplexArray array) {
    return array.map(v -> new Complex(Math.ceil(v.getReal()), Math.ceil(v.getImaginary())));
}

From source file:org.briljantframework.array.base.BaseArrayRoutines.java

@Override
public ComplexArray floor(ComplexArray array) {
    return array.map(v -> new Complex(Math.floor(v.getReal()), Math.floor(v.getImaginary())));
}

From source file:org.briljantframework.example.array.Perf.java

private static int mandel(double re, double im) {
    int n = 0;/*w w w .  j ava 2 s.  c o  m*/
    Complex z = new Complex(re, im);
    Complex c = new Complex(re, im);
    for (n = 0; n <= 79; ++n) {
        if (z.abs() > 2.0) {
            n -= 1;
            break;
        }

        // z = z*z + c
        z = z.multiply(z).add(c);
    }
    return n + 1;
}

From source file:org.briljantframework.math.transform.DiscreteFourierTransform.java

public static ComplexArray ifft(ComplexArray a) {
    ComplexArray copy = ComplexArray.zeros(a.size());
    for (int i = 0; i < a.size(); i++) {
        Complex c = a.get(i);//  w  ww  .j a  v a 2 s  . co m
        copy.set(i, new Complex(c.getImaginary(), c.getReal()));
    }
    fftInplace(copy);

    int n = copy.size();

    // Reversing and scaling
    for (int i = 0; i < n; i++) {
        Complex c = copy.get(i);
        copy.set(i, new Complex(c.getImaginary() / n, c.getReal() / n));
    }
    return copy;
}

From source file:org.briljantframework.math.transform.DiscreteFourierTransform.java

private static void transformBluestein(ComplexArray a) {
    // Find a power-of-2 convolution length m such that m >= n * 2 + 1
    int n = a.size();
    if (n >= 0x20000000) { // n >= 536870912
        throw new IllegalArgumentException("");
    }/*from   w  w w.  j  a  v a2 s .  c o m*/
    int m = Integer.highestOneBit(n * 2 + 1) << 1;

    // Trigonometric tables
    DoubleArray cosTable = DoubleArray.zeros(n);
    DoubleArray sinTable = DoubleArray.zeros(n);
    for (int i = 0; i < n; i++) {
        int j = (int) ((long) i * i % (n * 2));
        cosTable.set(i, Math.cos(Math.PI * j / n));
        sinTable.set(i, Math.sin(Math.PI * j / n));
    }

    ComplexArray an = ComplexArray.zeros(m);
    ComplexArray bn = ComplexArray.zeros(m);

    bn.set(0, new Complex(cosTable.get(0), sinTable.get(0)));
    for (int i = 0; i < n; i++) {
        double cos = cosTable.get(i);
        double sin = sinTable.get(i);
        Complex complex = a.get(i);
        double real = complex.getReal() * cos + complex.getImaginary() * sin;
        double imag = -complex.getReal() * sin + complex.getImaginary() * cos;
        an.set(i, new Complex(real, imag));

        int j = i + 1;
        if (j < n) {
            Complex bcVal = Complex.valueOf(cosTable.get(j), sinTable.get(j));
            bn.set(j, bcVal);
            bn.set(m - j, bcVal);
        }
    }

    // Convolution
    ComplexArray cc = convolve(an, bn);
    for (int i = 0; i < n; i++) {
        double cos = cosTable.get(i);
        double sin = sinTable.get(i);

        Complex cv = cc.get(i);
        double real = cv.getReal() * cos + cv.getImaginary() * sin;
        double imag = -cv.getReal() * sin + cv.getImaginary() * cos;
        a.set(i, new Complex(real, imag));
    }
}

From source file:org.briljantframework.math.transform.DiscreteFourierTransform.java

private static ComplexArray convolve(ComplexArray x, ComplexArray y) {
    int n = x.size();
    ComplexArray xt = fft(x);/*from   w  ww  . j  a v  a 2s . c o m*/
    ComplexArray yt = fft(y);

    for (int i = 0; i < n; i++) {
        xt.set(i, xt.get(i).multiply(yt.get(i)));
    }

    // TODO: 02/12/15 performance
    for (int i = 0; i < n; i++) {
        Complex complex = xt.get(i);
        xt.set(i, new Complex(complex.getImaginary(), complex.getReal()));
    }

    fftInplace(xt); // inverse transform, since xt is reversed above
    for (int i = 0; i < n; i++) {
        Complex c = xt.get(i);
        xt.set(i, Complex.valueOf(c.getImaginary() / n, c.getReal() / n));
    }
    return xt;
}

From source file:org.briljantframework.math.transform.DiscreteFourierTransform.java

private static void transformRadix2(ComplexArray a) {
    final int n = a.size();
    int levels = (int) Math.floor(Math.log(n) / Math.log(2));
    if (1 << levels != n) {
        throw new IllegalArgumentException();
    }/*  w  ww . j  a  v  a 2s.  com*/

    DoubleArray cosTable = DoubleArray.zeros(n / 2);
    DoubleArray sinTable = DoubleArray.zeros(n / 2);
    final double v = 2 * Math.PI;
    for (int i = 0; i < n / 2; i++) {
        cosTable.set(i, Math.cos(v * i / n));
        sinTable.set(i, Math.sin(v * i / n));
    }

    // Bit-reversed addressing permutation (i.e. even addresses in the first half and odd in the
    // second half)
    for (int i = 0; i < n; i++) {
        int j = Integer.reverse(i) >>> (32 - levels);
        if (j > i) {
            a.swap(j, i);
        }
    }

    // Cooley-Tukey decimation-in-time radix-2 FFT
    for (int size = 2; size <= n; size *= 2) {
        int halfSize = size / 2;
        int tableStep = n / size;
        for (int i = 0; i < n; i += size) {
            for (int j = i, k = 0; j < i + halfSize; j++, k += tableStep) {
                Complex hjVal = a.get(j + halfSize);
                Complex jVal = a.get(j);
                double cos = cosTable.get(k);
                double sin = sinTable.get(k);
                double tpre = hjVal.getReal() * cos + hjVal.getImaginary() * sin;
                double tpim = -hjVal.getReal() * sin + hjVal.getImaginary() * cos;
                a.set(j + halfSize, new Complex(jVal.getReal() - tpre, jVal.getImaginary() - tpim));
                a.set(j, new Complex(jVal.getReal() + tpre, jVal.getImaginary() + tpim));
            }
        }
        if (size == n) {
            break;
        }
    }
}

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

@Override
public ByteDataset ipower(final Object b) {
    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] = (byte) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                    } // INT_USE
                }//w w  w  .ja  va2 s .  c  o m
            } 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] = (byte) (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] = (byte) (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] = (byte) (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] = (byte) (long) v; // PRIM_TYPE_LONG // ADD_CAST
                } // INT_USE
            }
        }
    }
    setDirty();
    return this;
}

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

/**
 * Get complex value at absolute index in the internal array.
 * /*from   ww w  . j  a v a  2s  . c om*/
 * This is an internal method with no checks so can be dangerous. Use with care or ideally with an iterator.
 *
 * @param index absolute index
 * @return value
 */
public Complex getComplexAbs(final int index) {
    return new Complex(data[index], data[index + 1]);
}

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

@Override
public Object getObjectAbs(final int index) {
    return new Complex(data[index], data[index + 1]);
}