Skip to content

Commit f749140

Browse files
committed
Improve MeanDeviation,StandardDeviation
1 parent 6fe9f0b commit f749140

File tree

3 files changed

+55
-33
lines changed

3 files changed

+55
-33
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3249,7 +3249,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
32493249
// an empty IAST cannot be folded
32503250
return F.NIL;
32513251
}
3252-
return F.Fold(ast.arg1(), F.First(arg2), F.Rest(arg2));
3252+
return F.Fold(ast.arg1(), arg2.first(), arg2.rest());
32533253
}
32543254
IExpr temp = engine.evaluate(ast.arg3());
32553255
if (temp.isAST()) {

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

+36-32
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
import org.matheclipse.core.interfaces.IStringX;
5555
import org.matheclipse.core.interfaces.ISymbol;
5656
import org.matheclipse.core.reflection.system.NSum;
57+
import it.unimi.dsi.fastutil.ints.IntArrayList;
58+
import it.unimi.dsi.fastutil.ints.IntList;
5759

5860
public class StatisticsFunctions {
5961

@@ -4935,42 +4937,40 @@ private static final class MeanDeviation extends AbstractFunctionEvaluator {
49354937
@Override
49364938
public IExpr evaluate(final IAST ast, EvalEngine engine) {
49374939
IExpr arg1 = ast.arg1();
4938-
int[] dim = arg1.isMatrix();
4939-
if (dim == null && arg1.isListOfLists()) {
4940-
return F.NIL;
4941-
}
4942-
if (dim != null) {
4943-
return arg1.mapMatrixColumns(dim, x -> F.MeanDeviation(x));
4944-
}
4945-
4946-
int length = arg1.isVector();
4947-
if (length > 0) {
49484940

4949-
if (arg1.isRealVector()) {
4950-
double[] values = arg1.toDoubleVector();
4951-
if (values == null) {
4941+
final IntList dimensions = LinearAlgebra.dimensions(arg1, S.List, Integer.MAX_VALUE, true);
4942+
if (dimensions.size() != 0) {
4943+
switch (dimensions.size()) {
4944+
case 1:
4945+
int length = dimensions.getInt(0);
4946+
if (arg1.isRealVector()) {
4947+
double[] values = arg1.toDoubleVector();
4948+
if (values == null) {
4949+
return F.NIL;
4950+
}
4951+
double mean = StatUtils.mean(values);
4952+
double[] newValues = new double[length];
4953+
for (int i = 0; i < length; i++) {
4954+
newValues[i] = Math.abs(values[i] - mean);
4955+
}
4956+
return F.num(StatUtils.mean(newValues));
4957+
}
4958+
arg1 = arg1.normal(false);
4959+
if (arg1.isList()) {
4960+
IAST vector = (IAST) arg1;
4961+
int size = vector.size();
4962+
IASTAppendable sum = F.PlusAlloc(size);
4963+
final IExpr mean = S.Mean.of(engine, F.Negate(vector));
4964+
vector.forEach(x -> sum.append(F.Abs(F.Plus(x, mean))));
4965+
return F.Times(F.Power(F.ZZ(size - 1), -1), sum);
4966+
}
49524967
return F.NIL;
4953-
}
4954-
double mean = StatUtils.mean(values);
4955-
double[] newValues = new double[length];
4956-
for (int i = 0; i < length; i++) {
4957-
newValues[i] = Math.abs(values[i] - mean);
4958-
}
4959-
return F.num(StatUtils.mean(newValues));
4968+
case 2:
4969+
return arg1.mapMatrixColumns(dimensions.toIntArray(), x -> F.MeanDeviation(x));
4970+
default:
4971+
return F.ArrayReduce(S.MeanDeviation, arg1, F.C1);
49604972
}
4961-
4962-
arg1 = arg1.normal(false);
4963-
if (arg1.isList()) {
4964-
IAST vector = (IAST) arg1;
4965-
int size = vector.size();
4966-
IASTAppendable sum = F.PlusAlloc(size);
4967-
final IExpr mean = S.Mean.of(engine, F.Negate(vector));
4968-
vector.forEach(x -> sum.append(F.Abs(F.Plus(x, mean))));
4969-
return F.Times(F.Power(F.ZZ(size - 1), -1), sum);
4970-
}
4971-
return F.NIL;
49724973
}
4973-
49744974
if (arg1.isNumber()) {
49754975
// Rectangular array expected at position `1` in `2`.
49764976
return Errors.printMessage(ast.topHead(), "rectt", F.list(F.C1, ast), engine);
@@ -7271,6 +7271,10 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
72717271
}
72727272
return standardDeviation(arg1);
72737273
}
7274+
IntArrayList dimensions = LinearAlgebra.dimensions(list);
7275+
if (dimensions.size() > 2) {
7276+
return F.ArrayReduce(S.StandardDeviation, list, F.C1);
7277+
}
72747278
// The argument `1` should have at least `2` elements.
72757279
return Errors.printMessage(S.StandardDeviation, "shlen", F.List(list, F.C2));
72767280
}

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

+18
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public int[] expectedArgSize(IAST ast) {
9797
}
9898
}
9999

100+
100101
/**
101102
*
102103
*
@@ -217,6 +218,7 @@ public int[] expectedArgSize(IAST ast) {
217218
}
218219
}
219220

221+
220222
private static class HodgeDual extends AbstractEvaluator {
221223

222224
@Override
@@ -252,6 +254,7 @@ public int[] expectedArgSize(IAST ast) {
252254
}
253255
}
254256

257+
255258
private static class KroneckerProduct extends TensorProduct {
256259

257260
@Override
@@ -323,6 +326,7 @@ public int[] expectedArgSize(IAST ast) {
323326

324327
}
325328

329+
326330
private static class LeviCivitaTensor extends AbstractEvaluator {
327331

328332
@Override
@@ -370,6 +374,7 @@ public int[] expectedArgSize(IAST ast) {
370374
}
371375
}
372376

377+
373378
/**
374379
*
375380
*
@@ -455,6 +460,7 @@ public int[] expectedArgSize(IAST ast) {
455460
}
456461
}
457462

463+
458464
/**
459465
*
460466
*
@@ -565,6 +571,7 @@ public int[] expectedArgSize(IAST ast) {
565571

566572
}
567573

574+
568575
/**
569576
*
570577
*
@@ -709,6 +716,7 @@ public int[] expectedArgSize(IAST ast) {
709716
public void setUp(final ISymbol newSymbol) {}
710717
}
711718

719+
712720
private static class TensorDimensions extends AbstractEvaluator {
713721

714722
@Override
@@ -801,6 +809,7 @@ public void setUp(final ISymbol newSymbol) {
801809
}
802810
}
803811

812+
804813
private static class TensorSymmetry extends AbstractEvaluator {
805814

806815
@Override
@@ -974,6 +983,7 @@ public void setUp(final ISymbol newSymbol) {
974983
}
975984
}
976985

986+
977987
private static class TensorProduct extends AbstractEvaluator {
978988

979989
@Override
@@ -1055,6 +1065,7 @@ public void setUp(final ISymbol newSymbol) {
10551065
}
10561066
}
10571067

1068+
10581069
private static class TensorRank extends AbstractEvaluator {
10591070

10601071
@Override
@@ -1122,6 +1133,7 @@ public void setUp(final ISymbol newSymbol) {
11221133
}
11231134
}
11241135

1136+
11251137
private static class RotationTransform extends AbstractFunctionEvaluator {
11261138

11271139
@Override
@@ -1145,6 +1157,7 @@ public int[] expectedArgSize(IAST ast) {
11451157
}
11461158
}
11471159

1160+
11481161
private static class ScalingTransform extends AbstractFunctionEvaluator {
11491162

11501163
@Override
@@ -1195,6 +1208,8 @@ public int[] expectedArgSize(IAST ast) {
11951208
return ARGS_1_2;
11961209
}
11971210
}
1211+
1212+
11981213
private static class ShearingTransform extends AbstractFunctionEvaluator {
11991214

12001215
@Override
@@ -1228,6 +1243,7 @@ public int[] expectedArgSize(IAST ast) {
12281243
}
12291244
}
12301245

1246+
12311247
private static class TransformationFunction extends AbstractFunctionEvaluator {
12321248

12331249
@Override
@@ -1255,6 +1271,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
12551271

12561272
}
12571273

1274+
12581275
private static class TranslationTransform extends AbstractFunctionEvaluator {
12591276

12601277
@Override
@@ -1284,6 +1301,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
12841301
public int[] expectedArgSize(IAST ast) {
12851302
return ARGS_1_1;
12861303
}
1304+
12871305
}
12881306

12891307
private static Map<IExpr, IAST> tensorProperties(IAssumptions oldAssumptions,

0 commit comments

Comments
 (0)