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

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

Introduction

In this page you can find the example usage for org.apache.commons.math.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.renjin.primitives.Ops.java

@Deferrable
@Builtin("^")/*from   w  ww .  ja v  a2  s  .  c o m*/
@DataParallel(PreserveAttributeStyle.ALL)
public static Complex power(Complex x, Complex y) {
    // handle common cases with better precision than the log(exp(x)*y) trick used by Apache Commons
    if (y.getImaginary() == 0) {
        double yr = y.getReal();
        if (yr == 0) {
            return new Complex(1, 0);
        } else if (yr == 1) {
            return x;
        } else {
            int k = (int) yr;
            if (k == yr && k < 65536) {
                return power(x, k);
            }
        }
    }

    return x.pow(y);
}

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

/**
 * Raise a complex number to an integer power
 * @param x the complex number//from ww  w.  ja  v a  2s  . c  o  m
 * @param k the power to raise the complex number
 * @return  the result
 */
private static Complex power(Complex x, int k) {
    if (k < 0) {
        return reciprocal(power(x, -k));
    } else {
        Complex result = new Complex(1, 0);
        while (k > 0) {
            result = multiply(result, x);
            k--;
        }
        return result;
    }
}

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

private static Complex reciprocal(Complex value) {
    // LICENSE: this code is derived from the division code,
    // which is transcribed code from GCC, which is licensed under GPL

    double c = value.getReal();
    double d = value.getImaginary();

    double x;//from  ww w.jav  a2  s.c  om
    double y;
    if (Math.abs(c) < Math.abs(d)) {
        double ratio = c / d;
        double denominator = (c * ratio) + d;
        x = ratio / denominator;
        y = -1 / denominator;
    } else {
        double ratio = d / c;
        double denominator = (d * ratio) + c;
        x = 1 / denominator;
        y = -ratio / denominator;
    }

    if (isNaN(x) && isNaN(y)) {
        if (c == 0.0 && d == 0.0) {
            x = copySign(Double.POSITIVE_INFINITY, c);
            y = copySign(Double.NaN, c);
        } else if (isInfinite(c) || isInfinite(d)) {
            double rc = convertInf(c);
            double rd = convertInf(d);
            x = 0.0 * rc;
            y = 0.0 * (-rd);
        }
    }
    return new Complex(x, y);
}

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

@Builtin
@GroupGeneric// w  ww . j a  v a 2 s.c  o m
public static SEXP sum(@Current Context context, @ArgumentList ListVector arguments,
        @NamedFlag("na.rm") boolean removeNA) throws IOException {
    double realSum = 0;
    boolean haveDouble = false;
    double imaginarySum = 0;
    boolean haveComplex = false;

    if (arguments.length() == 1 && arguments.get(0) instanceof DoubleVector && !removeNA) {
        return new DeferredSum((Vector) arguments.get(0), AttributeMap.EMPTY);
    }

    for (SEXP argument : arguments) {
        if (argument instanceof IntVector || argument instanceof LogicalVector) {
            AtomicVector vector = (AtomicVector) argument;
            for (int i = 0; i != argument.length(); ++i) {
                if (vector.isElementNA(i)) {
                    if (!removeNA) {
                        return haveDouble ? new DoubleArrayVector(DoubleVector.NA)
                                : new IntArrayVector(IntVector.NA);
                    }
                } else {
                    int element = vector.getElementAsInt(i);
                    realSum += element;
                }
            }
        } else if (argument instanceof DoubleVector) {
            DoubleVector vector = (DoubleVector) argument;
            haveDouble = true;
            for (int i = 0; i != vector.length(); ++i) {
                if (vector.isElementNA(i)) {
                    if (!removeNA) {
                        return new DoubleArrayVector(DoubleVector.NA);
                    }
                } else {
                    realSum += vector.getElementAsDouble(i);
                }
            }
        } else if (argument instanceof ComplexVector) {
            ComplexVector vector = (ComplexVector) argument;
            haveComplex = true;
            for (int i = 0; i != vector.length(); ++i) {
                if (vector.isElementNA(i)) {
                    if (!removeNA) {
                        return new ComplexArrayVector(ComplexVector.NA);
                    }
                } else {
                    Complex z = vector.getElementAsComplex(i);
                    realSum += z.getReal();
                    imaginarySum += z.getImaginary();
                }
            }

        } else {
            throw new EvalException("invalid 'type' (" + argument.getTypeName() + ") of argument");
        }
    }
    if (haveComplex) {
        return new ComplexArrayVector(new Complex(realSum, imaginarySum));

    } else if (haveDouble) {
        return new DoubleArrayVector(realSum);

    } else {
        if (realSum < Integer.MIN_VALUE || realSum > Integer.MAX_VALUE) {
            Warning.emitWarning(context, context.getCall(), false, "Integer overflow - use sum(as.numeric(.))");
            return new IntArrayVector(IntVector.NA);
        }
        return new IntArrayVector((int) realSum);
    }
}

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

@Generic
@Primitive("as.complex")
public static Complex asComplex(@Recycle double x) {
    return new Complex(x, 0);
}

From source file:org.renjin.sexp.AbstractAtomicVector.java

@Override
public Complex getElementAsComplex(int index) {
    return new Complex(getElementAsDouble(index), 0);
}

From source file:petascope.wcps.server.core.SDU.java

public static List<Complex> str2complex(String string) {
    List<String> strings = str2string(string);

    List<Complex> complexes = new ArrayList<Complex>(strings.size());
    Iterator<String> i = strings.iterator();

    while (i.hasNext()) {
        String[] complex = i.next().split("#");

        complexes.add(new Complex(new Double(complex[0]), new Double(complex[1])));
    }/*w  w w  .  ja v a 2 s  . c  o m*/

    return complexes;

}

From source file:pl.dp.bz.poid.fouriertest.FourierProc.java

public FourierProc(BufferedImage image) {
    this.image = copyImage(image);

    this.imageWidth = image.getWidth();
    this.imageHeight = image.getHeight();

    fourierImage = new Complex[image.getWidth()][image.getHeight()];
    for (int i = 0; i < fourierImage.length; i++) {
        for (int j = 0; j < fourierImage[i].length; j++) {
            fourierImage[i][j] = new Complex(0.0, 0.0);
        }//from   w ww  . java 2 s  .co  m
    }
}

From source file:pl.dp.bz.poid.fouriertest.FourierProc.java

public void computeFourierTransform() {
    //Wprowadzenie pocztkowych danych
    for (int x = 0; x < image.getWidth(); x++) {
        for (int y = 0; y < image.getHeight(); y++) {
            double[] tab = new double[1];
            tab = image.getRaster().getPixel(x, y, tab);
            fourierImage[x][y] = new Complex(tab[0], 0.0);
        }//from   w ww  .  jav  a  2  s.c om
    }
    //Wiersze
    for (int i = 0; i < fourierImage.length; i++) {
        fourierImage[i] = computeDIFForOneDimension(fourierImage[i]);
    }
    //Kolumny
    fourierImage = convertTable(fourierImage);
    for (int i = 0; i < fourierImage.length; i++) {
        fourierImage[i] = computeDIFForOneDimension(fourierImage[i]);
    }
    fourierImage = convertTable(fourierImage);
    //        fourierImage = changeImageQuadrants(fourierImage);
}

From source file:pl.dp.bz.poid.fouriertest.FourierProc.java

/**
 * Robi filtr dolnoprzepustowy. Tworzy koo, na Fourierze, ktrego bok jest
 * odlegy od rodka o distanceFromCenter//  w  w  w . ja v  a2  s.  co  m
 *
 * @param radius
 */
public void computeHighPassFilter(int radius, int circleCenterX, int circleCenterY) {
    fourierImage = FourierProc.changeImageQuadrants(fourierImage);
    if (isCircleInImage(radius, circleCenterX, circleCenterY)) {
        for (int x = 0; x < imageWidth; x++) {
            for (int y = 0; y < imageHeight; y++) {
                if (computeDistance(circleCenterX, circleCenterY, x, y) <= radius) {
                    fourierImage[x][y] = new Complex(0, 0);
                }
            }
        }
    }
    fourierImage = FourierProc.changeImageQuadrants(fourierImage);
}