Search in sources :

Example 16 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class DrawBlock method eval.

public Type eval(Instance context) {
    Simulator simulator = Simulator.instance.get();
    if (simulator == null)
        return new Scalar(0);
    Holder H = getHolder(simulator, context);
    double now;
    if (simulator.currentEvent == null)
        now = 0;
    else
        now = (float) simulator.currentEvent.t;
    Matrix p = (Matrix) operands[1].eval(context);
    double x = p.get(0);
    double y = p.get(1);
    double w = 0;
    if (operands.length > 2)
        w = ((Scalar) operands[2].eval(context)).value;
    double h = 0;
    if (operands.length > 3)
        h = ((Scalar) operands[3].eval(context)).value;
    // white
    double color = 0xFFFFFF;
    if (operands.length > 4)
        color = ((Scalar) operands[4].eval(context)).value;
    H.drawBlock(now, x, y, w, h, (int) color);
    return new Scalar(0);
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 17 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class Gaussian method eval.

public Type eval(Instance context) throws EvaluationException {
    Random random;
    Simulator simulator = Simulator.instance.get();
    if (simulator == null)
        random = new Random();
    else
        random = simulator.random;
    if (operands.length == 0)
        return new Scalar(random.nextGaussian());
    Type sigma = operands[0].eval(context);
    if (sigma instanceof Scalar) {
        return new Scalar(random.nextGaussian() * ((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.nextGaussian() * 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.nextGaussian() * scale.get(0, i));
            return result;
        } else {
            Matrix temp = new MatrixDense(columns, 1);
            for (int i = 0; i < columns; i++) temp.set(i, random.nextGaussian());
            return sigma.multiply(temp);
        }
    } else {
        // We could throw an exception, but this is easy enough.
        return new Scalar(random.nextGaussian());
    }
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) Random(java.util.Random) MatrixDense(gov.sandia.n2a.linear.MatrixDense) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 18 with Matrix

use of gov.sandia.n2a.language.type.Matrix 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;
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) Variable(gov.sandia.n2a.eqset.Variable) EquationEntry(gov.sandia.n2a.eqset.EquationEntry) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 19 with Matrix

use of gov.sandia.n2a.language.type.Matrix 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;
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) ASTConstant(gov.sandia.n2a.language.parse.ASTConstant) MatrixDense(gov.sandia.n2a.language.type.MatrixDense) Text(gov.sandia.n2a.language.type.Text) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 20 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class AccessElement method eval.

public Type eval(Instance instance) {
    Matrix A = (Matrix) operands[0].eval(instance);
    double row = ((Scalar) operands[1].eval(instance)).value;
    double column = 0;
    if (operands.length > 2)
        column = ((Scalar) operands[2].eval(instance)).value;
    // This access function does bounds check.
    return new Scalar(A.get(row, column, Matrix.INTERPOLATE));
}
Also used : ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) Matrix(gov.sandia.n2a.language.type.Matrix) Scalar(gov.sandia.n2a.language.type.Scalar)

Aggregations

Matrix (gov.sandia.n2a.language.type.Matrix)37 Scalar (gov.sandia.n2a.language.type.Scalar)26 Text (gov.sandia.n2a.language.type.Text)12 Type (gov.sandia.n2a.language.Type)11 Simulator (gov.sandia.n2a.backend.internal.Simulator)9 Operator (gov.sandia.n2a.language.Operator)9 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)8 MatrixDense (gov.sandia.n2a.linear.MatrixDense)7 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)6 Constant (gov.sandia.n2a.language.Constant)6 EvaluationException (gov.sandia.n2a.language.EvaluationException)6 AccessVariable (gov.sandia.n2a.language.AccessVariable)5 Variable (gov.sandia.n2a.eqset.Variable)4 Input (gov.sandia.n2a.language.function.Input)4 Output (gov.sandia.n2a.language.function.Output)4 Add (gov.sandia.n2a.language.operator.Add)4 Function (gov.sandia.n2a.language.Function)3 Event (gov.sandia.n2a.language.function.Event)3 Modulo (gov.sandia.n2a.language.operator.Modulo)3 Random (java.util.Random)3