Search in sources :

Example 11 with IComplex

use of org.matheclipse.core.interfaces.IComplex in project symja_android_library by axkr.

the class DoubleFormFactory method convertTimesOperator.

private void convertTimesOperator(final StringBuilder buf, final IAST timesAST, final InfixOperator oper, final int precedence, boolean caller) {
    boolean showOperator = true;
    int currPrecedence = oper.getPrecedence();
    if (currPrecedence < precedence) {
        append(buf, "(");
    }
    if (timesAST.size() > 1) {
        IExpr arg1 = timesAST.arg1();
        if (arg1.isReal() && timesAST.size() > 2 && !timesAST.arg2().isNumber()) {
            if (arg1.isMinusOne()) {
                append(buf, "-");
                showOperator = false;
            } else {
                convertNumber(buf, (ISignedNumber) arg1, Precedence.PLUS, caller);
            }
        } else if (arg1.isComplex() && timesAST.size() > 2) {
            convertComplex(buf, (IComplex) arg1, oper.getPrecedence(), caller);
        } else {
            if (caller == PLUS_CALL) {
                append(buf, "+");
            }
            convertInternal(buf, arg1, oper.getPrecedence(), false);
        }
    }
    for (int i = 2; i < timesAST.size(); i++) {
        if (showOperator) {
            append(buf, oper.getOperatorString());
        } else {
            showOperator = true;
        }
        convertInternal(buf, timesAST.get(i), oper.getPrecedence(), false);
    }
    if (currPrecedence < precedence) {
        append(buf, ")");
    }
}
Also used : IComplex(org.matheclipse.core.interfaces.IComplex) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 12 with IComplex

use of org.matheclipse.core.interfaces.IComplex in project symja_android_library by axkr.

the class AbstractArgMultiple method binaryOperator.

@Override
public IExpr binaryOperator(final IExpr o0, final IExpr o1) {
    IExpr result = F.NIL;
    if (o0 instanceof INum) {
        // use specialized methods for numeric mode
        if (o1 instanceof INum) {
            result = e2DblArg((INum) o0, (INum) o1);
        } else if (o1.isInteger()) {
            result = e2DblArg((INum) o0, F.num((IInteger) o1));
        } else if (o1.isFraction()) {
            result = e2DblArg((INum) o0, F.num((IFraction) o1));
        } else if (o1 instanceof IComplexNum) {
            if (o0 instanceof ApfloatNum) {
                result = e2DblComArg(F.complexNum(((ApfloatNum) o0).apfloatValue()), (IComplexNum) o1);
            } else {
                result = e2DblComArg(F.complexNum(((INum) o0).getRealPart()), (IComplexNum) o1);
            }
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o1 instanceof INum) {
        // use specialized methods for numeric mode
        if (o0.isInteger()) {
            result = e2DblArg(F.num((IInteger) o0), (INum) o1);
        } else if (o0.isFraction()) {
            result = e2DblArg(F.num((IFraction) o0), (INum) o1);
        } else if (o0 instanceof IComplexNum) {
            if (o1 instanceof ApfloatNum) {
                result = e2DblComArg((IComplexNum) o0, F.complexNum(((ApfloatNum) o1).apfloatValue()));
            } else {
                result = e2DblComArg((IComplexNum) o0, F.complexNum(((INum) o1).getRealPart()));
            }
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    }
    if (o0 instanceof IComplexNum) {
        // use specialized methods for complex numeric mode
        if (o1 instanceof INum) {
            result = e2DblComArg((IComplexNum) o0, F.complexNum(((INum) o1).getRealPart()));
        } else if (o1.isInteger()) {
            result = e2DblComArg((IComplexNum) o0, F.complexNum((IInteger) o1));
        } else if (o1.isFraction()) {
            result = e2DblComArg((IComplexNum) o0, F.complexNum((IFraction) o1));
        } else if (o1 instanceof IComplexNum) {
            result = e2DblComArg((IComplexNum) o0, (IComplexNum) o1);
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o1 instanceof IComplexNum) {
        // use specialized methods for complex numeric mode
        if (o0 instanceof INum) {
            result = e2DblComArg(F.complexNum(((INum) o0).getRealPart()), (IComplexNum) o1);
        } else if (o0.isInteger()) {
            result = e2DblComArg(F.complexNum((IInteger) o0), (IComplexNum) o1);
        } else if (o0.isFraction()) {
            result = e2DblComArg(F.complexNum((IFraction) o0), (IComplexNum) o1);
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    }
    if (o0 instanceof IInteger) {
        if (o1 instanceof IInteger) {
            return e2IntArg((IInteger) o0, (IInteger) o1);
        }
        if (o1 instanceof IFraction) {
            return e2FraArg(F.fraction((IInteger) o0, F.C1), (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg(F.complex((IInteger) o0, F.C0), (IComplex) o1);
        }
    } else if (o0 instanceof IFraction) {
        if (o1 instanceof IInteger) {
            return e2FraArg((IFraction) o0, F.fraction((IInteger) o1, F.C1));
        }
        if (o1 instanceof IFraction) {
            return e2FraArg((IFraction) o0, (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg(F.complex((IFraction) o0), (IComplex) o1);
        }
    } else if (o0 instanceof IComplex) {
        if (o1 instanceof IInteger) {
            return eComIntArg((IComplex) o0, (IInteger) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg((IComplex) o0, (IComplex) o1);
        }
    }
    result = e2ObjArg(o0, o1);
    if (result.isPresent()) {
        return result;
    }
    if (o0 instanceof ISymbol) {
        if (o1 instanceof ISymbol) {
            return e2SymArg((ISymbol) o0, (ISymbol) o1);
        }
    }
    if (o0 instanceof IAST) {
        IAST a0 = (IAST) o0;
        if (o1 instanceof IInteger) {
            return eFunIntArg(a0, (IInteger) o1);
        }
        if (o1 instanceof IAST) {
            return e2FunArg(a0, (IAST) o1);
        }
    }
    return F.NIL;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IComplex(org.matheclipse.core.interfaces.IComplex) ISymbol(org.matheclipse.core.interfaces.ISymbol) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) INum(org.matheclipse.core.interfaces.INum)

Example 13 with IComplex

use of org.matheclipse.core.interfaces.IComplex in project symja_android_library by axkr.

the class AbstractArg2 method binaryOperator.

public IExpr binaryOperator(final IExpr o0, final IExpr o1) {
    IExpr result = F.NIL;
    if (o0.isNumber() && o1.isNumber()) {
        result = e2NumericArg(o0, o1);
        if (result.isPresent()) {
            return result;
        }
    }
    result = e2ObjArg(o0, o1);
    if (result.isPresent()) {
        return result;
    }
    if (o0 instanceof IInteger) {
        if (o1 instanceof IInteger) {
            return e2IntArg((IInteger) o0, (IInteger) o1);
        }
        if (o1 instanceof IFraction) {
            return e2FraArg(F.fraction((IInteger) o0, F.C1), (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg(F.complex((IInteger) o0, F.C0), (IComplex) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof IFraction) {
        if (o1 instanceof IInteger) {
            return e2FraArg((IFraction) o0, F.fraction((IInteger) o1, F.C1));
        }
        if (o1 instanceof IFraction) {
            return e2FraArg((IFraction) o0, (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg(F.complex((IFraction) o0), (IComplex) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof IComplex) {
        if (o1 instanceof IInteger) {
            return eComIntArg((IComplex) o0, (IInteger) o1);
        }
        if (o1 instanceof IFraction) {
            return eComFraArg((IComplex) o0, (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg((IComplex) o0, (IComplex) o1);
        }
    }
    if (o0 instanceof ISymbol) {
        if (o1 instanceof ISymbol) {
            return e2SymArg((ISymbol) o0, (ISymbol) o1);
        }
    }
    if (o0 instanceof IAST) {
        if (o1 instanceof IInteger) {
            return eFunIntArg((IAST) o0, (IInteger) o1);
        }
        if (o1 instanceof IAST) {
            return e2FunArg((IAST) o0, (IAST) o1);
        }
    }
    return F.NIL;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IComplex(org.matheclipse.core.interfaces.IComplex) ISymbol(org.matheclipse.core.interfaces.ISymbol) IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 14 with IComplex

use of org.matheclipse.core.interfaces.IComplex in project symja_android_library by axkr.

the class TeXFormFactory method convert.

public void convert(final StringBuffer buf, final Object o, final int precedence) {
    if (o instanceof IExpr) {
        IExpr expr = (IExpr) o;
        String str = CONSTANT_EXPRS.get(expr);
        if (str != null) {
            buf.append(str);
            return;
        }
    }
    if (o instanceof IAST) {
        final IAST f = ((IAST) o);
        IExpr h = f.head();
        if (h.isSymbol()) {
            IConverter converter = reflection(((ISymbol) h).getSymbolName());
            if ((converter != null) && (converter.convert(buf, f, precedence))) {
                return;
            }
        }
        convertAST(buf, f);
        return;
    }
    if (o instanceof IInteger) {
        convertInteger(buf, (IInteger) o, precedence);
        return;
    }
    if (o instanceof IFraction) {
        convertFraction(buf, (IFraction) o, precedence);
        return;
    }
    if (o instanceof INum) {
        convertDouble(buf, (INum) o, precedence);
        return;
    }
    if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence);
        return;
    }
    if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence);
        return;
    }
    if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
        return;
    }
    if (o instanceof BigFraction) {
        convertFraction(buf, (BigFraction) o, precedence);
        return;
    }
    convertString(buf, o.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IComplex(org.matheclipse.core.interfaces.IComplex) ISymbol(org.matheclipse.core.interfaces.ISymbol) BigFraction(org.hipparchus.fraction.BigFraction) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) INum(org.matheclipse.core.interfaces.INum)

Example 15 with IComplex

use of org.matheclipse.core.interfaces.IComplex in project symja_android_library by axkr.

the class OutputFormFactory method convert.

private void convert(final Appendable buf, final IExpr o, final int precedence, boolean isASTHead) throws IOException {
    if (o instanceof IAST) {
        final IAST list = (IAST) o;
        if (!list.isPresent()) {
            append(buf, "NIL");
            return;
        }
        if (o.isDataset()) {
            // TODO improve output
            buf.append(o.toString());
            return;
        } else if (o.isAssociation()) {
            convertAssociation(buf, (IAssociation) o);
            return;
        } else if (o.isAST(S.Association, 1)) {
            buf.append("<||>");
            return;
        }
        IExpr header = list.head();
        if (!header.isSymbol()) {
            // print expressions like: f(#1, y)& [x]
            IAST[] derivStruct = list.isDerivativeAST1();
            if (derivStruct != null) {
                IAST a1Head = derivStruct[0];
                IAST headAST = derivStruct[1];
                if (a1Head.isAST1() && a1Head.arg1().isInteger() && headAST.isAST1() && (headAST.arg1().isSymbol() || headAST.arg1().isAST()) && derivStruct[2] != null) {
                    try {
                        int n = ((IInteger) a1Head.arg1()).toInt();
                        if (n == 1 || n == 2) {
                            IExpr symbolOrAST = headAST.arg1();
                            convert(buf, symbolOrAST, Integer.MIN_VALUE, false);
                            if (n == 1) {
                                append(buf, "'");
                            } else if (n == 2) {
                                append(buf, "''");
                            }
                            convertArgs(buf, symbolOrAST, list);
                            return;
                        }
                    } catch (ArithmeticException ae) {
                    }
                }
            }
            convert(buf, header, Integer.MIN_VALUE, true);
            // avoid fast StackOverflow
            append(buf, "[");
            for (int i = 1; i < list.size(); i++) {
                convert(buf, list.get(i), Integer.MIN_VALUE, false);
                if (i < list.argSize()) {
                    append(buf, ",");
                }
            }
            append(buf, "]");
            return;
        }
        if (header.isSymbol()) {
            ISymbol head = (ISymbol) header;
            int functionID = head.ordinal();
            if (functionID > ID.UNKNOWN) {
                switch(functionID) {
                    case ID.TwoWayRule:
                    case ID.UndirectedEdge:
                        if (list.isAST2()) {
                            convert(buf, list.arg1(), Integer.MIN_VALUE, false);
                            buf.append("<->");
                            convert(buf, list.arg2(), Integer.MIN_VALUE, false);
                            return;
                        }
                        break;
                    case ID.DirectedEdge:
                        if (list.isAST2()) {
                            convert(buf, list.arg1(), Integer.MIN_VALUE, false);
                            buf.append("->");
                            convert(buf, list.arg2(), Integer.MIN_VALUE, false);
                            return;
                        }
                        break;
                }
            }
            final Operator operator = getOperator(head);
            if (operator != null) {
                if (operator instanceof PostfixOperator) {
                    if (list.isAST1()) {
                        convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
                        return;
                    }
                } else {
                    if (convertOperator(operator, list, buf, isASTHead ? Integer.MAX_VALUE : precedence, head)) {
                        return;
                    }
                }
            }
            if (functionID > ID.UNKNOWN) {
                switch(functionID) {
                    case ID.Inequality:
                        if (list.size() > 3 && convertInequality(buf, list, precedence)) {
                            return;
                        }
                        break;
                    case ID.Quantity:
                        // if (head.equals(F.SeriesData) && (list.size() == 7)) {
                        if (list instanceof IQuantity) {
                            if (convertQuantityData(buf, (IQuantity) list, precedence)) {
                                return;
                            }
                        }
                        break;
                    case ID.SeriesData:
                        // if (head.equals(F.SeriesData) && (list.size() == 7)) {
                        if (list instanceof ASTSeriesData) {
                            if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
                                return;
                            }
                        }
                        break;
                    case ID.SparseArray:
                        if (list.isSparseArray()) {
                            buf.append(list.toString());
                            return;
                        }
                        break;
                    case ID.Parenthesis:
                        convertArgs(buf, S.Parenthesis, list);
                        return;
                    case ID.List:
                        convertList(buf, list, false);
                        return;
                    case ID.MatrixForm:
                        if (list.isASTOrAssociation() && list.size() > 1) {
                            // see also MatrixForm in MathML or TeX format for "graphical representation".
                            IExpr normal = list.arg1().normal(false);
                            if (normal.isList()) {
                                // && normal.isMatrix() != null) {
                                IntList dims = LinearAlgebra.dimensions((IAST) normal, S.List);
                                convertList(buf, (IAST) normal, dims.size() >= 2);
                                return;
                            }
                            convert(buf, normal, Integer.MIN_VALUE, false);
                            return;
                        }
                        break;
                    case ID.Out:
                        if (list.isAST1() && list.arg1().isInteger()) {
                            int lineNumber = list.arg1().toIntDefault();
                            if (lineNumber == -1) {
                                buf.append("%");
                                return;
                            } else if (lineNumber == -2) {
                                buf.append("%%");
                                return;
                            }
                        }
                        break;
                    case ID.Part:
                        if (list.size() >= 3) {
                            convertPart(buf, list);
                            return;
                        }
                        break;
                    case ID.Slot:
                        if (list.isAST1() && list.arg1().isInteger()) {
                            convertSlot(buf, list);
                            return;
                        }
                        break;
                    case ID.SlotSequence:
                        if (list.isAST1() && list.arg1().isInteger()) {
                            convertSlotSequence(buf, list);
                            return;
                        }
                        break;
                    case ID.Defer:
                    case ID.HoldForm:
                        if (list.isAST1()) {
                            convert(buf, list.arg1(), Integer.MIN_VALUE, false);
                            return;
                        }
                        break;
                    case ID.DirectedInfinity:
                        if (list.isDirectedInfinity()) {
                            // head.equals(F.DirectedInfinity))
                            if (list.isAST0()) {
                                append(buf, "ComplexInfinity");
                                return;
                            }
                            if (list.isAST1()) {
                                if (list.arg1().isOne()) {
                                    append(buf, "Infinity");
                                    return;
                                } else if (list.arg1().isMinusOne()) {
                                    if (Precedence.PLUS < precedence) {
                                        append(buf, "(");
                                    }
                                    append(buf, "-Infinity");
                                    if (Precedence.PLUS < precedence) {
                                        append(buf, ")");
                                    }
                                    return;
                                } else if (list.arg1().isImaginaryUnit()) {
                                    append(buf, "I*Infinity");
                                    return;
                                } else if (list.arg1().isNegativeImaginaryUnit()) {
                                    append(buf, "-I*Infinity");
                                    return;
                                }
                            }
                        }
                        break;
                    case ID.Optional:
                        if (list.isAST2() && (list.arg1().isBlank() || list.arg1().isPattern())) {
                            convert(buf, list.arg1(), Integer.MIN_VALUE, false);
                            buf.append(":");
                            convert(buf, list.arg2(), Integer.MIN_VALUE, false);
                            return;
                        }
                        break;
                    case ID.Complex:
                        if (list.isAST2()) {
                            // used for visual comparison of steps
                            boolean isZeroRealPart = list.arg1().isZero();
                            final int prec = isZeroRealPart ? Precedence.TIMES : Precedence.PLUS;
                            if (prec < precedence) {
                                append(buf, "(");
                            }
                            if (isZeroRealPart) {
                                buf.append("I*");
                                convert(buf, list.arg2(), Precedence.TIMES, false);
                            } else {
                                convert(buf, list.arg1(), Precedence.PLUS, false);
                                buf.append("+I*");
                                convert(buf, list.arg2(), Precedence.TIMES, false);
                            }
                            if (prec < precedence) {
                                append(buf, ")");
                            }
                            return;
                        }
                        break;
                    case ID.Rational:
                        if (list.isAST2()) {
                            // used for visual comparison of steps
                            IExpr numerator = list.arg1();
                            final boolean isNegative = numerator.isNegative();
                            final int prec = isNegative ? Precedence.PLUS : Precedence.TIMES;
                            if (prec < precedence) {
                                append(buf, "(");
                            }
                            convert(buf, list.arg1(), Precedence.DIVIDE, false);
                            buf.append("/");
                            convert(buf, list.arg2(), Precedence.DIVIDE, false);
                            if (prec < precedence) {
                                append(buf, ")");
                            }
                            return;
                        }
                        break;
                }
            } else {
                if (list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
                    convertList(buf, list, false);
                    return;
                }
            }
        }
        convertAST(buf, list);
    } else if (o instanceof ISignedNumber) {
        convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
    } else if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
    } else if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
    } else if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
    } else if (o instanceof IPatternObject) {
        convertPattern(buf, (IPatternObject) o);
    } else if (o instanceof IStringX) {
        convertString(buf, ((IStringX) o).toString());
    } else {
        convertString(buf, o.toString());
    }
}
Also used : PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) Operator(org.matheclipse.parser.client.operator.Operator) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator) PrefixOperator(org.matheclipse.parser.client.operator.PrefixOperator) IAssociation(org.matheclipse.core.interfaces.IAssociation) ISymbol(org.matheclipse.core.interfaces.ISymbol) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IntList(it.unimi.dsi.fastutil.ints.IntList) IQuantity(org.matheclipse.core.tensor.qty.IQuantity) ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) IComplex(org.matheclipse.core.interfaces.IComplex) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) IStringX(org.matheclipse.core.interfaces.IStringX) ASTRealVector(org.matheclipse.core.expression.ASTRealVector)

Aggregations

IComplex (org.matheclipse.core.interfaces.IComplex)22 IExpr (org.matheclipse.core.interfaces.IExpr)18 IAST (org.matheclipse.core.interfaces.IAST)13 ISymbol (org.matheclipse.core.interfaces.ISymbol)11 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)10 IInteger (org.matheclipse.core.interfaces.IInteger)10 IFraction (org.matheclipse.core.interfaces.IFraction)9 INum (org.matheclipse.core.interfaces.INum)8 IRational (org.matheclipse.core.interfaces.IRational)6 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)4 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)4 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)3 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)3 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)2 ComplexNum (org.matheclipse.core.expression.ComplexNum)2 Num (org.matheclipse.core.expression.Num)2 INumber (org.matheclipse.core.interfaces.INumber)2 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)2 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)2 Operator (org.matheclipse.parser.client.operator.Operator)2