Search in sources :

Example 31 with Scalar

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

the class Comparison method simplify.

public Operator simplify(Variable from) {
    Operator result = super.simplify(from);
    if (result != this)
        return result;
    // In this case, our value can be constant, even if the operands themselves are not.
    if (// This method is crude, but should be sufficient for simple cases.
    operand0.render().equals(operand1.render())) {
        from.changed = true;
        releaseDependencies(from);
        operand0 = operand1 = new Constant(new Scalar(0));
        return new Constant(eval(null));
    }
    return this;
}
Also used : Scalar(gov.sandia.n2a.language.type.Scalar)

Example 32 with Scalar

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

the class Constant method getOperandsFrom.

@SuppressWarnings("unchecked")
public void getOperandsFrom(SimpleNode node) {
    Object o = node.jjtGetValue();
    if (o instanceof UnitValue) {
        unitValue = (UnitValue) node.jjtGetValue();
        Unit<?> unit = unitValue.unit;
        if (// naked number, so assume already in SI
        unit == null) {
            value = new Scalar(unitValue.value);
        } else // there was a unit given, so convert
        {
            @SuppressWarnings("rawtypes") Unit si = unit.getSystemUnit();
            value = new Scalar(unit.getConverterTo(si).convert(unitValue.value));
        }
    } else {
        value = (Type) o;
    }
}
Also used : Unit(javax.measure.Unit) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 33 with Scalar

use of gov.sandia.n2a.language.type.Scalar 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 34 with Scalar

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

the class Norm method eval.

public Type eval(Instance context) {
    double n = ((Scalar) operands[0].eval(context)).value;
    Matrix A = (Matrix) operands[1].eval(context);
    return new Scalar(A.norm(n));
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 35 with Scalar

use of gov.sandia.n2a.language.type.Scalar 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

Scalar (gov.sandia.n2a.language.type.Scalar)42 Type (gov.sandia.n2a.language.Type)17 AccessVariable (gov.sandia.n2a.language.AccessVariable)14 Variable (gov.sandia.n2a.eqset.Variable)13 Matrix (gov.sandia.n2a.language.type.Matrix)11 Constant (gov.sandia.n2a.language.Constant)10 Operator (gov.sandia.n2a.language.Operator)10 Text (gov.sandia.n2a.language.type.Text)8 EquationSet (gov.sandia.n2a.eqset.EquationSet)6 ArrayList (java.util.ArrayList)6 Simulator (gov.sandia.n2a.backend.internal.Simulator)5 Instance (gov.sandia.n2a.language.type.Instance)5 MatrixDense (gov.sandia.n2a.language.type.MatrixDense)5 TreeSet (java.util.TreeSet)5 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)4 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)4 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)3 VariableReference (gov.sandia.n2a.eqset.VariableReference)3 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)2 EvaluationException (gov.sandia.n2a.language.EvaluationException)2