Search in sources :

Example 6 with Constant

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);
            }
        }
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) Constant(gov.sandia.n2a.language.Constant) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding) Text(gov.sandia.n2a.language.type.Text)

Example 7 with Constant

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;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Add(gov.sandia.n2a.language.operator.Add) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Text(gov.sandia.n2a.language.type.Text)

Example 8 with Constant

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;
}
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 9 with Constant

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;
}
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 10 with Constant

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

Aggregations

Constant (gov.sandia.n2a.language.Constant)19 Operator (gov.sandia.n2a.language.Operator)14 AccessVariable (gov.sandia.n2a.language.AccessVariable)11 Scalar (gov.sandia.n2a.language.type.Scalar)10 Type (gov.sandia.n2a.language.Type)9 Text (gov.sandia.n2a.language.type.Text)6 Visitor (gov.sandia.n2a.language.Visitor)5 EquationSet (gov.sandia.n2a.eqset.EquationSet)4 Variable (gov.sandia.n2a.eqset.Variable)4 ParseException (gov.sandia.n2a.language.ParseException)3 Input (gov.sandia.n2a.language.function.Input)3 Output (gov.sandia.n2a.language.function.Output)3 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)2 Function (gov.sandia.n2a.language.Function)2 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)2 Add (gov.sandia.n2a.language.operator.Add)2 TreeSet (java.util.TreeSet)2 MNode (gov.sandia.n2a.db.MNode)1 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)1 MPart (gov.sandia.n2a.eqset.MPart)1