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

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

Introduction

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

Prototype

public double getImaginary() 

Source Link

Document

Access the imaginary part.

Usage

From source file:com.calc.BracerParser.java

/**
 * Evaluates once parsed math expression with "var" variable included
 *
 * @param variableValue User-specified <code>Double</code> value
 * @return <code>String</code> representation of the result
 * @throws <code>ParseException</code> if the input expression is not
 *                                     correct
 * @since 3.0/* w  w w  . j a v a  2s  . com*/
 */
public String evaluate(double variableValue) throws ParseException {
    /* check if is there something to evaluate */
    if (stackRPN.empty()) {
        return "";
    }

    /* clean answer stack */
    stackAnswer.clear();

    /* get the clone of the RPN stack for further evaluating */
    @SuppressWarnings("unchecked")
    Stack<String> stackRPN = (Stack<String>) this.stackRPN.clone();

    /* enroll the variable value into expression */
    Collections.replaceAll(stackRPN, VARIABLE, Double.toString(variableValue));

    /* evaluating the RPN expression */
    while (!stackRPN.empty()) {
        String token = stackRPN.pop();
        if (isNumber(token)) {
            stackAnswer.push(token);
        } else if (isOperator(token)) {
            Complex a = complexFormat.parse(stackAnswer.pop());
            Complex b = complexFormat.parse(stackAnswer.pop());
            boolean aBoolean = a.getReal() == 1.0;
            boolean bBoolean = b.getReal() == 1.0;
            switch (token) {
            case "+":
                stackAnswer.push(complexFormat.format(b.add(a)));
                break;
            case "-":
                stackAnswer.push(complexFormat.format(b.subtract(a)));
                break;
            case "*":
                stackAnswer.push(complexFormat.format(b.multiply(a)));
                break;
            case "/":
                stackAnswer.push(complexFormat.format(b.divide(a)));
                break;
            case "|":
                stackAnswer.push(String.valueOf(aBoolean || bBoolean ? "1" : "0"));
                break;
            case "&":
                stackAnswer.push(String.valueOf(aBoolean && bBoolean ? "1" : "0"));
                break;
            }
        } else if (isFunction(token)) {
            Complex a = complexFormat.parse(stackAnswer.pop());
            boolean aBoolean = a.getReal() == 1.0;
            switch (token) {
            case "fat":
                stackAnswer.push(complexFormat.format(a.abs()));
                break;
            case "fib":
                stackAnswer.push(complexFormat.format(a.acos()));
                break;
            case "arg":
                stackAnswer.push(complexFormat.format(a.getArgument()));
                break;
            case "asin":
                stackAnswer.push(complexFormat.format(a.asin()));
                break;
            case "atan":
                stackAnswer.push(complexFormat.format(a.atan()));
                break;
            case "conj":
                stackAnswer.push(complexFormat.format(a.conjugate()));
                break;
            case "cos":
                stackAnswer.push(complexFormat.format(a.cos()));
                break;
            case "cosh":
                stackAnswer.push(complexFormat.format(a.cosh()));
                break;
            case "exp":
                stackAnswer.push(complexFormat.format(a.exp()));
                break;
            case "imag":
                stackAnswer.push(complexFormat.format(a.getImaginary()));
                break;
            case "log":
                stackAnswer.push(complexFormat.format(a.log()));
                break;
            case "neg":
                stackAnswer.push(complexFormat.format(a.negate()));
                break;
            case "real":
                stackAnswer.push(complexFormat.format(a.getReal()));
                break;
            case "sin":
                stackAnswer.push(complexFormat.format(a.sin()));
                break;
            case "sinh":
                stackAnswer.push(complexFormat.format(a.sinh()));
                break;
            case "sqrt":
                stackAnswer.push(complexFormat.format(a.sqrt()));
                break;
            case "tan":
                stackAnswer.push(complexFormat.format(a.tan()));
                break;
            case "tanh":
                stackAnswer.push(complexFormat.format(a.tanh()));
                break;
            case "pow":
                Complex b = complexFormat.parse(stackAnswer.pop());
                stackAnswer.push(complexFormat.format(b.pow(a)));
                break;
            case "not":
                stackAnswer.push(String.valueOf(!aBoolean ? "1" : "0"));
                break;
            }
        }
    }

    if (stackAnswer.size() > 1) {
        throw new ParseException("Some operator is missing", 0);
    }

    return stackAnswer.pop();
}

From source file:org.briljantframework.array.AbstractComplexArray.java

@Override
public double[] data() {
    double[] data = new double[size() * 2];
    int j = 0;/*from w  ww  .  j a v a 2 s. co m*/
    for (int i = 0; i < size(); i++) {
        Complex c = get(i);
        data[j] = c.getReal();
        data[j + 1] = c.getImaginary();
        j += 2;
    }
    return data;
}

From source file:org.briljantframework.complex.MutableComplex.java

public MutableComplex(Complex complex) {
    this(complex.getReal(), complex.getImaginary());
}

From source file:org.briljantframework.complex.MutableComplex.java

public MutableComplex plus(Complex other) {
    if (isNaN) {/*from  w  ww. ja  va  2  s .  c om*/
        return this;
    }
    real += other.getReal();
    imag += other.getImaginary();
    return this;
}

From source file:org.briljantframework.complex.MutableComplex.java

public MutableComplex minus(Complex other) {
    if (isNaN) {//from  ww w .jav  a 2 s. co m
        return this;
    }
    real += other.getReal();
    imag += other.getImaginary();
    return this;
}

From source file:org.briljantframework.complex.MutableComplex.java

public MutableComplex multiply(Complex other) {
    if (isNaN) {/*from   ww  w. j av  a 2s .  com*/
        return this;
    }
    double oreal = other.getReal();
    double oimag = other.getImaginary();
    real = real * oreal - imag * oimag;
    imag = real * oimag + imag * oreal;
    return this;
}

From source file:org.briljantframework.complex.MutableComplex.java

public MutableComplex div(Complex other) {
    if (isNaN) {//from  w  w w  .  jav a  2 s.  c om
        return this;
    }
    double oreal = other.getReal();
    double oimag = other.getImaginary();
    if (Math.abs(oreal) < Math.abs(oimag)) {
        double q = oreal / oimag;
        double denominator = oreal * q + oimag;
        real = (real * q + imag) / denominator;
        imag = (imag * q - real) / denominator;
    } else {
        double q = oimag / oreal;
        double denominator = oimag * q + oreal;
        real = (imag * q + real) / denominator;
        imag = (imag - real * q) / denominator;
    }
    return this;
}

From source file:org.briljantframework.data.Is.java

/**
 * Check if value is NA/*from  w  w w  .  j  av a2 s. c o m*/
 *
 * @param value the value
 * @return true if value is NA
 */
public static boolean NA(Complex value) {
    if (value == null) {
        return true;
    } else {
        return Is.NA(value.getImaginary()) || Is.NA(value.getReal());
    }
}

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);
        copy.set(i, new Complex(c.getImaginary(), c.getReal()));
    }/*from www .j a  va 2s. c  o m*/
    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   www . j  a  va  2  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));
    }
}