Search in sources :

Example 1 with MatrixDense

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

the class BuildMatrix method eval.

public Type eval(Instance context) throws EvaluationException {
    int columns = operands.length;
    if (columns == 0)
        return new MatrixDense();
    int rows = operands[0].length;
    if (rows == 0)
        return new MatrixDense();
    Matrix result = new MatrixDense(rows, columns);
    for (int c = 0; c < columns; c++) {
        for (int r = 0; r < rows; r++) {
            if (operands[c][r] == null) {
                result.set(r, c, 0);
            } else {
                Type o = operands[c][r].eval(context);
                if (o instanceof Scalar)
                    result.set(r, c, ((Scalar) o).value);
                else if (o instanceof Text)
                    result.set(r, c, Double.valueOf(((Text) o).value));
                else if (o instanceof Matrix)
                    result.set(r, c, ((Matrix) o).get(0, 0));
                else
                    throw new EvaluationException("Can't construct matrix element from the given type.");
            }
        }
    }
    return result;
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) MatrixDense(gov.sandia.n2a.language.type.MatrixDense) Text(gov.sandia.n2a.language.type.Text) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 2 with MatrixDense

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

the class Gaussian method eval.

public Type eval(Instance context) throws EvaluationException {
    Random random;
    Simulator simulator = Simulator.getSimulator(context);
    if (simulator == null)
        random = new Random();
    else
        random = simulator.random;
    if (operands.length == 0)
        return new Scalar(random.nextGaussian());
    Type sigma = operands[0].eval(context);
    if (sigma instanceof Scalar) {
        return new Scalar(random.nextGaussian() * ((Scalar) sigma).value);
    } 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.nextGaussian() * 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.nextGaussian() * scale.get(0, i));
            return result;
        } else {
            Matrix temp = new MatrixDense(columns, 1);
            for (int i = 0; i < columns; i++) temp.set(i, random.nextGaussian());
            return sigma.multiply(temp);
        }
    } else {
        // We could throw an exception, but this is easy enough.
        return new Scalar(random.nextGaussian());
    }
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) Random(java.util.Random) MatrixDense(gov.sandia.n2a.language.type.MatrixDense) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 3 with MatrixDense

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

the class Grid method eval.

public Type eval(Instance context) throws EvaluationException {
    // collect parameters into arrays
    int i = (int) Math.floor(((Scalar) operands[0].eval(context)).value);
    int nx = 1;
    int ny = 1;
    int nz = 1;
    boolean raw = false;
    if (operands.length >= 2)
        nx = (int) Math.floor(((Scalar) operands[1].eval(context)).value);
    if (operands.length >= 3)
        ny = (int) Math.floor(((Scalar) operands[2].eval(context)).value);
    if (operands.length >= 4)
        nz = (int) Math.floor(((Scalar) operands[3].eval(context)).value);
    if (operands.length >= 5) {
        Type mode = operands[4].eval(context);
        if (mode instanceof Text && ((Text) mode).value.contains("raw"))
            raw = true;
    }
    // compute xyz in stride order
    Matrix result = new MatrixDense(3, 1);
    // stride x
    int sx = ny * nz;
    if (raw) {
        // (i / sx) is an integer operation, so remainder is truncated.
        result.set(0, i / sx);
        i %= sx;
        result.set(1, i / nz);
        result.set(2, i % nz);
    } else {
        result.set(0, ((i / sx) + 0.5) / nx);
        i %= sx;
        result.set(1, ((i / nz) + 0.5) / ny);
        result.set(2, ((i % nz) + 0.5) / nz);
    }
    return result;
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) MatrixDense(gov.sandia.n2a.language.type.MatrixDense) Text(gov.sandia.n2a.language.type.Text) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 4 with MatrixDense

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

the class BuildMatrix method simplify.

public Operator simplify(Variable from) {
    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 (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);
                if (// stop evaluating if we already know we are not constant
                isConstant) {
                    if (operands[c][r] instanceof Constant) {
                        Type o = ((Constant) operands[c][r]).value;
                        if (o instanceof Scalar)
                            A.set(r, c, ((Scalar) o).value);
                        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;
        return new Constant(A);
    }
    return this;
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) ASTConstant(gov.sandia.n2a.language.parse.ASTConstant) MatrixDense(gov.sandia.n2a.language.type.MatrixDense) Text(gov.sandia.n2a.language.type.Text) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 5 with MatrixDense

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

the class Uniform method eval.

public Type eval(Instance context) throws EvaluationException {
    Random random;
    Simulator simulator = Simulator.getSimulator(context);
    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) {
        return new Scalar(random.nextDouble() * ((Scalar) sigma).value);
    } 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 easy enough.
        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.language.type.MatrixDense) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Aggregations

Matrix (gov.sandia.n2a.language.type.Matrix)5 MatrixDense (gov.sandia.n2a.language.type.MatrixDense)5 Scalar (gov.sandia.n2a.language.type.Scalar)5 Type (gov.sandia.n2a.language.Type)3 Text (gov.sandia.n2a.language.type.Text)3 Simulator (gov.sandia.n2a.backend.internal.Simulator)2 Random (java.util.Random)2 ASTConstant (gov.sandia.n2a.language.parse.ASTConstant)1