Search in sources :

Example 1 with Array2DRowFieldMatrix

use of org.hipparchus.linear.Array2DRowFieldMatrix in project symja_android_library by axkr.

the class Convert method list2Matrix.

/**
 * Return the augmented FieldMatrix <code>listMatrix | listVector</code>
 *
 * @param listMatrix
 * @param listVector
 * @return
 * @throws ClassCastException
 * @throws IndexOutOfBoundsException
 */
public static FieldMatrix<IExpr> list2Matrix(final IAST listMatrix, final IAST listVector) throws ClassCastException, IndexOutOfBoundsException {
    if (listMatrix == null || listVector == null) {
        return null;
    }
    if (!listMatrix.isList() || !listVector.isList()) {
        return null;
    }
    if (listMatrix.size() != listVector.size()) {
        return null;
    }
    IAST currInRow = (IAST) listMatrix.arg1();
    if (currInRow.isAST0()) {
        // special case 0-Matrix
        IExpr[][] array = new IExpr[0][0];
        return new Array2DRowFieldMatrix<IExpr>(array, false);
    }
    final int rowSize = listMatrix.argSize();
    final int colSize = currInRow.argSize();
    final IExpr[][] elements = new IExpr[rowSize][colSize + 1];
    for (int i = 1; i < rowSize + 1; i++) {
        currInRow = (IAST) listMatrix.get(i);
        if (currInRow.head() != S.List || colSize != currInRow.argSize()) {
            return null;
        }
        for (int j = 1; j < colSize + 1; j++) {
            elements[i - 1][j - 1] = currInRow.get(j);
        }
        elements[i - 1][colSize] = listVector.get(i);
    }
    return new Array2DRowFieldMatrix<IExpr>(elements, false);
}
Also used : Array2DRowFieldMatrix(org.hipparchus.linear.Array2DRowFieldMatrix) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 2 with Array2DRowFieldMatrix

use of org.hipparchus.linear.Array2DRowFieldMatrix in project symja_android_library by axkr.

the class Convert method list2ComplexMatrix.

/**
 * Returns a <code>FieldMatrix<Complex></code> if possible.
 *
 * @param expr
 * @return <code>null</code> if the conversion isn't possible.
 * @throws ClassCastException
 * @throws IndexOutOfBoundsException
 */
public static FieldMatrix<Complex> list2ComplexMatrix(IExpr expr) throws ClassCastException, IndexOutOfBoundsException {
    if (expr == null) {
        return null;
    }
    int[] dim = expr.isMatrix();
    if (dim == null || dim[0] == 0 || dim[1] == 0) {
        return null;
    }
    if (expr.isSparseArray()) {
        // TODO optimize for sparse arrays
        // ISparseArray array = (ISparseArray) expr;
        expr = ((ISparseArray) expr).normal(false);
    }
    if (expr.isList()) {
        try {
            IAST list = (IAST) expr;
            IAST currInRow = (IAST) list.arg1();
            if (currInRow.isAST0()) {
                // special case 0-Matrix
                Complex[][] array = new Complex[0][0];
                return new Array2DRowFieldMatrix<Complex>(array, false);
            }
            final int rowSize = expr.argSize();
            final int colSize = currInRow.argSize();
            final Complex[][] elements = new Complex[rowSize][colSize];
            for (int i = 1; i < rowSize + 1; i++) {
                currInRow = (IAST) list.get(i);
                if (currInRow.isVector() < 0 || colSize != currInRow.argSize()) {
                    return null;
                }
                for (int j = 1; j < colSize + 1; j++) {
                    elements[i - 1][j - 1] = currInRow.get(j).evalComplex();
                }
            }
            return new Array2DRowFieldMatrix<Complex>(elements, false);
        } catch (ValidateException vex) {
        // pass
        }
    }
    return null;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) Array2DRowFieldMatrix(org.hipparchus.linear.Array2DRowFieldMatrix) IAST(org.matheclipse.core.interfaces.IAST) Complex(org.hipparchus.complex.Complex)

Aggregations

Array2DRowFieldMatrix (org.hipparchus.linear.Array2DRowFieldMatrix)2 IAST (org.matheclipse.core.interfaces.IAST)2 Complex (org.hipparchus.complex.Complex)1 ValidateException (org.matheclipse.core.eval.exception.ValidateException)1 IExpr (org.matheclipse.core.interfaces.IExpr)1