use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Solve method integerSolve.
public static IAST integerSolve(final IAST list, final IAST variables) {
IAST result = F.List();
TreeMap<ISymbol, IntVariable> map = new TreeMap<ISymbol, IntVariable>();
// Create a constraint network
Network net = new Network();
for (int i = 1; i < variables.size(); i++) {
if (variables.get(i) instanceof ISymbol) {
map.put((ISymbol) variables.get(i), new IntVariable(net));
}
}
IAST temp;
IntVariable lhs;
IntVariable rhs;
for (int i = 1; i < list.size(); i++) {
if (list.get(i) instanceof IAST) {
temp = (IAST) list.get(i);
if (temp.isAST2()) {
lhs = integerVariable(net, map, temp.arg1());
rhs = integerVariable(net, map, temp.arg2());
if (temp.isAST(F.Equal, 3)) {
lhs.equals(rhs);
} else if (temp.isAST(F.Unequal, 3)) {
lhs.notEquals(rhs);
} else if (temp.isAST(F.Greater, 3)) {
lhs.gt(rhs);
} else if (temp.isAST(F.GreaterEqual, 3)) {
lhs.ge(rhs);
} else if (temp.isAST(F.LessEqual, 3)) {
lhs.le(rhs);
} else if (temp.isAST(F.Less, 3)) {
lhs.lt(rhs);
} else {
return null;
}
}
}
}
Solver solver = new DefaultSolver(net);
solver.findAll(new SolutionHandler() {
@Override
public void solved(Solver solver, Solution solution) {
if (solution != null) {
IAST temp = F.List();
for (Entry<ISymbol, IntVariable> entry : map.entrySet()) {
temp.append(F.Rule(entry.getKey(), F.integer(solution.getIntValue(entry.getValue()))));
}
result.append(temp);
}
}
}, 10000);
// }
return result;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Sum method definiteSumInfinity.
/**
* Evaluate the definite sum: <code>Sum[arg1, {var, from, Infinity}]</code>
*
* @param arg1
* the first argument of the <code>Sum[]</code> function.
* @param list
* constructed as <code>{Symbol: var, Integer: from, Infinity}</code>
* @return
*/
private IExpr definiteSumInfinity(final IExpr expr, final IIterator iterator, IAST list, EvalEngine engine) {
final ISymbol var = iterator.getVariable();
final IExpr from = iterator.getLowerLimit();
final IExpr to = iterator.getUpperLimit();
if (expr.isZero()) {
return F.C0;
}
if (from.isInteger() && !from.isOne()) {
IExpr subSum = engine.evaluateNull(F.Sum(expr, F.List(var, C1, to)));
if (subSum.isPresent()) {
if (engine.evalTrue(F.Less(from, C1))) {
return F.Plus(F.Sum(expr, F.List(var, from, C0)), subSum);
}
if (engine.evalTrue(F.Greater(from, C1))) {
return F.Subtract(subSum, F.Sum(expr, F.List(var, C1, from.minus(F.C1))));
}
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class AbstractArgMultiple method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (ast.isAST2()) {
IExpr temp = binaryOperator(ast.arg1(), ast.arg2());
if (temp.isPresent()) {
return temp;
}
return evaluateHashsRepeated(ast);
}
if (ast.size() > 3) {
IAST tempAST = ast.clone();
final ISymbol sym = tempAST.topHead();
final IAST result = F.ast(sym);
IExpr tres;
IExpr temp = tempAST.arg1();
boolean evaled = false;
int i = 2;
while (i < tempAST.size()) {
tres = binaryOperator(temp, tempAST.get(i));
if (!tres.isPresent()) {
for (int j = i + 1; j < tempAST.size(); j++) {
tres = binaryOperator(temp, tempAST.get(j));
if (tres.isPresent()) {
evaled = true;
temp = tres;
tempAST.remove(j);
break;
}
}
if (!tres.isPresent()) {
result.append(temp);
if (i == tempAST.size() - 1) {
result.append(tempAST.get(i));
} else {
temp = tempAST.get(i);
}
i++;
}
} else {
evaled = true;
temp = tres;
if (i == (tempAST.size() - 1)) {
result.append(temp);
}
i++;
}
}
if (evaled) {
if ((result.isAST1()) && ((sym.getAttributes() & ISymbol.ONEIDENTITY) == ISymbol.ONEIDENTITY)) {
return result.arg1();
}
return result;
}
if (tempAST.size() > 2) {
return evaluateHashsRepeated(tempAST);
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class AbstractArgMultiple method binaryOperator.
@Override
public IExpr binaryOperator(final IExpr o0, final IExpr o1) {
IExpr result = F.NIL;
if (o0 instanceof INum) {
// use specialized methods for numeric mode
if (o1 instanceof INum) {
result = e2DblArg((INum) o0, (INum) o1);
} else if (o1.isInteger()) {
result = e2DblArg((INum) o0, F.num((IInteger) o1));
} else if (o1.isFraction()) {
result = e2DblArg((INum) o0, F.num((IFraction) o1));
} else if (o1 instanceof IComplexNum) {
if (o0 instanceof ApfloatNum) {
result = e2DblComArg(F.complexNum(((ApfloatNum) o0).apfloatValue()), (IComplexNum) o1);
} else {
result = e2DblComArg(F.complexNum(((INum) o0).getRealPart()), (IComplexNum) o1);
}
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o1 instanceof INum) {
// use specialized methods for numeric mode
if (o0.isInteger()) {
result = e2DblArg(F.num((IInteger) o0), (INum) o1);
} else if (o0.isFraction()) {
result = e2DblArg(F.num((IFraction) o0), (INum) o1);
} else if (o0 instanceof IComplexNum) {
if (o1 instanceof ApfloatNum) {
result = e2DblComArg((IComplexNum) o0, F.complexNum(((ApfloatNum) o1).apfloatValue()));
} else {
result = e2DblComArg((IComplexNum) o0, F.complexNum(((INum) o1).getRealPart()));
}
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
}
if (o0 instanceof IComplexNum) {
// use specialized methods for complex numeric mode
if (o1 instanceof INum) {
result = e2DblComArg((IComplexNum) o0, F.complexNum(((INum) o1).getRealPart()));
} else if (o1.isInteger()) {
result = e2DblComArg((IComplexNum) o0, F.complexNum((IInteger) o1));
} else if (o1.isFraction()) {
result = e2DblComArg((IComplexNum) o0, F.complexNum((IFraction) o1));
} else if (o1 instanceof IComplexNum) {
result = e2DblComArg((IComplexNum) o0, (IComplexNum) o1);
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o1 instanceof IComplexNum) {
// use specialized methods for complex numeric mode
if (o0 instanceof INum) {
result = e2DblComArg(F.complexNum(((INum) o0).getRealPart()), (IComplexNum) o1);
} else if (o0.isInteger()) {
result = e2DblComArg(F.complexNum((IInteger) o0), (IComplexNum) o1);
} else if (o0.isFraction()) {
result = e2DblComArg(F.complexNum((IFraction) o0), (IComplexNum) o1);
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
}
if (o0 instanceof IInteger) {
if (o1 instanceof IInteger) {
return e2IntArg((IInteger) o0, (IInteger) o1);
}
if (o1 instanceof IFraction) {
return e2FraArg(F.fraction((IInteger) o0, F.C1), (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg(F.complex((IInteger) o0, F.C0), (IComplex) o1);
}
} else if (o0 instanceof IFraction) {
if (o1 instanceof IInteger) {
return e2FraArg((IFraction) o0, F.fraction((IInteger) o1, F.C1));
}
if (o1 instanceof IFraction) {
return e2FraArg((IFraction) o0, (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg(F.complex((IFraction) o0), (IComplex) o1);
}
} else if (o0 instanceof IComplex) {
if (o1 instanceof IInteger) {
return eComIntArg((IComplex) o0, (IInteger) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg((IComplex) o0, (IComplex) o1);
}
}
result = e2ObjArg(o0, o1);
if (result.isPresent()) {
return result;
}
if (o0 instanceof ISymbol) {
if (o1 instanceof ISymbol) {
return e2SymArg((ISymbol) o0, (ISymbol) o1);
}
}
if (o0 instanceof IAST) {
IAST a0 = (IAST) o0;
if (o1 instanceof IInteger) {
return eFunIntArg(a0, (IInteger) o1);
}
if (o1 instanceof IAST) {
return e2FunArg(a0, (IAST) o1);
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class AbstractArg2 method binaryOperator.
public IExpr binaryOperator(final IExpr o0, final IExpr o1) {
IExpr result = F.NIL;
if (o0.isNumber() && o1.isNumber()) {
result = e2NumericArg(o0, o1);
if (result.isPresent()) {
return result;
}
}
result = e2ObjArg(o0, o1);
if (result.isPresent()) {
return result;
}
if (o0 instanceof IInteger) {
if (o1 instanceof IInteger) {
return e2IntArg((IInteger) o0, (IInteger) o1);
}
if (o1 instanceof IFraction) {
return e2FraArg(F.fraction((IInteger) o0, F.C1), (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg(F.complex((IInteger) o0, F.C0), (IComplex) o1);
}
return F.NIL;
}
if (o0 instanceof IFraction) {
if (o1 instanceof IInteger) {
return e2FraArg((IFraction) o0, F.fraction((IInteger) o1, F.C1));
}
if (o1 instanceof IFraction) {
return e2FraArg((IFraction) o0, (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg(F.complex((IFraction) o0), (IComplex) o1);
}
return F.NIL;
}
if (o0 instanceof IComplex) {
if (o1 instanceof IInteger) {
return eComIntArg((IComplex) o0, (IInteger) o1);
}
if (o1 instanceof IFraction) {
return eComFraArg((IComplex) o0, (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg((IComplex) o0, (IComplex) o1);
}
}
if (o0 instanceof ISymbol) {
if (o1 instanceof ISymbol) {
return e2SymArg((ISymbol) o0, (ISymbol) o1);
}
}
if (o0 instanceof IAST) {
if (o1 instanceof IInteger) {
return eFunIntArg((IAST) o0, (IInteger) o1);
}
if (o1 instanceof IAST) {
return e2FunArg((IAST) o0, (IAST) o1);
}
}
return F.NIL;
}
Aggregations