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.EvalTestCase.java

protected Complex complex(double real) {
    return new Complex(real, 0);
}

From source file:org.renjin.EvalTestCase.java

protected Complex complex(double real, double imaginary) {
    return new Complex(real, imaginary);
}

From source file:org.renjin.parser.RLexer.java

private SEXP mkComplex(String s) {
    SEXP t = Null.INSTANCE;/*from ww  w .j a  v  a 2 s .c om*/
    double f = ParseUtil.parseDouble(s);

    if (parseOptions.isGenerateCode()) {
        t = new ComplexArrayVector(new Complex(0, f));
    }

    return t;
}

From source file:org.renjin.parser.RLexer.java

private int lookupKeyword(String s) {
    int i;/* w ww  .  j ava 2 s.  c o m*/
    for (i = 0; i != keywords.length; i++) {
        if (keywords[i].name.equals(s)) {
            switch (keywords[i].token) {
            case NULL_CONST:
                yylval = Null.INSTANCE;
                break;
            case NUM_CONST:
                if (parseOptions.isGenerateCode()) {
                    switch (i) {
                    case 1:
                        yylval = new LogicalArrayVector(Logical.NA);
                        break;
                    case 2:
                        yylval = new LogicalArrayVector(true);
                        break;
                    case 3:
                        yylval = new LogicalArrayVector(false);
                        break;
                    case 4:
                        yylval = new DoubleArrayVector(Double.POSITIVE_INFINITY);
                        break;
                    case 5:
                        yylval = new DoubleArrayVector(Double.NaN);
                        break;
                    case 6:
                        yylval = new IntArrayVector(IntVector.NA);
                        break;
                    case 7:
                        yylval = new DoubleArrayVector(DoubleVector.NA);
                        break;
                    case 8:
                        yylval = StringVector.valueOf(StringVector.NA);
                        break;
                    case 9:
                        yylval = new ComplexArrayVector(new Complex(DoubleVector.NA, DoubleVector.NA));
                        break;
                    }
                } else {
                    yylval = Null.INSTANCE;
                }
                break;
            case FUNCTION:
            case WHILE:
            case REPEAT:
            case FOR:
            case IF:
            case NEXT:
            case BREAK:
                yylval = Symbol.get(s);
                break;
            case IN:
            case ELSE:
                break;
            case SYMBOL:
                yylval = Symbol.get(s);
                break;
            }
            return keywords[i].token;
        }
    }
    return 0;
}

From source file:org.renjin.primitives.io.serialization.RDataReader.java

private SEXP readComplexExp(int flags) throws IOException {
    int length = in.readInt();
    Complex[] values = new Complex[length];
    for (int i = 0; i != length; ++i) {
        values[i] = new Complex(in.readDouble(), in.readDouble());
    }//from  w w  w  .j a  v a 2 s  . c  om
    return new ComplexArrayVector(values, readAttributes(flags));
}

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

@Builtin
@Deferrable/*w  ww .jav a  2  s .  c  o  m*/
@DataParallel
public static Complex round(Complex x, int digits) {
    return new Complex(round(x.getReal(), digits), round(x.getImaginary(), digits));
}

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

@Test
public void complexExp() {
    assertThat(eval("exp(2+1i)"), closeTo(new Complex(3.992324, 6.217676), 0.00001));
    assertThat(eval("exp(4+3i)"), closeTo(new Complex(-54.05176, 7.70489), 0.00001));

}

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

@Builtin("-")
@DataParallel(PreserveAttributeStyle.ALL)
public static Complex negative(Complex x) {
    return new Complex(-x.getReal(), -x.getImaginary());
}

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

@Builtin("/")
@DataParallel(PreserveAttributeStyle.ALL)
public static Complex divide(Complex dividend, Complex divisor) {
    // LICENSE: transcribed code from GCC, which is licensed under GPL
    // libgcc2 - Translated by Tomas Kalibera
    // The Apache Commons math version does not handle edge cases
    // exactly the same as R/GCC does.

    double a = dividend.getReal();
    double b = dividend.getImaginary();
    double c = divisor.getReal();
    double d = divisor.getImaginary();

    double x;//  w  w  w  .  j  a  va2  s  . c  om
    double y;

    if (Math.abs(c) < Math.abs(d)) {
        double ratio = c / d;
        double denominator = (c * ratio) + d;
        x = ((a * ratio) + b) / denominator;
        y = ((b * ratio) - a) / denominator;
    } else {
        double ratio = d / c;
        double denominator = (d * ratio) + c;
        x = ((b * ratio) + a) / denominator;
        y = (b - (a * ratio)) / denominator;
    }

    if (isNaN(x) && isNaN(y)) {
        if (c == 0.0 && d == 0.0 && (!isNaN(a) || !isNaN(b))) {
            x = copySign(Double.POSITIVE_INFINITY, c) * a;
            y = copySign(Double.POSITIVE_INFINITY, c) * b;

        } else if ((isInfinite(a) || isInfinite(b)) && isFinite(c) && isFinite(d)) {
            double ra = convertInf(a);
            double rb = convertInf(b);
            x = Double.POSITIVE_INFINITY * (ra * c + rb * d);
            y = Double.POSITIVE_INFINITY * (rb * c - ra * d);

        } else if ((isInfinite(c) || isInfinite(d)) && isFinite(a) && isFinite(b)) {
            double rc = convertInf(c);
            double rd = convertInf(d);
            x = 0.0 * (a * rc + b * rd);
            y = 0.0 * (b * rc - a * rd);
        }
    }
    return new Complex(x, y);
}

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

@Builtin("*")
@DataParallel(PreserveAttributeStyle.ALL)
public static Complex multiply(Complex x, Complex y) {
    // LICENSE: transcribed code from GCC, which is licensed under GPL
    // libgcc2 - Adapted by Tomas Kalibera
    // The Apache Commons math version does not handle edge cases
    // exactly the same as R/GCC does.

    double a = x.getReal();
    double b = x.getImaginary();
    double c = y.getReal();
    double d = y.getImaginary();

    double ac = a * c;
    double bd = b * d;
    double bc = b * c;
    double ad = a * d;

    double real = ac - bd;
    double imag = bc + ad;

    if (Double.isNaN(real) && Double.isNaN(imag)) {
        boolean recalc = false;
        double ra = a;
        double rb = b;
        double rc = c;
        double rd = d;
        if (Double.isInfinite(ra) || Double.isInfinite(rb)) {
            ra = convertInf(ra);//from  ww w. j  a va2  s  .c o  m
            rb = convertInf(rb);
            rc = convertNaN(rc);
            rd = convertNaN(rd);
            recalc = true;
        }
        if (Double.isInfinite(rc) || Double.isInfinite(rd)) {
            rc = convertInf(rc);
            rd = convertInf(rd);
            ra = convertNaN(ra);
            rb = convertNaN(rb);
            recalc = true;
        }
        if (!recalc && (Double.isInfinite(ac) || Double.isInfinite(bd) || Double.isInfinite(ad)
                || Double.isInfinite(bc))) {
            ra = convertNaN(ra);
            rb = convertNaN(rb);
            rc = convertNaN(rc);
            rd = convertNaN(rd);
            recalc = true;
        }
        if (recalc) {
            real = Double.POSITIVE_INFINITY * (ra * rc - rb * rd);
            imag = Double.POSITIVE_INFINITY * (ra * rd + rb * rc);
        }
    }
    return new Complex(real, imag);
}