Search in sources :

Example 11 with INum

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

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

the class Validate method checkListOfLongs.

/**
	 * Check the argument, if it's an {@code IAST} of {@code long} values in the range [{@code startValue},
	 * Long.MAX_VALUE]
	 * 
	 * @throws WrongArgumentType
	 */
public static long[] checkListOfLongs(IExpr arg, long startValue) {
    if (arg.isList()) {
        IAST list = (IAST) arg;
        long[] result = new long[list.size() - 1];
        long longValue = 0;
        try {
            IExpr expr;
            for (int i = 1; i < list.size(); i++) {
                expr = list.get(i);
                // the following may throw an ArithmeticException
                if (expr instanceof IInteger) {
                    longValue = ((IInteger) expr).toLong();
                } else if (expr instanceof INum) {
                    longValue = ((INum) expr).toLong();
                }
                if (startValue > longValue) {
                    throw new WrongArgumentType(expr, "Trying to convert the expression into the integer range: " + startValue + " - " + Long.MAX_VALUE);
                }
                result[i - 1] = longValue;
            }
            return result;
        } catch (ArithmeticException ae) {
        //
        }
    }
    throw new WrongArgumentType(arg, "Trying to convert the given list into a list of long numbers: " + arg);
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) INum(org.matheclipse.core.interfaces.INum)

Example 13 with INum

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

the class AbstractTrigArg1 method numericEval.

@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 2);
    IExpr arg1 = ast.arg1();
    if (arg1 instanceof INum) {
        if (arg1 instanceof ApfloatNum) {
            return e1ApfloatArg(((ApfloatNum) arg1).apfloatValue());
        }
        return e1DblArg(((Num) arg1).doubleValue());
    }
    if (arg1 instanceof IComplexNum) {
        if (arg1 instanceof ApcomplexNum) {
            return e1ApcomplexArg(((ApcomplexNum) arg1).apcomplexValue());
        }
        return e1ComplexArg(((ComplexNum) arg1).complexValue());
    }
    return evaluateArg1(arg1);
}
Also used : IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IExpr(org.matheclipse.core.interfaces.IExpr) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum) INum(org.matheclipse.core.interfaces.INum)

Example 14 with INum

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

the class Expr2Object method toPolynomialMap.

/**
	 * 
	 * @param expr
	 * @param sym
	 * @return <code>null</code> if the expression couldn't be converted to a
	 *         polynomial.
	 */
public static OpenIntToDoubleHashMap toPolynomialMap(IExpr expr, ISymbol sym) {
    try {
        OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap();
        if (expr.isPlus()) {
            IAST plus = (IAST) expr;
            for (int i = 1; i < plus.size(); i++) {
                if (plus.get(i).isTimes()) {
                    IAST times = (IAST) plus.get(i);
                    double coeff = 0;
                    int exp = -1;
                    for (int j = 1; j < times.size(); j++) {
                        if (times.get(j).isPower()) {
                            IAST power = (IAST) times.get(j);
                            if (power.arg1().equals(sym)) {
                                if (exp != (-1)) {
                                    return null;
                                }
                                IExpr res = F.evaln(power.arg2());
                                if (!(res instanceof INum)) {
                                    return null;
                                }
                                exp = DoubleMath.roundToInt(((INum) res).doubleValue(), RoundingMode.UNNECESSARY);
                                if (exp < 0) {
                                    return null;
                                }
                                continue;
                            }
                        } else if (times.get(j).isSymbol()) {
                            if (times.get(j).equals(sym)) {
                                if (exp != (-1)) {
                                    return null;
                                }
                                exp = 1;
                                continue;
                            }
                        }
                        if (times.get(j) instanceof INum) {
                            coeff += ((INum) times.get(j)).doubleValue();
                            continue;
                        }
                        IExpr res = F.evaln(times.get(j));
                        if (!(res instanceof INum)) {
                            return null;
                        }
                        coeff += ((INum) res).doubleValue();
                    }
                    if (exp == (-1)) {
                        exp = 0;
                    }
                    addCoefficient(map, coeff, exp);
                    continue;
                } else if (plus.get(i).isPower()) {
                    IAST power = (IAST) plus.get(i);
                    if (power.arg1().equals(sym)) {
                        IExpr res = F.evaln(power.arg2());
                        if (!(res instanceof INum)) {
                            return null;
                        }
                        int exp = DoubleMath.roundToInt(((INum) res).doubleValue(), RoundingMode.UNNECESSARY);
                        if (exp < 0) {
                            return null;
                        }
                        addCoefficient(map, 1.0, exp);
                        continue;
                    }
                    return null;
                } else if (plus.get(i).isSymbol()) {
                    if (plus.equalsAt(i, sym)) {
                        addCoefficient(map, 1.0, 1);
                        continue;
                    }
                    return null;
                }
                if (plus.get(i) instanceof INum) {
                    addCoefficient(map, ((INum) plus.get(i)).doubleValue(), 0);
                    continue;
                }
                IExpr res = F.evaln(plus.get(i));
                if (!(res instanceof INum)) {
                    return null;
                }
                addCoefficient(map, ((INum) res).doubleValue(), 0);
            }
            return map;
        }
    } catch (Exception ex) {
    }
    return null;
}
Also used : IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) OpenIntToDoubleHashMap(org.hipparchus.util.OpenIntToDoubleHashMap) INum(org.matheclipse.core.interfaces.INum)

Example 15 with INum

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

Aggregations

INum (org.matheclipse.core.interfaces.INum)16 IExpr (org.matheclipse.core.interfaces.IExpr)15 IAST (org.matheclipse.core.interfaces.IAST)12 IInteger (org.matheclipse.core.interfaces.IInteger)9 ISymbol (org.matheclipse.core.interfaces.ISymbol)9 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 IFraction (org.matheclipse.core.interfaces.IFraction)6 IComplex (org.matheclipse.core.interfaces.IComplex)4 IRational (org.matheclipse.core.interfaces.IRational)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)2 Num (org.matheclipse.core.expression.Num)2 BigFraction (org.hipparchus.fraction.BigFraction)1 OpenIntToDoubleHashMap (org.hipparchus.util.OpenIntToDoubleHashMap)1 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1