Search in sources :

Example 11 with Matrix

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

the class Output method eval.

public Type eval(Instance context) {
    Type result = operands[1].eval(context);
    Simulator simulator = Simulator.instance.get();
    if (simulator == null)
        return result;
    String mode = null;
    if (operands.length > 3)
        mode = operands[3].eval(context).toString();
    String path = ((Text) operands[0].eval(context)).value;
    boolean raw = mode != null && mode.contains("raw");
    Holder H = Holder.get(simulator, path, raw);
    String column = getColumnName(context);
    double now;
    if (simulator.currentEvent == null)
        now = 0;
    else
        now = (float) simulator.currentEvent.t;
    if (result instanceof Matrix) {
        Matrix A = (Matrix) result;
        int rows = A.rows();
        int cols = A.columns();
        if (rows == 1) {
            for (int c = 0; c < cols; c++) H.trace(now, column + "(" + c + ")", (float) A.get(0, c), mode);
        } else if (cols == 1) {
            for (int r = 0; r < rows; r++) H.trace(now, column + "(" + r + ")", (float) A.get(r, 0), mode);
        } else {
            for (int r = 0; r < rows; r++) {
                for (int c = 0; c < cols; c++) {
                    H.trace(now, column + "(" + r + "," + c + ")", (float) A.get(r, c), mode);
                }
            }
        }
    } else {
        H.trace(now, column, (float) ((Scalar) result).value, mode);
    }
    return result;
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) Text(gov.sandia.n2a.language.type.Text) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 12 with Matrix

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

the class DrawDisc 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 radius = 0;
    if (operands.length > 2)
        radius = ((Scalar) operands[2].eval(context)).value;
    // white
    double color = 0xFFFFFF;
    if (operands.length > 3)
        color = ((Scalar) operands[3].eval(context)).value;
    H.drawDisc(now, x, y, radius, (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 13 with Matrix

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

the class Grid method eval.

public Type eval(Instance context) throws EvaluationException {
    // collect parameters into arrays
    int i = (int) Math.floor(((Scalar) operands[0].eval(context)).value);
    int nx = 1;
    int ny = 1;
    int nz = 1;
    boolean raw = false;
    if (operands.length >= 2)
        nx = (int) Math.floor(((Scalar) operands[1].eval(context)).value);
    if (operands.length >= 3)
        ny = (int) Math.floor(((Scalar) operands[2].eval(context)).value);
    if (operands.length >= 4)
        nz = (int) Math.floor(((Scalar) operands[3].eval(context)).value);
    if (operands.length >= 5) {
        Type mode = operands[4].eval(context);
        if (mode instanceof Text && ((Text) mode).value.contains("raw"))
            raw = true;
    }
    // compute xyz in stride order
    Matrix result = new MatrixDense(3, 1);
    // stride x
    int sx = ny * nz;
    if (raw) {
        // (i / sx) is an integer operation, so remainder is truncated.
        result.set(0, i / sx);
        i %= sx;
        result.set(1, i / nz);
        result.set(2, i % nz);
    } else {
        result.set(0, ((i / sx) + 0.5) / nx);
        i %= sx;
        result.set(1, ((i / nz) + 0.5) / ny);
        result.set(2, ((i % nz) + 0.5) / nz);
    }
    return result;
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) MatrixDense(gov.sandia.n2a.linear.MatrixDense) Text(gov.sandia.n2a.language.type.Text) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 14 with Matrix

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

the class Norm method determineExponent.

public void determineExponent(ExponentContext context) {
    // A
    Operator op0 = operands[0];
    op0.determineExponent(context);
    // n
    Operator op1 = null;
    if (operands.length > 1) {
        op1 = operands[1];
        op1.determineExponent(context);
    }
    Instance instance = new Instance() {

        // all AccessVariable objects will reach here first, and get back the Variable.type field
        public Type get(VariableReference r) throws EvaluationException {
            return r.variable.type;
        }
    };
    Matrix A = (Matrix) op0.eval(instance);
    int Asize = A.rows() * A.columns();
    int centerNew = center;
    int exponentNew = exponent;
    if (op0.exponent != UNKNOWN) {
        // For n==1 (sum of elements), which is the most expensive in terms of bits.
        int shift = (int) Math.floor(Math.log(Asize) / Math.log(2));
        // Don't shift center into negative territory.
        shift = Math.min(op0.center, shift);
        centerNew = op0.center - shift;
        exponentNew = op0.exponent + shift;
    }
    if (op1 instanceof Constant) {
        double n = op1.getDouble();
        if (n == 0) {
            // Result is an integer
            centerNew = 0;
            exponentNew = MSB;
        } else if (Double.isInfinite(n)) {
            centerNew = op0.center;
            exponentNew = op0.exponent;
        }
    // It would be nice to have some way to interpolate between the 3 bounding cases.
    }
    updateExponent(context, exponentNew, centerNew);
}
Also used : Operator(gov.sandia.n2a.language.Operator) Matrix(gov.sandia.n2a.language.type.Matrix) VariableReference(gov.sandia.n2a.eqset.VariableReference) Instance(gov.sandia.n2a.language.type.Instance) Constant(gov.sandia.n2a.language.Constant)

Example 15 with Matrix

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

the class Atan method determineExponentNext.

public void determineExponentNext() {
    Operator y = operands[0];
    int next;
    if (operands.length == 1) {
        if (y.getType() instanceof Matrix) {
            next = y.exponent;
        } else {
            // atan(y) = atan2(y,1), so treat x as 1
            // If y is so small that all its bits are lost, then angle can be treated as zero.
            // If y is so large that all the bits of x are lost, then angle can be treated as pi.
            next = Math.max(0, y.exponent);
        }
    } else {
        Operator x = operands[1];
        next = Math.max(x.exponent, y.exponent);
        x.exponentNext = next;
        x.determineExponentNext();
    }
    y.exponentNext = next;
    y.determineExponentNext();
}
Also used : Operator(gov.sandia.n2a.language.Operator) Matrix(gov.sandia.n2a.language.type.Matrix)

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