Skip to content

Commit

Permalink
Refactor: use Optional instead of returning null
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Nov 28, 2023
1 parent 859cb53 commit 34a9d8c
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 335 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static org.matheclipse.core.expression.S.Times;
import static org.matheclipse.core.expression.S.x;
import static org.matheclipse.core.expression.S.y;
import java.util.Optional;
import java.util.function.DoubleFunction;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
Expand Down Expand Up @@ -1438,16 +1439,17 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
}

if (arg1.isTimes() || arg1.isPower()) {
IExpr[] parts = Algebra.fractionalPartsTimesPower((IAST) arg1, true, false, false,
false, false, false);
if (parts != null && !parts[1].isOne()
&& !(parts[1].isAST(S.Sign) || parts[0].isAST(S.Sign))) {
final IExpr numerator = parts[0];
final IExpr denominator = parts[1];
IExpr tmp = engine.evaluate(F.Times(F.Sign(numerator), //
F.Power(F.Sign(denominator), F.CN1)));
if (!tmp.equals(arg1)) {
return F.DirectedInfinity(tmp);
Optional<IExpr[]> parts = Algebra.fractionalPartsTimesPower((IAST) arg1, true, false,
false, false, false, false);
if (parts.isPresent()) {
final IExpr numerator = parts.get()[0];
final IExpr denominator = parts.get()[1];
if (!denominator.isOne() && !(denominator.isAST(S.Sign) || numerator.isAST(S.Sign))) {
IExpr tmp = engine.evaluate(F.Times(F.Sign(numerator), //
F.Power(F.Sign(denominator), F.CN1)));
if (!tmp.equals(arg1)) {
return F.DirectedInfinity(tmp);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -396,10 +397,10 @@ public IExpr visit(IASTMutable ast) {
return F.NIL;
}
if (ast.isTimes()) {
IExpr[] parts = Algebra.fractionalParts(ast, false);
if (parts != null) {
IExpr numerator = parts[0];
IExpr denominator = parts[1];
Optional<IExpr[]> parts = Algebra.fractionalParts(ast, false);
if (parts.isPresent()) {
IExpr numerator = parts.get()[0];
IExpr denominator = parts.get()[1];
if (!denominator.isFree(variable)) {
numerator.accept(this);
return roots(denominator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.matheclipse.core.basic.Config;
Expand Down Expand Up @@ -828,12 +829,12 @@ private static IExpr substituteInfinity(final IAST arg1, LimitData data, EvalEng
IExpr y = F.Power(x, F.CN1); // substituting by 1/x
IExpr temp = F.evalQuiet(F.subst(arg1, x, y));
if (temp.isTimes()) {
IExpr[] parts =
Optional<IExpr[]> parts =
Algebra.fractionalPartsTimesPower((IAST) temp, false, false, true, true, true, true);
if (parts != null) {
if (!parts[1].isOne()) { // denominator != 1
if (parts.isPresent()) {
if (!parts.get()[1].isOne()) { // denominator != 1
LimitData ndData = new LimitData(x, F.C0, F.Rule(x, F.C0), data.direction());
temp = numeratorDenominatorLimit(parts[0], parts[1], ndData, engine);
temp = numeratorDenominatorLimit(parts.get()[0], parts.get()[1], ndData, engine);
if (temp.isPresent()) {
return temp;
}
Expand All @@ -849,9 +850,9 @@ private static IExpr timesLimit(final IAST timesAST, LimitData data, EvalEngine
if (!isFreeResult.arg1().isOne()) {
return F.Times(isFreeResult.arg1(), data.limit(isFreeResult.arg2()));
}
IExpr[] parts =
Optional<IExpr[]> parts =
Algebra.fractionalPartsTimesPower(timesAST, false, false, true, true, true, true);
if (parts == null) {
if (parts.isEmpty()) {
IAST[] timesPolyFiltered = timesAST.filter(x -> x.isPolynomial(data.variable));
if (timesPolyFiltered[0].size() > 1 && timesPolyFiltered[1].size() > 1) {
IExpr first = engine.evaluate(data.limit(timesPolyFiltered[0].oneIdentity1()));
Expand Down Expand Up @@ -886,8 +887,8 @@ private static IExpr timesLimit(final IAST timesAST, LimitData data, EvalEngine
}
} else {

IExpr numerator = parts[0];
IExpr denominator = parts[1];
IExpr numerator = parts.get()[0];
IExpr denominator = parts.get()[1];
IExpr limit = data.limitValue();
ISymbol symbol = data.variable();
if (limit.isInfinity() || limit.isNegativeInfinity()) {
Expand All @@ -901,7 +902,7 @@ private static IExpr timesLimit(final IAST timesAST, LimitData data, EvalEngine
}
}

IExpr plusResult = Algebra.partsApart(parts, symbol, engine);
IExpr plusResult = Algebra.partsApart(parts.get(), symbol, engine);
// Algebra.partialFractionDecompositionRational(new PartialFractionGenerator(),
// parts,symbol);
if (plusResult.isPlus()) {
Expand Down Expand Up @@ -1095,9 +1096,10 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
return F.NIL;
}
if (function.isTimes()) {
IExpr[] numeratorDenominatorParts = Algebra.fractionalParts(function, false);
if (numeratorDenominatorParts != null) {
return quotientTaylorFunction(numeratorDenominatorParts, x, x0, m, n);
Optional<IExpr[]> numeratorDenominatorParts =
Algebra.fractionalParts(function, false);
if (numeratorDenominatorParts.isPresent()) {
return quotientTaylorFunction(numeratorDenominatorParts.get(), x, x0, m, n);
}
}

Expand Down Expand Up @@ -1693,10 +1695,10 @@ public static ASTSeriesData seriesDataRecursive(final IExpr function, IExpr x, I
return temp;
}
} else if (function.isTimes()) {
IExpr[] numeratorDenominatorParts = Algebra.fractionalParts(function, false);
if (numeratorDenominatorParts != null) {
Optional<IExpr[]> numeratorDenominatorParts = Algebra.fractionalParts(function, false);
if (numeratorDenominatorParts.isPresent()) {
ASTSeriesData sd =
Algebra.polynomialTaylorSeries(numeratorDenominatorParts, x, x0, n, denominator);
Algebra.polynomialTaylorSeries(numeratorDenominatorParts.get(), x, x0, n, denominator);
if (sd != null) {
return sd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apfloat.Apcomplex;
Expand Down Expand Up @@ -961,13 +962,14 @@ public boolean convert(final StringBuilder buf, final IAST f, final int preceden
*/
public boolean convertTimesFraction(final StringBuilder buf, final IAST f, final int precedence,
final boolean caller) {
IExpr[] parts = Algebra.fractionalPartsTimesPower(f, false, true, false, false, false, false);
if (parts == null) {
Optional<IExpr[]> parts =
Algebra.fractionalPartsTimesPower(f, false, true, false, false, false, false);
if (parts.isEmpty()) {
convertTimesOperator(buf, f, precedence, false, caller);
return true;
}
final IExpr numerator = parts[0];
final IExpr denominator = parts[1];
final IExpr numerator = parts.get()[0];
final IExpr denominator = parts.get()[1];
precedenceOpen(buf, precedence);
if (!denominator.isOne()) {
boolean arg1Negate = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.matheclipse.core.form.output;

import java.io.PrintWriter;
import java.util.Optional;
import org.matheclipse.core.builtin.Algebra;
import org.matheclipse.core.expression.S;
import org.matheclipse.core.interfaces.IAST;
Expand Down Expand Up @@ -207,14 +208,15 @@ private void convertTimes(IAST times, final int precedence, boolean caller) {
* @param ast
*/
private void convertTimesPowerFraction(IAST ast, final int precedence) {
IExpr[] parts = Algebra.fractionalPartsTimesPower(ast, true, false, false, false, false, false);
Optional<IExpr[]> parts =
Algebra.fractionalPartsTimesPower(ast, true, false, false, false, false, false);
// IExpr[] parts = Algebra.getFractionalParts(ast, false);
if (parts == null) {
if (parts.isEmpty()) {
print(ast.toString());
return;
}
final IExpr numerator = parts[0];
final IExpr denominator = parts[1];
final IExpr numerator = parts.get()[0];
final IExpr denominator = parts.get()[1];
if (!denominator.isOne()) {
printFraction(numerator.toString(), denominator.toString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.util.Optional;
import org.apfloat.Apcomplex;
import org.apfloat.Apfloat;
import org.hipparchus.linear.RealMatrix;
Expand Down Expand Up @@ -577,20 +578,20 @@ private void convertPlusOperatorReversed(final StringBuilder buf, final IAST plu

private void convertTimesFraction(final StringBuilder buf, final IAST timesAST,
final InfixOperator oper, final int precedence, boolean caller) {
IExpr[] parts =
Optional<IExpr[]> parts =
Algebra.fractionalPartsTimesPower(timesAST, true, false, false, false, false, false);
if (parts == null) {
if (parts.isEmpty()) {
convertTimesOperator(buf, timesAST, oper, precedence, caller);
return;
}
final IExpr numerator = parts[0];
final IExpr denominator = parts[1];
final IExpr numerator = parts.get()[0];
final IExpr denominator = parts.get()[1];
if (!denominator.isOne()) {
int currPrecedence = oper.getPrecedence();
if (currPrecedence < precedence) {
append(buf, "(");
}
final IExpr fraction = parts[2];
final IExpr fraction = parts.get()[2];
if (fraction != null) {
convertNumber(buf, (IReal) fraction, Precedence.PLUS, caller);
append(buf, "*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apfloat.Apcomplex;
Expand Down Expand Up @@ -904,20 +905,20 @@ public void convertPlusArgument(final Appendable buf, IExpr plusArg, boolean cal

private void convertTimesFraction(final Appendable buf, final IAST timesAST,
final InfixOperator oper, final int precedence, boolean caller) throws IOException {
IExpr[] parts =
Optional<IExpr[]> parts =
Algebra.fractionalPartsTimesPower(timesAST, true, false, false, false, false, false);
if (parts == null) {
if (parts.isEmpty()) {
convertTimesOperator(buf, timesAST, oper, precedence, caller);
return;
}
final IExpr numerator = parts[0];
final IExpr denominator = parts[1];
final IExpr numerator = parts.get()[0];
final IExpr denominator = parts.get()[1];
if (!denominator.isOne()) {
int currPrecedence = oper.getPrecedence();
if (currPrecedence < precedence) {
append(buf, "(");
}
final IExpr fraction = parts[2];
final IExpr fraction = parts.get()[2];
if (fraction != null) {
convertNumber(buf, (IReal) fraction, Precedence.PLUS, caller);
append(buf, "*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apfloat.Apcomplex;
Expand Down Expand Up @@ -1113,21 +1114,21 @@ public boolean convert(final StringBuilder buffer, final IAST f, final int prece

private void convertTimesFraction(final StringBuilder buf, final IAST timesAST,
final InfixOperator oper, final int precedence, boolean caller) {
IExpr[] parts =
Optional<IExpr[]> parts =
Algebra.fractionalPartsTimesPower(timesAST, true, false, false, false, false, false);
if (parts == null) {
if (parts.isEmpty()) {
convertTimesOperator(buf, timesAST, oper, precedence, caller);
return;
}
IExpr numerator = parts[0];
final IExpr denominator = parts[1];
IExpr numerator = parts.get()[0];
final IExpr denominator = parts.get()[1];
if (!denominator.isOne()) {
boolean isNegative = numerator.isNegativeSigned();
if (isNegative) {
numerator = numerator.negate();
}
precedenceOpen(buf, precedence);
final IExpr fraction = parts[2];
final IExpr fraction = parts.get()[2];
if (fraction != null) {
fFactory.convertNumber(buf, fraction, Precedence.PLUS, caller);
buf.append(fFactory.symbolOptions.getTimesSymbol());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.matheclipse.core.expression.F.Times;
import static org.matheclipse.core.expression.S.Integrate;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -675,9 +676,9 @@ private static IExpr callRestIntegrate(IAST arg1, final IExpr x, final EvalEngin

if (arg1AST.size() >= 3 && arg1AST.isFree(S.Integrate) && arg1AST.isPlusTimesPower()) {
if (!arg1AST.isEvalFlagOn(IAST.IS_DECOMPOSED_PARTIAL_FRACTION) && x.isSymbol()) {
IExpr[] parts = Algebra.fractionalParts(arg1, true);
if (parts != null) {
IExpr temp = Algebra.partsApart(parts, x, engine);
Optional<IExpr[]> parts = Algebra.fractionalParts(arg1, true);
if (parts.isPresent()) {
IExpr temp = Algebra.partsApart(parts.get(), x, engine);
if (temp.isPresent() && !temp.equals(arg1)) {
if (temp.isPlus()) {
return mapIntegrate((IAST) temp, x);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.matheclipse.core.reflection.system;

import java.util.Optional;
import org.matheclipse.core.basic.Config;
import org.matheclipse.core.builtin.Algebra;
import org.matheclipse.core.eval.EvalEngine;
Expand Down Expand Up @@ -159,9 +160,9 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
}
}
if (arg1.isTimes() || arg1.isPower()) {
IExpr[] parts = Algebra.fractionalParts(arg1, false);
if (parts != null) {
IExpr temp = Algebra.partsApart(parts, s, engine);
Optional<IExpr[]> parts = Algebra.fractionalParts(arg1, false);
if (parts.isPresent()) {
IExpr temp = Algebra.partsApart(parts.get(), s, engine);
// IExpr temp = Algebra.partialFractionDecompositionRational(new
// PartialFractionGenerator(),
// parts,s);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.matheclipse.core.system;

import static org.junit.Assert.assertEquals;
import java.io.StringWriter;

import java.util.Optional;
import org.junit.Test;
import org.matheclipse.core.basic.Config;
import org.matheclipse.core.builtin.Algebra;
Expand All @@ -12,8 +13,6 @@
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.parser.client.ParserConfig;

import static org.junit.Assert.assertEquals;

/** Tests system.reflection classes */
public class MainTestCase extends ExprEvaluatorTestCase {

Expand Down Expand Up @@ -3343,9 +3342,9 @@ public void testSystem400() {
EvalEngine engine = EvalEngine.get();
IExpr exprNumerator = engine.parse("8+12*x+20*x^2+12*x^3+8*x^4+3*x^5");
IExpr exprDenominator = engine.parse("8*x+12*x^3+6*x^5+x^7");
IExpr[] result = Algebra.cancelGCD(exprNumerator, exprDenominator);
assertEquals(result[0].toString(), "1");
assertEquals(result[1].toString(), "4+6*x+8*x^2+3*x^3");
Optional<IExpr[]> result = Algebra.cancelGCD(exprNumerator, exprDenominator);
assertEquals(result.get()[0].toString(), "1");
assertEquals(result.get()[1].toString(), "4+6*x+8*x^2+3*x^3");
}

@Test
Expand Down

0 comments on commit 34a9d8c

Please sign in to comment.