Search in sources :

Example 16 with Type

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

Example 17 with Type

use of gov.sandia.n2a.language.Type 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)

Example 18 with Type

use of gov.sandia.n2a.language.Type in project n2a by frothga.

the class OR method simplify.

public Operator simplify(Variable from) {
    Operator result = super.simplify(from);
    if (result != this)
        return result;
    if (operand0 instanceof Constant) {
        Type c0 = ((Constant) operand0).value;
        if (c0 instanceof Scalar) {
            from.changed = true;
            double value = ((Scalar) c0).value;
            if (value == 0)
                return operand1;
            else
                return new Constant(new Scalar(1));
        }
    } else if (operand1 instanceof Constant) {
        Type c1 = ((Constant) operand1).value;
        if (c1 instanceof Scalar) {
            from.changed = true;
            double value = ((Scalar) c1).value;
            if (value == 0)
                return operand0;
            else
                return new Constant(new Scalar(1));
        }
    }
    return this;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Type(gov.sandia.n2a.language.Type) Constant(gov.sandia.n2a.language.Constant) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 19 with Type

use of gov.sandia.n2a.language.Type in project n2a by frothga.

the class Instance method finishEvent.

/**
 *        Sets any values touched by a zero-delay event as if they were set by the most recent sim cycle.
 *        @param v Implicitly, this is an "externalWrite" variable.
 */
public void finishEvent(Variable v) {
    Type current = get(v);
    Type buffered = getFinal(v);
    switch(v.assignment) {
        case Variable.ADD:
            setFinal(v, current.add(buffered));
            set(v, v.type);
            break;
        case Variable.MULTIPLY:
        case Variable.DIVIDE:
            setFinal(v, current.multiply(buffered));
            if (v.type instanceof Matrix)
                set(v, ((Matrix) v.type).identity());
            else
                set(v, new Scalar(1));
            break;
        case Variable.MIN:
            setFinal(v, current.min(buffered));
            if (v.type instanceof Matrix)
                set(v, ((Matrix) v.type).clear(Double.POSITIVE_INFINITY));
            else
                set(v, new Scalar(Double.POSITIVE_INFINITY));
            break;
        case Variable.MAX:
            setFinal(v, current.max(buffered));
            if (v.type instanceof Matrix)
                set(v, ((Matrix) v.type).clear(Double.NEGATIVE_INFINITY));
            else
                set(v, new Scalar(Double.NEGATIVE_INFINITY));
            break;
        default:
            // REPLACE
            setFinal(v, buffered);
    }
}
Also used : Type(gov.sandia.n2a.language.Type)

Example 20 with Type

use of gov.sandia.n2a.language.Type in project n2a by frothga.

the class Scalar method convert.

/**
 *        General utility: given a string containing a number with units, convert to the scaled SI value.
 */
public static double convert(String expression) {
    try {
        Operator op = Operator.parse(expression);
        double sign = 1;
        if (op instanceof Negate) {
            op = ((Negate) op).operand;
            sign = -1;
        }
        if (!(op instanceof Constant))
            return 0;
        Type result = ((Constant) op).value;
        if (result instanceof Scalar)
            return ((Scalar) result).value * sign;
    } catch (ParseException e) {
    }
    return 0;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Type(gov.sandia.n2a.language.Type) Constant(gov.sandia.n2a.language.Constant) Negate(gov.sandia.n2a.language.operator.Negate) ParseException(gov.sandia.n2a.language.ParseException)

Aggregations

Type (gov.sandia.n2a.language.Type)24 Scalar (gov.sandia.n2a.language.type.Scalar)17 Operator (gov.sandia.n2a.language.Operator)10 Constant (gov.sandia.n2a.language.Constant)9 Variable (gov.sandia.n2a.eqset.Variable)8 AccessVariable (gov.sandia.n2a.language.AccessVariable)7 Matrix (gov.sandia.n2a.language.type.Matrix)6 Text (gov.sandia.n2a.language.type.Text)6 Instance (gov.sandia.n2a.language.type.Instance)5 Simulator (gov.sandia.n2a.backend.internal.Simulator)4 EquationSet (gov.sandia.n2a.eqset.EquationSet)4 MatrixDense (gov.sandia.n2a.language.type.MatrixDense)3 ArrayList (java.util.ArrayList)3 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)2 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)2 ParseException (gov.sandia.n2a.language.ParseException)2 Visitor (gov.sandia.n2a.language.Visitor)2 Random (java.util.Random)2 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)1 Conversion (gov.sandia.n2a.backend.internal.InternalBackendData.Conversion)1