use of gov.sandia.n2a.backend.internal.Simulator 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