Search in sources :

Example 31 with Matrix

use of gov.sandia.n2a.language.type.Matrix 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)

Example 32 with Matrix

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

the class MatrixDense method min.

public MatrixDense min(Type that) throws EvaluationException {
    if (that instanceof MatrixDense) {
        MatrixDense B = (MatrixDense) that;
        int oh = Math.min(rows, B.rows);
        int ow = Math.min(columns, B.columns);
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int stepB = B.strideC - oh * B.strideR;
        int a = offset;
        int b = B.offset;
        int r = 0;
        int end = rows * ow;
        while (r < end) {
            int overlapEnd = r + oh;
            int columnEnd = r + rows;
            while (r < overlapEnd) {
                result.data[r++] = Math.min(data[a], B.data[b]);
                a += strideR;
                b += B.strideR;
            }
            while (r < columnEnd) {
                result.data[r++] = Math.min(data[a], 0);
                a += strideR;
            }
            a += stepA;
            b += stepB;
        }
        end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = Math.min(data[a], 0);
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    if (that instanceof Matrix) {
        Matrix B = (Matrix) that;
        int oh = Math.min(rows, B.rows());
        int ow = Math.min(columns, B.columns());
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int a = offset;
        int r = 0;
        for (int col = 0; col < ow; col++) {
            int row = 0;
            for (; row < oh; row++) {
                result.data[r++] = Math.min(data[a], B.get(row, col));
                a += strideR;
            }
            for (; row < rows; row++) {
                result.data[r++] = Math.min(data[a], 0);
                a += strideR;
            }
            a += stepA;
        }
        for (int col = ow; col < columns; col++) {
            for (int row = 0; row < rows; row++) {
                result.data[r++] = Math.min(data[a], 0);
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    if (that instanceof Scalar) {
        double scalar = ((Scalar) that).value;
        MatrixDense result = new MatrixDense(rows, columns);
        int step = strideC - rows * strideR;
        int i = offset;
        int r = 0;
        int end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = Math.min(data[i], scalar);
                i += strideR;
            }
            i += step;
        }
        return result;
    }
    throw new EvaluationException("type mismatch");
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) EvaluationException(gov.sandia.n2a.language.EvaluationException) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 33 with Matrix

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

the class MatrixDense method multiplyElementwise.

public MatrixDense multiplyElementwise(Type that) throws EvaluationException {
    if (that instanceof MatrixDense) {
        MatrixDense B = (MatrixDense) that;
        int oh = Math.min(rows, B.rows);
        int ow = Math.min(columns, B.columns);
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int stepB = B.strideC - oh * B.strideR;
        int a = offset;
        int b = B.offset;
        int r = 0;
        int end = rows * ow;
        while (r < end) {
            int overlapEnd = r + oh;
            int columnEnd = r + rows;
            while (r < overlapEnd) {
                result.data[r++] = data[a] * B.data[b];
                a += strideR;
                b += B.strideR;
            }
            while (r < columnEnd) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
            b += stepB;
        }
        end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    if (that instanceof Matrix) {
        Matrix B = (Matrix) that;
        int oh = Math.min(rows, B.rows());
        int ow = Math.min(columns, B.columns());
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int a = offset;
        int r = 0;
        for (int col = 0; col < ow; col++) {
            int row = 0;
            for (; row < oh; row++) {
                result.data[r++] = data[a] * B.get(row, col);
                a += strideR;
            }
            for (; row < rows; row++) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
        }
        for (int col = ow; col < columns; col++) {
            for (int row = 0; row < rows; row++) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    if (that instanceof Scalar) {
        double scalar = ((Scalar) that).value;
        MatrixDense result = new MatrixDense(rows, columns);
        int step = strideC - rows * strideR;
        int i = offset;
        int r = 0;
        int end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = data[i] * scalar;
                i += strideR;
            }
            i += step;
        }
        return result;
    }
    throw new EvaluationException("type mismatch");
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) EvaluationException(gov.sandia.n2a.language.EvaluationException) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 34 with Matrix

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

the class MatrixDense method max.

public MatrixDense max(Type that) throws EvaluationException {
    if (that instanceof MatrixDense) {
        MatrixDense B = (MatrixDense) that;
        int oh = Math.min(rows, B.rows);
        int ow = Math.min(columns, B.columns);
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int stepB = B.strideC - oh * B.strideR;
        int a = offset;
        int b = B.offset;
        int r = 0;
        int end = rows * ow;
        while (r < end) {
            int overlapEnd = r + oh;
            int columnEnd = r + rows;
            while (r < overlapEnd) {
                result.data[r++] = Math.max(data[a], B.data[b]);
                a += strideR;
                b += B.strideR;
            }
            while (r < columnEnd) {
                result.data[r++] = Math.max(data[a], 0);
                a += strideR;
            }
            a += stepA;
            b += stepB;
        }
        end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = Math.max(data[a], 0);
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    if (that instanceof Matrix) {
        Matrix B = (Matrix) that;
        int oh = Math.min(rows, B.rows());
        int ow = Math.min(columns, B.columns());
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int a = offset;
        int r = 0;
        for (int col = 0; col < ow; col++) {
            int row = 0;
            for (; row < oh; row++) {
                result.data[r++] = Math.max(data[a], B.get(row, col));
                a += strideR;
            }
            for (; row < rows; row++) {
                result.data[r++] = Math.max(data[a], 0);
                a += strideR;
            }
            a += stepA;
        }
        for (int col = ow; col < columns; col++) {
            for (int row = 0; row < rows; row++) {
                result.data[r++] = Math.max(data[a], 0);
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    if (that instanceof Scalar) {
        double scalar = ((Scalar) that).value;
        MatrixDense result = new MatrixDense(rows, columns);
        int step = strideC - rows * strideR;
        int i = offset;
        int r = 0;
        int end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = Math.max(data[i], scalar);
                i += strideR;
            }
            i += step;
        }
        return result;
    }
    throw new EvaluationException("type mismatch");
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) EvaluationException(gov.sandia.n2a.language.EvaluationException) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 35 with Matrix

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

the class MatrixDense method multiply.

public MatrixDense multiply(Type that) throws EvaluationException {
    if (that instanceof MatrixDense) {
        MatrixDense B = (MatrixDense) that;
        int h = rows;
        int w = B.columns;
        int m = Math.min(columns, B.rows);
        MatrixDense result = new MatrixDense(h, w);
        int b = B.offset;
        int r = 0;
        int end = rows * B.columns;
        while (r < end) {
            int a = offset;
            int columnEnd = r + rows;
            while (r < columnEnd) {
                double sum = 0;
                int i = a;
                int j = b;
                int rowEnd = j + m * B.strideR;
                while (j != rowEnd) {
                    sum += data[i] * B.data[j];
                    i += strideC;
                    j += B.strideR;
                }
                result.data[r++] = sum;
                a += strideR;
            }
            b += B.strideC;
        }
        return result;
    }
    if (that instanceof Matrix) {
        Matrix B = (Matrix) that;
        int h = rows;
        int w = B.columns();
        int m = Math.min(columns, B.rows());
        MatrixDense result = new MatrixDense(h, w);
        int r = 0;
        for (int col = 0; col < w; col++) {
            int a = offset;
            for (int row = 0; row < h; row++) {
                double sum = 0;
                int i = a;
                for (int j = 0; j < m; j++) {
                    sum += data[i] * B.get(j, col);
                    i += strideC;
                }
                result.data[r++] = sum;
                a += strideR;
            }
        }
        return result;
    }
    if (that instanceof Scalar) {
        double scalar = ((Scalar) that).value;
        MatrixDense result = new MatrixDense(rows, columns);
        int step = strideC - rows * strideR;
        int i = offset;
        int r = 0;
        int end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = data[i] * scalar;
                i += strideR;
            }
            i += step;
        }
        return result;
    }
    throw new EvaluationException("type mismatch");
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) EvaluationException(gov.sandia.n2a.language.EvaluationException) Scalar(gov.sandia.n2a.language.type.Scalar)

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