use of org.matheclipse.core.tensor.qty.IQuantity in project symja_android_library by axkr.
the class PlusOp method plus.
/**
* Add an argument <code>arg</code> to this <code>Plus()</code> expression.
*
* @param arg
* @return <code>F.Indeterminate</code> if the result is indeterminated, <code>F.NIL</code>
* otherwise.
*/
public IExpr plus(final IExpr arg) {
// }
if (arg.isIndeterminate()) {
return S.Indeterminate;
}
try {
if (numberValue.isPresent() && numberValue.isDirectedInfinity()) {
if (numberValue.isComplexInfinity()) {
if (arg.isDirectedInfinity()) {
return S.Indeterminate;
}
numberValue = F.CComplexInfinity;
evaled = true;
return F.NIL;
} else if (numberValue.isInfinity()) {
if (arg.isInfinity()) {
evaled = true;
return F.NIL;
}
if (arg.isDirectedInfinity()) {
return S.Indeterminate;
}
if (arg.isRealResult()) {
evaled = true;
return F.NIL;
}
} else if (numberValue.isNegativeInfinity()) {
if (arg.isNegativeInfinity()) {
evaled = true;
return F.NIL;
}
if (arg.isDirectedInfinity()) {
// Indeterminate expression `1` encountered.
IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(numberValue, arg)), EvalEngine.get());
return S.Indeterminate;
}
if (arg.isRealResult()) {
evaled = true;
return F.NIL;
}
}
}
if (arg.isNumber()) {
if (arg.isZero()) {
evaled = true;
return F.NIL;
}
if (!numberValue.isPresent()) {
numberValue = arg;
return F.NIL;
}
if (numberValue.isNumber()) {
numberValue = numberValue.plus(arg);
evaled = true;
return F.NIL;
}
if (numberValue.isInfinity()) {
if (arg.isNegativeInfinity()) {
// Indeterminate expression `1` encountered.
IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(numberValue, arg)), EvalEngine.get());
return S.Indeterminate;
}
numberValue = F.CInfinity;
evaled = true;
return F.NIL;
}
if (numberValue.isNegativeInfinity()) {
numberValue = negativeInfinityPlus(arg);
if (numberValue.isIndeterminate()) {
return S.Indeterminate;
}
evaled = true;
return F.NIL;
}
return F.NIL;
} else if (arg.isQuantity()) {
// if (arg.isQuantity()) {
if (!numberValue.isPresent()) {
numberValue = arg;
return F.NIL;
}
IQuantity q = (IQuantity) arg;
IExpr temp = q.plus(numberValue, true);
if (temp.isPresent()) {
evaled = true;
numberValue = temp;
} else {
if (addMerge(q, F.C1)) {
evaled = true;
}
}
return F.NIL;
// }
} else if (arg.isAST()) {
final IAST ast = (IAST) arg;
final int headID = ((IAST) arg).headID();
if (headID >= ID.DirectedInfinity) {
switch(headID) {
case ID.DirectedInfinity:
if (arg.isDirectedInfinity()) {
if (!numberValue.isPresent()) {
numberValue = arg;
if (arg.isComplexInfinity()) {
if (plusMap != null && plusMap.size() > 0) {
evaled = true;
}
} else {
if (plusMap != null) {
Iterator<Entry<IExpr, IExpr>> iterator = plusMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry<IExpr, IExpr> entry = iterator.next();
if (entry.getKey().isRealResult()) {
iterator.remove();
evaled = true;
}
}
}
}
return F.NIL;
}
if (arg.isInfinity()) {
if (numberValue.isNegativeInfinity()) {
// Indeterminate expression `1` encountered.
IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(arg, numberValue)), EvalEngine.get());
return S.Indeterminate;
}
numberValue = F.CInfinity;
evaled = true;
return F.NIL;
} else if (arg.isNegativeInfinity()) {
numberValue = negativeInfinityPlus(numberValue);
if (numberValue.isIndeterminate()) {
return S.Indeterminate;
}
evaled = true;
return F.NIL;
} else if (arg.isComplexInfinity()) {
if (numberValue.isDirectedInfinity()) {
// Indeterminate expression `1` encountered.
IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(arg, numberValue)), EvalEngine.get());
return S.Indeterminate;
}
numberValue = F.CComplexInfinity;
evaled = true;
return F.NIL;
}
}
break;
case ID.Times:
if (ast.size() > 1) {
if (ast.arg1().isNumber()) {
if (addMerge(ast.rest().oneIdentity1(), ast.arg1())) {
evaled = true;
}
return F.NIL;
}
if (addMerge(ast, F.C1)) {
evaled = true;
}
}
return F.NIL;
case ID.Interval:
if (arg.isInterval()) {
if (!numberValue.isPresent()) {
numberValue = arg;
return F.NIL;
}
IExpr temp;
if (numberValue.isInterval()) {
temp = IntervalSym.plus((IAST) numberValue, (IAST) arg);
} else {
temp = IntervalSym.plus(numberValue, (IAST) arg);
}
if (temp.isPresent()) {
numberValue = temp;
evaled = true;
} else {
if (addMerge(arg, F.C1)) {
evaled = true;
}
}
return F.NIL;
}
break;
// break;
case ID.SeriesData:
if (arg instanceof ASTSeriesData) {
if (!numberValue.isPresent()) {
numberValue = arg;
return F.NIL;
}
numberValue = ((ASTSeriesData) arg).plus(numberValue);
evaled = true;
return F.NIL;
}
break;
}
}
// } else if (!numberValue.isPresent() && arg.isRealResult()) {
// numberValue = arg;
// return F.NIL;
}
if (addMerge(arg, F.C1)) {
evaled = true;
}
} catch (ValidateException | LimitException e) {
LOGGER.debug("PlusOp.plus() failed", e);
throw e;
} catch (SymjaMathException sme) {
LOGGER.debug("PlusOp.plus() failed", sme);
}
return F.NIL;
}
use of org.matheclipse.core.tensor.qty.IQuantity in project symja_android_library by axkr.
the class OutputFormFactory method convert.
private void convert(final Appendable buf, final IExpr o, final int precedence, boolean isASTHead) throws IOException {
if (o instanceof IAST) {
final IAST list = (IAST) o;
if (!list.isPresent()) {
append(buf, "NIL");
return;
}
if (o.isDataset()) {
// TODO improve output
buf.append(o.toString());
return;
} else if (o.isAssociation()) {
convertAssociation(buf, (IAssociation) o);
return;
} else if (o.isAST(S.Association, 1)) {
buf.append("<||>");
return;
}
IExpr header = list.head();
if (!header.isSymbol()) {
// print expressions like: f(#1, y)& [x]
IAST[] derivStruct = list.isDerivativeAST1();
if (derivStruct != null) {
IAST a1Head = derivStruct[0];
IAST headAST = derivStruct[1];
if (a1Head.isAST1() && a1Head.arg1().isInteger() && headAST.isAST1() && (headAST.arg1().isSymbol() || headAST.arg1().isAST()) && derivStruct[2] != null) {
try {
int n = ((IInteger) a1Head.arg1()).toInt();
if (n == 1 || n == 2) {
IExpr symbolOrAST = headAST.arg1();
convert(buf, symbolOrAST, Integer.MIN_VALUE, false);
if (n == 1) {
append(buf, "'");
} else if (n == 2) {
append(buf, "''");
}
convertArgs(buf, symbolOrAST, list);
return;
}
} catch (ArithmeticException ae) {
}
}
}
convert(buf, header, Integer.MIN_VALUE, true);
// avoid fast StackOverflow
append(buf, "[");
for (int i = 1; i < list.size(); i++) {
convert(buf, list.get(i), Integer.MIN_VALUE, false);
if (i < list.argSize()) {
append(buf, ",");
}
}
append(buf, "]");
return;
}
if (header.isSymbol()) {
ISymbol head = (ISymbol) header;
int functionID = head.ordinal();
if (functionID > ID.UNKNOWN) {
switch(functionID) {
case ID.TwoWayRule:
case ID.UndirectedEdge:
if (list.isAST2()) {
convert(buf, list.arg1(), Integer.MIN_VALUE, false);
buf.append("<->");
convert(buf, list.arg2(), Integer.MIN_VALUE, false);
return;
}
break;
case ID.DirectedEdge:
if (list.isAST2()) {
convert(buf, list.arg1(), Integer.MIN_VALUE, false);
buf.append("->");
convert(buf, list.arg2(), Integer.MIN_VALUE, false);
return;
}
break;
}
}
final Operator operator = getOperator(head);
if (operator != null) {
if (operator instanceof PostfixOperator) {
if (list.isAST1()) {
convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
return;
}
} else {
if (convertOperator(operator, list, buf, isASTHead ? Integer.MAX_VALUE : precedence, head)) {
return;
}
}
}
if (functionID > ID.UNKNOWN) {
switch(functionID) {
case ID.Inequality:
if (list.size() > 3 && convertInequality(buf, list, precedence)) {
return;
}
break;
case ID.Quantity:
// if (head.equals(F.SeriesData) && (list.size() == 7)) {
if (list instanceof IQuantity) {
if (convertQuantityData(buf, (IQuantity) list, precedence)) {
return;
}
}
break;
case ID.SeriesData:
// if (head.equals(F.SeriesData) && (list.size() == 7)) {
if (list instanceof ASTSeriesData) {
if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
return;
}
}
break;
case ID.SparseArray:
if (list.isSparseArray()) {
buf.append(list.toString());
return;
}
break;
case ID.Parenthesis:
convertArgs(buf, S.Parenthesis, list);
return;
case ID.List:
convertList(buf, list, false);
return;
case ID.MatrixForm:
if (list.isASTOrAssociation() && list.size() > 1) {
// see also MatrixForm in MathML or TeX format for "graphical representation".
IExpr normal = list.arg1().normal(false);
if (normal.isList()) {
// && normal.isMatrix() != null) {
IntList dims = LinearAlgebra.dimensions((IAST) normal, S.List);
convertList(buf, (IAST) normal, dims.size() >= 2);
return;
}
convert(buf, normal, Integer.MIN_VALUE, false);
return;
}
break;
case ID.Out:
if (list.isAST1() && list.arg1().isInteger()) {
int lineNumber = list.arg1().toIntDefault();
if (lineNumber == -1) {
buf.append("%");
return;
} else if (lineNumber == -2) {
buf.append("%%");
return;
}
}
break;
case ID.Part:
if (list.size() >= 3) {
convertPart(buf, list);
return;
}
break;
case ID.Slot:
if (list.isAST1() && list.arg1().isInteger()) {
convertSlot(buf, list);
return;
}
break;
case ID.SlotSequence:
if (list.isAST1() && list.arg1().isInteger()) {
convertSlotSequence(buf, list);
return;
}
break;
case ID.Defer:
case ID.HoldForm:
if (list.isAST1()) {
convert(buf, list.arg1(), Integer.MIN_VALUE, false);
return;
}
break;
case ID.DirectedInfinity:
if (list.isDirectedInfinity()) {
// head.equals(F.DirectedInfinity))
if (list.isAST0()) {
append(buf, "ComplexInfinity");
return;
}
if (list.isAST1()) {
if (list.arg1().isOne()) {
append(buf, "Infinity");
return;
} else if (list.arg1().isMinusOne()) {
if (Precedence.PLUS < precedence) {
append(buf, "(");
}
append(buf, "-Infinity");
if (Precedence.PLUS < precedence) {
append(buf, ")");
}
return;
} else if (list.arg1().isImaginaryUnit()) {
append(buf, "I*Infinity");
return;
} else if (list.arg1().isNegativeImaginaryUnit()) {
append(buf, "-I*Infinity");
return;
}
}
}
break;
case ID.Optional:
if (list.isAST2() && (list.arg1().isBlank() || list.arg1().isPattern())) {
convert(buf, list.arg1(), Integer.MIN_VALUE, false);
buf.append(":");
convert(buf, list.arg2(), Integer.MIN_VALUE, false);
return;
}
break;
case ID.Complex:
if (list.isAST2()) {
// used for visual comparison of steps
boolean isZeroRealPart = list.arg1().isZero();
final int prec = isZeroRealPart ? Precedence.TIMES : Precedence.PLUS;
if (prec < precedence) {
append(buf, "(");
}
if (isZeroRealPart) {
buf.append("I*");
convert(buf, list.arg2(), Precedence.TIMES, false);
} else {
convert(buf, list.arg1(), Precedence.PLUS, false);
buf.append("+I*");
convert(buf, list.arg2(), Precedence.TIMES, false);
}
if (prec < precedence) {
append(buf, ")");
}
return;
}
break;
case ID.Rational:
if (list.isAST2()) {
// used for visual comparison of steps
IExpr numerator = list.arg1();
final boolean isNegative = numerator.isNegative();
final int prec = isNegative ? Precedence.PLUS : Precedence.TIMES;
if (prec < precedence) {
append(buf, "(");
}
convert(buf, list.arg1(), Precedence.DIVIDE, false);
buf.append("/");
convert(buf, list.arg2(), Precedence.DIVIDE, false);
if (prec < precedence) {
append(buf, ")");
}
return;
}
break;
}
} else {
if (list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
convertList(buf, list, false);
return;
}
}
}
convertAST(buf, list);
} else if (o instanceof ISignedNumber) {
convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
} else if (o instanceof IComplexNum) {
convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
} else if (o instanceof IComplex) {
convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
} else if (o instanceof ISymbol) {
convertSymbol(buf, (ISymbol) o);
} else if (o instanceof IPatternObject) {
convertPattern(buf, (IPatternObject) o);
} else if (o instanceof IStringX) {
convertString(buf, ((IStringX) o).toString());
} else {
convertString(buf, o.toString());
}
}
Aggregations