Search in sources :

Example 26 with IFraction

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

the class ASCIIPrettyPrinter3 method convertNumber.

/**
	 * 
	 * @param number
	 * @param caller
	 * @return <code>true</code> if the number is not equal <code>-1</code> or <code>1</code>
	 */
private boolean convertNumber(ISignedNumber number, boolean caller) {
    boolean negative = false;
    if (number.isNegative()) {
        number = number.negate();
        negative = true;
    }
    if (caller == PLUS_CALL) {
        if (negative) {
            print(" - ");
        } else {
            print(" + ");
        }
        if (!number.isOne()) {
            if (number.isFraction()) {
                IFraction frac = (IFraction) number;
                printFraction(frac.toBigNumerator().toString(), frac.toBigDenominator().toString());
            } else {
                convert(number, 0, NO_PLUS_CALL);
            }
            return true;
        }
    } else {
        if (number.isFraction()) {
            if (negative) {
                if (fractionPrinted) {
                    print(" - ");
                } else {
                    print("- ");
                }
            }
            IFraction frac = (IFraction) number;
            printFraction(frac.toBigNumerator().toString(), frac.toBigDenominator().toString());
        } else {
            if (negative) {
                print("-");
            }
            print(number.toString());
        }
        return true;
    }
    return false;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction)

Example 27 with IFraction

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

the class Power method convert.

/**
	 * Converts a given function into the corresponding MathML output
	 * 
	 * @param buf
	 *            StringBuffer for MathML output
	 * @param f
	 *            The math function which should be converted to MathML
	 */
@Override
public boolean convert(final StringBuffer buf, final IAST f, final int precedence) {
    if (f.size() != 3) {
        return false;
    }
    IExpr arg1 = f.arg1();
    IExpr arg2 = f.arg2();
    IExpr exp = arg2;
    IExpr den = F.C1;
    int useMROOT = 0;
    if (arg2.isFraction()) {
        IFraction f2 = ((IFraction) arg2);
        if (f2.isPositive()) {
            exp = f2.getNumerator();
            if (f2.equals(F.C1D2)) {
                fFactory.tagStart(buf, "msqrt");
                useMROOT = 1;
            } else {
                den = f2.getDenominator();
                fFactory.tagStart(buf, "mroot");
                useMROOT = 2;
            }
        }
    }
    if (useMROOT > 0 && exp.isOne()) {
        fFactory.convert(buf, arg1, fPrecedence);
    } else {
        fFactory.tagStart(buf, "msup");
        precedenceOpen(buf, precedence);
        fFactory.convert(buf, arg1, fPrecedence);
        fFactory.convert(buf, exp, fPrecedence);
        precedenceClose(buf, precedence);
        fFactory.tagEnd(buf, "msup");
    }
    if (useMROOT == 1) {
        fFactory.tagEnd(buf, "msqrt");
    } else if (useMROOT == 2) {
        fFactory.convert(buf, den, fPrecedence);
        fFactory.tagEnd(buf, "mroot");
    }
    return true;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 28 with IFraction

use of org.matheclipse.core.interfaces.IFraction 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 29 with IFraction

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

the class ContinuedFraction method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 2, 3);
    IExpr arg1 = ast.arg1();
    int maxIterations = Integer.MAX_VALUE;
    if (ast.isAST2() && ast.arg2().isInteger()) {
        maxIterations = Validate.checkIntType(ast, 2);
    }
    if (arg1 instanceof INum) {
        arg1 = F.fraction(((INum) arg1).getRealPart());
    } else if (arg1.isAST() || arg1.isSymbol() && arg1.isNumericFunction()) {
        IExpr num = engine.evalN(arg1);
        if (num instanceof INum) {
            arg1 = F.fraction(((INum) num).getRealPart());
        }
    }
    if (arg1.isRational()) {
        IRational rat = (IRational) arg1;
        IAST continuedFractionList;
        if (rat.getDenominator().isOne()) {
            continuedFractionList = F.List(rat.getNumerator());
        } else if (rat.getNumerator().isOne()) {
            continuedFractionList = F.ListAlloc(2);
            continuedFractionList.append(F.C0);
            continuedFractionList.append(rat.getDenominator());
        } else {
            IFraction temp = F.fraction(rat.getNumerator(), rat.getDenominator());
            IInteger quotient;
            IInteger remainder;
            continuedFractionList = F.ListAlloc(10);
            while (temp.getDenominator().compareInt(1) > 0 && (0 < maxIterations--)) {
                quotient = temp.getNumerator().div(temp.getDenominator());
                remainder = temp.getNumerator().mod(temp.getDenominator());
                continuedFractionList.append(quotient);
                temp = F.fraction(temp.getDenominator(), remainder);
                if (temp.getDenominator().isOne()) {
                    continuedFractionList.append(temp.getNumerator());
                }
            }
        }
        return continuedFractionList;
    }
    return F.NIL;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IInteger(org.matheclipse.core.interfaces.IInteger) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) INum(org.matheclipse.core.interfaces.INum)

Aggregations

IFraction (org.matheclipse.core.interfaces.IFraction)29 IInteger (org.matheclipse.core.interfaces.IInteger)16 IExpr (org.matheclipse.core.interfaces.IExpr)14 IAST (org.matheclipse.core.interfaces.IAST)11 ISymbol (org.matheclipse.core.interfaces.ISymbol)9 BigInteger (java.math.BigInteger)8 INum (org.matheclipse.core.interfaces.INum)7 IComplex (org.matheclipse.core.interfaces.IComplex)6 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)6 GenPolynomial (edu.jas.poly.GenPolynomial)3 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)3 ExpVector (edu.jas.poly.ExpVector)2 BigFraction (org.hipparchus.fraction.BigFraction)2 JASIExpr (org.matheclipse.core.convert.JASIExpr)2 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)2 IRational (org.matheclipse.core.interfaces.IRational)2 BigInteger (edu.jas.arith.BigInteger)1 BigRational (edu.jas.arith.BigRational)1 ModLong (edu.jas.arith.ModLong)1 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)1