Skip to content

Commit ac20e1a

Browse files
committed
Impr.ArrayReduce,Mean,Median,MedianFilter,Quantile,Quartiles,Variance
1 parent f749140 commit ac20e1a

File tree

11 files changed

+239
-136
lines changed

11 files changed

+239
-136
lines changed

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ protected IExpr filterHead() {
3333
return S.Min;
3434
}
3535

36+
protected boolean isValid(IExpr arg1) {
37+
return true;
38+
}
39+
3640
@Override
3741
public IExpr evaluate(final IAST ast, EvalEngine engine) {
3842
try {
@@ -44,9 +48,11 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
4448
return Errors.printMessage(ast.topHead(), "zznotimpl",
4549
F.List(F.stringx("\"with dimension other than 1\"")), engine);
4650
}
47-
final int radius = ast.arg2().toIntDefault();
48-
if (radius >= 0) {
49-
return filterHead(list, radius, filterHead(), engine);
51+
if (isValid(list)) {
52+
final int radius = ast.arg2().toIntDefault();
53+
if (radius >= 0) {
54+
return filterHead(list, radius, filterHead(), engine);
55+
}
5056
}
5157
}
5258
} catch (RuntimeException rex) {
@@ -94,6 +100,12 @@ protected IExpr filterHead() {
94100
}
95101

96102
private static class MedianFilter extends MinFilter {
103+
104+
@Override
105+
protected boolean isValid(IExpr arg1) {
106+
return arg1.forAllLeaves(x -> x.isRealResult());
107+
}
108+
97109
@Override
98110
protected IExpr filterHead() {
99111
return S.Median;

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

+98-84
Original file line numberDiff line numberDiff line change
@@ -5965,93 +5965,18 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
59655965
} else {
59665966
arg2 = F.NIL;
59675967
}
5968-
if (arg1.isList()) {
5969-
IAST tensor = (IAST) arg1;
5970-
// IntArrayList dimension = dimensions(tensor, tensor.head(), Integer.MAX_VALUE);
5971-
IAST dimensions = (IAST) F.Dimensions(tensor).eval(engine);
5972-
int length = dimensions.argSize();
5973-
if (length < 1) {
5974-
// Error messages inherits to S.ConjugateTranspose
5975-
// The first two levels of `1` cannot be transposed.
5976-
return Errors.printMessage(ast.topHead(), "nmtx", F.List(ast), engine);
5977-
}
5978-
final int[] permutation = getPermutation(arg2, length, ast, engine);
5979-
if (permutation == null) {
5980-
return F.NIL;
5981-
}
5982-
for (int i = 0; i < permutation.length; i++) {
5983-
if (permutation[i] > permutation.length) {
5984-
// Entry `1` in `2` is out of bounds for a permutation of length `3`.
5985-
return Errors.printMessage(ast.topHead(), "perm2",
5986-
F.List(F.ZZ(i + 1), arg2, F.ZZ(permutation.length)), engine);
5987-
}
5988-
}
5989-
5990-
5991-
if (dimensions.argSize() == 1) {
5992-
if (arg2.isPresent()) {
5993-
if (arg2.isList1() && arg2.first().isOne()) {
5994-
return tensor;
5995-
}
5996-
// Entry `1` in `2` is out of bounds for a permutation of length `3`.
5997-
return Errors.printMessage(ast.topHead(), "perm2", F.List(F.C2, arg2, F.C1), engine);
5998-
} else {
5999-
return tensor;
6000-
}
6001-
}
6002-
IASTMutable indices;
6003-
IAST range = ListFunctions.range(dimensions.size());
6004-
int[] dimensionsPermutated;
6005-
int[] originalIndices;
6006-
if (arg2.isPresent()) {
6007-
originalIndices = Combinatoric.permute(range, (IAST) arg2);
6008-
dimensionsPermutated = Combinatoric.permute(dimensions, (IAST) arg2);
6009-
} else {
6010-
// start with {2,1,3,...} as default permutation
6011-
indices = range.setAtCopy(1, range.arg2());
6012-
indices.set(2, range.arg1());
6013-
originalIndices = indices.toIntVector();
6014-
dimensionsPermutated = Combinatoric.permute(dimensions, indices);
6015-
}
6016-
if (dimensionsPermutated == null || originalIndices == null) {
6017-
return F.NIL;
6018-
}
6019-
return new TransposePermute(tensor, dimensions.toIntVector(), dimensionsPermutated,
6020-
permutation, originalIndices, x -> transform(x)).transposeRecursive();
6021-
5968+
final IntArrayList dimensions =
5969+
LinearAlgebra.dimensions(arg1, S.List, Integer.MAX_VALUE, false);
5970+
int length = dimensions.size();
5971+
if (length < 1) {
5972+
// Error messages inherits to S.ConjugateTranspose
5973+
// The first two levels of `1` cannot be transposed.
5974+
return Errors.printMessage(ast.topHead(), "nmtx", F.List(ast), engine);
60225975
}
5976+
return transpose(arg1, arg2, dimensions, x -> transform(x), ast, engine);
5977+
}
60235978

6024-
if (arg1.isSparseArray()) {
6025-
ISparseArray tensor = (ISparseArray) arg1;
6026-
int[] dimension = tensor.getDimension();
6027-
final int[] permutation;
6028-
int length = dimension.length;
6029-
if (length < 2) {
6030-
// Error messages inherits to ConjugateTranspose
6031-
// The first two levels of `1` cannot be transposed.
6032-
return Errors.printMessage(ast.topHead(), "nmtx", F.List(ast), engine);
6033-
}
6034-
permutation = getPermutation(arg2, length, ast, engine);
6035-
if (permutation == null) {
6036-
return F.NIL;
6037-
}
6038-
ISparseArray transposed = tensor.transpose(permutation, x -> transform(x));
6039-
if (transposed != null) {
6040-
return transposed;
6041-
}
6042-
// get error details:
6043-
for (int i = 0; i < permutation.length; i++) {
6044-
if (permutation[i] > permutation.length) {
6045-
// Entry `1` in `2` is out of bounds for a permutation of length `3`.
6046-
return Errors.printMessage(ast.topHead(), "perm2",
6047-
F.List(F.ZZ(i + 1), arg2, F.ZZ(permutation.length)), engine);
6048-
}
6049-
}
6050-
6051-
}
60525979

6053-
return F.NIL;
6054-
}
60555980

60565981
/**
60575982
*
@@ -6080,6 +6005,11 @@ private static int[] getPermutation(final IExpr permutationList, int length, fin
60806005
return null;
60816006
}
60826007

6008+
@Override
6009+
public int status() {
6010+
return ImplementationStatus.PARTIAL_SUPPORT;
6011+
}
6012+
60836013
@Override
60846014
public int[] expectedArgSize(IAST ast) {
60856015
return ARGS_1_2;
@@ -6215,6 +6145,11 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
62156145
return F.matrix((i, j) -> i <= j - k ? matrix.getEntry(i, j) : F.C0, m, n);
62166146
}
62176147

6148+
@Override
6149+
public int status() {
6150+
return ImplementationStatus.PARTIAL_SUPPORT;
6151+
}
6152+
62186153
@Override
62196154
public int[] expectedArgSize(IAST ast) {
62206155
return ARGS_1_2;
@@ -6341,6 +6276,11 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
63416276
return F.NIL;
63426277
}
63436278

6279+
@Override
6280+
public int status() {
6281+
return ImplementationStatus.PARTIAL_SUPPORT;
6282+
}
6283+
63446284
@Override
63456285
public int[] expectedArgSize(IAST ast) {
63466286
return ARGS_2_2;
@@ -7104,5 +7044,79 @@ public static IExpr toSphericalCoordinates(IExpr x, IExpr y, IExpr z) {
71047044
F.ArcTan(x, y));
71057045
}
71067046

7047+
public static IExpr transpose(final IExpr tensor, final IExpr permutation,
7048+
final IntList dimensions, Function<? super IExpr, ? extends IExpr> function,
7049+
final IAST ast, EvalEngine engine) {
7050+
final int length = dimensions.size();
7051+
final int[] permutationArray = Transpose.getPermutation(permutation, length, ast, engine);
7052+
if (permutationArray == null) {
7053+
return F.NIL;
7054+
}
7055+
if (tensor.isList()) {
7056+
IAST dimensionsList = F.List(dimensions.toIntArray());
7057+
IAST tensorList = (IAST) tensor;
7058+
for (int i = 0; i < permutationArray.length; i++) {
7059+
if (permutationArray[i] > permutationArray.length) {
7060+
// Entry `1` in `2` is out of bounds for a permutation of length `3`.
7061+
return Errors.printMessage(ast.topHead(), "perm2",
7062+
F.List(F.ZZ(i + 1), permutation, F.ZZ(permutationArray.length)), engine);
7063+
}
7064+
}
7065+
7066+
if (dimensions.size() == 1) {
7067+
if (permutation.isPresent()) {
7068+
if (permutation.isList1() && permutation.first().isOne()) {
7069+
return tensorList;
7070+
}
7071+
// Entry `1` in `2` is out of bounds for a permutation of length `3`.
7072+
return Errors.printMessage(ast.topHead(), "perm2", F.List(F.C2, permutation, F.C1),
7073+
engine);
7074+
} else {
7075+
return tensorList;
7076+
}
7077+
}
7078+
IASTMutable indices;
7079+
IAST range = ListFunctions.range(dimensions.size() + 1);
7080+
int[] dimensionsPermutated;
7081+
int[] originalIndices;
7082+
if (permutation.isPresent()) {
7083+
originalIndices = Combinatoric.permute(range, (IAST) permutation);
7084+
dimensionsPermutated = Combinatoric.permute(dimensionsList, (IAST) permutation);
7085+
} else {
7086+
// start with {2,1,3,...} as default permutation
7087+
indices = range.setAtCopy(1, range.arg2());
7088+
indices.set(2, range.arg1());
7089+
originalIndices = indices.toIntVector();
7090+
dimensionsPermutated = Combinatoric.permute(dimensionsList, indices);
7091+
}
7092+
if (dimensionsPermutated == null || originalIndices == null) {
7093+
return F.NIL;
7094+
}
7095+
return new Transpose.TransposePermute(tensorList, dimensions.toIntArray(),
7096+
dimensionsPermutated,
7097+
permutationArray, originalIndices, function).transposeRecursive();
7098+
7099+
}
7100+
7101+
if (tensor.isSparseArray()) {
7102+
ISparseArray sparseArray = (ISparseArray) tensor;
7103+
ISparseArray transposed = sparseArray.transpose(permutationArray, function);
7104+
if (transposed != null) {
7105+
return transposed;
7106+
}
7107+
// get error details:
7108+
for (int i = 0; i < permutationArray.length; i++) {
7109+
if (permutationArray[i] > permutationArray.length) {
7110+
// Entry `1` in `2` is out of bounds for a permutation of length `3`.
7111+
return Errors.printMessage(ast.topHead(), "perm2",
7112+
F.List(F.ZZ(i + 1), permutation, F.ZZ(permutationArray.length)), engine);
7113+
}
7114+
}
7115+
7116+
}
7117+
7118+
return F.NIL;
7119+
}
7120+
71077121
private LinearAlgebra() {}
71087122
}

0 commit comments

Comments
 (0)