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

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

Introduction

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

Prototype

public Complex atan() 

Source Link

Document

Compute the <a href="http://mathworld.wolfram.com/InverseTangent.html" TARGET="_top"> inverse tangent</a> of this complex number.

Usage

From source file:com.autsia.bracer.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 ww.j  a  va2s .  c  om
 */
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 "abs":
                stackAnswer.push(complexFormat.format(a.abs()));
                break;
            case "acos":
                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: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/*ww w  . ja  v  a  2  s . c o m*/
 */
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();
}