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;
}
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;
}
Aggregations