use of gov.sandia.n2a.language.Constant in project n2a by frothga.
the class Output method determineVariableName.
// This method should be called by analysis, with v set to the variable that holds this equation.
public void determineVariableName(Variable v) {
// Ensure that the first operand is a file name.
// If no file name is specified, stdout is used. We get the same effect by specifying the empty string, so insert it here.
// This makes parameter processing much simpler elsewhere.
int length = operands.length;
if (length > 0 && !isStringExpression(v, operands[0])) {
variableName = variableName0;
Operator[] newOperands = new Operator[length + 1];
for (int i = 0; i < length; i++) newOperands[i + 1] = operands[i];
newOperands[0] = new Constant(new Text());
operands = newOperands;
} else {
variableName = variableName1;
}
if (// Column name not specified
length < 3) {
if (variableName == null)
variableName = v.nameString();
EquationSet container = v.container;
if (// regular part
container.connectionBindings == null) {
dependOnIndex(v, container);
} else // connection
{
// depend on all endpoints
for (ConnectionBinding c : container.connectionBindings) {
dependOnIndex(v, c.endpoint);
}
}
}
}
use of gov.sandia.n2a.language.Constant in project n2a by frothga.
the class Output method isStringExpression.
/**
* Determines if the given operator is a string expression.
* This is the case if any operator it depends on is a string.
* If an operand is a variable, then its equations must contain a string.
*/
public static boolean isStringExpression(Variable v, Operator op) {
class StringVisitor extends Visitor {
boolean foundString;
Variable from;
public StringVisitor(Variable from) {
this.from = from;
from.visited = null;
}
public boolean visit(Operator op) {
if (op instanceof Constant) {
Constant c = (Constant) op;
if (c.value instanceof Text)
foundString = true;
return false;
}
if (op instanceof AccessVariable) {
AccessVariable av = (AccessVariable) op;
Variable v = av.reference.variable;
// Prevent infinite recursion
Variable p = from;
while (p != null) {
if (p == v)
return false;
p = p.visited;
}
v.visited = from;
from = v;
v.visit(this);
from = v.visited;
return false;
}
// no reason to dig any further
if (foundString)
return false;
// Add is the only operator that can propagate string values. All other operators and functions return scalars or matrices.
return op instanceof Add;
}
}
StringVisitor visitor = new StringVisitor(v);
op.visit(visitor);
return visitor.foundString;
}
use of gov.sandia.n2a.language.Constant in project n2a by frothga.
the class AND 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) {
operand1.releaseDependencies(from);
return new Constant(new Scalar(0));
}
return operand1;
}
} 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) {
operand0.releaseDependencies(from);
return new Constant(new Scalar(0));
}
return operand0;
}
}
return this;
}
use of gov.sandia.n2a.language.Constant in project n2a by frothga.
the class Multiply method simplify.
public Operator simplify(Variable from) {
Operator result = super.simplify(from);
if (result != this)
return result;
// This will be reversed below if we don't actually make a change.
from.changed = true;
if (operand0 instanceof Constant) {
Type c0 = ((Constant) operand0).value;
if (c0 instanceof Scalar) {
double value = ((Scalar) c0).value;
if (value == 1)
return operand1;
if (value == 0) {
operand1.releaseDependencies(from);
return new Constant(new Scalar(0));
}
}
} else if (operand1 instanceof Constant) {
Type c1 = ((Constant) operand1).value;
if (c1 instanceof Scalar) {
double value = ((Scalar) c1).value;
if (value == 1)
return operand0;
if (value == 0) {
operand0.releaseDependencies(from);
return new Constant(new Scalar(0));
}
}
}
from.changed = false;
return this;
}
use of gov.sandia.n2a.language.Constant in project n2a by frothga.
the class MultiplyElementwise method simplify.
public Operator simplify(Variable from) {
Operator result = super.simplify(from);
if (result != this)
return result;
// This will be reversed below if we don't actually make a change.
from.changed = true;
if (operand0 instanceof Constant) {
Type c0 = ((Constant) operand0).value;
if (c0 instanceof Scalar) {
double value = ((Scalar) c0).value;
if (value == 1)
return operand1;
}
} else if (operand1 instanceof Constant) {
Type c1 = ((Constant) operand1).value;
if (c1 instanceof Scalar) {
double value = ((Scalar) c1).value;
if (value == 1)
return operand0;
}
}
from.changed = false;
return this;
}
Aggregations