Search in sources :

Example 21 with Matrix

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

the class BuildMatrix method simplify.

public Operator simplify(Variable from, boolean evalOnly) {
    int cols = operands.length;
    if (cols == 0)
        return this;
    int rows = operands[0].length;
    if (rows == 0)
        return this;
    // potential constant to replace us
    Matrix A = new MatrixDense(rows, cols);
    // any element that is not constant will change this to false
    boolean isConstant = true;
    // for fixed-point analysis
    int cent = 0;
    int pow = 0;
    int count = 0;
    for (int c = 0; c < cols; c++) {
        for (int r = 0; r < rows; r++) {
            if (operands[c][r] == null) {
                A.set(r, c, 0);
            } else {
                operands[c][r] = operands[c][r].simplify(from, evalOnly);
                // stop building A if we already know we are not constant
                if (!isConstant)
                    continue;
                if (operands[c][r] instanceof Constant) {
                    Constant op = (Constant) operands[c][r];
                    Type o = op.value;
                    if (o instanceof Scalar) {
                        double v = ((Scalar) o).value;
                        A.set(r, c, v);
                        if (v != 0) {
                            int tempCent = MSB / 2;
                            if (op.unitValue != null) {
                                int bits = (int) Math.ceil(op.unitValue.digits * Math.log(10) / Math.log(2));
                                tempCent = Math.max(tempCent, bits - 1);
                                tempCent = Math.min(tempCent, MSB);
                            }
                            cent += tempCent;
                            pow += Math.getExponent(v);
                            count++;
                        }
                    } else if (o instanceof Text)
                        A.set(r, c, Double.valueOf(((Text) o).value));
                    else if (o instanceof Matrix)
                        A.set(r, c, ((Matrix) o).get(0, 0));
                    else
                        throw new EvaluationException("Can't construct matrix element from the given type.");
                } else {
                    isConstant = false;
                }
            }
        }
    }
    if (isConstant) {
        from.changed = true;
        Constant result = new Constant(A);
        result.parent = parent;
        if (count > 0) {
            cent /= count;
            pow /= count;
            result.center = cent;
            result.exponent = pow + MSB - cent;
        } else {
            result.center = MSB / 2;
            result.exponent = MSB - result.center;
        }
        return result;
    }
    return this;
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) ASTConstant(gov.sandia.n2a.language.parse.ASTConstant) MatrixDense(gov.sandia.n2a.linear.MatrixDense) Text(gov.sandia.n2a.language.type.Text) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 22 with Matrix

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

the class Norm method eval.

public Type eval(Instance context) {
    Matrix A = (Matrix) operands[0].eval(context);
    double n;
    if (operands.length > 1)
        n = ((Scalar) operands[1].eval(context)).value;
    else
        n = 2;
    return new Scalar(A.norm(n));
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 23 with Matrix

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

the class Atan method eval.

public Type eval(Instance context) {
    double y;
    double x;
    Type arg = operands[0].eval(context);
    if (arg instanceof Matrix) {
        x = ((Matrix) arg).get(0);
        y = ((Matrix) arg).get(1);
    } else // arg must be Scalar; otherwise the casts below will throw an exception.
    {
        y = ((Scalar) arg).value;
        if (operands.length == 1)
            return new Scalar(Math.atan(y));
        x = ((Scalar) operands[1].eval(context)).value;
    }
    return new Scalar(Math.atan2(y, x));
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 24 with Matrix

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

the class DrawSegment method eval.

public Type eval(Instance context) {
    Simulator simulator = Simulator.instance.get();
    if (simulator == null)
        return new Scalar(0);
    Holder H = getHolder(simulator, context);
    double now;
    if (simulator.currentEvent == null)
        now = 0;
    else
        now = (float) simulator.currentEvent.t;
    Matrix p = (Matrix) operands[1].eval(context);
    double x = p.get(0);
    double y = p.get(1);
    p = (Matrix) operands[2].eval(context);
    double x2 = p.get(0);
    double y2 = p.get(1);
    double width = 0;
    if (operands.length > 3)
        width = ((Scalar) operands[3].eval(context)).value;
    // white
    double color = 0xFFFFFF;
    if (operands.length > 4)
        color = ((Scalar) operands[4].eval(context)).value;
    H.drawSegment(now, x, y, x2, y2, width, (int) color);
    return new Scalar(0);
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 25 with Matrix

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

the class RendererPython method render.

public boolean render(Operator op) {
    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);
            if (ae.operands.length >= 3) {
                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 AND) {
        AND and = (AND) op;
        and.render(this, " and ");
        return true;
    }
    if (op instanceof BuildMatrix) {
        BuildMatrix b = (BuildMatrix) op;
        result.append(b.name);
        return true;
    }
    if (op instanceof Constant) {
        Constant c = (Constant) op;
        Type o = c.value;
        if (o instanceof Scalar) {
            result.append(print(((Scalar) o).value));
            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 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 will convert to either 1 or 0.
        result.append("bool (flags & 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);
        result.append(")");
        return true;
    }
    if (op instanceof Gaussian) {
        Gaussian g = (Gaussian) op;
        result.append("gaussian(");
        if (g.operands.length > 0) {
            g.operands[0].render(this);
        }
        result.append(")");
        return true;
    }
    if (op instanceof Grid) {
        // TODO: needs library implementation
        Grid g = (Grid) op;
        boolean raw = g.operands.length >= 5 && g.operands[4].getString().contains("raw");
        result.append("grid");
        if (raw)
            result.append("Raw");
        result.append("(");
        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(")");
        return true;
    }
    if (op instanceof Input) {
        // TODO: needs library implementation
        Input i = (Input) op;
        String mode = "";
        if (i.operands.length == 2)
            mode = i.operands[1].getString();
        else if (i.operands.length >= 4)
            mode = i.operands[3].getString();
        if (mode.contains("columns")) {
            result.append(i.name + "->getColumns ()");
        } else {
            Operator op1 = i.operands[1];
            Operator op2 = i.operands[2];
            result.append(i.name + ".get");
            if (mode.contains("raw"))
                result.append("Raw");
            result.append("(");
            op1.render(this);
            result.append(", ");
            op2.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);
        return true;
    }
    if (op instanceof Modulo) {
        Modulo m = (Modulo) op;
        Operator a = m.operand0;
        Operator b = m.operand1;
        a.render(this);
        result.append(" % ");
        b.render(this);
        return true;
    }
    if (op instanceof Norm) {
        Norm n = (Norm) op;
        Operator A = n.operands[0];
        result.append("numpy.linalg.norm(");
        A.render(this);
        result.append(", ");
        n.operands[1].render(this);
        result.append(")");
        return true;
    }
    if (op instanceof OR) {
        OR or = (OR) op;
        or.render(this, " or ");
        return true;
    }
    if (op instanceof Output) {
        Output o = (Output) op;
        result.append(o.name + "->trace (Simulator::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);
        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);
        result.append(")");
        return true;
    }
    if (op instanceof ReadMatrix) {
        ReadMatrix r = (ReadMatrix) op;
        String mode = "";
        int lastParm = r.operands.length - 1;
        if (lastParm > 0) {
            if (r.operands[lastParm] instanceof Constant) {
                Constant c = (Constant) r.operands[lastParm];
                if (c.value instanceof Text) {
                    mode = ((Text) c.value).value;
                }
            }
        }
        if (mode.equals("rows")) {
            result.append(r.name + "->rows ()");
        } else if (mode.equals("columns")) {
            result.append(r.name + "->columns ()");
        } else {
            result.append(r.name + "->get");
            if (mode.equals("raw"))
                result.append("Raw");
            result.append("(");
            r.operands[1].render(this);
            result.append(", ");
            r.operands[2].render(this);
            result.append(")");
        }
        return true;
    }
    if (op instanceof SquareRoot) {
        SquareRoot s = (SquareRoot) op;
        Operator a = s.operands[0];
        result.append("sqrt(");
        a.render(this);
        return true;
    }
    if (op instanceof Tangent) {
        Tangent t = (Tangent) op;
        Operator a = t.operands[0];
        result.append("tan(");
        a.render(this);
        result.append(")");
        return true;
    }
    if (op instanceof Uniform) {
        Uniform u = (Uniform) op;
        result.append("uniform(");
        if (u.operands.length > 0) {
            u.operands[0].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) Constant(gov.sandia.n2a.language.Constant) Grid(gov.sandia.n2a.language.function.Grid) Uniform(gov.sandia.n2a.language.function.Uniform) 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) SquareRoot(gov.sandia.n2a.language.function.SquareRoot) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Output(gov.sandia.n2a.language.function.Output) OR(gov.sandia.n2a.language.operator.OR) AccessVariable(gov.sandia.n2a.language.AccessVariable) Log(gov.sandia.n2a.language.function.Log) Modulo(gov.sandia.n2a.language.operator.Modulo) Text(gov.sandia.n2a.language.type.Text) Tangent(gov.sandia.n2a.language.function.Tangent) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) Type(gov.sandia.n2a.language.Type) AND(gov.sandia.n2a.language.operator.AND) 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)

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