/** * Add two integers, checking for overflow. * * @param x an addend * @param y an addend * @return the sum <code>x+y</code> * @throws org.matheclipse.parser.client.math.MathException if the result * can not be represented as an int * @since 1.1 */ public static int addAndCheck( int x, int y ) { long s = (long) x + (long) y; if ( s < Integer.MIN_VALUE || s > Integer.MAX_VALUE ) { throw new MathException( "overflow: add" ); } return (int) s; }
System.err.println(e.getMessage()); } catch (Exception e) { e.printStackTrace();
e.printStackTrace(); return e.getMessage(); } catch (final Exception e) { if (e instanceof ExceptionContextProvider) {
System.err.println( e.getMessage() );
/** * Multiply two integers, checking for overflow. * * @param x a factor * @param y a factor * @return the product <code>x*y</code> * @throws org.matheclipse.parser.client.math.MathException if the result * can not be represented as an int * @since 1.1 */ public static int mulAndCheck( int x, int y ) { long m = ((long) x) * ((long) y); if ( m < Integer.MIN_VALUE || m > Integer.MAX_VALUE ) { throw new MathException( "overflow: mul" ); } return (int) m; }
return e.getMessage();
/** * Hint: function does not check parameters for null, although null as input is likely to cause problems * subsequently. * * @param value * @param unit * for instance Unit.of("m*s^-1") * @return * @throws Exception * if value is instance of {@code Quantity} */ static IExpr of(IExpr value, IUnit unit) { if (value instanceof IQuantity) { // return value; throw MathException.of(value); } return QuantityImpl.of(value, unit); }
/** * Subtract two integers, checking for overflow. * * @param x * the minuend * @param y * the subtrahend * @return the difference <code>x-y</code> * @throws MathException * if the result can not be represented as an int * @since 1.1 */ public static int subAndCheck(int x, int y) { long s = (long) x - (long) y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { throw new MathException("overflow: subtract"); } return (int) s; }
@Override public IExpr evaluate(final IAST ast, EvalEngine engine) { Validate.checkRange(ast, 2, 3); try { if (ast.size() == 2) { IExpr arg1 = engine.evaluate(ast.arg1()); if (arg1.isString()) { return IQuantity.of(F.C1, IUnit.of(arg1.toString())); } } if (ast.size() == 3) { IExpr arg1 = engine.evaluate(ast.arg1()); IExpr arg2 = engine.evaluate(ast.arg2()); if (arg2.isString()) { return IQuantity.of(arg1, IUnit.of(arg2.toString())); } } } catch (MathException e) { engine.printMessage("Quantity: " + e.getMessage()); if (Config.SHOW_STACKTRACE) { e.printStackTrace(); } } return F.NIL; }
@Override // from Unit public IUnit multiply(IExpr factor) { if (factor instanceof ISignedNumber) { NavigableMap<String, IExpr> map = new TreeMap<>(); for (Entry<String, IExpr> entry : navigableMap.entrySet()) { // TODO this may not always use the defined UnitHelper.EvalEngine IExpr value = F.Times.of(UnitHelper.ENGINE, entry.getValue(), factor); if (!value.isZero()) map.put(entry.getKey(), value); } return new UnitImpl(map); } throw MathException.of(factor); }
/** * Subtract two integers, checking for overflow. * * @param x the minuend * @param y the subtrahend * @return the difference <code>x-y</code> * @throws org.matheclipse.parser.client.math.MathException if the result * can not be represented as an int * @since 1.1 */ public static int subAndCheck( int x, int y ) { long s = (long) x - (long) y; if ( s < Integer.MIN_VALUE || s > Integer.MAX_VALUE ) { throw new MathException( "overflow: subtract" ); } return (int) s; }
@Override public IExpr evaluate(final IAST ast, EvalEngine engine) { Validate.checkRange(ast, 2, 3); try { if (ast.size() == 2) { IExpr arg1 = engine.evaluate(ast.arg1()); if (arg1.isQuantity()) { return ((IAST) arg1).arg1(); } } else if (ast.size() == 3) { IExpr arg1 = engine.evaluate(ast.arg1()); IExpr arg2 = engine.evaluate(ast.arg2()); if (arg1.isQuantity()) { ch.ethz.idsc.tensor.qty.QuantityMagnitude quantityMagnitude = ch.ethz.idsc.tensor.qty.QuantityMagnitude .SI(); IUnit unit = IUnit.of(arg2.toString()); UnaryOperator<IExpr> suo = quantityMagnitude.in(unit); return suo.apply(arg1); } } } catch (MathException e) { engine.printMessage("QuantityMagnitude: " + e.getMessage()); if (Config.SHOW_STACKTRACE) { e.printStackTrace(); } } return F.NIL; }
public IExpr arcTan(IExpr x) { if (x instanceof IQuantity) { IQuantity quantity = (IQuantity) x; if (unit.equals(quantity.unit())) return F.ArcTan.of(quantity.value(), arg1); } throw MathException.of(x, this); }
/** * Add two integers, checking for overflow. * * @param x * an addend * @param y * an addend * @return the sum <code>x+y</code> * @throws MathException * if the result can not be represented as an int * @since 1.1 */ public static int addAndCheck(int x, int y) { long s = (long) x + (long) y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { throw new MathException("overflow: add"); } return (int) s; }
@Override public IExpr evaluate(final IAST ast, EvalEngine engine) { Validate.checkRange(ast, 2, 3); try { if (ast.size() == 2) { IExpr arg1 = engine.evaluate(ast.arg1()); if (arg1.isQuantity()) { return UnitSystem.SI().apply(arg1); } } else if (ast.size() == 3) { IExpr arg1 = engine.evaluate(ast.arg1()); IExpr arg2 = engine.evaluate(ast.arg2()); if (arg1.isQuantity()) { ch.ethz.idsc.tensor.qty.UnitConvert unitConvert = ch.ethz.idsc.tensor.qty.UnitConvert.SI(); IUnit unit = IUnit.of(arg2.toString()); return unitConvert.to(unit).apply(arg1); } } } catch (MathException e) { engine.printMessage("UnitConvert: " + e.getMessage()); if (Config.SHOW_STACKTRACE) { e.printStackTrace(); } } return F.NIL; }
/** * Hint: function does not check parameters for null, although null as input is likely to cause problems * subsequently. * * @param value * @param string * for instance "m*s^-2" * @return * @throws Exception * if value is instance of {@code Quantity} */ static IExpr of(IExpr value, String string) { if (value instanceof IQuantity) throw MathException.of(value); return QuantityImpl.of(value, IUnit.of(string)); }
/** * Multiply two integers, checking for overflow. * * @param x * a factor * @param y * a factor * @return the product <code>x*y</code> * @throws MathException * if the result can not be represented as an int * @since 1.1 */ public static int mulAndCheck(int x, int y) { long m = ((long) x) * ((long) y); if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) { throw new MathException("overflow: mul"); } return (int) m; }
public static IExpr evalApply(IExpr f, IExpr expr, IAST evaledAST, int lastIndex, boolean heads, EvalEngine engine) { java.util.function.Function<IExpr, IExpr> af = x -> x.isAST() ? ((IAST) x).setAtCopy(0, f) : F.NIL; try { VisitorLevelSpecification level = null; if (lastIndex == 3) { level = new VisitorLevelSpecification(af, evaledAST.get(lastIndex), heads, engine); } else { level = new VisitorLevelSpecification(af, 0); } if (!expr.isAtom()) { return expr.accept(level).orElse(expr); } else { // arg2 is an Atom to which the head f couldn't be applied if (evaledAST.size() >= 3) { if (f.isFunction()) { return F.unaryAST1(f, expr); } return expr; } } } catch (final MathException e) { engine.printMessage(e.getMessage()); } catch (final ArithmeticException e) { } return F.NIL; }
@Override public IExpr power(final IExpr exponent) { if (exponent instanceof IQuantity) { throw MathException.of(this, exponent); } return of(F.Power.of(arg1, exponent), unit.multiply(exponent)); }