Search in sources :

Example 11 with MatrixDense

use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.

the class Matrix method LE.

public Matrix LE(Type that) throws EvaluationException {
    int w = columns();
    int h = rows();
    if (that instanceof Scalar) {
        double b = ((Scalar) that).value;
        MatrixDense result = new MatrixDense(h, w);
        for (int c = 0; c < w; c++) {
            for (int r = 0; r < h; r++) {
                result.set(r, c, (get(r, c) <= b) ? 1 : 0);
            }
        }
        return result;
    }
    if (that instanceof Matrix) {
        Matrix B = (Matrix) that;
        w = Math.min(w, B.columns());
        h = Math.min(h, B.rows());
        MatrixDense result = new MatrixDense(h, w);
        for (int c = 0; c < w; c++) {
            for (int r = 0; r < h; r++) {
                result.set(r, c, (get(r, c) <= B.get(r, c)) ? 1 : 0);
            }
        }
        return result;
    }
    throw new EvaluationException("type mismatch");
}
Also used : MatrixDense(gov.sandia.n2a.linear.MatrixDense) EvaluationException(gov.sandia.n2a.language.EvaluationException)

Example 12 with MatrixDense

use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.

the class Matrix method AND.

public Matrix AND(Type that) throws EvaluationException {
    int w = columns();
    int h = rows();
    if (that instanceof Scalar) {
        double b = ((Scalar) that).value;
        MatrixDense result = new MatrixDense(h, w);
        for (int c = 0; c < w; c++) {
            for (int r = 0; r < h; r++) {
                result.set(r, c, (get(r, c) * b != 0) ? 1 : 0);
            }
        }
        return result;
    }
    if (that instanceof Matrix) {
        Matrix B = (Matrix) that;
        w = Math.min(w, B.columns());
        h = Math.min(h, B.rows());
        MatrixDense result = new MatrixDense(h, w);
        for (int c = 0; c < w; c++) {
            for (int r = 0; r < h; r++) {
                result.set(r, c, (get(r, c) * B.get(r, c) != 0) ? 1 : 0);
            }
        }
        return result;
    }
    throw new EvaluationException("type mismatch");
}
Also used : MatrixDense(gov.sandia.n2a.linear.MatrixDense) EvaluationException(gov.sandia.n2a.language.EvaluationException)

Example 13 with MatrixDense

use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.

the class Matrix method NOT.

public Type NOT() throws EvaluationException {
    int w = columns();
    int h = rows();
    // should create pseudo-inverse
    if (h != w)
        throw new EvaluationException("Can't invert non-square matrix (because we don't know about pseudo-inverse).");
    if (h == 1)
        return new Scalar(1 / get(0, 0));
    if (h == 2) {
        MatrixDense result = new MatrixDense(2, 2);
        double q = determinant();
        if (q == 0)
            throw new EvaluationException("invert: Matrix is singular!");
        result.set(0, 0, get(1, 1) / q);
        result.set(1, 0, get(1, 0) / -q);
        result.set(0, 1, get(0, 1) / -q);
        result.set(1, 1, get(0, 0) / q);
        return result;
    }
    if (h == 3) {
        MatrixDense result = new MatrixDense(3, 3);
        double q = determinant();
        if (q == 0)
            throw new EvaluationException("invert: Matrix is singular!");
        result.set(0, 0, det22(1, 2, 1, 2) / q);
        result.set(1, 0, det22(1, 2, 2, 0) / q);
        result.set(2, 0, det22(1, 2, 0, 1) / q);
        result.set(0, 1, det22(0, 2, 2, 1) / q);
        result.set(1, 1, det22(0, 2, 0, 2) / q);
        result.set(2, 1, det22(0, 2, 1, 0) / q);
        result.set(0, 2, det22(0, 1, 1, 2) / q);
        result.set(1, 2, det22(0, 1, 2, 0) / q);
        result.set(2, 2, det22(0, 1, 0, 1) / q);
        return result;
    }
    throw new EvaluationException("Can't invert matrices larger then 3x3 (because we are not using a good numerical library).");
}
Also used : MatrixDense(gov.sandia.n2a.linear.MatrixDense) EvaluationException(gov.sandia.n2a.language.EvaluationException)

Example 14 with MatrixDense

use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.

the class Matrix method NE.

public Type NE(Type that) throws EvaluationException {
    if (that instanceof Matrix)
        return new Scalar(compareTo(that) != 0 ? 1 : 0);
    if (that instanceof Scalar) {
        double b = ((Scalar) that).value;
        int w = columns();
        int h = rows();
        MatrixDense result = new MatrixDense(h, w);
        for (int c = 0; c < w; c++) {
            for (int r = 0; r < h; r++) {
                result.set(r, c, (get(r, c) != b) ? 1 : 0);
            }
        }
        return result;
    }
    throw new EvaluationException("type mismatch");
}
Also used : MatrixDense(gov.sandia.n2a.linear.MatrixDense) EvaluationException(gov.sandia.n2a.language.EvaluationException)

Example 15 with MatrixDense

use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.

the class Matrix method GE.

public Matrix GE(Type that) throws EvaluationException {
    int w = columns();
    int h = rows();
    if (that instanceof Scalar) {
        double b = ((Scalar) that).value;
        MatrixDense result = new MatrixDense(h, w);
        for (int c = 0; c < w; c++) {
            for (int r = 0; r < h; r++) {
                result.set(r, c, (get(r, c) >= b) ? 1 : 0);
            }
        }
        return result;
    }
    if (that instanceof Matrix) {
        Matrix B = (Matrix) that;
        w = Math.min(w, B.columns());
        h = Math.min(h, B.rows());
        MatrixDense result = new MatrixDense(h, w);
        for (int c = 0; c < w; c++) {
            for (int r = 0; r < h; r++) {
                result.set(r, c, (get(r, c) >= B.get(r, c)) ? 1 : 0);
            }
        }
        return result;
    }
    throw new EvaluationException("type mismatch");
}
Also used : MatrixDense(gov.sandia.n2a.linear.MatrixDense) EvaluationException(gov.sandia.n2a.language.EvaluationException)

Aggregations

MatrixDense (gov.sandia.n2a.linear.MatrixDense)36 EvaluationException (gov.sandia.n2a.language.EvaluationException)24 Matrix (gov.sandia.n2a.language.type.Matrix)7 Scalar (gov.sandia.n2a.language.type.Scalar)7 Type (gov.sandia.n2a.language.Type)5 Text (gov.sandia.n2a.language.type.Text)4 Simulator (gov.sandia.n2a.backend.internal.Simulator)3 Random (java.util.Random)3 Variable (gov.sandia.n2a.eqset.Variable)1 Constant (gov.sandia.n2a.language.Constant)1 Operator (gov.sandia.n2a.language.Operator)1 ASTConstant (gov.sandia.n2a.language.parse.ASTConstant)1 FactorQR (gov.sandia.n2a.linear.FactorQR)1 MatrixSparse (gov.sandia.n2a.linear.MatrixSparse)1 OutputParser (gov.sandia.n2a.ui.jobs.OutputParser)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1