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;
}
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);
}
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;
}
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();
}
}
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;
}
Aggregations