use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class EquationSet method isSingleton.
/**
* Determines if this equation set has a fixed size of 1.
*/
public boolean isSingleton() {
Variable n = find(new Variable("$n", 0));
// We only do more work if $n exists. Non-existent $n is the same as $n==1
if (n == null)
return true;
// make sure no other orders of $n exist
Variable n2 = variables.higher(n);
// higher orders means $n is dynamic
if (n2.name.equals("$n"))
return false;
// check contents of $n
if (n.assignment != Variable.REPLACE)
return false;
if (n.equations.size() != 1)
return false;
EquationEntry ne = n.equations.first();
// If we can't evaluate $n, then we treat it as 1
if (ne.expression == null)
return true;
Instance bypass = new Instance() {
public Type get(VariableReference r) throws EvaluationException {
// we evaluate $n in init cycle
if (r.variable.name.equals("$init"))
return new Scalar(1);
// During init all other vars are 0, even if they have an initialization conditioned on $init. IE: those values won't be seen until after the init cycle.
return new Scalar(0);
}
};
Type value = n.eval(bypass);
if (value instanceof Scalar && ((Scalar) value).value != 1)
return false;
// Notice that we could fall through if $n is not a Scalar. That is an error, but since $n can't be evaluated properly, we treat is as 1.
return true;
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Variable method simplify.
public boolean simplify() {
if (equations == null)
return false;
changed = false;
TreeSet<EquationEntry> nextEquations = new TreeSet<EquationEntry>();
TreeSet<EquationEntry> alwaysTrue = new TreeSet<EquationEntry>();
for (EquationEntry e : equations) {
if (e.expression != null) {
e.expression = e.expression.simplify(this);
}
if (e.condition != null) {
e.condition = e.condition.simplify(this);
e.ifString = e.condition.render();
}
if (e.condition instanceof Constant) {
Constant c = (Constant) e.condition;
if (c.value instanceof Scalar) {
double value = ((Scalar) c.value).value;
if (value == 0) {
// Drop equation because it will never fire.
changed = true;
continue;
}
alwaysTrue.add(e);
}
} else if (e.ifString.isEmpty() && e.expression instanceof AccessVariable && ((AccessVariable) e.expression).reference.variable == this) {
// Drop this default equation because it is redundant. Simulator always copies value to next cycle when no equation fires.
changed = true;
continue;
}
nextEquations.add(e);
}
if (// Default equation will never be included in alwaysTrue.
alwaysTrue.size() == 1) {
changed = true;
equations = alwaysTrue;
// Make the equation unconditional, since it always fires anyway.
EquationEntry e = equations.first();
e.condition = null;
e.ifString = "";
} else {
equations = nextEquations;
}
return changed;
}
use of gov.sandia.n2a.language.type.Scalar 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.Scalar in project n2a by frothga.
the class AccessElement method eval.
public Type eval(Instance instance) {
Matrix A = (Matrix) operands[0].eval(instance);
int row = (int) ((Scalar) operands[1].eval(instance)).value;
int column = 0;
if (operands.length > 2)
column = (int) ((Scalar) operands[2].eval(instance)).value;
return new Scalar(A.get(row, column));
}
use of gov.sandia.n2a.language.type.Scalar 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;
}
Aggregations