Skip to content

Commit 079dd37

Browse files
committed
WIP #1104 make factoring of common terms more consistent in Simplify
`Simplify(a-a*b-a*c) -> a*(1-b-c)` `Simplify(a-a*b+a*c) -> a*(1-b+c)`
1 parent 27cd93d commit 079dd37

File tree

2 files changed

+94
-84
lines changed

2 files changed

+94
-84
lines changed

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/SimplifyFunctions.java

+62-63
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ public boolean checkLessEqual(IExpr expr) {
7777
}
7878

7979
public boolean checkLess(IExpr expr) {
80+
long counter = complexityFunction.apply(expr);
81+
if (counter < this.minCounter) {
82+
this.minCounter = counter;
83+
this.result = expr;
84+
return true;
85+
}
86+
return false;
87+
}
88+
89+
public boolean checkLessPlusTimesPower(IExpr expr) {
8090
long counter = complexityFunction.apply(expr);
8191
if (counter < this.minCounter) {
8292
this.minCounter = counter;
@@ -347,43 +357,6 @@ private IExpr tryTransformations(IExpr expr) {
347357
if (temp.isPresent()) {
348358
simplifiedResult.checkLessEqual(temp);
349359
}
350-
351-
Optional<IExpr[]> commonFactors =
352-
Algebra.InternalFindCommonFactorPlus.findCommonFactors((IAST) expr, true);
353-
if (commonFactors.isPresent()) {
354-
temp = eval(F.Times(commonFactors.get()[0], commonFactors.get()[1]));
355-
simplifiedResult.checkLessEqual(temp);
356-
}
357-
358-
if (simplifiedResult.result.isPlus()) {
359-
temp = tryPlusLog((IAST) simplifiedResult.result);
360-
} else {
361-
temp = tryPlusLog((IAST) expr);
362-
}
363-
if (temp.isPresent()) {
364-
temp = eval(temp);
365-
simplifiedResult.checkLessEqual(temp);
366-
}
367-
// } else if (expr.isExp() && expr.second().isTimes()) {
368-
// IAST times = (IAST) expr.second();
369-
// IExpr i = Times.of(times, F.CNI, F.Power(F.Pi, F.CN1));
370-
// if (i.isRational()) {
371-
// IRational rat = (IRational) i;
372-
// if (rat.isGT(F.C1) || rat.isLE(F.CN1)) {
373-
// IInteger t = rat.trunc();
374-
// t = t.add(t.mod(F.C2));
375-
// // exp(I*(i - t)*Pi)
376-
// return F.Exp.of(F.Times(F.CI, F.Pi, F.Subtract(i, t)));
377-
// } else {
378-
// IRational t1 = rat.multiply(F.C6).normalize();
379-
// IRational t2 = rat.multiply(F.C4).normalize();
380-
// if (t1.isInteger() || t2.isInteger()) {
381-
// // Cos(- I*times) + I*Sin(- I*times)
382-
// return F.Plus.of(F.Cos(F.Times(F.CNI, times)),
383-
// F.Times(F.CI, F.Sin(F.Times(F.CNI, times))));
384-
// }
385-
// }
386-
// }
387360
}
388361

389362
if (simplifiedResult.result.isAST()) {
@@ -393,7 +366,7 @@ private IExpr tryTransformations(IExpr expr) {
393366
try {
394367
temp = F.evalExpandAll(expr);
395368
expandAllCounter = fComplexityFunction.apply(temp);
396-
simplifiedResult.checkLess(temp);
369+
simplifiedResult.checkLessPlusTimesPower(temp);
397370
} catch (RuntimeException rex) {
398371
Errors.rethrowsInterruptException(rex);
399372
//
@@ -407,17 +380,17 @@ private IExpr tryTransformations(IExpr expr) {
407380

408381
try {
409382
temp = eval(F.TrigExpand(expr));
410-
simplifiedResult.checkLess(temp);
383+
simplifiedResult.checkLessPlusTimesPower(temp);
411384
} catch (ValidateException ve) {
412385
//
413386
}
414387

415388
try {
416389
temp = eval(F.TrigToExp(expr));
417-
if (!simplifiedResult.checkLess(temp)) {
390+
if (!simplifiedResult.checkLessPlusTimesPower(temp)) {
418391
if (fFullSimplify) {
419392
temp = eval(F.Factor(temp));
420-
simplifiedResult.checkLess(temp);
393+
simplifiedResult.checkLessPlusTimesPower(temp);
421394
}
422395
}
423396
} catch (ValidateException ve) {
@@ -426,7 +399,7 @@ private IExpr tryTransformations(IExpr expr) {
426399

427400
try {
428401
temp = eval(F.TrigReduce(expr));
429-
simplifiedResult.checkLess(temp);
402+
simplifiedResult.checkLessPlusTimesPower(temp);
430403
} catch (ValidateException ve) {
431404
//
432405
}
@@ -436,7 +409,7 @@ private IExpr tryTransformations(IExpr expr) {
436409
IExpr together = expr;
437410
if (simplifiedResult.minCounter < Config.MAX_SIMPLIFY_TOGETHER_LEAFCOUNT) {
438411
together = eval(F.Together(expr));
439-
simplifiedResult.checkLess(together);
412+
simplifiedResult.checkLessPlusTimesPower(together);
440413
}
441414

442415
if (fFullSimplify) {
@@ -449,7 +422,7 @@ private IExpr tryTransformations(IExpr expr) {
449422
// have
450423
// a new minimal expression
451424
IExpr divide = F.Divide(parts[0], parts[1]);
452-
simplifiedResult.checkLess(divide);
425+
simplifiedResult.checkLessPlusTimesPower(divide);
453426

454427
if (!numerator.isOne() && //
455428
!denominator.isOne()) {
@@ -458,7 +431,7 @@ private IExpr tryTransformations(IExpr expr) {
458431
}
459432

460433
temp = eval(F.ExpToTrig(expr));
461-
simplifiedResult.checkLess(temp);
434+
simplifiedResult.checkLessPlusTimesPower(temp);
462435

463436
}
464437

@@ -472,7 +445,7 @@ private IExpr tryTransformations(IExpr expr) {
472445
temp = F.NIL;
473446
if (fFullSimplify && expandAllCounter < 50) { // Config.MAX_SIMPLIFY_FACTOR_LEAFCOUNT) {
474447
temp = eval(F.Factor(expr));
475-
simplifiedResult.checkLess(temp);
448+
simplifiedResult.checkLessPlusTimesPower(temp);
476449
}
477450
// if (fFullSimplify
478451
// && (minCounter >= Config.MAX_SIMPLIFY_FACTOR_LEAFCOUNT || !temp.equals(expr))) {
@@ -487,7 +460,7 @@ private IExpr tryTransformations(IExpr expr) {
487460
// } else
488461
if (expandAllCounter < Config.MAX_SIMPLIFY_FACTOR_LEAFCOUNT) {
489462
temp = eval(F.FactorSquareFree(expr));
490-
simplifiedResult.checkLess(temp);
463+
simplifiedResult.checkLessPlusTimesPower(temp);
491464
}
492465

493466
} catch (ValidateException ve) {
@@ -498,11 +471,37 @@ private IExpr tryTransformations(IExpr expr) {
498471
if (!fNoApart //
499472
&& simplifiedResult.minCounter < Config.MAX_SIMPLIFY_APART_LEAFCOUNT) {
500473
temp = eval(F.Apart(expr));
501-
simplifiedResult.checkLess(temp);
474+
simplifiedResult.checkLessPlusTimesPower(temp);
502475
}
503476
} catch (ValidateException ve) {
504477
//
505478
}
479+
480+
// expr = simplifiedResult.result;
481+
if (expr.isPlus()) {
482+
temp = Algebra.factorTermsPlus((IAST) expr, EvalEngine.get());
483+
if (temp.isPresent()) {
484+
simplifiedResult.checkLessEqual(temp);
485+
}
486+
487+
Optional<IExpr[]> commonFactors =
488+
Algebra.InternalFindCommonFactorPlus.findCommonFactors((IAST) expr, true);
489+
if (commonFactors.isPresent()) {
490+
temp = eval(F.Times(commonFactors.get()[0], commonFactors.get()[1]));
491+
simplifiedResult.checkLessEqual(temp);
492+
}
493+
494+
if (simplifiedResult.result.isPlus()) {
495+
temp = tryPlusLog((IAST) simplifiedResult.result);
496+
} else {
497+
temp = tryPlusLog((IAST) expr);
498+
}
499+
if (temp.isPresent()) {
500+
temp = eval(temp);
501+
simplifiedResult.checkLessEqual(temp);
502+
}
503+
}
504+
506505
return simplifiedResult.result;
507506
} catch (LimitException aele) {
508507
//
@@ -532,7 +531,7 @@ private void tryPolynomialQuotientRemainder(IExpr numerator, IExpr denominator,
532531
temp.second().isZero()) {
533532
// the remainder is 0 here:
534533
IExpr arg1 = temp.first();
535-
if (sResult.checkLess(arg1)) {
534+
if (sResult.checkLessPlusTimesPower(arg1)) {
536535
evaled = true;
537536
break;
538537
}
@@ -546,7 +545,7 @@ private void tryPolynomialQuotientRemainder(IExpr numerator, IExpr denominator,
546545
temp.second().isZero()) {
547546
// the remainder is 0 here:
548547
IExpr arg1 = temp.first().reciprocal();
549-
if (sResult.checkLess(arg1)) {
548+
if (sResult.checkLessPlusTimesPower(arg1)) {
550549
break;
551550
}
552551
}
@@ -605,8 +604,8 @@ public IExpr visit(IASTMutable ast) {
605604
return temp;
606605
}
607606
}
608-
temp = F.evalExpandAll(ast);
609-
sResult.checkLess(temp);
607+
// temp = F.evalExpandAll(ast);
608+
// sResult.checkLess(temp);
610609

611610
functionExpand(ast, sResult);
612611
return sResult.result;
@@ -623,7 +622,7 @@ private IExpr visitPower(IASTMutable powerAST, SimplifiedResult sResult) {
623622
IExpr expr = eval(F.Expand(F.Times(plus1, plus2)));
624623
if (expr.isNumber() && !expr.isZero()) {
625624
IExpr powerSimplified = S.Times.of(expr.inverse(), plus2);
626-
if (sResult.checkLess(powerSimplified)) {
625+
if (sResult.checkLessPlusTimesPower(powerSimplified)) {
627626
return powerSimplified;
628627
}
629628
}
@@ -861,14 +860,14 @@ private IExpr reduceConjugateFactors(IASTMutable timesAST, SimplifiedResult sRes
861860
}
862861
temp = eval(F.Expand(temp));
863862
temp = numberFactors.times(temp);
864-
if (sResult.checkLess(temp)) {
863+
if (sResult.checkLessPlusTimesPower(temp)) {
865864
if (temp.isAtom()) {
866865
return temp;
867866
}
868867
}
869868
if (temp.isTimes()) {
870869
temp = eval(F.Expand(temp));
871-
if (sResult.checkLess(temp)) {
870+
if (sResult.checkLessPlusTimesPower(temp)) {
872871
if (temp.isAtom()) {
873872
return temp;
874873
}
@@ -911,7 +910,7 @@ private IExpr visitPlus(IASTMutable plusAST, SimplifiedResult sResult) {
911910
if (!temp.isPlus()) {
912911
return temp;
913912
}
914-
if (sResult.checkLess(temp)) {
913+
if (sResult.checkLessPlusTimesPower(temp)) {
915914
temp = sResult.result;
916915
if (temp.isPlus()) {
917916
plusAST = (IASTMutable) sResult.result;
@@ -939,7 +938,7 @@ private IExpr visitPlus(IASTMutable plusAST, SimplifiedResult sResult) {
939938
temp = hashRuleMap.evaluateRepeated(plusAST, fEngine);
940939
if (temp.isPresent()) {
941940
temp = eval(temp);
942-
if (sResult.checkLess(temp)) {
941+
if (sResult.checkLessPlusTimesPower(temp)) {
943942
return temp;
944943
}
945944
}
@@ -1130,7 +1129,7 @@ private IExpr tryArg1IsOnePlus(IASTMutable plusAST, SimplifiedResult sResult) {
11301129
}
11311130
IExpr temp = result.oneIdentity0();
11321131
if (temp.isPlus()) {
1133-
sResult.checkLess(temp);
1132+
sResult.checkLessPlusTimesPower(temp);
11341133
return F.NIL;
11351134
}
11361135
return temp;
@@ -1233,7 +1232,7 @@ private void functionExpand(IExpr expr, SimplifiedResult sResult) { // long minC
12331232
if (expr.isBooleanFunction()) {
12341233
try {
12351234
expr = eval(F.BooleanMinimize(expr));
1236-
sResult.checkLess(expr);
1235+
sResult.checkLessPlusTimesPower(expr);
12371236
return;
12381237
} catch (RuntimeException rex) {
12391238
Errors.rethrowsInterruptException(rex);
@@ -1242,7 +1241,7 @@ private void functionExpand(IExpr expr, SimplifiedResult sResult) { // long minC
12421241
} else if (fFullSimplify) {
12431242
try {
12441243
expr = eval(F.FunctionExpand(expr));
1245-
sResult.checkLess(expr);
1244+
sResult.checkLessPlusTimesPower(expr);
12461245
} catch (RuntimeException rex) {
12471246
Errors.rethrowsInterruptException(rex);
12481247
//
@@ -1252,7 +1251,7 @@ private void functionExpand(IExpr expr, SimplifiedResult sResult) { // long minC
12521251
IExpr re = expr.first().re();
12531252
IExpr im = expr.first().im();
12541253
IExpr temp = argReXImY(re, im, fEngine);
1255-
sResult.checkLess(temp);
1254+
sResult.checkLessPlusTimesPower(temp);
12561255
} catch (RuntimeException rex) {
12571256
Errors.rethrowsInterruptException(rex);
12581257
//
@@ -1272,7 +1271,7 @@ private void functionExpand(IExpr expr, SimplifiedResult sResult) { // long minC
12721271
result.set(i, arg.first());
12731272
}
12741273
}
1275-
sResult.checkLess(F.Mod(result, arg2Mod));
1274+
sResult.checkLessPlusTimesPower(F.Mod(result, arg2Mod));
12761275
}
12771276
} else if (expr.isTimes()) {
12781277
try {
@@ -1303,7 +1302,7 @@ private void functionExpand(IExpr expr, SimplifiedResult sResult) { // long minC
13031302
if (TIMES_ORDERLESS_MATCHER != null) {
13041303
IAST temp = TIMES_ORDERLESS_MATCHER.evaluateRepeatedNoCache((IAST) expr, fEngine);
13051304
if (temp.isPresent()) {
1306-
sResult.checkLess(temp);
1305+
sResult.checkLessPlusTimesPower(temp);
13071306
}
13081307
}
13091308
} catch (RuntimeException rex) {

0 commit comments

Comments
 (0)