diff --git a/src/com/jwetherell/algorithms/data_structures/Matrix.java b/src/com/jwetherell/algorithms/data_structures/Matrix.java index 0d814e18..e1c01bad 100644 --- a/src/com/jwetherell/algorithms/data_structures/Matrix.java +++ b/src/com/jwetherell/algorithms/data_structures/Matrix.java @@ -162,33 +162,31 @@ public Matrix add(Matrix 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; @@ -201,33 +199,31 @@ public Matrix subtract(Matrix 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;