Search in sources :

Example 26 with Matrix

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

the class RendererCfp method render.

public boolean render(Operator op) {
    if (op == latch) {
        latch = null;
        return super.render(op);
    }
    // one or more exponent parameters to the end of the usual function call.
    if (operatorsWithExponent.contains(op.getClass()))
        return super.render(op);
    // These are ordered to trap specific cases before more general ones.
    if (op instanceof OperatorLogical) {
        // Return the boolean itself if being consumed by a condition or by another logical operator.
        if (op.parent == null || op.parent instanceof OperatorLogical)
            return super.render(op);
        result.append("(");
        escalate(op);
        result.append(" ? ");
        if (op instanceof NOT) {
            result.append("0 : " + print(1, op.exponentNext) + ")");
        } else {
            result.append(print(1, op.exponentNext) + " : 0)");
        }
        return true;
    }
    if (op instanceof Multiply || op instanceof MultiplyElementwise) {
        OperatorBinary b = (OperatorBinary) op;
        // Explanation of shift -- The exponent of the result will be the sum of the exponents
        // of the two operands. That new exponent will be associated with bit position 2*MSB.
        // We want the exponent at bit position MSB.
        // Exponent at MSB position after a direct integer multiply.
        int exponentRaw = b.operand0.exponentNext + b.operand1.exponentNext - Operator.MSB;
        int shift = exponentRaw - b.exponentNext;
        if (shift == 0) {
            escalate(b);
        } else {
            if (b.getType() instanceof Matrix) {
                if (shift > 0) {
                    result.append("shift (");
                    escalate(b);
                    result.append(", " + shift + ")");
                } else {
                    if (b instanceof Multiply || b.operand0.isScalar() || b.operand1.isScalar()) {
                        result.append("multiply (");
                    } else // MultiplyElementwise and both operands are matrices
                    {
                        result.append("multiplyElementwise (");
                    }
                    if (b.operand0.isScalar()) {
                        // Always put the scalar in second position, so we need only one form of multiply().
                        b.operand1.render(this);
                        result.append(", ");
                        b.operand0.render(this);
                    } else {
                        b.operand0.render(this);
                        result.append(", ");
                        b.operand1.render(this);
                    }
                    result.append(", " + -shift + ")");
                }
            } else {
                if (shift > 0) {
                    result.append("(");
                    escalate(b);
                    result.append(" << " + shift);
                    result.append(")");
                } else {
                    if (!parentAccepts64bit(b))
                        result.append("(int32_t) ");
                    result.append("(");
                    if (!provides64bit(b.operand0))
                        result.append("(int64_t) ");
                    escalate(b);
                    result.append(" >> " + -shift);
                    result.append(")");
                }
            }
        }
        return true;
    }
    if (op instanceof Divide) {
        Divide d = (Divide) op;
        // Explanation of shift -- In a division, the quotient is effectively down-shifted by
        // the number of bits in the denominator, and its exponent is the difference between
        // the exponents of the numerator and denominator.
        // Exponent in MSB from a direct integer division.
        int exponentRaw = d.operand0.exponentNext - d.operand1.exponentNext + Operator.MSB;
        int shift = exponentRaw - d.exponentNext;
        if (shift == 0) {
            escalate(d);
        } else {
            if (d.getType() instanceof Matrix) {
                if (shift > 0) {
                    result.append("divide (");
                    d.operand0.render(this);
                    result.append(", ");
                    d.operand1.render(this);
                    result.append(", " + shift + ")");
                } else {
                    result.append("shift (");
                    escalate(d);
                    result.append(", " + shift + ")");
                }
            } else {
                if (shift > 0) {
                    if (!parentAccepts64bit(d))
                        result.append("(int32_t) ");
                    result.append("((");
                    if (!provides64bit(d.operand0))
                        result.append("(int64_t) ");
                    // OperatorBinary.render() will add parentheses around operand0 if it has lower
                    // precedence than division. This includes the case where it has lower precedence
                    // than shift, so we are safe.
                    d.render(this, " << " + shift + ") / ");
                    result.append(")");
                } else {
                    result.append("(");
                    escalate(d);
                    result.append(" >> " + -shift + ")");
                }
            }
        }
        return true;
    }
    if (op instanceof Modulo) {
        Modulo m = (Modulo) op;
        int shift = m.exponent - m.exponentNext;
        if (m.operand0.exponentNext == m.operand1.exponentNext) {
            if (shift == 0)
                return super.render(m);
            result.append("(");
            escalate(m);
            result.append(printShift(shift) + ")");
        } else {
            if (shift != 0)
                result.append("(");
            result.append("modFloor (");
            m.operand0.render(this);
            result.append(", ");
            m.operand1.render(this);
            result.append(", " + m.operand0.exponentNext + ", " + m.operand1.exponentNext + ")");
            if (shift != 0)
                result.append(printShift(shift) + ")");
        }
        return true;
    }
    if (// Add, Subtract, Negate, Transpose
    op instanceof OperatorArithmetic) {
        int exponentOperand;
        if (op instanceof OperatorBinary)
            exponentOperand = ((OperatorBinary) op).operand0.exponentNext;
        else
            exponentOperand = ((OperatorUnary) op).operand.exponentNext;
        int shift = exponentOperand - op.exponentNext;
        if (shift == 0)
            return super.render(op);
        if (op.getType() instanceof Matrix) {
            result.append("shift (");
            escalate(op);
            result.append(", " + shift + ")");
        } else {
            result.append("(");
            // In C++, shift is just below addition in precedence.
            boolean needParens = op.precedence() > new Add().precedence();
            if (needParens)
                result.append("(");
            escalate(op);
            if (needParens)
                result.append(")");
            result.append(printShift(shift));
            result.append(")");
        }
        return true;
    }
    // These are listed in alphabetical order, with a catch-all at the end.
    if (// Actually just an operator, not a function
    op instanceof AccessVariable) {
        AccessVariable av = (AccessVariable) op;
        int shift = av.exponent - av.exponentNext;
        if (shift != 0) {
            if (av.getType() instanceof Matrix)
                result.append("shift ");
            result.append("(");
        }
        result.append(job.resolve(av.reference, this, false));
        if (useExponent && shift != 0) {
            if (av.getType() instanceof Matrix) {
                result.append(", " + shift);
            } else {
                result.append(printShift(shift));
            }
            result.append(")");
        }
        return true;
    }
    if (op instanceof Ceil) {
        Ceil f = (Ceil) op;
        Operator a = f.operands[0];
        // Ceil always sets operands[0].exponentNext to be same as f.exponentNext, so no shift is necessary.
        if (// LSB is above decimal, so ceil() operation is impossible.
        f.exponentNext >= Operator.MSB) {
            a.render(this);
        } else if (// All bits are below decimal
        f.exponentNext < 0) {
            result.append("0");
        } else {
            // Create a mask for bits below the decimal point.
            // When this mask is added to the number, it will add 1 to the first bit position
            // above the decimal if any bit is set under the mask. Afterward, used AND to remove
            // any residual bits below the decimal.
            // This works for both positive and negative numbers.
            int zeroes = Operator.MSB - f.exponentNext;
            int wholeMask = 0xFFFFFFFF << zeroes;
            int decimalMask = ~wholeMask;
            boolean needParens = a.precedence() >= new Add().precedence();
            result.append("(");
            if (needParens)
                result.append("(");
            a.render(this);
            if (needParens)
                result.append(")");
            result.append(" + " + decimalMask + " & " + wholeMask + ")");
        }
        return true;
    }
    if (op instanceof Cosine) {
        Cosine c = (Cosine) op;
        Operator a = c.operands[0];
        int shift = c.exponent - c.exponentNext;
        if (shift != 0)
            result.append("(");
        result.append("cos (");
        a.render(this);
        result.append(", " + a.exponentNext + ")");
        if (shift != 0)
            result.append(printShift(shift) + ")");
        return true;
    }
    if (op instanceof Event) {
        // See comment on OperatorLogical above. Since event() returns a boolean, we follow the same behavior here.
        if (op.parent == null || op.parent instanceof OperatorLogical)
            return super.render(op);
        Event e = (Event) op;
        int exponentRaw = Operator.MSB - e.eventType.valueIndex;
        int shift = exponentRaw - e.exponentNext;
        if (shift != 0)
            result.append("(");
        result.append("(flags & (" + bed.localFlagType + ") 0x1 << " + e.eventType.valueIndex + ")");
        if (shift != 0) {
            result.append(printShift(shift));
            result.append(")");
        }
        return true;
    }
    if (op instanceof Floor) {
        // See Ceil above for similar code.
        Floor f = (Floor) op;
        Operator a = f.operands[0];
        if (f.exponentNext >= Operator.MSB) {
            a.render(this);
        } else if (f.exponentNext < 0) {
            result.append("0");
        } else {
            // Mask off bits below the decimal point. This works for both positive and negative numbers.
            int zeroes = Operator.MSB - f.exponentNext;
            int wholeMask = 0xFFFFFFFF << zeroes;
            boolean needParens = a.precedence() >= new AND().precedence();
            result.append("(");
            if (needParens)
                result.append("(");
            a.render(this);
            if (needParens)
                result.append(")");
            result.append(" & " + wholeMask + ")");
        }
        return true;
    }
    if (op instanceof HyperbolicTangent) {
        HyperbolicTangent ht = (HyperbolicTangent) op;
        Operator a = ht.operands[0];
        int shift = ht.exponent - ht.exponentNext;
        if (shift != 0)
            result.append("(");
        result.append("tanh (");
        a.render(this);
        result.append(", " + a.exponentNext + ")");
        if (shift != 0)
            result.append(printShift(shift) + ")");
        return true;
    }
    if (op instanceof Round) {
        Round r = (Round) op;
        Operator a = r.operands[0];
        int shift = a.exponentNext - r.exponentNext;
        int decimalPlaces = Math.max(0, Operator.MSB - a.exponentNext);
        int mask = 0xFFFFFFFF << decimalPlaces;
        int half = 0;
        if (decimalPlaces > 0)
            half = 0x1 << decimalPlaces - 1;
        if (shift != 0)
            result.append("(");
        // Bitwise operators are low precedence, so we parenthesize them regardless.
        result.append("(");
        boolean needParens = a.precedence() > new Add().precedence();
        if (needParens)
            result.append("(");
        a.render(this);
        if (needParens)
            result.append(")");
        result.append(" + " + half + " & " + mask);
        // Close parens around bitwise operator
        result.append(")");
        if (shift != 0)
            result.append(printShift(shift) + ")");
        return true;
    }
    if (op instanceof Signum) {
        Signum s = (Signum) op;
        Operator a = s.operands[0];
        int one = 0x1 << Operator.MSB - s.exponentNext;
        boolean needParens = a.precedence() >= new LT().precedence();
        if (needParens)
            result.append("(");
        a.render(this);
        if (needParens)
            result.append(")");
        result.append(" < 0 ? " + -one + ":" + one + ")");
        return true;
    }
    if (op instanceof Sine) {
        Sine s = (Sine) op;
        Operator a = s.operands[0];
        int shift = s.exponent - s.exponentNext;
        if (shift != 0)
            result.append("(");
        result.append("sin (");
        a.render(this);
        result.append(", " + a.exponentNext + ")");
        if (shift != 0)
            result.append(printShift(shift) + ")");
        return true;
    }
    if (// AbsoluteValue, Grid, Max, Min
    op instanceof Function) {
        int shift = op.exponent - op.exponentNext;
        if (shift == 0)
            return super.render(op);
        if (op.getType() instanceof Matrix) {
            result.append("shift (");
            escalate(op);
            result.append(", " + shift + ")");
        } else {
            result.append("(");
            escalate(op);
            result.append(printShift(shift) + ")");
        }
        return true;
    }
    return super.render(op);
}
Also used : Add(gov.sandia.n2a.language.operator.Add) Operator(gov.sandia.n2a.language.Operator) LT(gov.sandia.n2a.language.operator.LT) Sine(gov.sandia.n2a.language.function.Sine) Divide(gov.sandia.n2a.language.operator.Divide) Function(gov.sandia.n2a.language.Function) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Matrix(gov.sandia.n2a.language.type.Matrix) HyperbolicTangent(gov.sandia.n2a.language.function.HyperbolicTangent) Multiply(gov.sandia.n2a.language.operator.Multiply) Round(gov.sandia.n2a.language.function.Round) OperatorArithmetic(gov.sandia.n2a.language.OperatorArithmetic) Floor(gov.sandia.n2a.language.function.Floor) AccessVariable(gov.sandia.n2a.language.AccessVariable) Modulo(gov.sandia.n2a.language.operator.Modulo) OperatorLogical(gov.sandia.n2a.language.OperatorLogical) MultiplyElementwise(gov.sandia.n2a.language.operator.MultiplyElementwise) OperatorBinary(gov.sandia.n2a.language.OperatorBinary) Signum(gov.sandia.n2a.language.function.Signum) NOT(gov.sandia.n2a.language.operator.NOT) AND(gov.sandia.n2a.language.operator.AND) Event(gov.sandia.n2a.language.function.Event) Ceil(gov.sandia.n2a.language.function.Ceil) Cosine(gov.sandia.n2a.language.function.Cosine)

Example 27 with Matrix

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

the class RendererC method render.

public boolean render(Operator op) {
    for (ProvideOperator po : job.extensions) {
        Boolean result = po.render(this, op);
        if (result != null)
            return result;
    }
    if (op instanceof Add) {
        Add a = (Add) op;
        // Check if this is a string expression
        if (a.name != null) {
            result.append(a.name);
            return true;
        }
        return false;
    }
    if (op instanceof AccessElement) {
        AccessElement ae = (AccessElement) op;
        ae.operands[0].render(this);
        if (ae.operands.length == 2) {
            result.append("[");
            ae.operands[1].render(this);
            result.append("]");
        } else if (ae.operands.length == 3) {
            result.append("(");
            ae.operands[1].render(this);
            result.append(",");
            ae.operands[2].render(this);
            result.append(")");
        }
        return true;
    }
    if (op instanceof AccessVariable) {
        AccessVariable av = (AccessVariable) op;
        result.append(job.resolve(av.reference, this, false));
        return true;
    }
    if (op instanceof Atan) {
        Atan atan = (Atan) op;
        int shift = atan.exponent - atan.exponentNext;
        if (useExponent && shift != 0)
            result.append("(");
        if (atan.operands.length > 1 || useExponent)
            result.append("atan2 (");
        else
            result.append("atan (");
        Operator y = atan.operands[0];
        if (y.getType() instanceof Matrix) {
            y.render(this);
            result.append("[1], ");
            y.render(this);
            result.append("[0]");
        } else {
            y.render(this);
            if (atan.operands.length > 1) {
                result.append(", ");
                // x
                atan.operands[1].render(this);
            } else if (useExponent) {
                int shiftX = Operator.MSB - y.exponent;
                int x = shiftX >= 0 ? 0x1 << shiftX : 0;
                result.append(", " + x);
            }
        }
        result.append(")");
        if (useExponent && shift != 0)
            result.append(printShift(shift) + ")");
        return true;
    }
    if (op instanceof BuildMatrix) {
        BuildMatrix b = (BuildMatrix) op;
        result.append(b.name);
        return true;
    }
    if (op instanceof Columns) {
        Columns c = (Columns) op;
        c.operands[0].render(this);
        result.append(".columns ()");
        return true;
    }
    if (op instanceof Constant) {
        Constant c = (Constant) op;
        Type o = c.value;
        if (o instanceof Scalar) {
            result.append(print(((Scalar) o).value, c.exponentNext));
            return true;
        }
        if (o instanceof Text) {
            result.append("\"" + o.toString() + "\"");
            return true;
        }
        if (o instanceof Matrix) {
            result.append(c.name);
            return true;
        }
        return false;
    }
    if (op instanceof Delay) {
        Delay d = (Delay) op;
        if (d.operands.length == 1) {
            result.append("(");
            d.operands[0].render(this);
            result.append(")");
            return true;
        }
        result.append("delay" + d.index + ".step (Simulator<" + job.T + ">::instance.currentEvent->t, ");
        d.operands[1].render(this);
        result.append(", ");
        d.operands[0].render(this);
        result.append(", ");
        if (d.operands.length > 2)
            d.operands[2].render(this);
        else
            result.append("0");
        result.append(")");
        return true;
    }
    if (op instanceof Event) {
        Event e = (Event) op;
        // The cast to bool gets rid of the specific numeric value from flags.
        // If used in a numeric expression, it should convert to either 1 or 0.
        result.append("((bool) (flags & (" + bed.localFlagType + ") 0x1 << " + e.eventType.valueIndex + "))");
        return true;
    }
    if (op instanceof Exp) {
        Exp e = (Exp) op;
        Operator a = e.operands[0];
        result.append("exp (");
        a.render(this);
        if (useExponent)
            result.append(", " + e.exponentNext);
        result.append(")");
        return true;
    }
    if (op instanceof Gaussian) {
        Gaussian g = (Gaussian) op;
        result.append("gaussian<" + job.T + "> (");
        if (g.operands.length > 0) {
            g.operands[0].render(this);
        }
        result.append(")");
        return true;
    }
    if (op instanceof Grid) {
        Grid g = (Grid) op;
        boolean raw = g.operands.length >= 5 && g.operands[4].getString().contains("raw");
        int shift = g.exponent - g.exponentNext;
        if (useExponent && shift != 0)
            result.append("(");
        result.append("grid");
        if (raw)
            result.append("Raw");
        result.append("<" + job.T + "> (");
        int count = Math.min(4, g.operands.length);
        if (count > 0)
            g.operands[0].render(this);
        for (int i = 1; i < count; i++) {
            result.append(", ");
            g.operands[i].render(this);
        }
        result.append(")");
        if (useExponent && shift != 0)
            result.append(printShift(shift) + ")");
        return true;
    }
    if (op instanceof HyperbolicTangent) {
        HyperbolicTangent t = (HyperbolicTangent) op;
        Operator a = t.operands[0];
        result.append("tanh (");
        a.render(this);
        result.append(")");
        return true;
    }
    if (op instanceof Input) {
        Input i = (Input) op;
        result.append(i.name + "->get (");
        if (// return matrix
        i.operands.length < 3 || i.operands[2].getDouble() < 0) {
            boolean time = i.getMode().contains("time");
            String defaultLine = time ? "-INFINITY" : "0";
            if (i.operands.length > 1) {
                Operator op1 = i.operands[1];
                if (// expression for line
                op1.getType() instanceof Scalar)
                    // expression for line
                    op1.render(this);
                else
                    // op1 is probably the mode flag
                    result.append(defaultLine);
            } else // line is not specified. We're probably just retrieving a dummy matrix to get column count.
            {
                result.append(defaultLine);
            }
        } else // return scalar
        {
            i.operands[1].render(this);
            result.append(", ");
            i.operands[2].render(this);
        }
        result.append(")");
        return true;
    }
    if (op instanceof Log) {
        Log l = (Log) op;
        Operator a = l.operands[0];
        result.append("log (");
        a.render(this);
        if (useExponent)
            result.append(", " + a.exponentNext + ", " + l.exponentNext);
        result.append(")");
        return true;
    }
    if (op instanceof Max) {
        Max m = (Max) op;
        for (int i = 0; i < m.operands.length - 1; i++) {
            Operator a = m.operands[i];
            result.append("max (");
            renderType(a);
            result.append(", ");
        }
        Operator b = m.operands[m.operands.length - 1];
        renderType(b);
        for (int i = 0; i < m.operands.length - 1; i++) result.append(")");
        return true;
    }
    if (op instanceof Min) {
        Min m = (Min) op;
        for (int i = 0; i < m.operands.length - 1; i++) {
            Operator a = m.operands[i];
            result.append("min (");
            renderType(a);
            result.append(", ");
        }
        Operator b = m.operands[m.operands.length - 1];
        renderType(b);
        for (int i = 0; i < m.operands.length - 1; i++) result.append(")");
        return true;
    }
    if (op instanceof Modulo) {
        Modulo m = (Modulo) op;
        Operator a = m.operand0;
        Operator b = m.operand1;
        result.append("modFloor (");
        moduloParam(a);
        result.append(", ");
        moduloParam(b);
        result.append(")");
        return true;
    }
    if (op instanceof Norm) {
        Norm n = (Norm) op;
        Operator A = n.operands[0];
        result.append("norm (");
        A.render(this);
        result.append(", ");
        n.operands[1].render(this);
        if (useExponent)
            result.append(", " + A.exponentNext + ", " + n.exponentNext);
        result.append(")");
        return true;
    }
    if (op instanceof Output) {
        Output o = (Output) op;
        result.append(o.name + "->trace (Simulator<" + job.T + ">::instance.currentEvent->t, ");
        if (// column name is explicit
        o.hasColumnName) {
            o.operands[2].render(this);
        } else // column name is generated, so use prepared string value
        {
            result.append(o.columnName);
        }
        result.append(", ");
        o.operands[1].render(this);
        if (useExponent)
            result.append(", " + o.operands[1].exponentNext);
        result.append(", ");
        if (// No mode string
        o.operands.length < 4) {
            // null
            result.append("0");
        } else if (// Mode string is constant
        o.operands[3] instanceof Constant) {
            result.append("\"" + o.operands[3] + "\"");
        } else if (// Mode string is calculated
        o.operands[3] instanceof Add) {
            Add a = (Add) o.operands[3];
            // No need for cast or call c_str()
            result.append(a.name);
        }
        // else badness
        result.append(")");
        return true;
    }
    if (op instanceof Power) {
        Power p = (Power) op;
        Operator a = p.operand0;
        Operator b = p.operand1;
        result.append("pow (");
        a.render(this);
        result.append(", ");
        b.render(this);
        if (useExponent)
            result.append(", " + a.exponentNext + ", " + p.exponentNext);
        result.append(")");
        return true;
    }
    if (op instanceof Pulse) {
        Pulse p = (Pulse) op;
        Operator t = p.operands[0];
        result.append("pulse (");
        renderType(t);
        for (int i = 1; i < p.operands.length; i++) {
            result.append(", ");
            renderType(p.operands[i]);
        }
        result.append(")");
        return true;
    }
    if (op instanceof ReadMatrix) {
        ReadMatrix r = (ReadMatrix) op;
        // Currently, ReadMatrix sets its exponent = exponentNext, so we will never do a shift here.
        // Any shifting should be handled by matrixHelper while loading and converting the matrix to integer.
        // matrices are held in pointers, so need to dereference
        result.append("*" + r.name + "->A");
        return true;
    }
    if (op instanceof Rows) {
        Rows r = (Rows) op;
        r.operands[0].render(this);
        result.append(".rows ()");
        return true;
    }
    if (op instanceof Sat) {
        Sat s = (Sat) op;
        Operator a = s.operands[0];
        Operator lower = s.operands[1];
        Operator upper;
        if (s.operands.length >= 3)
            upper = s.operands[2];
        else
            upper = lower;
        result.append("max (");
        if (s.operands.length == 2)
            result.append("-1 * (");
        renderType(lower);
        if (s.operands.length == 2)
            result.append(")");
        result.append(", min (");
        renderType(upper);
        result.append(", ");
        renderType(a);
        result.append("))");
        return true;
    }
    if (op instanceof SquareRoot) {
        SquareRoot s = (SquareRoot) op;
        Operator a = s.operands[0];
        result.append("sqrt (");
        a.render(this);
        if (useExponent)
            result.append(", " + a.exponentNext + ", " + s.exponentNext);
        result.append(")");
        return true;
    }
    if (op instanceof SumSquares) {
        SumSquares ss = (SumSquares) op;
        Operator A = ss.operands[0];
        result.append("sumSquares (");
        A.render(this);
        if (useExponent)
            result.append(", " + A.exponentNext + ", " + ss.exponentNext);
        result.append(")");
        return true;
    }
    if (op instanceof Tangent) {
        Tangent t = (Tangent) op;
        Operator a = t.operands[0];
        result.append("tan (");
        a.render(this);
        if (useExponent)
            result.append(", " + a.exponentNext + ", " + t.exponentNext);
        result.append(")");
        return true;
    }
    if (op instanceof Uniform) {
        Uniform u = (Uniform) op;
        result.append("uniform<" + job.T + "> (");
        for (int i = 0; i < u.operands.length; i++) {
            if (i > 0)
                result.append(", ");
            u.operands[i].render(this);
        }
        result.append(")");
        return true;
    }
    return super.render(op);
}
Also used : Add(gov.sandia.n2a.language.operator.Add) Operator(gov.sandia.n2a.language.Operator) Max(gov.sandia.n2a.language.function.Max) Constant(gov.sandia.n2a.language.Constant) SumSquares(gov.sandia.n2a.language.function.SumSquares) Grid(gov.sandia.n2a.language.function.Grid) Sat(gov.sandia.n2a.language.function.Sat) Uniform(gov.sandia.n2a.language.function.Uniform) Delay(gov.sandia.n2a.language.function.Delay) Scalar(gov.sandia.n2a.language.type.Scalar) Input(gov.sandia.n2a.language.function.Input) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Matrix(gov.sandia.n2a.language.type.Matrix) HyperbolicTangent(gov.sandia.n2a.language.function.HyperbolicTangent) SquareRoot(gov.sandia.n2a.language.function.SquareRoot) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Output(gov.sandia.n2a.language.function.Output) Atan(gov.sandia.n2a.language.function.Atan) Rows(gov.sandia.n2a.language.function.Rows) AccessVariable(gov.sandia.n2a.language.AccessVariable) Log(gov.sandia.n2a.language.function.Log) Modulo(gov.sandia.n2a.language.operator.Modulo) Pulse(gov.sandia.n2a.language.function.Pulse) Columns(gov.sandia.n2a.language.function.Columns) Text(gov.sandia.n2a.language.type.Text) Tangent(gov.sandia.n2a.language.function.Tangent) HyperbolicTangent(gov.sandia.n2a.language.function.HyperbolicTangent) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) Type(gov.sandia.n2a.language.Type) Min(gov.sandia.n2a.language.function.Min) Event(gov.sandia.n2a.language.function.Event) Norm(gov.sandia.n2a.language.function.Norm) Gaussian(gov.sandia.n2a.language.function.Gaussian) AccessElement(gov.sandia.n2a.language.AccessElement) Exp(gov.sandia.n2a.language.function.Exp) Power(gov.sandia.n2a.language.operator.Power)

Example 28 with Matrix

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

the class Sphere method simplify.

public Operator simplify(Variable from, boolean evalOnly) {
    if (operands.length == 0) {
        // No need to set from.changed
        operands = new Operator[1];
        Operator sigma = new Constant(new MatrixDense(3, 1, 1));
        sigma.parent = this;
        operands[0] = sigma;
        return this;
    }
    // Any number of operands >= 1. We only pay attention to first operand.
    operands[0] = operands[0].simplify(from, evalOnly);
    Operator sigma = operands[0];
    if (sigma.isScalar()) {
        if (sigma.getDouble() == 0) {
            // Needed because we are replacing ourself with a simpler constant.
            from.changed = true;
            sigma.parent = parent;
            return sigma;
        }
        // No need to set from.changed
        operands = new Operator[1];
        sigma = new Constant(new MatrixDense(3, 1, sigma.getDouble()));
        sigma.parent = this;
        operands[0] = sigma;
        return this;
    } else if (sigma instanceof Constant) {
        Constant cs = (Constant) sigma;
        if (cs.value instanceof Matrix) {
            // No need to set from.changed for any of the transforms below ...
            Matrix A = (Matrix) cs.value;
            int rows = A.rows();
            int cols = A.columns();
            if (rows == 1 && cols > 1) {
                cs.value = A = A.transpose();
                rows = cols;
                cols = 1;
            }
            if (// Check for zeroes in single column.
            cols == 1) {
                int rank = (int) A.norm(0);
                if (rank < rows) {
                    from.changed = true;
                    MatrixDense B = new MatrixDense(rows, rank);
                    cs.value = B;
                    int c = 0;
                    for (int r = 0; r < rows; r++) {
                        double value = A.get(r);
                        if (value != 0)
                            B.set(r, c++, value);
                    }
                }
            }
        // Don't bother checking for columns that are all zeroes.
        // Since sigma is a constant, this should be obvious to the user.
        }
    }
    return this;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Matrix(gov.sandia.n2a.language.type.Matrix) Constant(gov.sandia.n2a.language.Constant) MatrixDense(gov.sandia.n2a.linear.MatrixDense)

Example 29 with Matrix

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

the class Sphere method eval.

// This code assumes that simplify() has been run, so operand 0 is a matrix of correct form.
public Type eval(Instance context) throws EvaluationException {
    Random random;
    Simulator simulator = Simulator.instance.get();
    if (simulator == null)
        random = new Random();
    else
        random = simulator.random;
    Matrix sigma = (Matrix) operands[0].eval(context);
    int rows = sigma.rows();
    int cols = sigma.columns();
    int dimension = cols == 1 ? rows : cols;
    MatrixDense temp = new MatrixDense(dimension, 1);
    for (int r = 0; r < dimension; r++) temp.set(r, random.nextGaussian());
    temp = temp.multiply(new Scalar(Math.pow(random.nextDouble(), 1.0 / dimension) / temp.norm(2)));
    if (cols == 1)
        return sigma.multiplyElementwise(temp);
    return sigma.multiply(temp);
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) Random(java.util.Random) MatrixDense(gov.sandia.n2a.linear.MatrixDense) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 30 with Matrix

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

the class SumSquares method determineExponent.

public void determineExponent(ExponentContext context) {
    // This function is related to both Norm and Power. See those classes for similar processing.
    // A
    Operator op0 = operands[0];
    op0.determineExponent(context);
    // Determine number of elements in A
    Instance instance = new Instance() {

        // all AccessVariable objects will reach here first, and get back the Variable.type field
        public Type get(VariableReference r) throws EvaluationException {
            return r.variable.type;
        }
    };
    Matrix A = (Matrix) op0.eval(instance);
    int Asize = A.rows() * A.columns();
    if (op0.exponent != UNKNOWN) {
        // log2(Asize)
        int shift = (int) Math.floor(Math.log(Asize) / Math.log(2));
        int centerNew = MSB / 2;
        int exponentNew = op0.centerPower() * 2 + shift + MSB - centerNew;
        updateExponent(context, exponentNew, centerNew);
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) Matrix(gov.sandia.n2a.language.type.Matrix) VariableReference(gov.sandia.n2a.eqset.VariableReference) Instance(gov.sandia.n2a.language.type.Instance)

Aggregations

Matrix (gov.sandia.n2a.language.type.Matrix)37 Scalar (gov.sandia.n2a.language.type.Scalar)26 Text (gov.sandia.n2a.language.type.Text)12 Type (gov.sandia.n2a.language.Type)11 Simulator (gov.sandia.n2a.backend.internal.Simulator)9 Operator (gov.sandia.n2a.language.Operator)9 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)8 MatrixDense (gov.sandia.n2a.linear.MatrixDense)7 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)6 Constant (gov.sandia.n2a.language.Constant)6 EvaluationException (gov.sandia.n2a.language.EvaluationException)6 AccessVariable (gov.sandia.n2a.language.AccessVariable)5 Variable (gov.sandia.n2a.eqset.Variable)4 Input (gov.sandia.n2a.language.function.Input)4 Output (gov.sandia.n2a.language.function.Output)4 Add (gov.sandia.n2a.language.operator.Add)4 Function (gov.sandia.n2a.language.Function)3 Event (gov.sandia.n2a.language.function.Event)3 Modulo (gov.sandia.n2a.language.operator.Modulo)3 Random (java.util.Random)3