use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class HornerScheme method collectTermN.
private void collectTermN(ISymbol sym, IExpr expr) {
if (expr instanceof IAST) {
IAST term = (IAST) expr;
if (term.isASTSizeGE(F.Times, 2)) {
for (int i = 1; i < term.size(); i++) {
if (sym.equals(term.get(i))) {
IAST temp = F.ast(term, F.Times, false, i, i + 1);
addToMap(F.CD1, temp);
return;
} else if (term.get(i).isAST(F.Power, 3)) {
IAST pow = (IAST) term.get(i);
if (pow.arg1().equals(sym) && pow.arg2().isSignedNumber()) {
IAST temp = F.ast(term, F.Times, false, i, i + 1);
addToMap((ISignedNumber) pow.arg2(), temp);
return;
}
}
}
} else if (term.isAST(F.Power, 3)) {
if (term.arg1().equals(sym) && term.arg2().isSignedNumber()) {
addToMap((ISignedNumber) term.arg2(), F.CD1);
return;
}
}
} else if (expr instanceof ISymbol && expr.equals(sym)) {
addToMap(F.CD1, F.CD1);
return;
}
addToMap(F.CD0, expr);
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class GenPolynomialMonomialIterator method create.
/**
* Create a <code>Polynomial</code> from the given <code>exprPoly</code>.
*
* @param exprPoly
* the polynomial expression
* @param coefficient
* set to <code>true</code> if called by the
* <code>Coefficient()</code> function
* @param checkNegativeExponents
* if <code>true</code> don't allow negative exponents
* @return
*/
public ExprPolynomial create(final IExpr exprPoly, boolean coefficient, boolean checkNegativeExponents) throws ArithmeticException, ClassCastException {
int ix = evzero.indexVar(exprPoly, getVars());
if (ix >= 0) {
ExpVectorLong e = new ExpVectorLong(vars.size() - 1, ix, 1L);
return getOne().multiply(e);
}
if (exprPoly instanceof IAST) {
final IAST ast = (IAST) exprPoly;
ExprPolynomial result = getZero();
ExprPolynomial p = getZero();
if (ast.isPlus()) {
IExpr expr = ast.arg1();
result = create(expr, coefficient, checkNegativeExponents);
for (int i = 2; i < ast.size(); i++) {
expr = ast.get(i);
p = create(expr, coefficient, checkNegativeExponents);
result = result.sum(p);
}
return result;
} else if (ast.isTimes()) {
IExpr expr = ast.arg1();
result = create(expr, coefficient, checkNegativeExponents);
for (int i = 2; i < ast.size(); i++) {
expr = ast.get(i);
p = create(expr, coefficient, checkNegativeExponents);
result = result.multiply(p);
}
return result;
} else if (ast.isPower()) {
final IExpr expr = ast.arg1();
ix = ExpVectorLong.indexVar(expr, getVars());
if (ix >= 0) {
int exponent = -1;
try {
exponent = Validate.checkPowerExponent(ast);
} catch (WrongArgumentType e) {
//
}
if (checkNegativeExponents && exponent < 0) {
throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
}
ExpVectorLong e = new ExpVectorLong(vars.size() - 1, ix, exponent);
return getOne().multiply(e);
}
// for (int i = 1; i < vars.size(); i++) {
// if (vars.get(i).equals(expr)) {
// int exponent = -1;
// try {
// exponent = Validate.checkPowerExponent(ast);
// } catch (WrongArgumentType e) {
// //
// }
// if (exponent < 0) {
// throw new ArithmeticException("JASConvert:expr2Poly - invalid
// exponent: " + ast.arg2().toString());
// }
// ExpVectorLong e = new ExpVectorLong(vars.size() - 1, i - 1,
// exponent);
// return getOne().multiply(e);
// }
// }
}
if (coefficient) {
return new ExprPolynomial(this, ast);
}
if (numericFunction) {
if (ast.isNumericFunction()) {
return new ExprPolynomial(this, ast);
}
}
} else if (exprPoly instanceof ISymbol) {
if (coefficient) {
return new ExprPolynomial(this, exprPoly);
}
if (numericFunction) {
if (exprPoly.isNumericFunction()) {
return new ExprPolynomial(this, exprPoly);
}
throw new ClassCastException(exprPoly.toString());
} else {
return new ExprPolynomial(this, exprPoly);
}
} else if (exprPoly.isNumber()) {
return new ExprPolynomial(this, exprPoly);
}
if (exprPoly.isFree(Predicates.in(vars), true)) {
return new ExprPolynomial(this, exprPoly);
}
throw new ClassCastException(exprPoly.toString());
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Solve method booleansSolve.
/**
* Solve boolean expressions recursivel.y
*
* @param expr
* @param variables
* @param maximumNumberOfResults
* @param position
* @param resultList
*/
protected static void booleansSolve(IExpr expr, IAST variables, int maximumNumberOfResults, int position, IAST resultList) {
if (maximumNumberOfResults > 0 && maximumNumberOfResults < resultList.size()) {
return;
}
if (variables.size() <= position) {
if (EvalEngine.get().evalTrue(expr)) {
IAST list = F.List();
for (int i = 1; i < variables.size(); i++) {
ISymbol sym = (ISymbol) variables.get(i);
list.append(F.Rule(sym, sym.get()));
}
resultList.append(list);
}
return;
}
IExpr sym = variables.get(position);
if (sym.isSymbol()) {
try {
((ISymbol) sym).pushLocalVariable(F.False);
booleansSolve(expr, variables, maximumNumberOfResults, position + 1, resultList);
} finally {
((ISymbol) sym).popLocalVariable();
}
try {
((ISymbol) sym).pushLocalVariable(F.True);
booleansSolve(expr, variables, maximumNumberOfResults, position + 1, resultList);
} finally {
((ISymbol) sym).popLocalVariable();
}
}
}
use of org.matheclipse.core.interfaces.ISymbol 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 org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Solve method rootsOfUnivariatePolynomial.
/**
* Evaluate the roots of a univariate polynomial with the Roots[] function.
*
* @param exprAnalyzer
* @param fListOfVariables
* @return
*/
private static IAST rootsOfUnivariatePolynomial(ExprAnalyzer exprAnalyzer, EvalEngine engine) {
IExpr expr = exprAnalyzer.getNumerator();
IExpr denom = exprAnalyzer.getDenominator();
// try to solve the expr for a symbol in the symbol set
for (ISymbol sym : exprAnalyzer.getSymbolSet()) {
IExpr temp = F.NIL;
if (expr.isNumericMode() && denom.isOne()) {
temp = NRoots.roots(expr, F.List(sym), engine);
}
if (!temp.isPresent()) {
temp = Roots.rootsOfVariable(expr, denom, F.List(sym), expr.isNumericMode(), engine);
}
if (temp.isPresent()) {
IAST resultList = F.List();
if (temp.isASTSizeGE(F.List, 2)) {
IAST rootsList = (IAST) temp;
for (IExpr root : rootsList) {
IAST rule = F.Rule(sym, root);
resultList.append(rule);
}
return resultList;
}
return F.NIL;
}
}
return F.NIL;
}
Aggregations