Search in sources :

Example 6 with Simulator

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());
    }
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) Random(java.util.Random) MatrixDense(gov.sandia.n2a.language.type.MatrixDense) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Aggregations

Simulator (gov.sandia.n2a.backend.internal.Simulator)6 Scalar (gov.sandia.n2a.language.type.Scalar)5 Type (gov.sandia.n2a.language.Type)4 Matrix (gov.sandia.n2a.language.type.Matrix)3 Text (gov.sandia.n2a.language.type.Text)3 MatrixDense (gov.sandia.n2a.language.type.MatrixDense)2 File (java.io.File)2 Random (java.util.Random)2 EventStep (gov.sandia.n2a.backend.internal.EventStep)1 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)1 EquationSet (gov.sandia.n2a.eqset.EquationSet)1 HostSystem (gov.sandia.n2a.execenvs.HostSystem)1 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1