use of jp.ac.kobe_u.cs.cream.IntVariable in project symja_android_library by axkr.
the class Solve method integerVariable.
public static IntVariable integerVariable(Network net, TreeMap<ISymbol, IntVariable> map, IExpr expr) throws ArithmeticException {
if (expr instanceof ISymbol) {
return map.get(expr);
}
if (expr instanceof IInteger) {
// throws ArithmeticException
int value = ((IInteger) expr).toInt();
return new IntVariable(net, value);
}
if (expr.isPlus()) {
IAST ast = (IAST) expr;
IntVariable result = integerVariable(net, map, ast.arg1());
for (int i = 2; i < ast.size(); i++) {
result = result.add(integerVariable(net, map, ast.get(i)));
}
return result;
} else if (expr.isTimes()) {
IAST ast = (IAST) expr;
IntVariable result = integerVariable(net, map, ast.arg1());
for (int i = 2; i < ast.size(); i++) {
result = result.multiply(integerVariable(net, map, ast.get(i)));
}
return result;
} else if (expr.isPower()) {
IAST ast = (IAST) expr;
if (ast.arg2().isInteger()) {
int value = ((IInteger) ast.arg2()).toInt();
if (value > 0) {
IntVariable result = integerVariable(net, map, ast.arg1());
for (int i = 1; i < value; i++) {
result = result.multiply(integerVariable(net, map, ast.arg1()));
}
return result;
}
}
} else if (expr.isASTSizeGE(F.Max, 3)) {
IAST ast = (IAST) expr;
IntVariable result = integerVariable(net, map, ast.arg1());
for (int i = 2; i < ast.size(); i++) {
result = result.max(integerVariable(net, map, ast.get(i)));
}
return result;
} else if (expr.isASTSizeGE(F.Min, 3)) {
IAST ast = (IAST) expr;
IntVariable result = integerVariable(net, map, ast.arg1());
for (int i = 2; i < ast.size(); i++) {
result = result.min(integerVariable(net, map, ast.get(i)));
}
return result;
} else if (expr.isAbs()) {
IAST ast = (IAST) expr;
return integerVariable(net, map, ast.arg1()).abs();
} else if (expr.isAST(F.Sign, 2)) {
IAST ast = (IAST) expr;
return integerVariable(net, map, ast.arg1()).sign();
}
throw new WrongArgumentType(expr, "for Solve(..., Integers)");
}
use of jp.ac.kobe_u.cs.cream.IntVariable 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;
}
Aggregations