Search in sources :

Example 6 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class MatrixDense method add.

public MatrixDense add(Matrix that) throws EvaluationException {
    if (that instanceof MatrixDense) {
        MatrixDense B = (MatrixDense) that;
        int oh = Math.min(rows, B.rows);
        int ow = Math.min(columns, B.columns);
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int stepB = B.strideC - oh * B.strideR;
        int a = offset;
        int b = B.offset;
        int r = 0;
        int end = rows * ow;
        while (r < end) {
            int overlapEnd = r + oh;
            int columnEnd = r + rows;
            while (r < overlapEnd) {
                result.data[r++] = data[a] + B.data[b];
                a += strideR;
                b += B.strideR;
            }
            while (r < columnEnd) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
            b += stepB;
        }
        end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    Matrix B = (Matrix) that;
    int oh = Math.min(rows, B.rows());
    int ow = Math.min(columns, B.columns());
    MatrixDense result = new MatrixDense(rows, columns);
    int stepA = strideC - rows * strideR;
    int a = offset;
    int r = 0;
    for (int col = 0; col < ow; col++) {
        int row = 0;
        for (; row < oh; row++) {
            result.data[r++] = data[a] + B.get(row, col);
            a += strideR;
        }
        for (; row < rows; row++) {
            result.data[r++] = data[a];
            a += strideR;
        }
        a += stepA;
    }
    for (int col = ow; col < columns; col++) {
        for (int row = 0; row < rows; row++) {
            result.data[r++] = data[a];
            a += strideR;
        }
        a += stepA;
    }
    return result;
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix)

Example 7 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class MatrixDense method subtract.

public MatrixDense subtract(Type that) throws EvaluationException {
    if (that instanceof MatrixDense) {
        MatrixDense B = (MatrixDense) that;
        int oh = Math.min(rows, B.rows);
        int ow = Math.min(columns, B.columns);
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int stepB = B.strideC - oh * B.strideR;
        int a = offset;
        int b = B.offset;
        int r = 0;
        int end = rows * ow;
        while (r < end) {
            int overlapEnd = r + oh;
            int columnEnd = r + rows;
            while (r < overlapEnd) {
                result.data[r++] = data[a] - B.data[b];
                a += strideR;
                b += B.strideR;
            }
            while (r < columnEnd) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
            b += stepB;
        }
        end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    if (that instanceof Matrix) {
        Matrix B = (Matrix) that;
        int oh = Math.min(rows, B.rows());
        int ow = Math.min(columns, B.columns());
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int a = offset;
        int r = 0;
        for (int col = 0; col < ow; col++) {
            int row = 0;
            for (; row < oh; row++) {
                result.data[r++] = data[a] - B.get(row, col);
                a += strideR;
            }
            for (; row < rows; row++) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
        }
        for (int col = ow; col < columns; col++) {
            for (int row = 0; row < rows; row++) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    if (that instanceof Scalar) {
        double scalar = ((Scalar) that).value;
        MatrixDense result = new MatrixDense(rows, columns);
        int step = strideC - rows * strideR;
        int i = offset;
        int r = 0;
        int end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = data[i] - scalar;
                i += strideR;
            }
            i += step;
        }
        return result;
    }
    throw new EvaluationException("type mismatch");
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) EvaluationException(gov.sandia.n2a.language.EvaluationException) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 8 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class UnitMap method eval.

public Type eval(Instance context) {
    Matrix A = (Matrix) operands[0].eval(context);
    double r = ((Scalar) operands[1].eval(context)).value;
    double c = 0.5;
    if (operands.length > 2) {
        Type op2 = operands[2].eval(context);
        if (op2 instanceof Scalar)
            c = ((Scalar) op2).value;
    }
    return new Scalar(A.get(r, c, Matrix.UNITMAP));
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 9 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class AccessElement method simplify.

public Operator simplify(Variable from, boolean evalOnly) {
    for (int i = 0; i < operands.length; i++) operands[i] = operands[i].simplify(from, evalOnly);
    if (operands.length == 1) {
        from.changed = true;
        operands[0].parent = parent;
        return operands[0];
    }
    // All operand positions beyond 0 are subscripts, presumably into a matrix at operands[0].
    // Attempt to replace the element access with a constant.
    int row = -1;
    int col = 0;
    if (operands[1] instanceof Constant) {
        Constant c = (Constant) operands[1];
        if (c.value instanceof Scalar)
            row = (int) ((Scalar) c.value).value;
    }
    if (operands.length > 2) {
        col = -1;
        if (operands[2] instanceof Constant) {
            Constant c = (Constant) operands[2];
            if (c.value instanceof Scalar)
                col = (int) ((Scalar) c.value).value;
        }
    }
    if (row < 0 || col < 0)
        return this;
    if (operands[0] instanceof Constant) {
        Constant c = (Constant) operands[0];
        if (c.value instanceof Matrix) {
            from.changed = true;
            Operator result = new Constant(new Scalar(((Matrix) c.value).get(row, col)));
            result.parent = parent;
            return result;
        }
    } else // If not constant (above), then operands[0] should always be an AccessVariable.
    {
        // Try to unpack the target variable and see if the specific element we want is constant
        AccessVariable av = (AccessVariable) operands[0];
        if (av.reference != null && av.reference.variable != null) {
            Variable v = av.reference.variable;
            if (v.equations != null && v.equations.size() == 1) {
                EquationEntry e = v.equations.first();
                if (// Only weird code would have a condition at all.
                e.expression instanceof BuildMatrix && (e.condition == null || e.condition.getDouble() != 0)) {
                    BuildMatrix b = (BuildMatrix) e.expression;
                    Operator element = b.getElement(row, col);
                    if (element != null && element instanceof Constant) {
                        from.changed = true;
                        if (!evalOnly)
                            operands[0].releaseDependencies(from);
                        element.parent = parent;
                        return element;
                    }
                }
            }
        }
    }
    return this;
}
Also used : ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) Matrix(gov.sandia.n2a.language.type.Matrix) Variable(gov.sandia.n2a.eqset.Variable) EquationEntry(gov.sandia.n2a.eqset.EquationEntry) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 10 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class BuildMatrix method eval.

public Type eval(Instance context) throws EvaluationException {
    int columns = operands.length;
    if (columns == 0)
        return new MatrixDense();
    int rows = operands[0].length;
    if (rows == 0)
        return new MatrixDense();
    Matrix result = new MatrixDense(rows, columns);
    for (int c = 0; c < columns; c++) {
        for (int r = 0; r < rows; r++) {
            if (operands[c][r] == null) {
                result.set(r, c, 0);
            } else {
                Type o = operands[c][r].eval(context);
                if (o instanceof Scalar)
                    result.set(r, c, ((Scalar) o).value);
                else if (o instanceof Text)
                    result.set(r, c, Double.valueOf(((Text) o).value));
                else if (o instanceof Matrix)
                    result.set(r, c, ((Matrix) o).get(0, 0));
                else
                    throw new EvaluationException("Can't construct matrix element from the given type.");
            }
        }
    }
    return result;
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) MatrixDense(gov.sandia.n2a.linear.MatrixDense) Text(gov.sandia.n2a.language.type.Text) Scalar(gov.sandia.n2a.language.type.Scalar)

Aggregations

Matrix (gov.sandia.n2a.language.type.Matrix)37 Scalar (gov.sandia.n2a.language.type.Scalar)26 Text (gov.sandia.n2a.language.type.Text)12 Type (gov.sandia.n2a.language.Type)11 Simulator (gov.sandia.n2a.backend.internal.Simulator)9 Operator (gov.sandia.n2a.language.Operator)9 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)8 MatrixDense (gov.sandia.n2a.linear.MatrixDense)7 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)6 Constant (gov.sandia.n2a.language.Constant)6 EvaluationException (gov.sandia.n2a.language.EvaluationException)6 AccessVariable (gov.sandia.n2a.language.AccessVariable)5 Variable (gov.sandia.n2a.eqset.Variable)4 Input (gov.sandia.n2a.language.function.Input)4 Output (gov.sandia.n2a.language.function.Output)4 Add (gov.sandia.n2a.language.operator.Add)4 Function (gov.sandia.n2a.language.Function)3 Event (gov.sandia.n2a.language.function.Event)3 Modulo (gov.sandia.n2a.language.operator.Modulo)3 Random (java.util.Random)3