use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Comparison method simplify.
public Operator simplify(Variable from) {
Operator result = super.simplify(from);
if (result != this)
return result;
// In this case, our value can be constant, even if the operands themselves are not.
if (// This method is crude, but should be sufficient for simple cases.
operand0.render().equals(operand1.render())) {
from.changed = true;
releaseDependencies(from);
operand0 = operand1 = new Constant(new Scalar(0));
return new Constant(eval(null));
}
return this;
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Constant method getOperandsFrom.
@SuppressWarnings("unchecked")
public void getOperandsFrom(SimpleNode node) {
Object o = node.jjtGetValue();
if (o instanceof UnitValue) {
unitValue = (UnitValue) node.jjtGetValue();
Unit<?> unit = unitValue.unit;
if (// naked number, so assume already in SI
unit == null) {
value = new Scalar(unitValue.value);
} else // there was a unit given, so convert
{
@SuppressWarnings("rawtypes") Unit si = unit.getSystemUnit();
value = new Scalar(unit.getConverterTo(si).convert(unitValue.value));
}
} else {
value = (Type) o;
}
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Input method eval.
public Type eval(Instance context) {
Type op1 = operands[1].eval(context);
String mode = "";
if (operands.length > 3)
mode = ((Text) operands[3].eval(context)).value;
else if (op1 instanceof Text)
mode = ((Text) op1).value;
boolean time = mode.contains("time");
Holder H = getRow(context, op1, time);
if (H == null)
return new Scalar(0);
if (mode.contains("columns")) {
int result = H.columnCount;
if (time)
result = Math.max(0, result - 1);
return new Scalar(result);
}
double column;
Type columnSpec = operands[2].eval(context);
if (columnSpec instanceof Text) {
Integer columnMapping = H.columnMap.get(((Text) columnSpec).value);
if (columnMapping == null)
return new Scalar(0);
// If it's in the column map, we can safely assume that the index is in range.
return new Scalar(H.currentValues[columnMapping]);
} else // just assume it is a Scalar
{
column = ((Scalar) columnSpec).value;
}
int columns = H.currentValues.length;
int lastColumn = columns - 1;
if (mode.contains("raw")) {
int c = (int) Math.round(column);
// time column is not included in raw index
if (time && c >= H.timeColumn)
c++;
if (c < 0)
c = 0;
else if (c >= columns)
c = lastColumn;
return new Scalar(H.currentValues[c]);
} else {
if (// time column is not included in interpolation
time)
// time column is not included in interpolation
column *= (lastColumn - 1);
else
column *= lastColumn;
int c = (int) Math.floor(column);
double b = column - c;
int d = c + 1;
if (time) {
// Implicitly, d will also be >= timeColumn.
if (c >= H.timeColumn)
c++;
if (d >= H.timeColumn)
d++;
}
if (c < 0) {
if (time && H.timeColumn == 0 && H.currentValues.length > 1)
return new Scalar(H.currentValues[1]);
return new Scalar(H.currentValues[0]);
}
if (c >= lastColumn) {
if (time && H.timeColumn == lastColumn && H.currentValues.length > 1)
return new Scalar(H.currentValues[lastColumn - 1]);
return new Scalar(H.currentValues[lastColumn]);
}
return new Scalar((1 - b) * H.currentValues[c] + b * H.currentValues[d]);
}
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Norm method eval.
public Type eval(Instance context) {
double n = ((Scalar) operands[0].eval(context)).value;
Matrix A = (Matrix) operands[1].eval(context);
return new Scalar(A.norm(n));
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Uniform method eval.
public Type eval(Instance context) throws EvaluationException {
Random random;
Simulator simulator = Simulator.getSimulator(context);
if (simulator == null)
random = new Random();
else
random = simulator.random;
if (operands.length == 0)
return new Scalar(random.nextDouble());
Type sigma = operands[0].eval(context);
if (sigma instanceof Scalar) {
return new Scalar(random.nextDouble() * ((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.nextDouble() * 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.nextDouble() * scale.get(0, i));
return result;
} else {
Matrix temp = new MatrixDense(columns, 1);
for (int i = 0; i < columns; i++) temp.set(i, random.nextDouble());
return sigma.multiply(temp);
}
} else {
// We could throw an exception, but this is easy enough.
return new Scalar(random.nextDouble());
}
}
Aggregations