use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Output method eval.
public Type eval(Instance context) {
Type result = operands[1].eval(context);
Simulator simulator = Simulator.getSimulator(context);
if (simulator == null)
return result;
String path = ((Text) operands[0].eval(context)).value;
Holder H = simulator.outputs.get(path);
if (H == null) {
H = new Holder(simulator, path);
if (operands.length > 3)
H.raw = operands[3].eval(context).toString().contains("raw");
}
// Determine column name
String column;
if (// column name is specified
operands.length > 2) {
column = operands[2].eval(context).toString();
} else // auto-generate column name
{
if (context instanceof InstanceTemporaries)
context = ((InstanceTemporaries) context).wrapped;
column = (String) context.valuesObject[index];
if (column == null) {
String prefix = context.path();
if (prefix.isEmpty())
column = variableName;
else
column = prefix + "." + variableName;
context.valuesObject[index] = column;
}
}
double now;
if (simulator.currentEvent == null)
now = 0;
else
now = (float) simulator.currentEvent.t;
H.trace(now, column, (float) ((Scalar) result).value);
return result;
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class ReadMatrix method eval.
public Type eval(Instance context) {
Simulator simulator = Simulator.getSimulator(context);
// absence of simulator indicates analysis phase, so opening files is unnecessary
if (simulator == null)
return new Scalar(0);
String path = ((Text) operands[0].eval(context)).value;
Matrix A = simulator.matrices.get(path);
if (A == null) {
A = Matrix.factory(new File(path).getAbsoluteFile());
simulator.matrices.put(path, A);
}
String mode = "";
int lastParm = operands.length - 1;
if (lastParm > 0) {
Type parmValue = operands[lastParm].eval(context);
if (parmValue instanceof Text)
mode = ((Text) parmValue).value;
}
if (mode.equals("columns"))
return new Scalar(A.columns());
if (mode.equals("rows"))
return new Scalar(A.rows());
int rows = A.rows();
int columns = A.columns();
int lastRow = rows - 1;
int lastColumn = columns - 1;
double row = ((Scalar) operands[1].eval(context)).value;
double column = ((Scalar) operands[2].eval(context)).value;
if (mode.equals("raw")) {
int r = (int) Math.floor(row);
int c = (int) Math.floor(column);
if (r < 0)
r = 0;
else if (r >= rows)
r = lastRow;
if (c < 0)
c = 0;
else if (c >= columns)
c = lastColumn;
return new Scalar(A.get(r, c));
} else {
row *= lastRow;
column *= lastColumn;
int r = (int) Math.floor(row);
int c = (int) Math.floor(column);
if (r < 0) {
if (c < 0)
return new Scalar(A.get(0, 0));
else if (c >= lastColumn)
return new Scalar(A.get(0, lastColumn));
else {
double b = column - c;
return new Scalar((1 - b) * A.get(0, c) + b * A.get(0, c + 1));
}
} else if (r >= lastRow) {
if (c < 0)
return new Scalar(A.get(lastRow, 0));
else if (c >= lastColumn)
return new Scalar(A.get(lastRow, lastColumn));
else {
double b = column - c;
return new Scalar((1 - b) * A.get(lastRow, c) + b * A.get(lastRow, c + 1));
}
} else {
double a = row - r;
double a1 = 1 - a;
if (c < 0)
return new Scalar(a1 * A.get(r, 0) + a * A.get(r + 1, 0));
else if (c >= lastColumn)
return new Scalar(a1 * A.get(r, lastColumn) + a * A.get(r + 1, lastColumn));
else {
double b = column - c;
return new Scalar((1 - b) * (a1 * A.get(r, c) + a * A.get(r + 1, c)) + b * (a1 * A.get(r, c + 1) + a * A.get(r + 1, c + 1)));
}
}
}
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class AND method simplify.
public Operator simplify(Variable from) {
Operator result = super.simplify(from);
if (result != this)
return result;
if (operand0 instanceof Constant) {
Type c0 = ((Constant) operand0).value;
if (c0 instanceof Scalar) {
from.changed = true;
double value = ((Scalar) c0).value;
if (value == 0) {
operand1.releaseDependencies(from);
return new Constant(new Scalar(0));
}
return operand1;
}
} else if (operand1 instanceof Constant) {
Type c1 = ((Constant) operand1).value;
if (c1 instanceof Scalar) {
from.changed = true;
double value = ((Scalar) c1).value;
if (value == 0) {
operand0.releaseDependencies(from);
return new Constant(new Scalar(0));
}
return operand0;
}
}
return this;
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Multiply method simplify.
public Operator simplify(Variable from) {
Operator result = super.simplify(from);
if (result != this)
return result;
// This will be reversed below if we don't actually make a change.
from.changed = true;
if (operand0 instanceof Constant) {
Type c0 = ((Constant) operand0).value;
if (c0 instanceof Scalar) {
double value = ((Scalar) c0).value;
if (value == 1)
return operand1;
if (value == 0) {
operand1.releaseDependencies(from);
return new Constant(new Scalar(0));
}
}
} else if (operand1 instanceof Constant) {
Type c1 = ((Constant) operand1).value;
if (c1 instanceof Scalar) {
double value = ((Scalar) c1).value;
if (value == 1)
return operand0;
if (value == 0) {
operand0.releaseDependencies(from);
return new Constant(new Scalar(0));
}
}
}
from.changed = false;
return this;
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class MultiplyElementwise method simplify.
public Operator simplify(Variable from) {
Operator result = super.simplify(from);
if (result != this)
return result;
// This will be reversed below if we don't actually make a change.
from.changed = true;
if (operand0 instanceof Constant) {
Type c0 = ((Constant) operand0).value;
if (c0 instanceof Scalar) {
double value = ((Scalar) c0).value;
if (value == 1)
return operand1;
}
} else if (operand1 instanceof Constant) {
Type c1 = ((Constant) operand1).value;
if (c1 instanceof Scalar) {
double value = ((Scalar) c1).value;
if (value == 1)
return operand0;
}
}
from.changed = false;
return this;
}
Aggregations