use of org.matheclipse.core.interfaces.INumber in project symja_android_library by axkr.
the class SignCmp method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 2);
IExpr arg1 = ast.arg1();
INumber number = arg1.evalNumber();
if (number != null) {
final int signum = number.complexSign();
return F.integer(signum);
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.INumber in project symja_android_library by axkr.
the class Normal method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 2);
IExpr arg1 = ast.arg1();
if (arg1.isAST()) {
IAST seriesData = (IAST) arg1;
if (seriesData.isAST(F.SeriesData) && (seriesData.size() == 7)) {
if (seriesData.arg3().isList()) {
try {
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();
IAST result = F.Plus();
for (int i = 1; i < list.size(); i++) {
// INumber exp = F.fraction(nmin, den).normalize();
// IExpr pow = x.subtract(x0).power(exp);
INumber exp = F.fraction(nmin + i - 1L, den).normalize();
IExpr pow = x.subtract(x0).power(exp);
result.append(F.Times(list.get(i), pow));
}
return result;
} catch (ArithmeticException ex) {
}
}
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.INumber in project symja_android_library by axkr.
the class AbstractFunctionEvaluator method extractFactorFromExpression.
/**
* Check if the expression has a complex number factor I.
*
* @param expression
* @param factor
* @param checkTimes
* check <code>Times(...)</code> expressions
* @return the negated negative expression or <code>F.NIL</code> if a
* negative expression couldn't be extracted.
*/
public static IExpr extractFactorFromExpression(final IExpr expression, INumber factor, boolean checkTimes) {
if (expression.isNumber()) {
if (((INumber) expression).equals(factor)) {
return F.C1;
}
} else {
if (expression.isAST()) {
if (checkTimes && expression.isTimes()) {
IAST timesAST = ((IAST) expression);
IExpr arg1 = timesAST.arg1();
if (arg1.isNumber()) {
if (((INumber) arg1).isImaginaryUnit()) {
return timesAST.removeAtClone(1).getOneIdentity(factor);
}
}
}
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.INumber in project symja_android_library by axkr.
the class AbstractFunctionEvaluator method getNormalizedNegativeExpression.
/**
* Check if the expression is canonical negative.
*
* @param expression
* @param checkTimesPlus
* check <code>Times(...)</code> and <code>Plus(...)</code>
* expressions
* @return the negated negative expression or <code>F.NIL</code> if a
* negative expression couldn't be extracted.
*/
public static IExpr getNormalizedNegativeExpression(final IExpr expression, boolean checkTimesPlus) {
IAST result = F.NIL;
if (expression.isNumber()) {
if (((INumber) expression).complexSign() < 0) {
return ((INumber) expression).negate();
}
return F.NIL;
}
if (expression.isAST()) {
if (checkTimesPlus && expression.isTimes()) {
IAST timesAST = ((IAST) expression);
IExpr arg1 = timesAST.arg1();
if (arg1.isNumber()) {
if (((INumber) arg1).complexSign() < 0) {
IExpr negNum = ((INumber) arg1).negate();
if (negNum.isOne()) {
return timesAST.removeAtClone(1).getOneIdentity(F.C1);
}
return timesAST.setAtClone(1, negNum);
}
} else if (arg1.isNegativeInfinity()) {
return timesAST.setAtClone(1, F.CInfinity);
}
} else if (checkTimesPlus && expression.isPlus()) {
IAST plusAST = ((IAST) expression);
IExpr arg1 = plusAST.arg1();
if (arg1.isNumber()) {
if (((INumber) arg1).complexSign() < 0) {
result = plusAST.copy();
result.set(1, arg1.negate());
for (int i = 2; i < plusAST.size(); i++) {
result.set(i, plusAST.get(i).negate());
}
return result;
}
} else if (arg1.isNegativeInfinity()) {
result = plusAST.copy();
result.set(1, F.CInfinity);
for (int i = 2; i < plusAST.size(); i++) {
result.set(i, plusAST.get(i).negate());
}
return result;
} else if (arg1.isTimes()) {
IExpr arg1Negated = getNormalizedNegativeExpression(arg1, checkTimesPlus);
if (arg1Negated.isPresent()) {
int positiveElementsCounter = 0;
result = plusAST.copy();
result.set(1, arg1Negated);
for (int i = 2; i < plusAST.size(); i++) {
IExpr temp = plusAST.get(i);
if (!temp.isTimes() && !temp.isPower()) {
return F.NIL;
}
arg1Negated = getNormalizedNegativeExpression(temp, checkTimesPlus);
if (arg1Negated.isPresent()) {
result.set(i, arg1Negated);
} else {
positiveElementsCounter++;
if (positiveElementsCounter * 2 >= plusAST.size() - 1) {
// than number of negative elements
return F.NIL;
}
result.set(i, temp.negate());
}
}
return result;
}
}
} else if (expression.isNegativeInfinity()) {
return F.CInfinity;
}
}
if (expression.isNegativeResult()) {
return F.eval(F.Negate(expression));
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.INumber 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);
}
Aggregations