use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.
the class MatrixDense method add.
public MatrixDense add(Matrix 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;
}
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;
}
use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.
the class MatrixDense method subtract.
public MatrixDense subtract(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");
}
use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.
the class UnitMap method eval.
public Type eval(Instance context) {
Matrix A = (Matrix) operands[0].eval(context);
double r = ((Scalar) operands[1].eval(context)).value;
double c = 0.5;
if (operands.length > 2) {
Type op2 = operands[2].eval(context);
if (op2 instanceof Scalar)
c = ((Scalar) op2).value;
}
return new Scalar(A.get(r, c, Matrix.UNITMAP));
}
use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.
the class AccessElement method simplify.
public Operator simplify(Variable from, boolean evalOnly) {
for (int i = 0; i < operands.length; i++) operands[i] = operands[i].simplify(from, evalOnly);
if (operands.length == 1) {
from.changed = true;
operands[0].parent = parent;
return operands[0];
}
// All operand positions beyond 0 are subscripts, presumably into a matrix at operands[0].
// Attempt to replace the element access with a constant.
int row = -1;
int col = 0;
if (operands[1] instanceof Constant) {
Constant c = (Constant) operands[1];
if (c.value instanceof Scalar)
row = (int) ((Scalar) c.value).value;
}
if (operands.length > 2) {
col = -1;
if (operands[2] instanceof Constant) {
Constant c = (Constant) operands[2];
if (c.value instanceof Scalar)
col = (int) ((Scalar) c.value).value;
}
}
if (row < 0 || col < 0)
return this;
if (operands[0] instanceof Constant) {
Constant c = (Constant) operands[0];
if (c.value instanceof Matrix) {
from.changed = true;
Operator result = new Constant(new Scalar(((Matrix) c.value).get(row, col)));
result.parent = parent;
return result;
}
} else // If not constant (above), then operands[0] should always be an AccessVariable.
{
// Try to unpack the target variable and see if the specific element we want is constant
AccessVariable av = (AccessVariable) operands[0];
if (av.reference != null && av.reference.variable != null) {
Variable v = av.reference.variable;
if (v.equations != null && v.equations.size() == 1) {
EquationEntry e = v.equations.first();
if (// Only weird code would have a condition at all.
e.expression instanceof BuildMatrix && (e.condition == null || e.condition.getDouble() != 0)) {
BuildMatrix b = (BuildMatrix) e.expression;
Operator element = b.getElement(row, col);
if (element != null && element instanceof Constant) {
from.changed = true;
if (!evalOnly)
operands[0].releaseDependencies(from);
element.parent = parent;
return element;
}
}
}
}
}
return this;
}
use of gov.sandia.n2a.language.type.Matrix 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;
}
Aggregations