Skip to content

Commit 0a17da2

Browse files
committed
Misc clean up; JUnit tests
1 parent e423a33 commit 0a17da2

File tree

24 files changed

+571
-218
lines changed

24 files changed

+571
-218
lines changed

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

+42-19
Original file line numberDiff line numberDiff line change
@@ -533,16 +533,15 @@ public void setUp(final ISymbol newSymbol) {
533533
* f(-3)
534534
* </pre>
535535
*/
536-
private static final class Condition extends AbstractCoreFunctionEvaluator {
536+
private static final class Condition extends AbstractCoreFunctionEvaluator
537+
implements IFastFunctionEvaluator {
537538

538539
@Override
539540
public final IExpr evaluate(final IAST ast, EvalEngine engine) {
540541
if (engine.isEvalRHSMode()) {
542+
IExpr arg1 = ast.arg1();
541543
IExpr arg2 = ast.arg2();
542-
if (engine.evalTrue(arg2)) {
543-
return ast.arg1();
544-
}
545-
throw ConditionException.CONDITION_NIL;
544+
return condition(arg1, arg2, engine);
546545
}
547546
return F.NIL;
548547
}
@@ -763,7 +762,8 @@ public void setUp(ISymbol newSymbol) {
763762
* | hi
764763
* </pre>
765764
*/
766-
private static final class Do extends AbstractCoreFunctionEvaluator {
765+
private static final class Do extends AbstractCoreFunctionEvaluator
766+
implements IFastFunctionEvaluator {
767767

768768
@Override
769769
public IExpr evaluate(final IAST ast, EvalEngine engine) {
@@ -1141,7 +1141,8 @@ public void setUp(final ISymbol newSymbol) {
11411141
* 120
11421142
* </pre>
11431143
*/
1144-
private static final class For extends AbstractCoreFunctionEvaluator {
1144+
private static final class For extends AbstractCoreFunctionEvaluator
1145+
implements IFastFunctionEvaluator {
11451146

11461147
@Override
11471148
public IExpr evaluate(final IAST ast, EvalEngine engine) {
@@ -1262,26 +1263,27 @@ public void setUp(final ISymbol newSymbol) {
12621263
* &gt;&gt; If(a, (*then*) b, (*else*) c);
12631264
* </pre>
12641265
*/
1265-
private static final class If extends AbstractCoreFunctionEvaluator {
1266+
private static final class If extends AbstractCoreFunctionEvaluator
1267+
implements IFastFunctionEvaluator {
12661268

12671269
@Override
12681270
public IExpr evaluate(final IAST ast, EvalEngine engine) {
12691271
final IExpr temp = engine.evaluate(ast.arg1());
12701272

12711273
if (temp.isFalse()) {
12721274
if (ast.size() >= 4) {
1273-
return ast.arg3();
1275+
return engine.evaluate(ast.arg3());
12741276
}
12751277

12761278
return S.Null;
12771279
}
12781280

1279-
if (temp.equals(S.True)) {
1280-
return ast.arg2();
1281+
if (temp.isTrue()) {
1282+
return engine.evaluate(ast.arg2());
12811283
}
12821284

12831285
if (ast.size() == 5) {
1284-
return ast.arg4();
1286+
return engine.evaluate(ast.arg4());
12851287
}
12861288

12871289
return F.NIL;
@@ -1525,7 +1527,8 @@ public int status() {
15251527
*
15261528
* </blockquote>
15271529
*/
1528-
private static final class Module extends AbstractCoreFunctionEvaluator {
1530+
private static final class Module extends AbstractCoreFunctionEvaluator
1531+
implements IFastFunctionEvaluator {
15291532
/** */
15301533
@Override
15311534
public IExpr evaluate(final IAST ast, EvalEngine engine) {
@@ -3064,7 +3067,8 @@ public void setUp(final ISymbol newSymbol) {
30643067
* Switch(b, b)
30653068
* </pre>
30663069
*/
3067-
private static final class Switch extends AbstractCoreFunctionEvaluator {
3070+
private static final class Switch extends AbstractCoreFunctionEvaluator
3071+
implements IFastFunctionEvaluator {
30683072

30693073
@Override
30703074
public IExpr evaluate(final IAST ast, EvalEngine engine) {
@@ -3543,7 +3547,8 @@ public void setUp(ISymbol newSymbol) {
35433547
* Which(a, b, c)
35443548
* </pre>
35453549
*/
3546-
private static final class Which extends AbstractCoreFunctionEvaluator {
3550+
private static final class Which extends AbstractCoreFunctionEvaluator
3551+
implements IFastFunctionEvaluator {
35473552

35483553
@Override
35493554
public IExpr evaluate(final IAST ast, EvalEngine engine) {
@@ -3617,7 +3622,8 @@ public void setUp(final ISymbol newSymbol) {
36173622
* 12
36183623
* </pre>
36193624
*/
3620-
private static final class While extends AbstractCoreFunctionEvaluator {
3625+
private static final class While extends AbstractCoreFunctionEvaluator
3626+
implements IFastFunctionEvaluator {
36213627

36223628
@Override
36233629
public IExpr evaluate(final IAST ast, EvalEngine engine) {
@@ -3668,7 +3674,8 @@ public void setUp(final ISymbol newSymbol) {
36683674
*
36693675
* </blockquote>
36703676
*/
3671-
private static final class With extends AbstractCoreFunctionEvaluator {
3677+
private static final class With extends AbstractCoreFunctionEvaluator
3678+
implements IFastFunctionEvaluator {
36723679
@Override
36733680
public IExpr evaluate(final IAST ast, EvalEngine engine) {
36743681
final IAST moduleVariablesList = Validate.checkLocalVariableList(ast, 1, engine);
@@ -3804,8 +3811,7 @@ public static boolean rememberModuleVariables(IAST variablesList, final String v
38043811
} else {
38053812
// Local variable specification `1` contains `2` which is not a symbol or an assignment to
38063813
// a symbol.
3807-
Errors.printMessage(S.Module, "lvsym", F.List(variablesList, varExpr),
3808-
engine);
3814+
Errors.printMessage(S.Module, "lvsym", F.List(variablesList, varExpr), engine);
38093815
return false;
38103816
}
38113817
}
@@ -4465,6 +4471,23 @@ private static IExpr assignPartValue(IAST lhs, int partPosition, IExpr value) {
44654471
return lhs.setAtCopy(partPosition, value);
44664472
}
44674473

4474+
/**
4475+
* If the second argument is true, evaluate the first argument and return the result. Otherwise
4476+
* throw a condition {@link ConditionException#CONDITION_NIL}.
4477+
*
4478+
* @param arg1
4479+
* @param arg2
4480+
* @param engine
4481+
* @throws ConditionException
4482+
*/
4483+
public static IExpr condition(IExpr arg1, IExpr arg2, EvalEngine engine)
4484+
throws ConditionException {
4485+
if (engine.evalTrue(arg2)) {
4486+
return engine.evaluate(arg1);
4487+
}
4488+
throw ConditionException.CONDITION_NIL;
4489+
}
4490+
44684491
/**
44694492
* Call <code>assignPart(element, ast, pos, value, engine)</code> recursively and assign the
44704493
* result to the given position in the result. <code>result[[position]] = resultValue</code>

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hipparchus.random.RandomDataGenerator;
1414
import org.hipparchus.stat.StatUtils;
1515
import org.hipparchus.stat.correlation.PearsonsCorrelation;
16+
import org.hipparchus.stat.descriptive.DescriptiveStatistics;
1617
import org.hipparchus.stat.descriptive.StreamingStatistics;
1718
import org.hipparchus.stat.projection.PCA;
1819
import org.hipparchus.util.MathUtils;
@@ -4564,9 +4565,10 @@ private static final class Kurtosis extends AbstractEvaluator {
45644565

45654566
@Override
45664567
public IExpr evaluate(final IAST ast, EvalEngine engine) {
4567-
if (ast.arg1().isAST()) {
4568-
if (ast.arg1().isList()) {
4569-
IAST list = (IAST) ast.arg1();
4568+
IExpr arg1 = ast.arg1();
4569+
if (arg1.isAST()) {
4570+
if (arg1.isList()) {
4571+
IAST list = (IAST) arg1;
45704572
if (list.argSize() < 2) {
45714573
// The argument `1` should have at least `2` elements.
45724574
return Errors.printMessage(ast.topHead(), "shlen", F.List(list, F.C2), engine);
@@ -4578,7 +4580,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
45784580
return F.Divide(F.CentralMoment(list, 4), F.Power(centralMoment, F.C2));
45794581
}
45804582

4581-
IAST dist = (IAST) ast.arg1();
4583+
IAST dist = (IAST) arg1;
45824584
if (dist.head().isSymbol()) {
45834585
ISymbol head = (ISymbol) dist.head();
45844586
if (dist.head().isSymbol()) {
@@ -7129,6 +7131,12 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
71297131
}
71307132

71317133
private static IExpr rootMeanSquareVector(IAST list) {
7134+
if (list.isRealVector()) {
7135+
double[] doubleVector = list.toDoubleVector();
7136+
DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics(doubleVector);
7137+
double quadraticMean = descriptiveStatistics.getQuadraticMean();
7138+
return F.num(quadraticMean);
7139+
}
71327140
IExpr sum = F.sum(i -> list.get(i).times(list.get(i)), 1, list.argSize());
71337141
return F.Times(F.Power(sum, F.C1D2), F.Power(F.ZZ(list.argSize()), F.CN1D2));
71347142
}

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

-18
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,6 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
107107
F.Times(F.CN1, F.Power(x, F.CN1), F.Plus(F.Negate(g), F.D(f, y))), F.D(g, x)));
108108

109109
}
110-
if (dimensions.size() == 2) {
111-
if (numberOfVars != 2 || dimensions.getInt(0) != 2 || dimensions.getInt(1) != 2) {
112-
// `1` does not define a metric in `2` dimensions.
113-
return Errors.printMessage(S.Curl, "bdmtrc",
114-
F.List(F.stringx("Polar"), F.ZZ(numberOfVars)), engine);
115-
}
116-
// 2x2 matrix
117-
IExpr f = list.getPart(1, 1);
118-
IExpr g = list.getPart(1, 2);
119-
IExpr h = list.getPart(2, 1);
120-
IExpr i = list.getPart(2, 2);
121-
122-
IExpr x = variables.arg1();
123-
IExpr y = variables.arg2();
124-
125-
}
126-
127-
128110
return F.NIL;
129111
}
130112

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/eval/ExprEvaluator.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.concurrent.ExecutorService;
88
import java.util.concurrent.Executors;
99
import java.util.concurrent.TimeUnit;
10-
import javax.annotation.concurrent.NotThreadSafe;
1110
import org.apfloat.ApfloatInterruptedException;
1211
import org.hipparchus.complex.Complex;
1312
import org.matheclipse.core.eval.exception.AbortException;
@@ -133,7 +132,7 @@
133132
* assertEquals(-0.5063656411097588, result, 0E-10);
134133
* </pre>
135134
*/
136-
@NotThreadSafe
135+
// @NotThreadSafe
137136
public class ExprEvaluator {
138137

139138
static {

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/eval/MathUtils.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ public static double integrate(String method, String fun, String v, String aS, S
9090
IExpr function = parse(fun, null);
9191
IExpr var = parse(v, null);
9292
IAST list = F.list(var, F.num(a), F.num(b));
93-
return NIntegrate.integrate("LegendreGauss", list, a, b, function,
94-
NIntegrate.DEFAULT_MAX_POINTS, NIntegrate.DEFAULT_MAX_ITERATIONS);
93+
return NIntegrate.integrateDouble(function, var, a, b, "LegendreGauss",
94+
NIntegrate.DEFAULT_MAX_POINTS, NIntegrate.DEFAULT_MAX_ITERATIONS, list.rest(),
95+
parser.getEvalEngine());
9596
}
9697

9798
/**

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/ASTSeriesData.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,7 @@ public String fullFormString() {
353353
public IAST toSeriesData() {
354354
// list of coefficients
355355
IASTAppendable coefficientList = F.mapRange(nMin, nMax, i -> coefficient(i));
356-
IAST seriesData =
357-
F.SeriesData(x, x0, coefficientList, F.ZZ(nMin), F.ZZ(truncate), F.ZZ(denominator));
358-
return seriesData;
356+
return F.SeriesData(x, x0, coefficientList, F.ZZ(nMin), F.ZZ(truncate), F.ZZ(denominator));
359357
}
360358

361359
@Override

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/AbstractAST.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -4613,20 +4613,28 @@ public boolean isRealResult() {
46134613
@Override
46144614
public boolean isRealVector() {
46154615
if (isList()) {
4616-
boolean containsNum = false;
4617-
for (int i = 1; i < size(); i++) {
4616+
final int size = size();
4617+
int i = 1;
4618+
while (i < size) {
46184619
if (get(i).isReal()) {
46194620
if (get(i) instanceof INum) {
46204621
if (!(get(i) instanceof Num)) {
46214622
return false;
46224623
}
4623-
containsNum = true;
4624+
i++;
4625+
while (i < size) {
4626+
if (!get(i).isReal()) {
4627+
return false;
4628+
}
4629+
i++;
4630+
}
4631+
return true;
46244632
}
4633+
i++;
46254634
} else {
46264635
return false;
46274636
}
46284637
}
4629-
return containsNum;
46304638
}
46314639
return false;
46324640
}

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/B2.java

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.function.ObjIntConsumer;
1515
import java.util.function.Predicate;
1616
import org.matheclipse.core.builtin.PredicateQ;
17+
import org.matheclipse.core.builtin.Programming;
1718
import org.matheclipse.core.eval.EvalEngine;
1819
import org.matheclipse.core.generic.ObjIntPredicate;
1920
import org.matheclipse.core.interfaces.IAST;

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/F.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -967,13 +967,13 @@ public static final void await() throws InterruptedException {
967967
n_DEFAULT, o_DEFAULT, p_DEFAULT, q_DEFAULT, r_DEFAULT, s_DEFAULT, t_DEFAULT, u_DEFAULT,
968968
v_DEFAULT, w_DEFAULT, x_DEFAULT, y_DEFAULT, z_DEFAULT, A_DEFAULT, B_DEFAULT, C_DEFAULT,
969969
F_DEFAULT, G_DEFAULT};
970-
short exprID = EXPRID_MAX_BUILTIN_LENGTH;
971970
for (short i = 0; i < S.BUILT_IN_SYMBOLS.length; i++) {
972971
IBuiltInSymbol symbol = S.BUILT_IN_SYMBOLS[i];
973972
if (symbol != null) {
974973
GLOBAL_IDS_MAP.put(symbol, (short) symbol.ordinal());
975974
}
976975
}
976+
short exprID = EXPRID_MAX_BUILTIN_LENGTH;
977977
for (short i = 0; i < COMMON_IDS.length; i++) {
978978
GLOBAL_IDS_MAP.put(COMMON_IDS[i], exprID++);
979979
}

0 commit comments

Comments
 (0)