Skip to content

Commit

Permalink
clean up PrincipalComponents
Browse files Browse the repository at this point in the history
- new message "mindet"
  "Input matrix contains an indeterminate entry."
  • Loading branch information
axkr committed Aug 30, 2023
1 parent f340b85 commit 6979fa3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4745,8 +4745,8 @@ private static IExpr powerExpandLogRational(IRational rationalNumber) {

for (int i = startPosition; i < factors.size(); i++) {
pair = (IAST) factors.get(i);
IExpr exponent = pair.exponent();
IExpr base = pair.base();
IExpr base = pair.arg1();
IExpr exponent = pair.arg2();
if (exponent.isOne()) {
logIntExpanded.append(F.Log(base));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5382,30 +5382,36 @@ private static final class PrincipalComponents extends AbstractFunctionOptionEva
public IExpr evaluate(final IAST ast, final int argSize, final IExpr[] option,
final EvalEngine engine, IAST originalAST) {
int[] dimension = ast.arg1().isMatrix();
RealMatrix matrix = ast.arg1().toRealMatrix();
if (dimension != null) {
if (dimension[0] == 1) {
if (dimension[1] == 0) {
return F.CEmptyList;
} else {
return F.List(F.constantArray(F.CD0, dimension[1]));
}
}
if (dimension[0] > 1 && dimension[1] > 0) {
PCA pca = null;
String method = option[0].toString();
if (method.equals("Covariance")) {
pca = new PCA(dimension[1]);
} else if (method.equals("Correlation")) {
pca = new PCA(dimension[1], true, true);
} else {
pca = new PCA(dimension[1]);
}
double[][] data = pca.fitAndTransform(matrix.getData());
return Convert.matrix2List(new Array2DRowRealMatrix(data));
if (dimension == null) {
return F.NIL;
}
if (dimension[0] == 1) {
if (dimension[1] == 0) {
return F.CEmptyList;
}
return F.List(F.constantArray(F.CD0, dimension[1]));
}
return F.NIL;

if (dimension[0] < 1 || dimension[1] <= 0) {
return F.NIL;
}

RealMatrix matrix = ast.arg1().toRealMatrix();
if (matrix == null) {
return F.NIL;
}

PCA pca = null;
String method = option[0].toString();
if (method.equals("Covariance")) {
pca = new PCA(dimension[1]);
} else if (method.equals("Correlation")) {
pca = new PCA(dimension[1], true, true);
} else {
pca = new PCA(dimension[1]);
}
double[][] data = pca.fitAndTransform(matrix.getData());
return Convert.matrix2List(new Array2DRowRealMatrix(data));
}

@Override
Expand Down Expand Up @@ -6248,8 +6254,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (vector.exists(x -> x.isReal() && !((IReal) x).isRange(F.C0, F.C1))) {
// The Quantile specification `1` should be a number or a list of numbers between
// `2` and `3`.
return Errors.printMessage(ast.topHead(), "nquan", F.list(q, F.C0, F.C1),
engine);
return Errors.printMessage(ast.topHead(), "nquan", F.list(q, F.C0, F.C1), engine);
}
return vector.mapThread(ast, 2);
} else {
Expand Down Expand Up @@ -7005,8 +7010,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
// numbers with length greater than the dimension of the array or two such arrays
// with
// of equal dimension.
return Errors.printMessage(ast.topHead(), "rctndm1", F.list(arg1, F.C1),
engine);
return Errors.printMessage(ast.topHead(), "rctndm1", F.list(arg1, F.C1), engine);
}
org.hipparchus.stat.inference.TTest tTest =
new org.hipparchus.stat.inference.TTest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public static void initGeneralMessages() {
"memlimit",
"This computation has exceeded the memeory limit settings of this evaluation engines instance.", //
"meprec", "Internal precision limit `1` reached while evaluating `2`.", //
"mindet", "Input matrix contains an indeterminate entry.", //
"minv", "The `1` arguments to `2` must be ordinary integers.", // or gaussian
"mseqs",
"Sequence specification or a list of sequence specifications expected at position `1` in `2`.", //
Expand Down Expand Up @@ -422,8 +423,7 @@ public static IExpr printMessage(ISymbol symbol, final MathException mex, EvalEn
* @param engine
* @return
*/
public static IExpr printMessage(ISymbol symbol, final Throwable exception,
EvalEngine engine) {
public static IExpr printMessage(ISymbol symbol, final Throwable exception, EvalEngine engine) {
if (Config.SHOW_STACKTRACE) {
exception.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,7 @@ public FieldVector<IExpr> toFieldVector(boolean copyArray) {
@Override
public RealMatrix toRealMatrix() {
if (fDimension.length == 2 && fDimension[0] > 0 && fDimension[1] > 0) {
IExpr value = null;
try {
OpenMapRealMatrix result = new OpenMapRealMatrix(fDimension[0], fDimension[1]);
if (!fDefaultValue.isZero()) {
Expand All @@ -2053,12 +2054,16 @@ public RealMatrix toRealMatrix() {
}
for (TrieNode<int[], IExpr> entry : fData.nodeSet()) {
int[] key = entry.getKey();
IExpr value = entry.getValue();
value = entry.getValue();
result.setEntry(key[0] - 1, key[1] - 1, value.evalf());
}
return result;
} catch (ArgumentTypeException rex) {

if (value != null && value.isIndeterminate()) {
// Input matrix contains an indeterminate entry.
Errors.printMessage(S.SparseArray, "mindet", F.List());
return null;
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19129,6 +19129,10 @@ public void testPrimePi() {
}

public void testPrincipleComponents() {
// message SparseArray: Input matrix contains an indeterminate entry.
check(
"PrincipalComponents(SparseArray({{0,0},{0,0}},0) ^ (I*1/3*Pi))", //
"PrincipalComponents(SparseArray(Number of elements: 4 Dimensions: {2,2} Default value: 0))");
check("PrincipalComponents({{0.25,0.33,0.45,0.01}},Method->\"Correlation\")", //
"{{0.0,0.0,0.0,0.0}}");
check("PrincipalComponents({{0.25,0.33,0.01}} )", //
Expand Down

0 comments on commit 6979fa3

Please sign in to comment.