/** * 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); }
@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); }
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); }
/** * 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)); }
@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)); }
/** * @param vector * with {@link IExpr} entries as {R, G, B, A} * @return encoding color as 0xAA:RR:GG:BB * @throws Exception * if either color value is outside the allowed range [0, ..., 255] */ public static Color toColor(IExpr vector) { if (vector.argSize() != 4) { throw MathException.of(vector); } IAST v=(IAST)vector; return new Color( // v.get(1).toIntDefault(Integer.MIN_VALUE), // v.get(2).toIntDefault(Integer.MIN_VALUE), // v.get(3).toIntDefault(Integer.MIN_VALUE), // v.get(4).toIntDefault(Integer.MIN_VALUE)); }
@Override public IExpr plus(final IExpr scalar) { boolean azero = arg1.isZero(); boolean bzero = scalar.isZero(); if (azero && !bzero) return scalar; // 0[m] + X(X!=0) gives X(X!=0) if (!azero && bzero) return this; // X(X!=0) + 0[m] gives X(X!=0) /** at this point the implication holds: azero == bzero */ if (scalar instanceof IQuantity) { IQuantity quantity = (IQuantity) scalar; if (unit.equals(quantity.unit())) return ofUnit(arg1.add(quantity.value())); // 0[m] + 0[m] gives 0[m] else if (azero) // explicit addition of zeros to ensure symmetry // for instance when numeric precision is different return arg1.add(quantity.value()); // 0[m] + 0[s] gives 0 } else // <- scalar is not an instance of Quantity if (azero) // return of value.add(scalar) is not required for symmetry // precision of this.value prevails over given scalar return this; // 0[kg] + 0 gives 0[kg] throw MathException.of(this, scalar); }