use of org.matheclipse.core.expression.ASTRealMatrix in project symja_android_library by axkr.
the class SerializableTest method testASTRealMatrix.
public void testASTRealMatrix() {
equalsCopy(new ASTRealMatrix(new double[][] { { 1.0, 2.0, 3.0 }, { 3.3, 4.4, 5.5 } }, false));
// PseudoInverse({{1,2,3},{3,4,5}})
EvalEngine engine = EvalEngine.get();
IExpr result = engine.evaluate(F.PseudoInverse(F.List(F.List(F.C1, F.C2, F.C3), F.List(F.C4, F.C5, F.C6))));
equalsCopy(result);
}
use of org.matheclipse.core.expression.ASTRealMatrix in project symja_android_library by axkr.
the class WXFTestCase method testBinarySerialize.
public void testBinarySerialize() {
// decimal 4611686018427387893
check(//
"BinarySerialize(2^62 -11) // Normal", "{56,58,76,245,255,255,255,255,255,255,63}");
check(//
"BinarySerialize(<|a->b,1:>2|>) // Normal", "{56,58,65,2,45,115,8,71,108,111,98,97,108,96,97,115,8,71,108,111,98,97,108,96,98,\n" + "58,67,1,67,2}");
check(//
"BinarySerialize(Quantity(12, \"Hours\")) // Normal", "{56,58,102,2,115,8,81,117,97,110,116,105,116,121,67,12,83,5,72,111,117,114,115}");
// check("BinarySerialize(N(Pi,30))", //
// "");
check(//
"BinarySerialize(SeriesData(x, 0, {1, 1, 1/2, 1/6, 1/24, 1/120}, 0, 6, 1)) // Normal", "{56,58,102,6,115,10,83,101,114,105,101,115,68,97,116,97,115,8,71,108,111,98,97,\n" + "108,96,120,67,0,102,6,115,4,76,105,115,116,67,1,67,1,102,2,115,8,82,97,116,105,\n" + "111,110,97,108,67,1,67,2,102,2,115,8,82,97,116,105,111,110,97,108,67,1,67,6,102,\n" + "2,115,8,82,97,116,105,111,110,97,108,67,1,67,24,102,2,115,8,82,97,116,105,111,\n" + "110,97,108,67,1,67,120,67,0,67,6,67,1}");
check(//
"BinarySerialize(Quantity(12, \"Hours\")) // Normal", "{56,58,102,2,115,8,81,117,97,110,116,105,116,121,67,12,83,5,72,111,117,114,115}");
double[] d0 = new double[] { 1.1, 2.34, 4.11 };
double[] d1 = new double[] { 3.1415, 100000.1234567, -100000.4711 };
ASTRealMatrix m = new ASTRealMatrix(new double[][] { d0, d1 }, false);
IExpr expr = F.Normal.of(F.BinarySerialize.of(m));
check(//
expr.toString(), "{56,58,193,35,2,2,3,154,153,153,153,153,153,241,63,184,30,133,235,81,184,2,64,\n" + "113,61,10,215,163,112,16,64,111,18,131,192,202,33,9,64,144,187,173,249,1,106,248,\n" + "64,82,39,160,137,7,106,248,192}");
ASTRealVector v = new ASTRealVector(new double[] { 1.1, 2.34, 4.11 }, false);
expr = F.Normal.of(F.BinarySerialize.of(v));
check(//
expr.toString(), "{56,58,193,35,1,3,154,153,153,153,153,153,241,63,184,30,133,235,81,184,2,64,113,\n" + "61,10,215,163,112,16,64}");
check(//
"BinarySerialize(x->y) // Normal", "{56,58,102,2,115,4,82,117,108,101,115,8,71,108,111,98,97,108,96,120,115,8,71,108,\n" + "111,98,97,108,96,121}");
check(//
"BinarySerialize(x___) // Normal", "{56,58,102,2,115,7,80,97,116,116,101,114,110,115,8,71,108,111,98,97,108,96,120,\n" + "102,0,115,17,66,108,97,110,107,78,117,108,108,83,101,113,117,101,110,99,101}");
check(//
"BinarySerialize(x__) // Normal", "{56,58,102,2,115,7,80,97,116,116,101,114,110,115,8,71,108,111,98,97,108,96,120,\n" + "102,0,115,13,66,108,97,110,107,83,101,113,117,101,110,99,101}");
check(//
"BinarySerialize(___) // Normal", "{56,58,102,0,115,17,66,108,97,110,107,78,117,108,108,83,101,113,117,101,110,99,\n" + "101}");
check(//
"BinarySerialize(__) // Normal", "{56,58,102,0,115,13,66,108,97,110,107,83,101,113,117,101,110,99,101}");
check(//
"BinarySerialize(-25!) // Normal", "{56,58,73,27,45,49,53,53,49,49,50,49,48,48,52,51,51,51,48,57,56,53,57,56,52,48,\n" + "48,48,48,48,48}");
check(//
"BinarySerialize(25!) // Normal", "{56,58,73,26,49,53,53,49,49,50,49,48,48,52,51,51,51,48,57,56,53,57,56,52,48,48,\n" + "48,48,48,48}");
check(//
"BinarySerialize(500000) // Normal", "{56,58,105,32,161,7,0}");
check(//
"BinarySerialize(-500000) // Normal", "{56,58,105,224,94,248,255}");
check(//
"BinarySerialize(-1111) // Normal", "{56,58,106,169,251}");
check(//
"BinarySerialize(1111) // Normal", "{56,58,106,87,4}");
check(//
"BinarySerialize(42) // Normal", "{56,58,67,42}");
check(//
"BinarySerialize(-42) // Normal", "{56,58,67,214}");
check(//
"BinarySerialize(x_.) // Normal", "{56,58,102,1,115,8,79,112,116,105,111,110,97,108,102,2,115,7,80,97,116,116,101,\n" + "114,110,115,8,71,108,111,98,97,108,96,120,102,0,115,5,66,108,97,110,107}");
check(//
"BinarySerialize(x_Integer) // Normal", "{56,58,102,2,115,7,80,97,116,116,101,114,110,115,8,71,108,111,98,97,108,96,120,\n" + "102,1,115,5,66,108,97,110,107,115,7,73,110,116,101,103,101,114}");
check(//
"BinarySerialize(x_) // Normal", "{56,58,102,2,115,7,80,97,116,116,101,114,110,115,8,71,108,111,98,97,108,96,120,\n" + "102,0,115,5,66,108,97,110,107}");
check(//
"BinarySerialize(_) // Normal", "{56,58,102,0,115,5,66,108,97,110,107}");
check(//
"BinarySerialize(_Integer) // Normal", "{56,58,102,1,115,5,66,108,97,110,107,115,7,73,110,116,101,103,101,114}");
check(//
"BinarySerialize(-1000.14157+I*42.1) // Normal", "{56,58,102,2,115,7,67,111,109,112,108,101,120,114,194,192,115,239,33,65,143,192,\n" + "114,205,204,204,204,204,12,69,64}");
check(//
"BinarySerialize(-3.14157) // Normal", "{56,58,114,253,193,192,115,239,33,9,192}");
check(//
"BinarySerialize(0.75) // Normal", "{56,58,114,0,0,0,0,0,0,232,63}");
check(//
"BinarySerialize(2/3+7/4*I) // Normal", "{56,58,102,2,115,7,67,111,109,112,108,101,120,102,2,115,8,82,97,116,105,111,110,\n" + "97,108,67,2,67,3,102,2,115,8,82,97,116,105,111,110,97,108,67,7,67,4}");
check(//
"BinarySerialize(2/3) // Normal", "{56,58,102,2,115,8,82,97,116,105,111,110,97,108,67,2,67,3}");
check(//
"BinarySerialize(Plot) // Normal", "{56,58,115,4,80,108,111,116}");
check(//
"BinarySerialize(\"hello!\") // Normal", "{56,58,83,6,104,101,108,108,111,33}");
check(//
"BinarySerialize({}) // Normal", "{56,58,102,0,115,4,76,105,115,116}");
check(//
"BinarySerialize(f( )) // Normal", "{56,58,102,0,115,8,71,108,111,98,97,108,96,102}");
check(//
"BinarySerialize(f(g)) // Normal", "{56,58,102,1,115,8,71,108,111,98,97,108,96,102,115,8,71,108,111,98,97,108,96,103}");
check(//
"BinarySerialize(f(g(x,y))) // Normal", "{56,58,102,1,115,8,71,108,111,98,97,108,96,102,102,2,115,8,71,108,111,98,97,108,\n" + "96,103,115,8,71,108,111,98,97,108,96,120,115,8,71,108,111,98,97,108,96,121}");
check(//
"BinarySerialize(f(g,2)) // Normal", "{56,58,102,2,115,8,71,108,111,98,97,108,96,102,115,8,71,108,111,98,97,108,96,103,\n" + "67,2}");
}
use of org.matheclipse.core.expression.ASTRealMatrix in project symja_android_library by axkr.
the class ListPod method addJSON.
@Override
public int addJSON(ArrayNode podsArray, int formats, EvalEngine engine) {
int numpods = 0;
boolean intList = list.forAll(x -> x.isInteger());
IExpr inExpr = list;
if (list.argSize() == 2) {
VariablesSet varSet = new VariablesSet(list);
IAST variables = varSet.getVarList();
if (variables.argSize() == 1) {
IExpr arg1 = list.arg1();
IExpr arg2 = list.arg2();
if (arg1.isNumericFunction(varSet) && arg2.isNumericFunction(varSet)) {
boolean isPoly1 = arg1.isPolynomial(variables);
boolean isPoly2 = arg2.isPolynomial(variables);
if (isPoly1 && isPoly2) {
inExpr = F.PolynomialQuotientRemainder(arg1, arg2, variables.arg1());
IExpr podOut = engine.evaluate(inExpr);
Pods.addSymjaPod(podsArray, inExpr, podOut, "Polynomial quotient and remainder", "Polynomial", formats, engine);
numpods++;
}
}
}
}
if (intList) {
numpods += integerListPods(podsArray, inExpr, list, formats, Pods.JSON_OBJECT_MAPPER, engine);
}
int[] matrixDimension = list.isMatrix();
if (matrixDimension != null) {
if (//
matrixDimension[0] >= 2 && matrixDimension[1] >= 2) {
if (//
matrixDimension[0] <= Config.MAX_MATRIX_DIMENSION_SIZE && matrixDimension[1] <= Config.MAX_MATRIX_DIMENSION_SIZE) {
double[][] matrix = list.toDoubleMatrix();
if (matrix == null) {
if (matrixDimension[0] == matrixDimension[1]) {
IExpr hermitianMatrixQ = F.HermitianMatrixQ(list);
boolean isHermitian = engine.evalTrue(hermitianMatrixQ);
if (isHermitian) {
Pods.addSymjaPod(podsArray, hermitianMatrixQ, F.NIL, "The matrix is hermitian (self-adjoint).", "Properties", "Matrix", formats, engine);
numpods++;
}
}
} else {
if (matrixDimension[0] == matrixDimension[1]) {
IExpr symmetricMatrixQ = F.SymmetricMatrixQ(list);
boolean isSymmetric = engine.evalTrue(symmetricMatrixQ);
if (isSymmetric) {
Pods.addSymjaPod(podsArray, symmetricMatrixQ, F.NIL, "The matrix is symmetric.", "Properties", "Matrix", formats, engine);
numpods++;
}
// new ASTRealMatrix(matrix, false));
IExpr detMatrix = F.Det(list);
IExpr podOut = engine.evaluate(detMatrix);
if (detMatrix.isZero()) {
Pods.addSymjaPod(podsArray, symmetricMatrixQ, F.NIL, "The matrix is singular", "Properties", "Matrix", formats, engine);
numpods++;
} else {
Pods.addSymjaPod(podsArray, detMatrix, F.NIL, "The determinant of the matrix is " + podOut.toString(), "Properties", "Matrix", formats, engine);
numpods++;
inExpr = F.Inverse(list);
podOut = engine.evaluate(inExpr);
Pods.addSymjaPod(podsArray, inExpr, podOut, "Inverse of matrix", "Matrix", formats, engine);
numpods++;
}
}
if (matrixDimension[1] == 2) {
ASTRealMatrix m = new ASTRealMatrix(matrix, false);
IExpr plot2D = F.ListPlot(m);
IExpr podOut = engine.evaluate(plot2D);
if (podOut.isAST(S.JSFormData, 3)) {
int form = Pods.internFormat(Pods.SYMJA, podOut.second().toString());
Pods.addPod(podsArray, inExpr, podOut, podOut.first().toString(), StringFunctions.inputForm(plot2D), "Plot points", "Plotter", form, engine);
numpods++;
}
}
}
}
}
} else {
double[] vector = list.toDoubleVector();
if (vector != null) {
ASTRealVector v = new ASTRealVector(vector, false);
if (!intList) {
inExpr = F.Total(v);
IExpr podOut = engine.evaluate(inExpr);
Pods.addSymjaPod(podsArray, inExpr, podOut, "Total", "List", formats, engine);
numpods++;
inExpr = F.Norm(v);
podOut = engine.evaluate(inExpr);
Pods.addSymjaPod(podsArray, inExpr, podOut, "Vector length", "List", formats, engine);
numpods++;
inExpr = F.Normalize(v);
podOut = engine.evaluate(inExpr);
Pods.addSymjaPod(podsArray, inExpr, podOut, "Normalized vector", "List", formats, engine);
numpods++;
}
if (vector.length > 2) {
IExpr plot2D = F.ListPlot(v);
IExpr podOut = engine.evaluate(plot2D);
if (podOut.isAST(S.JSFormData, 3)) {
int form = Pods.internFormat(Pods.SYMJA, podOut.second().toString());
Pods.addPod(podsArray, inExpr, podOut, podOut.first().toString(), StringFunctions.inputForm(plot2D), "Plot points", "Plotter", form, engine);
numpods++;
}
}
}
if (list.argSize() > 5) {
IExpr fivenum = F.FiveNum(list);
IExpr podOut = engine.evaluate(fivenum);
Pods.addSymjaPod(podsArray, fivenum, podOut, "Five-number summary", "Statistics", formats, engine);
numpods++;
IExpr histogram = F.Histogram(list);
podOut = engine.evaluate(histogram);
if (podOut.isAST(S.JSFormData, 3)) {
int form = Pods.internFormat(Pods.SYMJA, podOut.second().toString());
Pods.addPod(podsArray, inExpr, podOut, podOut.first().toString(), StringFunctions.inputForm(histogram), "Histogram", "Plotter", form, engine);
numpods++;
}
}
}
return numpods;
}
use of org.matheclipse.core.expression.ASTRealMatrix in project symja_android_library by axkr.
the class ComplexFormFactory method convertList.
public void convertList(final StringBuilder buf, final IAST list) {
if (list instanceof ASTRealVector) {
RealVector vector = ((ASTRealVector) list).getRealVector();
buf.append('{');
int size = vector.getDimension();
for (int i = 0; i < size; i++) {
convertDouble(buf, vector.getEntry(i));
if (i < size - 1) {
buf.append(",");
}
}
buf.append('}');
return;
}
if (list instanceof ASTRealMatrix) {
RealMatrix matrix = ((ASTRealMatrix) list).getRealMatrix();
buf.append('{');
int rows = matrix.getRowDimension();
int cols = matrix.getColumnDimension();
for (int i = 0; i < rows; i++) {
if (i != 0) {
buf.append(" ");
}
buf.append("{");
for (int j = 0; j < cols; j++) {
convertDouble(buf, matrix.getEntry(i, j));
if (j < cols - 1) {
buf.append(",");
}
}
buf.append('}');
if (i < rows - 1) {
buf.append(",");
buf.append('\n');
}
}
buf.append('}');
return;
}
if (list.isEvalFlagOn(IAST.IS_MATRIX)) {
if (!fEmpty) {
newLine(buf);
}
}
append(buf, "{");
final int listSize = list.size();
if (listSize > 1) {
convertInternal(buf, list.arg1());
}
for (int i = 2; i < listSize; i++) {
append(buf, ",");
if (list.isEvalFlagOn(IAST.IS_MATRIX)) {
newLine(buf);
append(buf, ' ');
}
convertInternal(buf, list.get(i));
}
append(buf, "}");
}
use of org.matheclipse.core.expression.ASTRealMatrix in project symja_android_library by axkr.
the class OutputFormFactory method convert.
private void convert(final Appendable buf, final IExpr o, final int precedence, boolean isASTHead) throws IOException {
if (o instanceof IAST) {
final IAST list = (IAST) o;
if (!list.isPresent()) {
append(buf, "NIL");
return;
}
if (o.isDataset()) {
// TODO improve output
buf.append(o.toString());
return;
} else if (o.isAssociation()) {
convertAssociation(buf, (IAssociation) o);
return;
} else if (o.isAST(S.Association, 1)) {
buf.append("<||>");
return;
}
IExpr header = list.head();
if (!header.isSymbol()) {
// print expressions like: f(#1, y)& [x]
IAST[] derivStruct = list.isDerivativeAST1();
if (derivStruct != null) {
IAST a1Head = derivStruct[0];
IAST headAST = derivStruct[1];
if (a1Head.isAST1() && a1Head.arg1().isInteger() && headAST.isAST1() && (headAST.arg1().isSymbol() || headAST.arg1().isAST()) && derivStruct[2] != null) {
try {
int n = ((IInteger) a1Head.arg1()).toInt();
if (n == 1 || n == 2) {
IExpr symbolOrAST = headAST.arg1();
convert(buf, symbolOrAST, Integer.MIN_VALUE, false);
if (n == 1) {
append(buf, "'");
} else if (n == 2) {
append(buf, "''");
}
convertArgs(buf, symbolOrAST, list);
return;
}
} catch (ArithmeticException ae) {
}
}
}
convert(buf, header, Integer.MIN_VALUE, true);
// avoid fast StackOverflow
append(buf, "[");
for (int i = 1; i < list.size(); i++) {
convert(buf, list.get(i), Integer.MIN_VALUE, false);
if (i < list.argSize()) {
append(buf, ",");
}
}
append(buf, "]");
return;
}
if (header.isSymbol()) {
ISymbol head = (ISymbol) header;
int functionID = head.ordinal();
if (functionID > ID.UNKNOWN) {
switch(functionID) {
case ID.TwoWayRule:
case ID.UndirectedEdge:
if (list.isAST2()) {
convert(buf, list.arg1(), Integer.MIN_VALUE, false);
buf.append("<->");
convert(buf, list.arg2(), Integer.MIN_VALUE, false);
return;
}
break;
case ID.DirectedEdge:
if (list.isAST2()) {
convert(buf, list.arg1(), Integer.MIN_VALUE, false);
buf.append("->");
convert(buf, list.arg2(), Integer.MIN_VALUE, false);
return;
}
break;
}
}
final Operator operator = getOperator(head);
if (operator != null) {
if (operator instanceof PostfixOperator) {
if (list.isAST1()) {
convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
return;
}
} else {
if (convertOperator(operator, list, buf, isASTHead ? Integer.MAX_VALUE : precedence, head)) {
return;
}
}
}
if (functionID > ID.UNKNOWN) {
switch(functionID) {
case ID.Inequality:
if (list.size() > 3 && convertInequality(buf, list, precedence)) {
return;
}
break;
case ID.Quantity:
// if (head.equals(F.SeriesData) && (list.size() == 7)) {
if (list instanceof IQuantity) {
if (convertQuantityData(buf, (IQuantity) list, precedence)) {
return;
}
}
break;
case ID.SeriesData:
// if (head.equals(F.SeriesData) && (list.size() == 7)) {
if (list instanceof ASTSeriesData) {
if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
return;
}
}
break;
case ID.SparseArray:
if (list.isSparseArray()) {
buf.append(list.toString());
return;
}
break;
case ID.Parenthesis:
convertArgs(buf, S.Parenthesis, list);
return;
case ID.List:
convertList(buf, list, false);
return;
case ID.MatrixForm:
if (list.isASTOrAssociation() && list.size() > 1) {
// see also MatrixForm in MathML or TeX format for "graphical representation".
IExpr normal = list.arg1().normal(false);
if (normal.isList()) {
// && normal.isMatrix() != null) {
IntList dims = LinearAlgebra.dimensions((IAST) normal, S.List);
convertList(buf, (IAST) normal, dims.size() >= 2);
return;
}
convert(buf, normal, Integer.MIN_VALUE, false);
return;
}
break;
case ID.Out:
if (list.isAST1() && list.arg1().isInteger()) {
int lineNumber = list.arg1().toIntDefault();
if (lineNumber == -1) {
buf.append("%");
return;
} else if (lineNumber == -2) {
buf.append("%%");
return;
}
}
break;
case ID.Part:
if (list.size() >= 3) {
convertPart(buf, list);
return;
}
break;
case ID.Slot:
if (list.isAST1() && list.arg1().isInteger()) {
convertSlot(buf, list);
return;
}
break;
case ID.SlotSequence:
if (list.isAST1() && list.arg1().isInteger()) {
convertSlotSequence(buf, list);
return;
}
break;
case ID.Defer:
case ID.HoldForm:
if (list.isAST1()) {
convert(buf, list.arg1(), Integer.MIN_VALUE, false);
return;
}
break;
case ID.DirectedInfinity:
if (list.isDirectedInfinity()) {
// head.equals(F.DirectedInfinity))
if (list.isAST0()) {
append(buf, "ComplexInfinity");
return;
}
if (list.isAST1()) {
if (list.arg1().isOne()) {
append(buf, "Infinity");
return;
} else if (list.arg1().isMinusOne()) {
if (Precedence.PLUS < precedence) {
append(buf, "(");
}
append(buf, "-Infinity");
if (Precedence.PLUS < precedence) {
append(buf, ")");
}
return;
} else if (list.arg1().isImaginaryUnit()) {
append(buf, "I*Infinity");
return;
} else if (list.arg1().isNegativeImaginaryUnit()) {
append(buf, "-I*Infinity");
return;
}
}
}
break;
case ID.Optional:
if (list.isAST2() && (list.arg1().isBlank() || list.arg1().isPattern())) {
convert(buf, list.arg1(), Integer.MIN_VALUE, false);
buf.append(":");
convert(buf, list.arg2(), Integer.MIN_VALUE, false);
return;
}
break;
case ID.Complex:
if (list.isAST2()) {
// used for visual comparison of steps
boolean isZeroRealPart = list.arg1().isZero();
final int prec = isZeroRealPart ? Precedence.TIMES : Precedence.PLUS;
if (prec < precedence) {
append(buf, "(");
}
if (isZeroRealPart) {
buf.append("I*");
convert(buf, list.arg2(), Precedence.TIMES, false);
} else {
convert(buf, list.arg1(), Precedence.PLUS, false);
buf.append("+I*");
convert(buf, list.arg2(), Precedence.TIMES, false);
}
if (prec < precedence) {
append(buf, ")");
}
return;
}
break;
case ID.Rational:
if (list.isAST2()) {
// used for visual comparison of steps
IExpr numerator = list.arg1();
final boolean isNegative = numerator.isNegative();
final int prec = isNegative ? Precedence.PLUS : Precedence.TIMES;
if (prec < precedence) {
append(buf, "(");
}
convert(buf, list.arg1(), Precedence.DIVIDE, false);
buf.append("/");
convert(buf, list.arg2(), Precedence.DIVIDE, false);
if (prec < precedence) {
append(buf, ")");
}
return;
}
break;
}
} else {
if (list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
convertList(buf, list, false);
return;
}
}
}
convertAST(buf, list);
} else if (o instanceof ISignedNumber) {
convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
} else if (o instanceof IComplexNum) {
convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
} else if (o instanceof IComplex) {
convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
} else if (o instanceof ISymbol) {
convertSymbol(buf, (ISymbol) o);
} else if (o instanceof IPatternObject) {
convertPattern(buf, (IPatternObject) o);
} else if (o instanceof IStringX) {
convertString(buf, ((IStringX) o).toString());
} else {
convertString(buf, o.toString());
}
}
Aggregations