use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.
the class Matrix method visit.
public Matrix visit(Visitor visitor) {
int w = columns();
int h = rows();
MatrixDense result = new MatrixDense(h, w);
for (int c = 0; c < w; c++) {
for (int r = 0; r < h; r++) {
result.set(r, c, visitor.apply(get(r, c)));
}
}
return result;
}
use of gov.sandia.n2a.linear.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;
}
use of gov.sandia.n2a.linear.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;
}
use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.
the class Gaussian 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.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());
}
}
use of gov.sandia.n2a.linear.MatrixDense 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;
}
Aggregations