Skip to content

Commit

Permalink
Improve calls to IAST#copyAppendable()
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Sep 13, 2024
1 parent 4891784 commit 7b48ea1
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -561,15 +561,15 @@ private static void calculateNumeratorGCD(IExpr arg, int position, IASTAppendabl
*/
private static Optional<IExpr[]> cancelPlusIntegerGCD(IAST numeratorPlus,
IInteger denominator) {
IASTAppendable plus = numeratorPlus.copyAppendable();
IASTAppendable gcd = F.ast(S.GCD, plus.size() + 1);
IASTAppendable gcd = F.ast(S.GCD, numeratorPlus.size() + 1);
gcd.append(denominator);
boolean evaled = !plus.exists((IExpr x) -> collectGCDFactors(x, gcd));
boolean evaled = !numeratorPlus.exists((IExpr x) -> collectGCDFactors(x, gcd));
if (evaled) {
// GCD() has attribute Orderless, so the arguments will
// be sorted by evaluation!
IExpr igcd = F.eval(gcd);
if (igcd.isInteger() && !igcd.isOne()) {
IASTAppendable plus = numeratorPlus.copyAppendable();
return calculatePlusIntegerGCD(plus, denominator, (IInteger) igcd);
}
}
Expand Down Expand Up @@ -615,15 +615,15 @@ private static IExpr cancelPowerTimes(IExpr powerTimesAST, EvalEngine engine)
IExpr p10 = parts.get()[1];
IExpr p11 = F.C1;
if (p00.isPlus()) {
IAST numParts = ((IAST) p00).partitionPlus(x -> isPolynomial(x), F.C0, F.C1, S.List);
IAST numParts = p00.partitionPlus(x -> isPolynomial(x), F.C0, F.C1, S.List);
if (numParts.isPresent() && !numParts.arg1().isOne()) {
p00 = numParts.arg1();
p01 = numParts.arg2();
}
}

if (p10.isPlus()) {
IAST denParts = ((IAST) p10).partitionPlus(x -> isPolynomial(x), F.C0, F.C1, S.List);
IAST denParts = p10.partitionPlus(x -> isPolynomial(x), F.C0, F.C1, S.List);
if (denParts.isPresent() && !denParts.arg1().isOne()) {
p10 = denParts.arg1();
p11 = denParts.arg2();
Expand Down Expand Up @@ -918,8 +918,9 @@ private IExpr collectSingleVariableRecursive(IExpr expr, IExpr x, final IAST lis
// "rest" variable
IPatternSequence blankNullRest =
F.$ps(F.Dummy("§rest§" + EvalEngine.incModuleCounter()), true);
IASTAppendable newLHS = ((IAST) x).copyAppendable();
newLHS.append(blankNullRest);
IASTAppendable newLHS = ((IAST) x).appendClone(blankNullRest);
// newLHS.append(blankNullRest);

final IPatternMatcher matcher = engine.evalPatternMatcher(newLHS);

collectTimesToMap(x, poly, matcher, defaultdict, rest, blankNullRest);
Expand Down Expand Up @@ -1271,8 +1272,8 @@ public void distributePositionRecursive(IASTAppendable stepResult, int position)
}

private void distributeStep(IExpr x, IAST stepResult, int position) {
IASTAppendable res2 = stepResult.copyAppendable();
res2.append(x);
IASTAppendable res2 = stepResult.appendClone(x);
// res2.append(x);
distributePositionRecursive(res2, position + 1);
}
}
Expand Down Expand Up @@ -4233,7 +4234,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
}
if (arg1.isAST()) {
ToRadicalsVisitor visitor = new ToRadicalsVisitor(ast);
temp = ((IAST) arg1).accept(visitor);
temp = arg1.accept(visitor);
if (temp.isPresent()) {
return temp;
}
Expand Down Expand Up @@ -5012,9 +5013,8 @@ public static IExpr cancelFractionalParts(IExpr powerTimesAST) throws JASConvers
}

if (numerator.isPlus() && denominator.isPlus()) {
IAST numParts = ((IAST) numerator).partitionPlus(x -> isPolynomial(x), F.C0, F.C1, S.List);
IAST denParts =
((IAST) denominator).partitionPlus(x -> isPolynomial(x), F.C0, F.C1, S.List);
IAST numParts = numerator.partitionPlus(x -> isPolynomial(x), F.C0, F.C1, S.List);
IAST denParts = denominator.partitionPlus(x -> isPolynomial(x), F.C0, F.C1, S.List);
if (denParts.isPresent() && !denParts.arg1().isOne()) {
Optional<IExpr[]> result = cancelGCD(numParts.arg1(), denParts.arg1());
if (result.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3613,7 +3613,7 @@ public static IExpr binaryOperator(IAST ast, final IExpr base, final IExpr expon
IExpr x = ((IAST) base).arg1();
IExpr mNeg = exponent.negate();
boolean disabledTrigRules = engine.isDisabledTrigRules();
int id = ((IAST) base).headID();
int id = base.headID();
switch (id) {
case ID.Tan:
// Tan(x_)^m_?(IntegerQ(#)&&#<0 &):=Cot(x)^(-m),
Expand Down Expand Up @@ -4153,7 +4153,7 @@ private static IExpr powerRealBaseOrRealExponent(final IExpr base, final IExpr e
}
try {
long n = ((IInteger) exponent).toLong();
return ((INumber) base).power(n);
return base.power(n);
} catch (ArithmeticException ae) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2242,15 +2242,14 @@ public IExpr evaluate(IAST ast, EvalEngine engine) {
}
boolean evaled = false;

IASTAppendable result = astEvaled.copyAppendable();
IExpr.COMPARE_TERNARY[] cResult = new IExpr.COMPARE_TERNARY[astEvaled.size()];
cResult[0] = IExpr.COMPARE_TERNARY.TRUE;
for (int i = 1; i < astEvaled.argSize(); i++) {
IExpr arg = result.get(i);
IExpr arg = astEvaled.get(i);
if (arg.equals(S.Undefined)) {
return S.Undefined;
}
final IExpr.COMPARE_TERNARY b = prepareCompare(arg, result.get(i + 1), engine);
final IExpr.COMPARE_TERNARY b = prepareCompare(arg, astEvaled.get(i + 1), engine);
if (b == IExpr.COMPARE_TERNARY.FALSE) {
return S.False;
}
Expand All @@ -2266,6 +2265,7 @@ public IExpr evaluate(IAST ast, EvalEngine engine) {
}
int i = 2;
evaled = false;
IASTAppendable result = astEvaled.copyAppendable();
for (int j = 1; j < astEvaled.size(); j++) {
if (cResult[j - 1] == IExpr.COMPARE_TERNARY.TRUE
&& cResult[j] == IExpr.COMPARE_TERNARY.TRUE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public synchronized IAST next() {
if (empty) {
throw new RuntimeException("invalid call of next()");
}
// IAST res = (IAST) current.clone();
IAST res = current.copyAppendable();

final IAST res = current.copy();
// search iterator which hasNext
int i = compit.size() - 1;
for (; i >= 0; i--) {
Expand Down Expand Up @@ -2687,10 +2687,9 @@ private static void tuples(final IAST originalList, final int n, IASTAppendable
RecursionLimitExceeded.throwIt(counter, ast);
}
}
IASTAppendable temp;
for (int j = 1; j < originalList.size(); j++) {
temp = subResult.copyAppendable();
temp.append(originalList.get(j));
IASTAppendable temp = subResult.appendClone(originalList.get(j));
// temp.append(originalList.get(j));
tuples(originalList, n - 1, result, temp, ast, engine);
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ public MultipleArrayFunction(final EvalEngine engine, final IAST headAST) {

@Override
public IExpr evaluate(final ISymbol[] variables, final IExpr[] index) {
final IASTAppendable ast = fHeadAST.copyAppendable();
final IASTAppendable ast = fHeadAST.copyAppendable(index.length);
return fEngine.evaluate(ast.appendArgs(0, index.length, i -> index[i]));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ private static void setBoxRatios(StringBuilder graphicControl, double[] rangeXY)
private static IExpr sliderWithFormulas(IExpr formula, IAST sliderRange, EvalEngine engine) {
JavaScriptFormFactory toJS =
new JavaScriptFormFactory(true, false, -1, -1, JavaScriptFormFactory.USE_MATHCELL);
IASTAppendable newsliderRange = sliderRange.copyAppendable();
IASTAppendable newsliderRange = sliderRange.copyAppendable(1);
double stepValue = 1.0;
double minValue = sliderRange.arg2().evalf();
double maxValue = sliderRange.arg3().evalf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
}
IExpr arg1 = ast.arg1();
if (arg1.isFunctionID(ID.Plot, ID.ParametricPlot, ID.ParametricPlot)) {
IASTAppendable temp = ((IAST) arg1).copyAppendable();
temp.append(F.Rule(S.JSForm, S.True));
IASTAppendable temp = ((IAST) arg1).appendClone(F.Rule(S.JSForm, S.True));
arg1 = temp;
}
arg1 = engine.evaluate(arg1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ private static IExpr powerLimit(final IAST powerAST, LimitData data, EvalEngine
}
}
if (base.isTimes()) {
IAST isFreeResult = ((IAST) base).partitionTimes(x -> x.isFree(data.variable(), true),
IAST isFreeResult = base.partitionTimes(x -> x.isFree(data.variable(), true),
F.C1, F.C1, S.List);
if (!isFreeResult.arg2().isOne()) {
return F.Times(F.Power(isFreeResult.arg1(), exponent),
Expand Down Expand Up @@ -1886,7 +1886,7 @@ public static IExpr polynomialSeriesCoefficient(IExpr univariatePolynomial, IExp
}
}
for (int i = 1; i < rest.size(); i++) {
IASTAppendable term = seriesTemplate.copyAppendable();
IASTMutable term = seriesTemplate.copy();
term.set(1, rest.get(i));
coefficientPlus.append(term);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public IExpr evaluate(IAST ast, EvalEngine engine) {
if (ast.argSize() < 2 || ast.argSize() > 4) {
return Errors.printArgMessage(ast, ARGS_2_4, engine);
}
IASTAppendable evaledAST = ast.copyAppendable();
IASTMutable evaledAST = ast.copy();
evaledAST.setArgs(evaledAST.size(), (int i) -> engine.evaluate(evaledAST.get(i)));

int lastIndex = evaledAST.argSize();
Expand Down Expand Up @@ -575,7 +575,7 @@ public IExpr evaluate(IAST ast, EvalEngine engine) {
int[] positions = null;
if (arg2.isInteger()) {
positions = new int[1];
positions[0] = ((IInteger) arg2).toIntDefault();
positions[0] = arg2.toIntDefault();
if (positions[0] == Integer.MIN_VALUE) {
return F.NIL;
}
Expand Down Expand Up @@ -2211,7 +2211,7 @@ public static IAST threadList(final IAST list, IExpr head, IExpr mapHead) {
int listLength = -1;

for (int i = 1; i < list.size(); i++) {
if ((list.get(i).isAST()) && (((IAST) list.get(i)).head().equals(head))) {
if ((list.get(i).isAST()) && (list.get(i).head().equals(head))) {
if (listLength == -1) {
listLength = ((IAST) list.get(i)).argSize();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@ public IAST next() {
return null;
}
IASTAppendable part = fResultList.copyAppendable();
IASTAppendable temp;
int j = 0;
for (int i = 1; i < partitionsIndex.length; i++) {
temp = fResultList.copyAppendable();
IASTAppendable temp = fResultList.copyAppendable();
for (int m = j; m < partitionsIndex[i]; m++) {
temp.append(fList.get(m + fOffset));
}
j = partitionsIndex[i];
part.append(temp);
}

temp = fResultList.copyAppendable();
IASTAppendable temp = fResultList.copyAppendable();
int n = fList.size() - fOffset;
for (int m = j; m < n; m++) {
temp.append(fList.get(m + fOffset));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static IExpr extractImaginaryUnit(final IExpr expression) {
*/
public static IExpr extractImaginaryUnit(final IExpr expression, boolean checkTimes) {
if (expression.isNumber()) {
if (((INumber) expression).isImaginaryUnit()) {
if (expression.isImaginaryUnit()) {
return F.C1;
}
if ((expression.isComplex() || expression.isComplexNumeric())//
Expand All @@ -186,7 +186,7 @@ public static IExpr extractImaginaryUnit(final IExpr expression, boolean checkTi
IAST timesAST = ((IAST) expression);
IExpr arg1 = timesAST.arg1();
if (arg1.isNumber()) {
if (((INumber) arg1).isImaginaryUnit()) {
if (arg1.isImaginaryUnit()) {
return timesAST.rest().oneIdentity1();
}
if ((arg1.isComplex() || arg1.isComplexNumeric())//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ public final IAST addEvalFlags(final int i) {
/** {@inheritDoc} */
@Override
public IASTAppendable appendAtClone(int position, IExpr expr) {
IASTAppendable ast = copyAppendable();
IASTAppendable ast = copyAppendable(1);
ast.append(position, expr);
return ast;
}
Expand Down Expand Up @@ -5172,7 +5172,11 @@ public IAST map(final Function<IExpr, ? extends IExpr> function, final int start
temp = function.apply(get(i));
if (temp.isPresent()) {
// something was evaluated - return a new IAST:
result = copyAppendable();
if (i > 1) {
result = copy();
} else {
result = copyAppendable();
}
result.set(i++, temp);
break;
}
Expand Down Expand Up @@ -5216,7 +5220,11 @@ public IAST mapLeaf(IExpr testHead, final Function<IExpr, IExpr> function,
}
if (temp.isPresent()) {
// something was evaluated - return a new IAST:
result = copyAppendable();
if (i > 0) {
result = copy();
} else {
result = copyAppendable();
}
result.set(i++, temp);
}
if (result.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,7 @@ public static Predicate<IExpr> toFreeQ(IExpr pattern) {
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);
IASTAppendable newPattern = ((IAST) pattern).appendClone(blankNullRest);
matcher = new PatternMatcher(newPattern);
} else {
matcher = new PatternMatcher(pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.matheclipse.core.expression.F;
import org.matheclipse.core.expression.S;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IASTAppendable;
import org.matheclipse.core.interfaces.IASTMutable;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.IPattern;
Expand Down Expand Up @@ -51,13 +50,13 @@ public IExpr visit(IASTMutable ast) {
return temp;
}
}
IASTAppendable result = F.NIL;
IASTMutable result = F.NIL;
int i = 1;
while (i < list.size()) {
temp = list.get(i).accept(this);
if (temp.isPresent()) {
// something was evaluated - return a new IAST:
result = list.copyAppendable();
result = list.copy();
result.set(i++, temp);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public ExprPolynomialRing(ExprRingFactory cf, IAST listOfVariables, int n, ExprT
nvar = n;
tord = t;
partial = false;
vars = listOfVariables.copyAppendable();
vars = listOfVariables.copy();
ZERO = new ExprPolynomial(this);
IExpr coeff = coFac.getONE();
evzero = new ExpVectorLong(nvar);
Expand Down Expand Up @@ -910,7 +910,7 @@ public IAST setVars(IAST v) {
"v not matching number of variables: " + v.toString() + ", nvar " + nvar);
}
IAST t = vars;
vars = v.copyAppendable(); // Arrays.copyOf(v, v.length); // > Java-5
vars = v.copy(); // Arrays.copyOf(v, v.length); // > Java-5
return t;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public SymbolicPolynomialRing(ExprRingFactory cf, IAST listOfVariables, int n,
nvar = n;
tord = t;
partial = false;
vars = listOfVariables.copyAppendable();
vars = listOfVariables.copy();
ZERO = new SymbolicPolynomial(this);
IExpr coeff = coFac.getONE();
evzero = new ExpVectorSymbolic(nvar);
Expand Down Expand Up @@ -968,7 +968,7 @@ public IAST setVars(IAST v) {
"v not matching number of variables: " + v.toString() + ", nvar " + nvar);
}
IAST t = vars;
vars = v.copyAppendable(); // Arrays.copyOf(v, v.length); // > Java-5
vars = v.copy(); // Arrays.copyOf(v, v.length); // > Java-5
return t;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private static IAST createDerivativeN(final IExpr header, final IAST args, IExpr

private static IAST addDerivative(final int pos, IAST deriveHead, final IExpr header,
final IAST args) {
IASTMutable derivativeHead1 = deriveHead.copyAppendable();
IASTMutable derivativeHead1 = deriveHead.copy();
for (int i = 1; i < derivativeHead1.size(); i++) {
if (i == pos) {
derivativeHead1.set(i, derivativeHead1.get(i).inc());
Expand Down
Loading

0 comments on commit 7b48ea1

Please sign in to comment.