use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.
the class Matrix method LE.
public Matrix LE(Type that) throws EvaluationException {
int w = columns();
int h = rows();
if (that instanceof Scalar) {
double b = ((Scalar) that).value;
MatrixDense result = new MatrixDense(h, w);
for (int c = 0; c < w; c++) {
for (int r = 0; r < h; r++) {
result.set(r, c, (get(r, c) <= b) ? 1 : 0);
}
}
return result;
}
if (that instanceof Matrix) {
Matrix B = (Matrix) that;
w = Math.min(w, B.columns());
h = Math.min(h, B.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, (get(r, c) <= B.get(r, c)) ? 1 : 0);
}
}
return result;
}
throw new EvaluationException("type mismatch");
}
use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.
the class Matrix method AND.
public Matrix AND(Type that) throws EvaluationException {
int w = columns();
int h = rows();
if (that instanceof Scalar) {
double b = ((Scalar) that).value;
MatrixDense result = new MatrixDense(h, w);
for (int c = 0; c < w; c++) {
for (int r = 0; r < h; r++) {
result.set(r, c, (get(r, c) * b != 0) ? 1 : 0);
}
}
return result;
}
if (that instanceof Matrix) {
Matrix B = (Matrix) that;
w = Math.min(w, B.columns());
h = Math.min(h, B.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, (get(r, c) * B.get(r, c) != 0) ? 1 : 0);
}
}
return result;
}
throw new EvaluationException("type mismatch");
}
use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.
the class Matrix method NOT.
public Type NOT() throws EvaluationException {
int w = columns();
int h = rows();
// should create pseudo-inverse
if (h != w)
throw new EvaluationException("Can't invert non-square matrix (because we don't know about pseudo-inverse).");
if (h == 1)
return new Scalar(1 / get(0, 0));
if (h == 2) {
MatrixDense result = new MatrixDense(2, 2);
double q = determinant();
if (q == 0)
throw new EvaluationException("invert: Matrix is singular!");
result.set(0, 0, get(1, 1) / q);
result.set(1, 0, get(1, 0) / -q);
result.set(0, 1, get(0, 1) / -q);
result.set(1, 1, get(0, 0) / q);
return result;
}
if (h == 3) {
MatrixDense result = new MatrixDense(3, 3);
double q = determinant();
if (q == 0)
throw new EvaluationException("invert: Matrix is singular!");
result.set(0, 0, det22(1, 2, 1, 2) / q);
result.set(1, 0, det22(1, 2, 2, 0) / q);
result.set(2, 0, det22(1, 2, 0, 1) / q);
result.set(0, 1, det22(0, 2, 2, 1) / q);
result.set(1, 1, det22(0, 2, 0, 2) / q);
result.set(2, 1, det22(0, 2, 1, 0) / q);
result.set(0, 2, det22(0, 1, 1, 2) / q);
result.set(1, 2, det22(0, 1, 2, 0) / q);
result.set(2, 2, det22(0, 1, 0, 1) / q);
return result;
}
throw new EvaluationException("Can't invert matrices larger then 3x3 (because we are not using a good numerical library).");
}
use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.
the class Matrix method NE.
public Type NE(Type that) throws EvaluationException {
if (that instanceof Matrix)
return new Scalar(compareTo(that) != 0 ? 1 : 0);
if (that instanceof Scalar) {
double b = ((Scalar) that).value;
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, (get(r, c) != b) ? 1 : 0);
}
}
return result;
}
throw new EvaluationException("type mismatch");
}
use of gov.sandia.n2a.linear.MatrixDense in project n2a by frothga.
the class Matrix method GE.
public Matrix GE(Type that) throws EvaluationException {
int w = columns();
int h = rows();
if (that instanceof Scalar) {
double b = ((Scalar) that).value;
MatrixDense result = new MatrixDense(h, w);
for (int c = 0; c < w; c++) {
for (int r = 0; r < h; r++) {
result.set(r, c, (get(r, c) >= b) ? 1 : 0);
}
}
return result;
}
if (that instanceof Matrix) {
Matrix B = (Matrix) that;
w = Math.min(w, B.columns());
h = Math.min(h, B.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, (get(r, c) >= B.get(r, c)) ? 1 : 0);
}
}
return result;
}
throw new EvaluationException("type mismatch");
}
Aggregations