Search in sources :

Example 21 with MatrixDense

use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.

the class Input method eval.

public Type eval(Instance context) {
    double line = Double.NEGATIVE_INFINITY;
    Type op1 = null;
    if (operands.length > 1)
        op1 = operands[1].eval(context);
    if (op1 instanceof Scalar)
        line = ((Scalar) op1).value;
    Holder H = getRow(context, line);
    if (H == null)
        return getType();
    int c = -1;
    boolean namedColumn = false;
    if (operands.length > 2) {
        Type columnSpec = operands[2].eval(context);
        if (columnSpec instanceof Text) {
            Integer columnMapping = H.columnMap.get(((Text) columnSpec).value);
            if (columnMapping == null)
                return new Scalar(0);
            c = columnMapping;
            namedColumn = true;
        } else // Otherwise, just assume it is a Scalar
        {
            c = (int) Math.round(((Scalar) columnSpec).value);
        }
    }
    int columns = H.currentValues.length;
    int lastColumn = columns - 1;
    if (H.smooth && line >= H.currentLine && Double.isFinite(H.currentLine) && !Double.isNaN(H.nextLine)) {
        // This should still work, even if line < H.currentLine.
        double b = (line - H.currentLine) / (H.nextLine - H.currentLine);
        double b1 = 1 - b;
        if (c >= 0) {
            // time column is not included in raw index
            if (H.time && !namedColumn && c >= H.timeColumn)
                c++;
            if (c >= columns)
                c = lastColumn;
            return new Scalar(b * H.nextValues[c] + b1 * H.currentValues[c]);
        } else {
            if (H.Alast == line)
                return H.A;
            // Create a new matrix
            if (columns > 1) {
                columns--;
                H.A = new MatrixDense(1, columns);
                int from = 0;
                for (int to = 0; to < columns; to++) {
                    if (from == H.timeColumn)
                        from++;
                    H.A.set(0, to, b * H.nextValues[from] + b1 * H.currentValues[from]);
                    from++;
                }
            } else // There is always at least 1 column, enforced by Holder.
            {
                H.A = new MatrixDense(1, 1);
                H.A.set(0, 0, b * H.nextValues[0] + b1 * H.currentValues[0]);
            }
            H.Alast = line;
            return H.A;
        }
    } else {
        if (c >= 0) {
            // time column is not included in raw index
            if (H.time && !namedColumn && c >= H.timeColumn)
                c++;
            if (c >= columns)
                c = lastColumn;
            return new Scalar(H.currentValues[c]);
        } else {
            if (H.Alast == H.currentLine)
                return H.A;
            // Create a new matrix
            if (H.time && columns > 1) {
                columns--;
                H.A = new MatrixDense(1, columns);
                int from = 0;
                for (int to = 0; to < columns; to++) {
                    if (from == H.timeColumn)
                        from++;
                    H.A.set(0, to, H.currentValues[from++]);
                }
            } else {
                H.A = new MatrixDense(H.currentValues, 0, 1, columns, columns, 1);
            }
            H.Alast = H.currentLine;
            return H.A;
        }
    }
}
Also used : Type(gov.sandia.n2a.language.Type) MatrixDense(gov.sandia.n2a.linear.MatrixDense) Text(gov.sandia.n2a.language.type.Text) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 22 with MatrixDense

use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.

the class Part method getXYZ.

public double[] getXYZ(Simulator simulator, boolean connect) {
    InternalBackendData bed = (InternalBackendData) equations.backendData;
    // default is ~[0,0,0]
    if (bed.xyz == null)
        return new double[3];
    // Either "constant" or stored
    if (!bed.xyz.hasAttribute("temporary"))
        return ((MatrixDense) get(bed.xyz)).getData();
    InstanceTemporaries temp;
    List<Variable> list;
    if (// evaluate in connect phase
    connect) {
        temp = new InstanceConnect(this, simulator);
        list = bed.XYZdependencies;
    } else // evaluate in live phase
    {
        temp = new InstanceTemporaries(this, simulator);
        list = bed.XYZdependenciesTemp;
    }
    for (Variable v : list) {
        Type result = v.eval(temp);
        if (result == null)
            temp.set(v, v.type);
        else
            temp.set(v, result);
    }
    Type result = bed.xyz.eval(temp);
    if (result == null)
        return new double[3];
    return ((MatrixDense) result).getData();
}
Also used : Type(gov.sandia.n2a.language.Type) Variable(gov.sandia.n2a.eqset.Variable) MatrixDense(gov.sandia.n2a.linear.MatrixDense)

Example 23 with MatrixDense

use of gov.sandia.n2a.linear.MatrixDense 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 24 with MatrixDense

use of gov.sandia.n2a.linear.MatrixDense 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 25 with MatrixDense

use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.

the class Uniform method eval.

public Type eval(Instance context) throws EvaluationException {
    Random random;
    Simulator simulator = Simulator.instance.get();
    if (simulator == null)
        random = new Random();
    else
        random = simulator.random;
    if (operands.length == 0)
        return new Scalar(random.nextDouble());
    Type sigma = operands[0].eval(context);
    if (sigma instanceof Scalar) {
        double u = random.nextDouble();
        double lo = ((Scalar) sigma).value;
        // Check if this is actually an interval form.
        if (operands.length > 1) {
            double hi = ((Scalar) operands[1].eval(context)).value;
            double step = 1;
            if (operands.length > 2)
                step = ((Scalar) operands[2].eval(context)).value;
            int steps = (int) Math.floor((hi - lo) / step) + 1;
            return new Scalar(lo + step * Math.floor(u * steps));
        }
        // Non-interval form. "lo" is really just "sigma"
        return new Scalar(u * lo);
    } else if (sigma instanceof Matrix) {
        Matrix scale = (Matrix) sigma;
        int rows = scale.rows();
        int columns = scale.columns();
        if (columns == 1) {
            Matrix result = new MatrixDense(rows, 1);
            for (int i = 0; i < rows; i++) result.set(i, random.nextDouble() * scale.get(i, 0));
            return result;
        } else if (rows == 1) {
            Matrix result = new MatrixDense(columns, 1);
            for (int i = 0; i < columns; i++) result.set(i, random.nextDouble() * scale.get(0, i));
            return result;
        } else {
            Matrix temp = new MatrixDense(columns, 1);
            for (int i = 0; i < columns; i++) temp.set(i, random.nextDouble());
            return sigma.multiply(temp);
        }
    } else {
        // We could throw an exception, but this is an adequate fallback.
        return new Scalar(random.nextDouble());
    }
}
Also used : Type(gov.sandia.n2a.language.Type) 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)

Aggregations

MatrixDense (gov.sandia.n2a.linear.MatrixDense)36 EvaluationException (gov.sandia.n2a.language.EvaluationException)24 Matrix (gov.sandia.n2a.language.type.Matrix)7 Scalar (gov.sandia.n2a.language.type.Scalar)7 Type (gov.sandia.n2a.language.Type)5 Text (gov.sandia.n2a.language.type.Text)4 Simulator (gov.sandia.n2a.backend.internal.Simulator)3 Random (java.util.Random)3 Variable (gov.sandia.n2a.eqset.Variable)1 Constant (gov.sandia.n2a.language.Constant)1 Operator (gov.sandia.n2a.language.Operator)1 ASTConstant (gov.sandia.n2a.language.parse.ASTConstant)1 FactorQR (gov.sandia.n2a.linear.FactorQR)1 MatrixSparse (gov.sandia.n2a.linear.MatrixSparse)1 OutputParser (gov.sandia.n2a.ui.jobs.OutputParser)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1