use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class AST2Expr method convertNode.
/**
* Converts a parsed ASTNode expression into an IExpr expression
*
* @param engine
* TODO
*/
private IExpr convertNode(ASTNode node, EvalEngine engine) throws ConversionException {
if (node == null) {
return null;
}
if (node instanceof FunctionNode) {
final FunctionNode functionNode = (FunctionNode) node;
int size = functionNode.size();
IAST ast;
switch(size) {
case 1:
ast = F.headAST0(convertNode(functionNode.get(0), engine));
break;
case 2:
ast = F.unaryAST1(convertNode(functionNode.get(0), engine), convertNode(functionNode.get(1), engine));
break;
case 3:
ast = F.binaryAST2(convertNode(functionNode.get(0), engine), convertNode(functionNode.get(1), engine), convertNode(functionNode.get(2), engine));
break;
case 4:
ast = F.ternaryAST3(convertNode(functionNode.get(0), engine), convertNode(functionNode.get(1), engine), convertNode(functionNode.get(2), engine), convertNode(functionNode.get(3), engine));
break;
default:
ast = F.ast(convertNode(functionNode.get(0), engine), functionNode.size(), false);
for (int i = 1; i < functionNode.size(); i++) {
ast.append(convertNode(functionNode.get(i), engine));
}
}
IExpr head = ast.head();
if (ast.isAST(F.N, 3)) {
try {
int precision = Validate.checkIntType(ast.arg2());
if (EvalEngine.isApfloat(precision)) {
fPrecision = precision;
ast.set(1, convertNode(functionNode.get(1), engine));
}
return ast;
} catch (WrongArgumentType wat) {
}
} else if (ast.isAST(F.Sqrt, 2)) {
// rewrite from input: Sqrt(x) => Power(x, 1/2)
return F.Power(ast.arg1(), F.C1D2);
} else if (ast.isAST(F.Exp, 2)) {
// rewrite from input: Exp(x) => E^x
return F.Power(F.E, ast.arg1());
} else if (ast.isPower() && ast.arg1().isPower() && ast.arg2().isMinusOne()) {
IAST arg1 = (IAST) ast.arg1();
if (arg1.arg2().isNumber()) {
// Power(x, - <number>)
return F.Power(arg1.arg1(), ((INumber) arg1.arg2()).negate());
}
} else if (ast.isASTSizeGE(F.GreaterEqual, 3)) {
ISymbol compareHead = F.Greater;
return rewriteLessGreaterAST(ast, compareHead);
} else if (ast.isASTSizeGE(F.Greater, 3)) {
ISymbol compareHead = F.GreaterEqual;
return rewriteLessGreaterAST(ast, compareHead);
} else if (ast.isASTSizeGE(F.LessEqual, 3)) {
ISymbol compareHead = F.Less;
return rewriteLessGreaterAST(ast, compareHead);
} else if (ast.isASTSizeGE(F.Less, 3)) {
ISymbol compareHead = F.LessEqual;
return rewriteLessGreaterAST(ast, compareHead);
} else if (head.equals(F.PatternHead)) {
final IExpr expr = Pattern.CONST.evaluate(ast, engine);
if (expr.isPresent()) {
return expr;
}
} else if (head.equals(F.BlankHead)) {
final IExpr expr = Blank.CONST.evaluate(ast, engine);
if (expr.isPresent()) {
return expr;
}
} else if (head.equals(F.Complex)) {
final IExpr expr = Arithmetic.CONST_COMPLEX.evaluate(ast, engine);
if (expr.isPresent()) {
return expr;
}
} else if (head.equals(F.Rational)) {
final IExpr expr = Arithmetic.CONST_RATIONAL.evaluate(ast, engine);
if (expr.isPresent()) {
return expr;
}
}
return ast;
}
if (node instanceof SymbolNode) {
String nodeStr = node.getString();
return convertSymbol(nodeStr, engine);
}
// PatternNode
if (node instanceof Pattern3Node) {
final Pattern3Node p3n = (Pattern3Node) node;
SymbolNode sn = p3n.getSymbol();
return F.$ps((ISymbol) convertNode(sn, engine), convertNode(p3n.getConstraint(), engine), p3n.isDefault(), true);
}
if (node instanceof Pattern2Node) {
final Pattern2Node p2n = (Pattern2Node) node;
SymbolNode sn = p2n.getSymbol();
return F.$ps((ISymbol) convertNode(sn, engine), convertNode(p2n.getConstraint(), engine), p2n.isDefault(), false);
}
if (node instanceof PatternNode) {
final PatternNode pn = (PatternNode) node;
SymbolNode sn = pn.getSymbol();
if (sn == null) {
// TODO ,
return F.$b(convertNode(pn.getConstraint(), engine));
// p2n.isDefault());
}
ASTNode defaultValue = pn.getDefaultValue();
if (defaultValue != null) {
return F.$p((ISymbol) convertNode(pn.getSymbol(), engine), convertNode(pn.getConstraint(), engine), convertNode(defaultValue, engine));
}
return F.$p((ISymbol) convertNode(pn.getSymbol(), engine), convertNode(pn.getConstraint(), engine), pn.isDefault());
}
if (node instanceof IntegerNode) {
final IntegerNode integerNode = (IntegerNode) node;
final String iStr = integerNode.getString();
if (iStr != null) {
return F.integer(iStr, integerNode.getNumberFormat());
}
return F.integer(integerNode.getIntValue());
}
if (node instanceof FractionNode) {
FractionNode fr = (FractionNode) node;
IInteger numerator = (IInteger) convertNode(fr.getNumerator(), engine);
IInteger denominator = (IInteger) convertNode(fr.getDenominator(), engine);
if (denominator.isZero()) {
return F.Rational(fr.isSign() ? numerator.negate() : numerator, denominator);
}
return F.Rational(fr.isSign() ? numerator.negate() : numerator, denominator);
// return F.fraction(numerator, fr.isSign() ? (IInteger) denominator.negate() : denominator);
}
if (node instanceof StringNode) {
return F.$str(node.getString());
}
if (node instanceof FloatNode) {
String nStr = node.getString();
String floatStr = nStr;
int index = nStr.indexOf("*^");
int exponent = 1;
if (index > 0) {
floatStr = nStr.substring(0, index);
exponent = Integer.parseInt(nStr.substring(index + 2));
}
if (EvalEngine.isApfloat(fPrecision)) {
Apfloat apfloatValue = new Apfloat(floatStr, fPrecision);
if (exponent != 1) {
// value * 10 ^ exponent
return F.num(apfloatValue.multiply(ApfloatMath.pow(new Apint(10), new Apint(exponent))));
}
return F.num(apfloatValue);
}
double doubleValue = Double.parseDouble(floatStr);
if (exponent != 1) {
// value * 10 ^ exponent
return F.num(doubleValue * Math.pow(10, exponent));
}
return F.num(doubleValue);
}
if (node instanceof DoubleNode) {
return F.num(((DoubleNode) node).doubleValue());
}
return F.userSymbol(node.toString(), engine);
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class JASConvert method monomialToExpr.
public boolean monomialToExpr(edu.jas.arith.BigInteger coeff, ExpVector exp, IAST monomTimes) {
if (!coeff.isONE()) {
IInteger coeffValue = F.integer(coeff.getVal());
monomTimes.append(coeffValue);
}
return expVectorToExpr(exp, monomTimes);
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class JASIExpr method integerPoly2Expr.
/**
* Convert a JAS integer polynomial to <code>IExpr</code>.
*
* @param poly
* @return
* @throws ArithmeticException
* @throws ClassCastException
*/
public IExpr integerPoly2Expr(final GenPolynomial<edu.jas.arith.BigInteger> poly) throws ArithmeticException, ClassCastException {
if (poly.length() == 0) {
return F.C0;
}
IAST result = F.Plus();
for (Monomial<edu.jas.arith.BigInteger> monomial : poly) {
edu.jas.arith.BigInteger coeff = monomial.coefficient();
ExpVector exp = monomial.exponent();
IInteger coeffValue = F.integer(coeff.getVal());
IAST monomTimes = F.Times(coeffValue);
long lExp;
for (int i = 0; i < exp.length(); i++) {
lExp = exp.getVal(i);
if (lExp != 0) {
monomTimes.append(F.Power(fVariables.get(i), F.integer(lExp)));
}
}
if (monomTimes.isAST1()) {
result.append(monomTimes.arg1());
} else {
result.append(monomTimes);
}
}
if (result.isAST1()) {
return result.arg1();
} else {
return result;
}
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class OutputFormFactory method convert.
public void convert(final Appendable buf, final IExpr o, final int precedence, boolean isASTHead) throws IOException {
if (o instanceof IAST) {
final IAST list = (IAST) o;
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() && derivStruct[2] != null) {
try {
int n = ((IInteger) a1Head.arg1()).toInt();
// IExpr arg1 = listArg1.arg1();
if (n == 1 || n == 2) {
ISymbol f = (ISymbol) headAST.arg1();
convertSymbol(buf, f);
if (n == 1) {
append(buf, "'");
} else if (n == 2) {
append(buf, "''");
}
convertArgs(buf, f, list);
return;
}
} catch (ArithmeticException ae) {
}
}
}
convert(buf, header, Integer.MIN_VALUE, true);
convertFunctionArgs(buf, list);
return;
}
ISymbol head = list.topHead();
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 (list.isList() || list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
convertList(buf, list);
return;
}
if (head.equals(F.Part) && (list.size() >= 3)) {
convertPart(buf, list);
return;
}
if (head.equals(F.Slot) && (list.isAST1()) && (list.arg1() instanceof IInteger)) {
convertSlot(buf, list);
return;
}
if (head.equals(F.SlotSequence) && (list.isAST1()) && (list.arg1() instanceof IInteger)) {
convertSlotSequence(buf, list);
return;
}
if ((head.equals(F.HoldForm) || head.equals(F.Defer)) && (list.isAST1())) {
convert(buf, list.arg1());
return;
}
if (head.equals(F.SeriesData) && (list.size() == 7)) {
if (convertSeriesData(buf, list, precedence)) {
return;
}
}
if (list.isDirectedInfinity()) {
// {
if (list.isAST0()) {
append(buf, "ComplexInfinity");
return;
}
if (list.isAST1()) {
if (list.arg1().isOne()) {
append(buf, "Infinity");
return;
} else if (list.arg1().isMinusOne()) {
if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
append(buf, "(");
}
append(buf, "-Infinity");
if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
append(buf, ")");
}
return;
} else if (list.arg1().isImaginaryUnit()) {
append(buf, "I*Infinity");
return;
} else if (list.arg1().isNegativeImaginaryUnit()) {
append(buf, "-I*Infinity");
return;
}
}
}
convertAST(buf, list);
return;
}
if (o instanceof ISignedNumber) {
convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
return;
}
if (o instanceof IComplexNum) {
convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
return;
}
if (o instanceof IComplex) {
convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
return;
}
if (o instanceof ISymbol) {
convertSymbol(buf, (ISymbol) o);
return;
}
if (o instanceof IPatternObject) {
convertPattern(buf, (IPatternObject) o);
return;
}
convertString(buf, o.toString());
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class OutputFormFactory method convertSeriesData.
/**
* Convert a <code>SeriesData[...]</code> expression.
*
* @param buf
* @param seriesData
* <code>SeriesData[x, x0, list, nmin, nmax, den]</code> expression
* @param precedence
* the precedence of the parent expression
* @return <code>true</code> if the conversion was successful
* @throws IOException
*/
public boolean convertSeriesData(final Appendable buf, final IAST seriesData, final int precedence) throws IOException {
int operPrecedence = ASTNodeFactory.PLUS_PRECEDENCE;
StringBuilder tempBuffer = new StringBuilder();
if (operPrecedence < precedence) {
append(tempBuffer, "(");
}
try {
IExpr plusArg;
// SeriesData[x, x0, list, nmin, nmax, den]
IExpr x = seriesData.arg1();
IExpr x0 = seriesData.arg2();
IAST list = (IAST) seriesData.arg3();
long nmin = ((IInteger) seriesData.arg4()).toLong();
long nmax = ((IInteger) seriesData.arg5()).toLong();
long den = ((IInteger) seriesData.get(6)).toLong();
int size = list.size();
boolean call = NO_PLUS_CALL;
if (size > 0) {
INumber exp = F.fraction(nmin, den).normalize();
IExpr pow = x.subtract(x0).power(exp);
call = convertSeriesDataArg(tempBuffer, list.arg1(), pow, call);
for (int i = 2; i < size; i++) {
exp = F.fraction(nmin + i - 1L, den).normalize();
pow = x.subtract(x0).power(exp);
call = convertSeriesDataArg(tempBuffer, list.get(i), pow, call);
}
plusArg = F.Power(F.O(x), F.fraction(nmax, den).normalize());
if (!plusArg.isZero()) {
convertPlusArgument(tempBuffer, plusArg, call);
call = PLUS_CALL;
}
} else {
return false;
}
} catch (Exception ex) {
return false;
}
if (operPrecedence < precedence) {
append(tempBuffer, ")");
}
buf.append(tempBuffer);
return true;
}
Aggregations