Search in sources :

Example 36 with Matrix

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

the class ReadMatrix method open.

public Matrix open(Instance context) {
    Simulator simulator = Simulator.instance.get();
    // absence of simulator indicates analysis phase, so opening files is unnecessary
    if (simulator == null)
        return null;
    String path = ((Text) operands[0].eval(context)).value;
    Object A = simulator.holders.get(path);
    if (A == null) {
        A = Matrix.factory(simulator.jobDir.resolve(path));
        simulator.holders.put(path, A);
    } else if (!(A instanceof Matrix)) {
        Backend.err.get().println("ERROR: Reopening file as a different resource type.");
        throw new Backend.AbortRun();
    }
    return (Matrix) A;
}
Also used : Backend(gov.sandia.n2a.plugins.extpoints.Backend) Matrix(gov.sandia.n2a.language.type.Matrix) Text(gov.sandia.n2a.language.type.Text) Simulator(gov.sandia.n2a.backend.internal.Simulator)

Example 37 with Matrix

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

the class MatrixSparse method add.

public Matrix add(Matrix that) throws EvaluationException {
    if (that instanceof MatrixSparse) {
        MatrixSparse B = (MatrixSparse) that;
        int w = columns();
        int h = rows();
        int Bw = B.columns();
        MatrixSparse result = new MatrixSparse(h, w);
        for (int c = 0; c < w; c++) {
            HashMap<Integer, Double> rows = data.get(c);
            if (rows == null)
                continue;
            for (Entry<Integer, Double> row : rows.entrySet()) result.set(row.getKey(), c, row.getValue());
        }
        for (int c = 0; c < Bw; c++) {
            HashMap<Integer, Double> rows = B.data.get(c);
            if (rows == null)
                continue;
            for (Entry<Integer, Double> row : rows.entrySet()) {
                int r = row.getKey();
                result.set(r, c, result.get(r, c) + row.getValue());
            }
        }
        return result;
    }
    Matrix B = (Matrix) that;
    int w = columns();
    int h = rows();
    int ow = Math.min(w, B.columns());
    int oh = Math.min(h, B.rows());
    MatrixDense result = new MatrixDense(h, w, emptyValue);
    for (int c = 0; c < w; c++) {
        HashMap<Integer, Double> rows = data.get(c);
        if (rows == null)
            continue;
        for (Entry<Integer, Double> row : rows.entrySet()) result.set(row.getKey(), c, row.getValue());
    }
    for (int c = 0; c < ow; c++) {
        for (int r = 0; r < oh; r++) result.set(r, c, result.get(r, c) + B.get(r, c));
    }
    return result;
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix)

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