use of org.matheclipse.core.eval.exception.WrongArgumentType 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.eval.exception.WrongArgumentType in project symja_android_library by axkr.
the class Expr2LP method expr2Constraint.
public LinearConstraint expr2Constraint() {
double[] coefficients = new double[fVariables.size()];
if (fExpr.isAST()) {
IAST ast = (IAST) fExpr;
if (ast.isAST(F.Equal, 3)) {
IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
if (num == null) {
return new LinearConstraint(coefficients, Relationship.EQ, 0);
}
return new LinearConstraint(coefficients, Relationship.EQ, -1 * num.doubleValue());
}
if (ast.isAST(F.GreaterEqual, 3)) {
IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
if (num == null) {
return new LinearConstraint(coefficients, Relationship.GEQ, 0);
}
return new LinearConstraint(coefficients, Relationship.GEQ, -1 * num.doubleValue());
}
if (ast.isAST(F.LessEqual, 3)) {
IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
if (num == null) {
return new LinearConstraint(coefficients, Relationship.LEQ, 0);
}
return new LinearConstraint(coefficients, Relationship.LEQ, -1 * num.doubleValue());
}
}
throw new WrongArgumentType(fExpr, "Conversion from expression to linear programming expression failed");
}
use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.
the class FindInstance method evaluate.
/**
* Try to find at least one solution for a set of equations (i.e.
* <code>Equal[...]</code> expressions).
*/
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 3, 4);
IAST vars = Validate.checkSymbolOrSymbolList(ast, 2);
if (ast.isAST3()) {
if (ast.arg3().equals(F.Booleans)) {
IAST resultList = F.List();
booleansSolve(ast.arg1(), vars, 1, 1, resultList);
return resultList;
}
throw new WrongArgumentType(ast, ast.arg3(), 3, "Booleans expected!");
}
IAST termsEqualZeroList = Validate.checkEquations(ast, 1);
return solveEquations(termsEqualZeroList, vars, 1, engine);
}
use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.
the class CoefficientList method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 3);
IExpr expr = F.evalExpandAll(ast.arg1());
ISymbol arg2 = Validate.checkSymbolType(ast, 2);
try {
ExprPolynomialRing ring = new ExprPolynomialRing(F.List(arg2));
ExprPolynomial poly = ring.create(expr);
if (poly.isZero()) {
return F.List();
}
return poly.coefficientList();
} catch (RuntimeException ex) {
throw new WrongArgumentType(ast, expr, 1, "Polynomial expected!");
}
}
use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.
the class CoefficientList method coefficientList.
/**
* Get the coefficient list of a univariate polynomial.
*
* @param polynomial
* @param variable
* @return <code>null</code> if the list couldn't be evaluated.
*/
public static double[] coefficientList(IExpr polynomial, final ISymbol variable) throws JASConversionException {
try {
ExprPolynomialRing ring = new ExprPolynomialRing(F.List(variable));
ExprPolynomial poly = ring.create(polynomial);
// PolynomialOld poly = new PolynomialOld(polynomial, (ISymbol) variable);
// if (!poly.isPolynomial()) {
// throw new WrongArgumentType(polynomial, "Polynomial expected!");
// }
IAST list = poly.coefficientList();
int degree = list.size() - 2;
double[] result = new double[degree + 1];
for (int i = 1; i < list.size(); i++) {
ISignedNumber temp = list.get(i).evalSignedNumber();
if (temp != null) {
result[i - 1] = temp.doubleValue();
} else {
return null;
}
}
return result;
} catch (RuntimeException ex) {
throw new WrongArgumentType(polynomial, "Polynomial expected!");
}
}
Aggregations