Skip to content

Fixed #168. Refactor: Removed unneeded for-loops from Matrix.add and Matrix.subtract #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 48 additions & 52 deletions src/com/jwetherell/algorithms/data_structures/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,33 +162,31 @@ public Matrix<T> add(Matrix<T> input) {
return output;
for (int r = 0; r < output.rows; r++) {
for (int c = 0; c < output.cols; c++) {
for (int i = 0; i < cols; i++) {
T m1 = this.get(r, c);
T m2 = input.get(r, c);
T result;
/* TODO: This is ugly and how to handle number overflow? */
if (m1 instanceof BigDecimal || m2 instanceof BigDecimal) {
BigDecimal result2 = ((BigDecimal)m1).add((BigDecimal)m2);
result = (T)result2;
} else if (m1 instanceof BigInteger || m2 instanceof BigInteger) {
BigInteger result2 = ((BigInteger)m1).add((BigInteger)m2);
result = (T)result2;
} else if (m1 instanceof Long || m2 instanceof Long) {
Long result2 = (m1.longValue() + m2.longValue());
result = (T)result2;
} else if (m1 instanceof Double || m2 instanceof Double) {
Double result2 = (m1.doubleValue() + m2.doubleValue());
result = (T)result2;
} else if (m1 instanceof Float || m2 instanceof Float) {
Float result2 = (m1.floatValue() + m2.floatValue());
result = (T)result2;
} else {
// Integer
Integer result2 = (m1.intValue() + m2.intValue());
result = (T)result2;
}
output.set(r, c, result);
T m1 = this.get(r, c);
T m2 = input.get(r, c);
T result;
/* TODO: This is ugly and how to handle number overflow? */
if (m1 instanceof BigDecimal || m2 instanceof BigDecimal) {
BigDecimal result2 = ((BigDecimal)m1).add((BigDecimal)m2);
result = (T)result2;
} else if (m1 instanceof BigInteger || m2 instanceof BigInteger) {
BigInteger result2 = ((BigInteger)m1).add((BigInteger)m2);
result = (T)result2;
} else if (m1 instanceof Long || m2 instanceof Long) {
Long result2 = (m1.longValue() + m2.longValue());
result = (T)result2;
} else if (m1 instanceof Double || m2 instanceof Double) {
Double result2 = (m1.doubleValue() + m2.doubleValue());
result = (T)result2;
} else if (m1 instanceof Float || m2 instanceof Float) {
Float result2 = (m1.floatValue() + m2.floatValue());
result = (T)result2;
} else {
// Integer
Integer result2 = (m1.intValue() + m2.intValue());
result = (T)result2;
}
output.set(r, c, result);
}
}
return output;
Expand All @@ -201,33 +199,31 @@ public Matrix<T> subtract(Matrix<T> input) {

for (int r = 0; r < output.rows; r++) {
for (int c = 0; c < output.cols; c++) {
for (int i = 0; i < cols; i++) {
T m1 = this.get(r, c);
T m2 = input.get(r, c);
T result;
/* TODO: This is ugly and how to handle number overflow? */
if (m1 instanceof BigDecimal || m2 instanceof BigDecimal) {
BigDecimal result2 = ((BigDecimal)m1).subtract((BigDecimal)m2);
result = (T)result2;
} else if (m1 instanceof BigInteger || m2 instanceof BigInteger) {
BigInteger result2 = ((BigInteger)m1).subtract((BigInteger)m2);
result = (T)result2;
} else if (m1 instanceof Long || m2 instanceof Long) {
Long result2 = (m1.longValue() - m2.longValue());
result = (T)result2;
} else if (m1 instanceof Double || m2 instanceof Double) {
Double result2 = (m1.doubleValue() - m2.doubleValue());
result = (T)result2;
} else if (m1 instanceof Float || m2 instanceof Float) {
Float result2 = (m1.floatValue() - m2.floatValue());
result = (T)result2;
} else {
// Integer
Integer result2 = (m1.intValue() - m2.intValue());
result = (T)result2;
}
output.set(r, c, result);
T m1 = this.get(r, c);
T m2 = input.get(r, c);
T result;
/* TODO: This is ugly and how to handle number overflow? */
if (m1 instanceof BigDecimal || m2 instanceof BigDecimal) {
BigDecimal result2 = ((BigDecimal)m1).subtract((BigDecimal)m2);
result = (T)result2;
} else if (m1 instanceof BigInteger || m2 instanceof BigInteger) {
BigInteger result2 = ((BigInteger)m1).subtract((BigInteger)m2);
result = (T)result2;
} else if (m1 instanceof Long || m2 instanceof Long) {
Long result2 = (m1.longValue() - m2.longValue());
result = (T)result2;
} else if (m1 instanceof Double || m2 instanceof Double) {
Double result2 = (m1.doubleValue() - m2.doubleValue());
result = (T)result2;
} else if (m1 instanceof Float || m2 instanceof Float) {
Float result2 = (m1.floatValue() - m2.floatValue());
result = (T)result2;
} else {
// Integer
Integer result2 = (m1.intValue() - m2.intValue());
result = (T)result2;
}
output.set(r, c, result);
}
}
return output;
Expand Down