use of org.matheclipse.core.expression.F in project symja_android_library by axkr.
the class SparseArrayExpr method arrayRules.
/**
* Create array rules from the nested lists. From array rules a sparse array can be created.
*
* @param nestedListsOfValues
* @return
*/
public static IAST arrayRules(IAST nestedListsOfValues, IExpr defaultValue) {
int depth = SparseArrayExpr.depth(nestedListsOfValues, 1);
if (depth < 0) {
return F.NIL;
}
// default value rule is additionally appended at the end!
IASTAppendable result = F.ListAlloc(F.allocMin32(F.allocLevel1(nestedListsOfValues, x -> x.isList()) + 2));
IASTMutable positions = F.constantArray(F.C1, depth);
if (SparseArrayExpr.arrayRulesRecursive(nestedListsOfValues, depth + 1, depth, positions, defaultValue, result)) {
result.append(F.Rule(F.constantArray(F.$b(), depth), defaultValue));
return result;
}
return F.NIL;
}
use of org.matheclipse.core.expression.F in project symja_android_library by axkr.
the class Predicates method toFreeQ.
/**
* Convert the pattern into a pattern-matching predicate used in {@link F#FreeQ(IExpr, IExpr)}.
* FreeQ does test for subsequences (MemberQ does not test for subsequences).
*
* @param pattern
* @return
* @see IExpr#isFree(Predicate, boolean)
*/
public static Predicate<IExpr> toFreeQ(IExpr pattern) {
if (pattern.isSymbol() || pattern.isNumber() || pattern.isString()) {
return x -> x.equals(pattern);
}
final IPatternMatcher matcher;
if (pattern.isOrderlessAST() && pattern.isFreeOfPatterns()) {
// append a BlankNullSequence[] to match the parts of an Orderless expression
IPatternSequence blankNullRest = F.$ps(null, true);
IASTAppendable newPattern = ((IAST) pattern).copyAppendable();
newPattern.append(blankNullRest);
matcher = new PatternMatcher(newPattern);
} else {
matcher = new PatternMatcher(pattern);
}
return matcher;
}
use of org.matheclipse.core.expression.F in project symja_android_library by axkr.
the class Eliminate method extractVariableRecursive.
/**
* Extract a value for the given <code>variabe</code>.
*
* @param exprWithVariable expression which contains the given <code>variabe</code>.
* @param exprWithoutVariable expression which doesn't contain the given <code>variabe</code>.
* @param variable the variable which should be eliminated.
* @param multipleValues if <code>true</code> multiple results are returned as list of values
* @return <code>F.NIL</code> if we can't find an equation for the given variable <code>x</code>.
*/
private static IExpr extractVariableRecursive(IExpr exprWithVariable, IExpr exprWithoutVariable, Predicate<IExpr> predicate, IExpr variable, boolean multipleValues, EvalEngine engine) {
if (exprWithVariable.equals(variable)) {
return exprWithoutVariable;
}
if (exprWithVariable.isAST()) {
IAST ast = (IAST) exprWithVariable;
if (ast.isAST1()) {
IASTAppendable inverseFunction = InverseFunction.getUnaryInverseFunction(ast, true);
if (inverseFunction.isPresent()) {
if (exprWithVariable.isAbs()) {
if (exprWithoutVariable.isNonNegativeResult()) {
// example: Abs(x-1) == 1
inverseFunction.append(exprWithoutVariable);
return extractVariableRecursive(ast.arg1(), inverseFunction, predicate, variable, multipleValues, engine);
}
return S.True;
} else {
// example: Sin(f(x)) == y -> f(x) == ArcSin(y)
inverseFunction.append(exprWithoutVariable);
return extractVariableRecursive(ast.arg1(), inverseFunction, predicate, variable, multipleValues, engine);
}
}
} else {
int size = ast.size();
if (size > 2) {
if (exprWithoutVariable.isZero() && ast.isPlus()) {
IAST elimZeroPlus = F.binaryAST2(elimzeroplus, ast, variable);
IExpr result = zeroPlusMatcher().apply(elimZeroPlus);
if (result.isPresent()) {
return resultWithIfunMessage(result, variable, exprWithoutVariable, multipleValues, engine);
}
}
IAST elimInverse = F.binaryAST2(eliminv, ast, variable);
IExpr result = inverseMatcher().apply(elimInverse);
if (result.isPresent()) {
return resultWithIfunMessage(result, variable, exprWithoutVariable, multipleValues, engine);
}
}
if (ast.isPlus()) {
// a + b + c....
if (//
exprWithoutVariable.isNumericFunction() && ast.isPolynomial(variable) && ast.isNumericFunction(variable)) {
IAST temp = RootsFunctions.rootsOfVariable(F.Subtract.of(ast, exprWithoutVariable), F.C1, F.list(variable), engine.isNumericMode(), engine);
if (temp.isList() && temp.size() > 1) {
if (!multipleValues || temp.size() == 2) {
return temp.first();
}
return temp;
}
}
IAST[] plusFilter = ast.filter(x -> x.isFree(predicate, true));
IAST plusWithoutVariable = plusFilter[0];
IAST plusWithVariable = plusFilter[1];
if (plusWithoutVariable.isAST0()) {
IExpr factor = engine.evaluateNIL(F.Factor(ast));
if (factor.isPresent() && factor.isTimes()) {
// a * b * c....
IAST times = (IAST) factor;
IAST[] timesFilter = times.filter(x -> x.isFree(predicate, true));
IAST timesWithoutVariable = timesFilter[0];
IAST timesWithVariable = timesFilter[1];
if (timesWithoutVariable.isAST0()) {
return F.NIL;
}
IExpr rhsWithoutVariable = engine.evaluate(F.Divide(exprWithoutVariable, timesWithoutVariable));
return extractVariableRecursive(timesWithVariable.oneIdentity1(), rhsWithoutVariable, predicate, variable, multipleValues, engine);
}
return F.NIL;
}
IExpr rhsWithoutVariable = engine.evaluate(F.Subtract(exprWithoutVariable, plusWithoutVariable));
return extractVariableRecursive(plusWithVariable.oneIdentity0(), rhsWithoutVariable, predicate, variable, multipleValues, engine);
} else if (ast.isTimes()) {
// a * b * c....
IAST[] timesFilter = ast.filter(x -> x.isFree(predicate, true));
IAST timesWithoutVariable = timesFilter[0];
IAST timesWithVariable = timesFilter[1];
if (timesWithoutVariable.isAST0()) {
IExpr[] numerDenom = Algebra.getNumeratorDenominator(ast, EvalEngine.get());
if (!numerDenom[1].isOne()) {
IExpr[] numerLinear = numerDenom[0].linear(variable);
if (numerLinear != null) {
IExpr[] denomLinear = numerDenom[1].linear(variable);
if (denomLinear != null) {
IExpr temp = EvalEngine.get().evaluate(numerLinear[1].subtract(denomLinear[1].times(exprWithoutVariable)));
if (!temp.isZero()) {
return numerLinear[0].negate().plus(denomLinear[0].times(exprWithoutVariable)).times(temp.power(-1L));
}
}
}
}
// no change for given expression
return F.NIL;
}
IExpr value = F.Divide(exprWithoutVariable, timesWithoutVariable);
return extractVariableRecursive(timesWithVariable.oneIdentity1(), value, predicate, variable, multipleValues, engine);
} else if (ast.isPower()) {
IExpr base = ast.base();
IExpr exponent = ast.exponent();
if (exponent.isFree(predicate, true)) {
// f(x) ^ a
printIfunMessage(engine);
IExpr value = engine.evaluate(F.Power(exprWithoutVariable, F.Divide(F.C1, exponent)));
return extractVariableRecursive(base, value, predicate, variable, multipleValues, engine);
} else if (base.isFree(predicate, true)) {
if (base.isE()) {
if (exponent.isRealResult()) {
// E ^ f(x) /; Element(f(x), Reals)
return extractVariableRecursive(exponent, F.Log(exprWithoutVariable), predicate, variable, multipleValues, engine);
}
// E ^ f(x) /; Element(f(x), Complexes)
IExpr c1 = F.C(1);
final IExpr exprwovar = exprWithoutVariable;
IExpr temp = // [$ ConditionalExpression(2*I*Pi*c1 + Log(exprwovar), Element(c1, Integers)) $]
F.ConditionalExpression(F.Plus(F.Times(F.C2, F.CI, S.Pi, c1), F.Log(exprwovar)), // $$;
F.Element(c1, S.Integers));
return extractVariableRecursive(exponent, temp, predicate, variable, multipleValues, engine);
}
// a ^ f(x)
IExpr value = F.Divide(F.Log(exprWithoutVariable), F.Log(base));
return extractVariableRecursive(exponent, value, predicate, variable, multipleValues, engine);
}
}
}
}
return F.NIL;
}
Aggregations