use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.
the class DrawBlock method eval.
public Type eval(Instance context) {
Simulator simulator = Simulator.instance.get();
if (simulator == null)
return new Scalar(0);
Holder H = getHolder(simulator, context);
double now;
if (simulator.currentEvent == null)
now = 0;
else
now = (float) simulator.currentEvent.t;
Matrix p = (Matrix) operands[1].eval(context);
double x = p.get(0);
double y = p.get(1);
double w = 0;
if (operands.length > 2)
w = ((Scalar) operands[2].eval(context)).value;
double h = 0;
if (operands.length > 3)
h = ((Scalar) operands[3].eval(context)).value;
// white
double color = 0xFFFFFF;
if (operands.length > 4)
color = ((Scalar) operands[4].eval(context)).value;
H.drawBlock(now, x, y, w, h, (int) color);
return new Scalar(0);
}
use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.
the class Gaussian method eval.
public Type eval(Instance context) throws EvaluationException {
Random random;
Simulator simulator = Simulator.instance.get();
if (simulator == null)
random = new Random();
else
random = simulator.random;
if (operands.length == 0)
return new Scalar(random.nextGaussian());
Type sigma = operands[0].eval(context);
if (sigma instanceof Scalar) {
return new Scalar(random.nextGaussian() * ((Scalar) sigma).value);
} else if (sigma instanceof Matrix) {
Matrix scale = (Matrix) sigma;
int rows = scale.rows();
int columns = scale.columns();
if (columns == 1) {
Matrix result = new MatrixDense(rows, 1);
for (int i = 0; i < rows; i++) result.set(i, random.nextGaussian() * scale.get(i, 0));
return result;
} else if (rows == 1) {
Matrix result = new MatrixDense(columns, 1);
for (int i = 0; i < columns; i++) result.set(i, random.nextGaussian() * scale.get(0, i));
return result;
} else {
Matrix temp = new MatrixDense(columns, 1);
for (int i = 0; i < columns; i++) temp.set(i, random.nextGaussian());
return sigma.multiply(temp);
}
} else {
// We could throw an exception, but this is easy enough.
return new Scalar(random.nextGaussian());
}
}
use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.
the class AccessElement method simplify.
public Operator simplify(Variable from) {
for (int i = 0; i < operands.length; i++) operands[i] = operands[i].simplify(from);
if (operands.length == 1) {
from.changed = true;
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;
return new Constant(new Scalar(((Matrix) c.value).get(row, col)));
}
} else {
// 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 (// Ideally, we would also ensure e.condition is satisfied. However, only weird code would have a condition at all.
e.expression instanceof BuildMatrix) {
BuildMatrix b = (BuildMatrix) e.expression;
Operator element = b.getElement(row, col);
if (element != null && element instanceof Constant) {
from.changed = true;
e.expression.releaseDependencies(from);
if (e.condition != null)
e.condition.releaseDependencies(from);
return element;
}
}
}
}
}
return this;
}
use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.
the class BuildMatrix method simplify.
public Operator simplify(Variable from) {
int cols = operands.length;
if (cols == 0)
return this;
int rows = operands[0].length;
if (rows == 0)
return this;
// potential constant to replace us
Matrix A = new MatrixDense(rows, cols);
// any element that is not constant will change this to false
boolean isConstant = true;
for (int c = 0; c < cols; c++) {
for (int r = 0; r < rows; r++) {
if (operands[c][r] == null) {
A.set(r, c, 0);
} else {
operands[c][r] = operands[c][r].simplify(from);
if (// stop evaluating if we already know we are not constant
isConstant) {
if (operands[c][r] instanceof Constant) {
Type o = ((Constant) operands[c][r]).value;
if (o instanceof Scalar)
A.set(r, c, ((Scalar) o).value);
else if (o instanceof Text)
A.set(r, c, Double.valueOf(((Text) o).value));
else if (o instanceof Matrix)
A.set(r, c, ((Matrix) o).get(0, 0));
else
throw new EvaluationException("Can't construct matrix element from the given type.");
} else {
isConstant = false;
}
}
}
}
}
if (isConstant) {
from.changed = true;
return new Constant(A);
}
return this;
}
use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.
the class AccessElement method eval.
public Type eval(Instance instance) {
Matrix A = (Matrix) operands[0].eval(instance);
double row = ((Scalar) operands[1].eval(instance)).value;
double column = 0;
if (operands.length > 2)
column = ((Scalar) operands[2].eval(instance)).value;
// This access function does bounds check.
return new Scalar(A.get(row, column, Matrix.INTERPOLATE));
}
Aggregations