Search in sources :

Example 1 with IComplexNum

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

the class MathMLContentFormFactory method convert.

public void convert(final StringBuffer buf, final IExpr o, final int precedence) {
    if (o instanceof IAST) {
        final IAST f = ((IAST) o);
        // System.out.println(f.getHeader().toString());
        // IConverter converter = (IConverter)
        // operTab.get(f.getHeader().toString());
        // if (converter == null) {
        // converter = reflection(f.getHeader().toString());
        // if (converter == null || (converter.convert(buf, f, 0) == false))
        // {
        // convertHeadList(buf, f);
        // }
        // } else {
        // if (converter.convert(buf, f, precedence) == false) {
        // convertHeadList(buf, f);
        // }
        // }
        IAST ast = f;
        IAST temp;
        if (f.topHead().hasFlatAttribute()) {
            // associative
            if ((temp = EvalAttributes.flatten(f)).isPresent()) {
                ast = temp;
            }
        }
        IExpr h = ast.head();
        if (h.isSymbol()) {
            IConverter converter = reflection(((ISymbol) h).getSymbolName());
            if (converter != null) {
                StringBuffer sb = new StringBuffer();
                if (converter.convert(sb, ast, precedence)) {
                    buf.append(sb);
                    return;
                }
            }
        }
        convertAST(buf, ast);
        return;
    }
    if (o instanceof INum) {
        convertDouble(buf, (INum) o, precedence);
        return;
    }
    if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence);
        return;
    }
    if (o instanceof IInteger) {
        convertInteger(buf, (IInteger) o, precedence);
        return;
    }
    if (o instanceof IFraction) {
        convertFraction(buf, (IFraction) o, precedence);
        return;
    }
    if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence);
        return;
    }
    if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
        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) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) INum(org.matheclipse.core.interfaces.INum)

Example 2 with IComplexNum

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

the class MathMLFormFactory method convert.

public void convert(final StringBuffer buf, final IExpr o, final int precedence) {
    String str = CONSTANT_EXPRS.get(o);
    if (str != null) {
        buf.append(str);
        return;
    }
    if (o instanceof IAST) {
        final IAST f = ((IAST) o);
        IAST ast = f;
        IAST temp;
        if (f.topHead().hasFlatAttribute()) {
            // associative
            if ((temp = EvalAttributes.flatten(f)).isPresent()) {
                ast = temp;
            }
        }
        IExpr h = ast.head();
        if (h.isSymbol()) {
            IConverter converter = reflection(((ISymbol) h).getSymbolName());
            if (converter != null) {
                StringBuffer sb = new StringBuffer();
                if (converter.convert(sb, ast, precedence)) {
                    buf.append(sb);
                    return;
                }
            }
        }
        convertAST(buf, ast);
        return;
    }
    if (o instanceof INum) {
        convertDouble(buf, (INum) o, precedence);
        return;
    }
    if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence);
        return;
    }
    if (o instanceof IInteger) {
        convertInteger(buf, (IInteger) o, precedence);
        return;
    }
    if (o instanceof IFraction) {
        convertFraction(buf, (IFraction) o, precedence);
        return;
    }
    if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence);
        return;
    }
    if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
        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) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) INum(org.matheclipse.core.interfaces.INum)

Example 3 with IComplexNum

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

the class JASConvert method expr2Poly.

/**
 * Convert the given expression into a <a href="http://krum.rz.uni-mannheim.de/jas/">JAS</a>
 * polynomial
 *
 * @param exprPoly
 * @param numeric2Rational if <code>true</code>, <code>INum</code> double values are converted to
 *        <code>BigRational</code> internally
 * @return
 * @throws ArithmeticException
 * @throws JASConversionException
 */
private GenPolynomial<C> expr2Poly(final IExpr exprPoly, boolean numeric2Rational) throws ArithmeticException, JASConversionException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        if (ast.isSlot()) {
            try {
                return fPolyFactory.univariate(ast.toString(), 1L);
            } catch (IllegalArgumentException iae) {
            // fall through
            }
        } else {
            GenPolynomial<C> result = fPolyFactory.getZERO();
            GenPolynomial<C> p = fPolyFactory.getZERO();
            if (ast.isPlus()) {
                IExpr expr = ast.arg1();
                result = expr2Poly(expr, numeric2Rational);
                for (int i = 2; i < ast.size(); i++) {
                    expr = ast.get(i);
                    p = expr2Poly(expr, numeric2Rational);
                    result = result.sum(p);
                }
                return result;
            } else if (ast.isTimes()) {
                IExpr expr = ast.arg1();
                result = expr2Poly(expr, numeric2Rational);
                for (int i = 2; i < ast.size(); i++) {
                    expr = ast.get(i);
                    p = expr2Poly(expr, numeric2Rational);
                    result = result.multiply(p);
                }
                return result;
            } else if (ast.isPower() && ast.base().isSymbol()) {
                final ISymbol base = (ISymbol) ast.base();
                int exponent = ast.exponent().toIntDefault();
                if (exponent < 0) {
                    throw new JASConversionException();
                // "JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                try {
                    return fPolyFactory.univariate(base.getSymbolName(), exponent);
                } catch (IllegalArgumentException iae) {
                // fall through
                }
            } else if (ast.isPower() && ast.arg1().isSlot()) {
                final IAST base = (IAST) ast.arg1();
                int exponent = ast.exponent().toIntDefault();
                if (exponent < 0) {
                    throw new JASConversionException();
                // throw new ArithmeticException(
                // "JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                try {
                    return fPolyFactory.univariate(base.toString(), exponent);
                } catch (IllegalArgumentException iae) {
                // fall through
                }
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        try {
            if (exprPoly.isIndeterminate()) {
                throw new JASConversionException();
            }
            return fPolyFactory.univariate(((ISymbol) exprPoly).getSymbolName(), 1L);
        } catch (IllegalArgumentException iae) {
        // fall through
        }
    } else if (exprPoly instanceof IInteger) {
        return fPolyFactory.fromInteger((java.math.BigInteger) ((IInteger) exprPoly).asType(java.math.BigInteger.class));
    } else if (exprPoly instanceof IFraction) {
        return fraction2Poly((IFraction) exprPoly);
    } else if (exprPoly instanceof IComplex) {
        // may throw JASConversionException
        return complex2Poly((IComplex) exprPoly);
    } else if (exprPoly instanceof INum && numeric2Rational) {
        IFraction frac = F.fraction(((INum) exprPoly).getRealPart());
        return fraction2Poly(frac);
    } else if (exprPoly instanceof IComplexNum && numeric2Rational) {
        if (F.isZero(((IComplexNum) exprPoly).getImaginaryPart())) {
            // the imaginary part is zero
            IFraction frac = F.fraction(((INum) exprPoly).getRealPart());
            return fraction2Poly(frac);
        }
    }
    throw new JASConversionException();
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) ISymbol(org.matheclipse.core.interfaces.ISymbol) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) IComplex(org.matheclipse.core.interfaces.IComplex) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) INum(org.matheclipse.core.interfaces.INum)

Example 4 with IComplexNum

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

the class DoubleFormFactory method convertInternal.

private void convertInternal(final StringBuilder buf, final IExpr o, final int precedence, boolean isASTHead) {
    if (o instanceof IAST) {
        final IAST list = (IAST) o;
        // }
        if (list.head().isSymbol()) {
            ISymbol head = (ISymbol) list.head();
            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;
                    }
                }
            }
        // int functionID = head.ordinal();
        // if (functionID > ID.UNKNOWN) {
        // switch (functionID) {
        // 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.List:
        // convertList(buf, list);
        // return;
        // 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()) {
        // convertInternal(buf, list.arg1());
        // 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 (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
        // append(buf, "(");
        // }
        // append(buf, "-Infinity");
        // if (ASTNodeFactory.PLUS_PRECEDENCE < 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())) {
        // convertInternal(buf, list.arg1());
        // buf.append(":");
        // convertInternal(buf, list.arg2());
        // return;
        // }
        // break;
        // }
        // } else {
        // if (list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
        // convertList(buf, list);
        // return;
        // }
        // }
        }
        convertAST(buf, list);
        return;
    }
    if (o instanceof ISignedNumber) {
        convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
        return;
    }
    if (o instanceof IPatternObject) {
        convertPattern(buf, (IPatternObject) o);
        return;
    }
    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) IComplex(org.matheclipse.core.interfaces.IComplex) ISymbol(org.matheclipse.core.interfaces.ISymbol) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IAST(org.matheclipse.core.interfaces.IAST)

Example 5 with IComplexNum

use of org.matheclipse.core.interfaces.IComplexNum 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)

Aggregations

IComplexNum (org.matheclipse.core.interfaces.IComplexNum)12 IExpr (org.matheclipse.core.interfaces.IExpr)11 IAST (org.matheclipse.core.interfaces.IAST)9 IComplex (org.matheclipse.core.interfaces.IComplex)8 INum (org.matheclipse.core.interfaces.INum)8 ISymbol (org.matheclipse.core.interfaces.ISymbol)8 IInteger (org.matheclipse.core.interfaces.IInteger)7 IFraction (org.matheclipse.core.interfaces.IFraction)6 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)3 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 Apfloat (org.apfloat.Apfloat)2 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)2 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)2 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)2 Operator (org.matheclipse.parser.client.operator.Operator)2 PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)2 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1 FixedPrecisionApfloatHelper (org.apfloat.FixedPrecisionApfloatHelper)1