Search in sources :

Example 96 with Operator

use of gov.sandia.n2a.language.Operator 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 (Exception 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) EvaluationException(gov.sandia.n2a.language.EvaluationException)

Example 97 with Operator

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

the class Log method determineExponent.

public void determineExponent(ExponentContext context) {
    Operator op = operands[0];
    op.determineExponent(context);
    if (op.exponent == UNKNOWN)
        return;
    int o = op.centerPower();
    int p = 0;
    if (o != 0)
        p = (int) (Math.signum(o) * Math.round(Math.log(Math.abs(o)) / Math.log(2)));
    int centerNew = MSB / 2;
    int exponentNew = p + MSB - centerNew;
    updateExponent(context, exponentNew, centerNew);
}
Also used : Operator(gov.sandia.n2a.language.Operator)

Example 98 with Operator

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

the class Min method simplify.

public Operator simplify(Variable from, boolean evalOnly) {
    Operator result = super.simplify(from, evalOnly);
    if (result != this)
        return result;
    // Check if Min appears as an operand. If so, merge its operands into ours
    ArrayList<Operator> newOperands = new ArrayList<Operator>(operands.length);
    boolean changed = false;
    for (Operator o : operands) {
        if (o instanceof Min) {
            Min m = (Min) o;
            newOperands.addAll(Arrays.asList(m.operands));
            changed = true;
        } else {
            newOperands.add(o);
        }
    }
    if (changed) {
        from.changed = true;
        Min newMin = new Min();
        newMin.parent = parent;
        newMin.operands = newOperands.toArray(new Operator[0]);
        for (Operator o : newMin.operands) o.parent = newMin;
        return result;
    }
    return this;
}
Also used : Operator(gov.sandia.n2a.language.Operator) ArrayList(java.util.ArrayList)

Example 99 with Operator

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

the class Output method determineExponentNext.

public void determineExponentNext() {
    // Value
    Operator op = operands[1];
    op.exponentNext = exponentNext;
    op.determineExponentNext();
    // Column identifier (name or index)
    if (operands.length >= 3) {
        op = operands[2];
        if (operands.length > 3 && operands[3].getString().contains("raw")) {
            // index, so pure integer
            op.exponentNext = MSB;
        } else {
            // name; can be a string or a float
            op.exponentNext = op.exponent;
        }
        op.determineExponentNext();
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator)

Example 100 with Operator

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

the class Add method simplify.

public Operator simplify(Variable from, boolean evalOnly) {
    Operator result = super.simplify(from, evalOnly);
    if (result != this)
        return result;
    if (// 0+B --> B
    operand0.isScalar() && operand0.getDouble() == 0) {
        from.changed = true;
        operand1.parent = parent;
        return operand1;
    } else if (// A+0 --> A
    operand1.isScalar() && operand1.getDouble() == 0) {
        from.changed = true;
        operand0.parent = parent;
        return operand0;
    } else if (// A+(-B) --> A-B
    operand1 instanceof Negate) {
        from.changed = true;
        Subtract s = new Subtract();
        s.parent = parent;
        s.operand0 = operand0;
        s.operand0.parent = s;
        s.operand1 = ((Negate) operand1).operand;
        s.operand1.parent = s;
        return s;
    }
    return this;
}
Also used : Operator(gov.sandia.n2a.language.Operator)

Aggregations

Operator (gov.sandia.n2a.language.Operator)104 AccessVariable (gov.sandia.n2a.language.AccessVariable)41 Constant (gov.sandia.n2a.language.Constant)37 Visitor (gov.sandia.n2a.language.Visitor)26 Variable (gov.sandia.n2a.eqset.Variable)17 ArrayList (java.util.ArrayList)16 Type (gov.sandia.n2a.language.Type)14 Scalar (gov.sandia.n2a.language.type.Scalar)14 Output (gov.sandia.n2a.language.function.Output)13 EquationSet (gov.sandia.n2a.eqset.EquationSet)12 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)10 Input (gov.sandia.n2a.language.function.Input)10 Text (gov.sandia.n2a.language.type.Text)10 Matrix (gov.sandia.n2a.language.type.Matrix)9 TreeSet (java.util.TreeSet)9 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)8 Add (gov.sandia.n2a.language.operator.Add)8 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)7 Event (gov.sandia.n2a.language.function.Event)7 Instance (gov.sandia.n2a.language.type.Instance)7