Search in sources :

Example 1 with ASTRealMatrix

use of org.matheclipse.core.expression.ASTRealMatrix in project symja_android_library by axkr.

the class OutputFormFactory method convertList.

public void convertList(final Appendable buf, final IAST list, boolean isMatrix) throws IOException {
    if (list instanceof ASTRealVector) {
        try {
            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('}');
        } catch (IOException e) {
            LOGGER.debug("OutputFormFactory.convertList() failed", e);
        }
        return;
    }
    if (list instanceof ASTRealMatrix) {
        try {
            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('}');
        } catch (IOException e) {
            LOGGER.debug("OutputFormFactory.convertList() failed", e);
        }
        return;
    }
    if (list.isEvalFlagOn(IAST.IS_MATRIX) || isMatrix) {
        if (!fEmpty) {
            newLine(buf);
        }
    }
    append(buf, "{");
    final int listSize = list.size();
    if (listSize > 1) {
        convert(buf, list.arg1(), Integer.MIN_VALUE, false);
    }
    for (int i = 2; i < listSize; i++) {
        append(buf, ",");
        if (list.isEvalFlagOn(IAST.IS_MATRIX) || isMatrix) {
            newLine(buf);
            append(buf, ' ');
        }
        convert(buf, list.get(i), Integer.MIN_VALUE, false);
    }
    append(buf, "}");
}
Also used : ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) RealMatrix(org.hipparchus.linear.RealMatrix) ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) RealVector(org.hipparchus.linear.RealVector) ASTRealVector(org.matheclipse.core.expression.ASTRealVector) IOException(java.io.IOException) ASTRealVector(org.matheclipse.core.expression.ASTRealVector)

Example 2 with ASTRealMatrix

use of org.matheclipse.core.expression.ASTRealMatrix in project symja_android_library by axkr.

the class MathMLFormFactory method convertAST.

private void convertAST(final StringBuilder buf, final IAST ast, final int precedence) {
    final IAST list = ast;
    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() && headAST.isAST1() && (headAST.arg1().isSymbol() || headAST.arg1().isAST())) {
                try {
                    int n = a1Head.arg1().toIntDefault();
                    if (n == 1 || n == 2) {
                        tagStart(buf, "mrow");
                        tagStart(buf, "msup");
                        IExpr symbolOrAST = headAST.arg1();
                        convertInternal(buf, symbolOrAST, Integer.MAX_VALUE, false);
                        if (n == 1) {
                            tag(buf, "mo", "&#8242;");
                        } else if (n == 2) {
                            tag(buf, "mo", "&#8242;&#8242;");
                        }
                        tagEnd(buf, "msup");
                        if (derivStruct[2] != null) {
                            convertArgs(buf, symbolOrAST, list);
                        }
                        tagEnd(buf, "mrow");
                        return;
                    }
                    tagStart(buf, "mrow");
                    IExpr symbolOrAST = headAST.arg1();
                    tagStart(buf, "msup");
                    convertInternal(buf, symbolOrAST, Integer.MAX_VALUE, false);
                    tagStart(buf, "mrow");
                    tag(buf, "mo", "(");
                    // supscript "(n)"
                    convertInternal(buf, a1Head.arg1(), Integer.MIN_VALUE, false);
                    tag(buf, "mo", ")");
                    tagEnd(buf, "mrow");
                    tagEnd(buf, "msup");
                    if (derivStruct[2] != null) {
                        convertArgs(buf, symbolOrAST, list);
                    }
                    tagEnd(buf, "mrow");
                    return;
                } catch (ArithmeticException ae) {
                }
            }
        }
        convertInternal(buf, header, Integer.MIN_VALUE, false);
        convertFunctionArgs(buf, list);
        return;
    }
    ISymbol head = list.topHead();
    final org.matheclipse.parser.client.operator.Operator operator = OutputFormFactory.getOperator(head);
    if (operator != null) {
        if (operator instanceof PostfixOperator) {
            if (list.isAST1()) {
                convertPostfixOperator(buf, list, (PostfixOperator) operator, operator.getPrecedence());
                return;
            }
        } else {
            if (convertOperator(operator, list, buf, operator.getPrecedence(), head)) {
                return;
            }
        }
    }
    if (list instanceof ASTSeriesData) {
        if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
            return;
        }
    }
    if (list.isList() || list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
        convertList(buf, list);
        return;
    }
    if (list.isAST(S.Parenthesis)) {
        convertArgs(buf, S.Parenthesis, list);
        return;
    }
    if (list.isInterval() && convertInterval(buf, list)) {
        return;
    }
    if (list.isAssociation()) {
        convertAssociation(buf, (IAssociation) list);
        return;
    }
    int functionID = ((ISymbol) list.head()).ordinal();
    if (functionID > ID.UNKNOWN) {
        switch(functionID) {
            case ID.Inequality:
                if (list.size() > 3 && convertInequality(buf, list, precedence)) {
                    return;
                }
                break;
            case ID.Part:
                if ((list.size() >= 3)) {
                    convertPart(buf, list);
                    return;
                }
                break;
            case ID.Slot:
                if ((list.isAST1()) && (list.arg1() instanceof IInteger)) {
                    convertSlot(buf, list);
                    return;
                }
                break;
            case ID.SlotSequence:
                if ((list.isAST1()) && (list.arg1() instanceof IInteger)) {
                    convertSlotSequence(buf, list);
                    return;
                }
                break;
            case ID.SparseArray:
                if (list.isSparseArray()) {
                    tagStart(buf, "mtext");
                    buf.append(list.toString());
                    tagEnd(buf, "mtext");
                    return;
                }
                break;
            case ID.Defer:
            case ID.HoldForm:
                if ((list.isAST1())) {
                    convertInternal(buf, list.arg1(), precedence, false);
                    return;
                }
                break;
            case ID.DirectedInfinity:
                if (list.isDirectedInfinity()) {
                    // head.equals(F.DirectedInfinity))
                    if (list.isAST0()) {
                        convertSymbol(buf, S.ComplexInfinity);
                        return;
                    }
                    if (list.isAST1()) {
                        if (list.arg1().isOne()) {
                            convertSymbol(buf, S.Infinity);
                            return;
                        } else if (list.arg1().isMinusOne()) {
                            convertInternal(buf, F.Times(F.CN1, S.Infinity), precedence, false);
                            return;
                        } else if (list.arg1().isImaginaryUnit()) {
                            convertInternal(buf, F.Times(F.CI, S.Infinity), precedence, false);
                            return;
                        } else if (list.arg1().isNegativeImaginaryUnit()) {
                            convertInternal(buf, F.Times(F.CNI, S.Infinity), precedence, false);
                            return;
                        }
                    }
                }
                break;
        }
    }
    // if (head.equals(F.SeriesData) && (list.size() == 7)) {
    // if (convertSeriesData(buf, list, precedence)) {
    // return;
    // }
    tagStart(buf, "mrow");
    convertHead(buf, ast.head());
    // &af; &#x2061;
    // tag(buf, "mo", "&#x2061;");
    tagStart(buf, "mrow");
    if (fRelaxedSyntax) {
        tag(buf, "mo", "(");
    } else {
        tag(buf, "mo", "[");
    }
    tagStart(buf, "mrow");
    for (int i = 1; i < ast.size(); i++) {
        convertInternal(buf, ast.get(i), Integer.MIN_VALUE, false);
        if (i < ast.argSize()) {
            tag(buf, "mo", ",");
        }
    }
    tagEnd(buf, "mrow");
    if (fRelaxedSyntax) {
        tag(buf, "mo", ")");
    } else {
        tag(buf, "mo", "]");
    }
    tagEnd(buf, "mrow");
    tagEnd(buf, "mrow");
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) IInteger(org.matheclipse.core.interfaces.IInteger) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ASTRealVector(org.matheclipse.core.expression.ASTRealVector)

Example 3 with ASTRealMatrix

use of org.matheclipse.core.expression.ASTRealMatrix in project symja_android_library by axkr.

the class DoubleFormFactory 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, "}");
}
Also used : ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) RealMatrix(org.hipparchus.linear.RealMatrix) ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) ASTRealVector(org.matheclipse.core.expression.ASTRealVector) RealVector(org.hipparchus.linear.RealVector) ASTRealVector(org.matheclipse.core.expression.ASTRealVector)

Example 4 with ASTRealMatrix

use of org.matheclipse.core.expression.ASTRealMatrix in project symja_android_library by axkr.

the class Convert method list2RealMatrix.

/**
 * Returns a RealMatrix if possible.
 *
 * @param listMatrix
 * @return a RealMatrix or <code>null</code> if the given list is no matrix.
 * @throws ClassCastException
 * @throws IndexOutOfBoundsException
 * @deprecated use {@link IExpr#toRealMatrix()}
 */
@Deprecated
public static RealMatrix list2RealMatrix(final IAST listMatrix) throws ClassCastException, IndexOutOfBoundsException {
    if (listMatrix == null) {
        return null;
    }
    if (listMatrix instanceof ASTRealMatrix) {
        return ((ASTRealMatrix) listMatrix).getRealMatrix();
    }
    final Object header = listMatrix.head();
    if (header != S.List) {
        return null;
    }
    IAST currInRow = (IAST) listMatrix.arg1();
    if (currInRow.isAST0()) {
        // special case 0-Matrix
        double[][] array = new double[0][0];
        return new Array2DRowRealMatrix(array, false);
    }
    final double[][] elements = listMatrix.toDoubleMatrix();
    if (elements == null) {
        return null;
    }
    return new Array2DRowRealMatrix(elements, false);
}
Also used : ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) Array2DRowRealMatrix(org.hipparchus.linear.Array2DRowRealMatrix) IAST(org.matheclipse.core.interfaces.IAST)

Example 5 with ASTRealMatrix

use of org.matheclipse.core.expression.ASTRealMatrix in project symja_android_library by axkr.

the class Object2Expr method convert.

/**
 * Converts the following Java objects into an IExpr expression
 *
 * <pre>
 * Java Object       -&gt; Symja object
 * -------------------------------------
 * null object          {@link S#Null} symbol
 * IExpr                {@link IExpr} type
 * Boolean              {@link S#True} or {@link S#False} symbol
 * BigInteger           {@link IInteger} value
 * BigDecimal           {@link INum} with {@link Apfloat#Apfloat(java.math.BigDecimal)} value
 * Double               {@link INum}  with doubleValue() value
 * Float                {@link INum}  with doubleValue() value
 * Integer              {@link IInteger} with intValue() value
 * Long                 {@link IInteger} with longValue() value
 * Number               {@link INum} with doubleValue() value
 * java.util.Collection list of elements
 *                      1..nth element of the list give the elements of the List()
 * Object[]             a list of converted objects
 * int[]                a list of {@link IInteger} integer values
 * double[]             a vector ASTRealVector of <code>double</code> values
 * double[][]           a matrix ASTRealMatrix of <code>double</code> values
 * Complex[]            a list of {@link IComplexNum} values
 * boolean[]            a list of {@link S#True} or {@link S#False} symbols
 *
 * </pre>
 *
 * @param parseString if <code>true</code> and <code>obj instanceof String</code> parse the string
 *        as a Symja expression
 * @param javaObject if <code>true</code> return a wrapper instanceof {@link JavaObjectExpr} if no
 *        other conversion was found
 */
public static IExpr convert(Object obj, boolean parseString, boolean javaObject) {
    if (obj == null) {
        return S.Null;
    }
    if (obj instanceof IExpr) {
        return (IExpr) obj;
    }
    if (obj instanceof String) {
        if (parseString) {
            final ExprParser parser = new ExprParser(EvalEngine.get());
            return parser.parse((String) obj);
        } else {
            return F.stringx((String) obj);
        }
    }
    if (obj instanceof Boolean) {
        return ((Boolean) obj) ? S.True : S.False;
    }
    if (obj instanceof Number) {
        if (obj instanceof org.hipparchus.fraction.Fraction) {
            org.hipparchus.fraction.Fraction frac = (org.hipparchus.fraction.Fraction) obj;
            return F.fraction(frac.getNumerator(), frac.getDenominator());
        }
        return convert((Number) obj);
    }
    if (obj instanceof java.util.Collection) {
        return convertList((java.util.Collection<?>) obj, parseString, javaObject);
    }
    if (obj instanceof org.hipparchus.complex.Complex) {
        org.hipparchus.complex.Complex cmp = (org.hipparchus.complex.Complex) obj;
        return F.complexNum(cmp.getReal(), cmp.getImaginary());
    }
    if (obj instanceof int[]) {
        return F.ast(S.List, (int[]) obj);
    }
    if (obj instanceof double[]) {
        return new ASTRealVector((double[]) obj, true);
    // return AST.newInstance(F.List, (double[]) obj);
    }
    if (obj instanceof double[][]) {
        return new ASTRealMatrix((double[][]) obj, true);
    }
    if (obj instanceof org.hipparchus.complex.Complex[]) {
        return F.ast(S.List, (org.hipparchus.complex.Complex[]) obj);
    }
    if (obj instanceof Object[]) {
        final Object[] array = (Object[]) obj;
        int length = array.length;
        final IASTAppendable list = F.ListAlloc(length);
        return list.appendArgs(0, length, i -> convert(array[i], parseString, javaObject));
    }
    if (obj instanceof boolean[]) {
        final boolean[] array = (boolean[]) obj;
        final IASTAppendable list = F.ListAlloc(array.length);
        for (int i = 0; i < array.length; i++) {
            list.append(array[i] ? S.True : S.False);
        }
        return list;
    }
    if (obj instanceof LocalDateTime) {
        return DateObjectExpr.newInstance((LocalDateTime) obj);
    }
    if (obj instanceof LocalTime) {
        return TimeObjectExpr.newInstance((LocalTime) obj);
    }
    if (javaObject) {
        return JavaObjectExpr.newInstance(obj);
    }
    return F.$str(obj.toString());
}
Also used : LocalDateTime(java.time.LocalDateTime) ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) ASTRealVector(org.matheclipse.core.expression.ASTRealVector) LocalTime(java.time.LocalTime) ExprParser(org.matheclipse.core.parser.ExprParser) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

ASTRealMatrix (org.matheclipse.core.expression.ASTRealMatrix)10 ASTRealVector (org.matheclipse.core.expression.ASTRealVector)8 IExpr (org.matheclipse.core.interfaces.IExpr)6 IAST (org.matheclipse.core.interfaces.IAST)4 RealMatrix (org.hipparchus.linear.RealMatrix)3 RealVector (org.hipparchus.linear.RealVector)3 ASTSeriesData (org.matheclipse.core.expression.ASTSeriesData)2 IInteger (org.matheclipse.core.interfaces.IInteger)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)2 IntList (it.unimi.dsi.fastutil.ints.IntList)1 IOException (java.io.IOException)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 Array2DRowRealMatrix (org.hipparchus.linear.Array2DRowRealMatrix)1 VariablesSet (org.matheclipse.core.convert.VariablesSet)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)1 IAssociation (org.matheclipse.core.interfaces.IAssociation)1 IComplex (org.matheclipse.core.interfaces.IComplex)1