Search in sources :

Example 11 with INumber

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

the class OutputFormFactory method convertApplyOperator.

public void convertApplyOperator(final Appendable buf, final IAST list, final InfixOperator oper, final int precedence) throws IOException {
    IExpr arg2 = list.arg2();
    if (arg2.isNumber()) {
        INumber exp = (INumber) arg2;
        if (exp.complexSign() < 0) {
            if (ASTNodeFactory.APPLY_PRECEDENCE < precedence) {
                append(buf, "(");
            }
            append(buf, "1/");
            if (exp.isMinusOne()) {
                convert(buf, list.arg1(), ASTNodeFactory.DIVIDE_PRECEDENCE, false);
                if (ASTNodeFactory.DIVIDE_PRECEDENCE < precedence) {
                    append(buf, ")");
                }
                return;
            }
            // flip presign of the exponent
            IAST pow = list.setAtCopy(2, exp.opposite());
            convertPowerOperator(buf, pow, oper, ASTNodeFactory.DIVIDE_PRECEDENCE);
            if (ASTNodeFactory.APPLY_PRECEDENCE < precedence) {
                append(buf, ")");
            }
            return;
        }
    }
    convertInfixOperator(buf, list, oper, precedence);
}
Also used : INumber(org.matheclipse.core.interfaces.INumber) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 12 with INumber

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

the class OutputFormFactory method convertSeriesData.

/**
	 * Convert a <code>SeriesData[...]</code> expression.
	 * 
	 * @param buf
	 * @param seriesData
	 *            <code>SeriesData[x, x0, list, nmin, nmax, den]</code> expression
	 * @param precedence
	 *            the precedence of the parent expression
	 * @return <code>true</code> if the conversion was successful
	 * @throws IOException
	 */
public boolean convertSeriesData(final Appendable buf, final IAST seriesData, final int precedence) throws IOException {
    int operPrecedence = ASTNodeFactory.PLUS_PRECEDENCE;
    StringBuilder tempBuffer = new StringBuilder();
    if (operPrecedence < precedence) {
        append(tempBuffer, "(");
    }
    try {
        IExpr plusArg;
        // SeriesData[x, x0, list, nmin, nmax, den]
        IExpr x = seriesData.arg1();
        IExpr x0 = seriesData.arg2();
        IAST list = (IAST) seriesData.arg3();
        long nmin = ((IInteger) seriesData.arg4()).toLong();
        long nmax = ((IInteger) seriesData.arg5()).toLong();
        long den = ((IInteger) seriesData.get(6)).toLong();
        int size = list.size();
        boolean call = NO_PLUS_CALL;
        if (size > 0) {
            INumber exp = F.fraction(nmin, den).normalize();
            IExpr pow = x.subtract(x0).power(exp);
            call = convertSeriesDataArg(tempBuffer, list.arg1(), pow, call);
            for (int i = 2; i < size; i++) {
                exp = F.fraction(nmin + i - 1L, den).normalize();
                pow = x.subtract(x0).power(exp);
                call = convertSeriesDataArg(tempBuffer, list.get(i), pow, call);
            }
            plusArg = F.Power(F.O(x), F.fraction(nmax, den).normalize());
            if (!plusArg.isZero()) {
                convertPlusArgument(tempBuffer, plusArg, call);
                call = PLUS_CALL;
            }
        } else {
            return false;
        }
    } catch (Exception ex) {
        return false;
    }
    if (operPrecedence < precedence) {
        append(tempBuffer, ")");
    }
    buf.append(tempBuffer);
    return true;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) INumber(org.matheclipse.core.interfaces.INumber) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IOException(java.io.IOException)

Example 13 with INumber

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

the class OutputFormFactory method convertPowerOperator.

public void convertPowerOperator(final Appendable buf, final IAST list, final InfixOperator oper, final int precedence) throws IOException {
    IExpr arg2 = list.arg2();
    if (arg2.isNumber()) {
        INumber exp = (INumber) arg2;
        if (exp.isNumEqualRational(F.C1D2)) {
            append(buf, "Sqrt");
            if (fRelaxedSyntax) {
                append(buf, "(");
            } else {
                append(buf, "[");
            }
            convert(buf, list.arg1(), 0, false);
            if (fRelaxedSyntax) {
                append(buf, ")");
            } else {
                append(buf, "]");
            }
            return;
        }
        if (exp.complexSign() < 0) {
            if (ASTNodeFactory.DIVIDE_PRECEDENCE < precedence) {
                append(buf, "(");
            }
            append(buf, "1/");
            if (exp.isMinusOne()) {
                convert(buf, list.arg1(), ASTNodeFactory.DIVIDE_PRECEDENCE, false);
                if (ASTNodeFactory.DIVIDE_PRECEDENCE < precedence) {
                    append(buf, ")");
                }
                return;
            }
            // flip presign of the exponent
            IAST pow = list.setAtCopy(2, exp.opposite());
            convertPowerOperator(buf, pow, oper, ASTNodeFactory.DIVIDE_PRECEDENCE);
            if (ASTNodeFactory.DIVIDE_PRECEDENCE < precedence) {
                append(buf, ")");
            }
            return;
        }
    }
    convertInfixOperator(buf, list, oper, precedence);
}
Also used : INumber(org.matheclipse.core.interfaces.INumber) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 14 with INumber

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

the class OutputFormFactory method convertTimesFraction.

private void convertTimesFraction(final Appendable buf, final IAST timesAST, final InfixOperator oper, final int precedence, boolean caller) throws IOException {
    IExpr[] parts = Algebra.fractionalPartsTimesPower(timesAST, true, false, false, false);
    if (parts == null) {
        convertTimesOperator(buf, timesAST, oper, precedence, caller);
        return;
    }
    final IExpr numerator = parts[0];
    final IExpr denominator = parts[1];
    if (!denominator.isOne()) {
        int currPrecedence = oper.getPrecedence();
        if (currPrecedence < precedence) {
            append(buf, "(");
        }
        final IExpr fraction = parts[2];
        if (fraction != null) {
            convertNumber(buf, (ISignedNumber) fraction, ASTNodeFactory.PLUS_PRECEDENCE, caller);
            append(buf, "*");
        }
        if (numerator.isSignedNumber()) {
            convertNumber(buf, (ISignedNumber) numerator, ASTNodeFactory.PLUS_PRECEDENCE, caller);
        } else if (numerator.isComplex() || numerator.isComplexNumeric()) {
            convertNumber(buf, (INumber) numerator, ASTNodeFactory.DIVIDE_PRECEDENCE, caller);
        } else {
            if (numerator.isTimes() && numerator.isAST2() && ((IAST) numerator).arg1().isMinusOne()) {
                append(buf, "-");
                convert(buf, ((IAST) numerator).arg2(), ASTNodeFactory.TIMES_PRECEDENCE, false);
            } else {
                if (caller == PLUS_CALL) {
                    append(buf, "+");
                }
                // insert numerator in buffer:
                if (numerator.isTimes()) {
                    convertTimesOperator(buf, (IAST) numerator, oper, ASTNodeFactory.DIVIDE_PRECEDENCE, NO_PLUS_CALL);
                } else {
                    convert(buf, numerator, ASTNodeFactory.DIVIDE_PRECEDENCE, false);
                }
            }
        }
        append(buf, "/");
        // insert denominator in buffer:
        if (denominator.isTimes()) {
            convertTimesOperator(buf, (IAST) denominator, oper, ASTNodeFactory.DIVIDE_PRECEDENCE, NO_PLUS_CALL);
        } else {
            convert(buf, denominator, ASTNodeFactory.DIVIDE_PRECEDENCE, false);
        }
        if (currPrecedence < precedence) {
            append(buf, ")");
        }
        return;
    }
    convertTimesOperator(buf, timesAST, oper, precedence, caller);
}
Also used : INumber(org.matheclipse.core.interfaces.INumber) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

INumber (org.matheclipse.core.interfaces.INumber)14 IExpr (org.matheclipse.core.interfaces.IExpr)12 IAST (org.matheclipse.core.interfaces.IAST)11 IInteger (org.matheclipse.core.interfaces.IInteger)4 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 IOException (java.io.IOException)1 Apfloat (org.apfloat.Apfloat)1 Apint (org.apfloat.Apint)1 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)1 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)1 ComplexNum (org.matheclipse.core.expression.ComplexNum)1 Num (org.matheclipse.core.expression.Num)1 IComplex (org.matheclipse.core.interfaces.IComplex)1 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)1 IFraction (org.matheclipse.core.interfaces.IFraction)1 INum (org.matheclipse.core.interfaces.INum)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1 ASTNode (org.matheclipse.parser.client.ast.ASTNode)1 FloatNode (org.matheclipse.parser.client.ast.FloatNode)1