use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Package method determineRuleHead.
/**
* Determine the head symbol of the given rule
*
* @param rule
* @param unprotectedSymbolSet
* @param convertedSymbolMap
*/
private static void determineRuleHead(IAST rule, HashSet<ISymbol> unprotectedSymbolSet, HashMap<String, ISymbol> convertedSymbolMap) {
ISymbol lhsHead;
if (rule.size() > 1 && (rule.head().equals(F.Set) || rule.head().equals(F.SetDelayed) || rule.head().equals(F.UpSet) || rule.head().equals(F.UpSetDelayed))) {
// determine the head to which this rule is associated
lhsHead = null;
if (rule.arg1().isAST()) {
lhsHead = ((IAST) rule.arg1()).topHead();
} else if (rule.arg1().isSymbol()) {
lhsHead = (ISymbol) rule.arg1();
}
if (lhsHead != null && !unprotectedSymbolSet.contains(lhsHead)) {
ISymbol toSymbol = convertedSymbolMap.get(lhsHead.toString());
if (toSymbol == null) {
// define a package private symbol
toSymbol = F.predefinedSymbol("@" + EvalEngine.getNextCounter() + lhsHead.toString());
convertedSymbolMap.put(lhsHead.toString(), toSymbol);
}
}
}
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Programming method rememberVariables.
/**
* Remember which local variable names (appended with the module counter) we use in the given
* <code>variablesMap</code>.
*
* @param variablesList
* initializer variables list from the <code>Module</code> function
* @param varAppend
* the module counter string which aer appended to the variable names.
* @param variablesMap
* the resulting module variables map
* @param engine
* the evaluation engine
*/
private static void rememberVariables(IAST variablesList, final String varAppend, final java.util.Map<ISymbol, ISymbol> variablesMap, final EvalEngine engine) {
ISymbol oldSymbol;
ISymbol newSymbol;
for (int i = 1; i < variablesList.size(); i++) {
if (variablesList.get(i).isSymbol()) {
oldSymbol = (ISymbol) variablesList.get(i);
newSymbol = F.userSymbol(oldSymbol.toString() + varAppend, engine);
variablesMap.put(oldSymbol, newSymbol);
newSymbol.pushLocalVariable();
} else {
if (variablesList.get(i).isAST(F.Set, 3)) {
final IAST setFun = (IAST) variablesList.get(i);
if (setFun.arg1().isSymbol()) {
oldSymbol = (ISymbol) setFun.arg1();
newSymbol = F.userSymbol(oldSymbol.toString() + varAppend, engine);
variablesMap.put(oldSymbol, newSymbol);
IExpr rightHandSide = setFun.arg2();
try {
rightHandSide = engine.evaluate(rightHandSide);
} catch (MathException me) {
if (Config.DEBUG) {
me.printStackTrace();
}
}
newSymbol.pushLocalVariable(rightHandSide);
}
}
}
}
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Element method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 3);
final IExpr arg2 = engine.evaluate(ast.arg2());
IExpr truthValue;
if (arg2.isSymbol()) {
final IExpr arg1 = engine.evaluate(ast.arg1());
if (arg1.isAST(F.Alternatives)) {
IAST list = (IAST) arg1;
for (int i = 1; i < list.size(); i++) {
truthValue = assumeDomain(arg1, (ISymbol) arg2);
if (truthValue.isPresent()) {
return truthValue;
}
}
return F.True;
} else {
return assumeDomain(arg1, (ISymbol) arg2);
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Iterator method create.
/**
* Iterator specification for functions like <code>Table()</code> or
* <code>Sum()</code> or <code>Product()</code>
*
* @param list
* a list representing an iterator specification
* @param symbol
* the variable symbol
* @param engine
* the evaluation engine
* @return the iterator
*/
public static IIterator<IExpr> create(final IAST list, final ISymbol symbol, final EvalEngine engine) {
EvalEngine evalEngine = engine;
IExpr lowerLimit;
IExpr upperLimit;
IExpr step;
ISymbol variable;
boolean fNumericMode;
boolean localNumericMode = evalEngine.isNumericMode();
try {
if (list.hasNumericArgument()) {
evalEngine.setNumericMode(true);
}
fNumericMode = evalEngine.isNumericMode();
switch(list.size()) {
case 2:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
step = F.C1;
variable = symbol;
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(symbol, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
return new RationalIterator(symbol, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isSignedNumber()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
break;
case 3:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = F.C1;
variable = symbol;
if (lowerLimit instanceof Num && upperLimit instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
}
if (lowerLimit.isInteger() && upperLimit.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(symbol, iLowerLimit, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational()) {
try {
return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, F.C1);
}
break;
case 4:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = evalEngine.evalWithoutNumericReset(list.arg3());
variable = symbol;
if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
}
if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
int iStep = ((IInteger) step).toInt();
return new IntIterator(symbol, iLowerLimit, iUpperLimit, iStep);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
try {
return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber() && step.isSignedNumber()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
}
break;
default:
lowerLimit = null;
upperLimit = null;
step = null;
variable = null;
}
return new ExprIterator(variable, evalEngine, lowerLimit, upperLimit, step, fNumericMode);
} finally {
evalEngine.setNumericMode(localNumericMode);
}
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class MagicProcessor method preProcessQues.
String preProcessQues() {
// Convert expression like "2*x+7=10" to Solve(2*x+7 - 10 == 0, x)
if (outPut.contains("=")) {
String processEq = inputQuestionIsPossiblyASystemOfEquation(outPut);
if (processEq != null) {
// Args will be handled in wrtArgumentMising
outPut = ((ISymbol) F.Solve).toString() + "(" + processEq + ")";
}
/*
* String [] list = outPut.split("="); if(list.length == 2) { String
* eq = list[1] + "- (" + list[0] + ")"; IExpr eqn =
* MathUtils.parse(eq, null); if(eqn.isAST() && isPolynomial((IAST)
* eqn)) { String vars = solve_get_arg_if_missing(eqn); isSysOfEq =
* true; outPut = ((Symbol) F.Solve).toString() + "(" + eq + " == 0"
* + "," + vars + ")"; } }
*/
}
IExpr ques = MathUtils.parse(outPut, null);
if (ques == null)
return outPut;
Log.debug("ques = " + ques.toString());
if (wrtArgumentMising(ques, F.Solve)) {
IExpr equations = getArg1(ques);
String vars = solve_get_arg_if_missing(equations);
if (vars != null && err == null) {
outPut = ((Symbol) F.Solve).toString() + "(" + equations.toString() + "," + vars + ")";
Log.debug(" Result after eq processing " + outPut);
}
}
if (wrtArgumentMising(ques, F.D)) {
IExpr fn = getArg1(ques);
// Extract variables from equations
org.matheclipse.core.convert.VariablesSet eVar = new org.matheclipse.core.convert.VariablesSet(fn);
String var = null;
if (eVar.isSize(1))
var = getVarString(eVar, false);
else
var = getVarString(eVar, true);
outPut = ((Symbol) F.D).toString() + "(" + fn.toString() + "," + var + ")";
}
if (wrtArgumentMising(ques, F.Integrate)) {
IExpr fn = getArg1(ques);
// Extract variables from equations
org.matheclipse.core.convert.VariablesSet eVar = new org.matheclipse.core.convert.VariablesSet(fn);
String var = null;
if (eVar.isSize(1))
var = getVarString(eVar, false);
else
var = getVarString(eVar, true);
outPut = ((Symbol) F.Integrate).toString() + "(" + fn.toString() + "," + var + ")";
}
Log.debug("Processed q = " + outPut);
return (err == null) ? outPut : err;
}
Aggregations