Skip to content

Commit f3af7f0

Browse files
committed
Fixed #100. feat: Added function rowMultiplication for different Number types to Operations.java
1 parent 69ffed7 commit f3af7f0

File tree

2 files changed

+68
-64
lines changed

2 files changed

+68
-64
lines changed

src/com/jwetherell/algorithms/data_structures/Matrix.java

+4-63
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.math.BigInteger;
55
import java.util.Comparator;
66

7+
import com.jwetherell.algorithms.mathematics.Operations;
8+
79
/**
810
* Matrx. This Matrix implementation is designed to be more efficient
911
* in cache. A matrix is a rectangular array of numbers, symbols, or expressions.
@@ -243,69 +245,7 @@ public Matrix<T> multiply(Matrix<T> input) {
243245
T[] row = getRow(r);
244246
T[] column = input.getColumn(c);
245247
T test = row[0];
246-
/* TODO: This is ugly and how to handle number overflow? */
247-
if (test instanceof BigDecimal) {
248-
BigDecimal result = BigDecimal.ZERO;
249-
for (int i = 0; i < cols; i++) {
250-
T m1 = row[i];
251-
T m2 = column[i];
252-
253-
BigDecimal result2 = ((BigDecimal)m1).multiply(((BigDecimal)m2));
254-
result = result.add(result2);
255-
}
256-
output.set(r, c, (T)result);
257-
} else if (test instanceof BigInteger) {
258-
BigInteger result = BigInteger.ZERO;
259-
for (int i = 0; i < cols; i++) {
260-
T m1 = row[i];
261-
T m2 = column[i];
262-
263-
BigInteger result2 = ((BigInteger)m1).multiply(((BigInteger)m2));
264-
result = result.add(result2);
265-
}
266-
output.set(r, c, (T)result);
267-
} else if (test instanceof Long) {
268-
Long result = 0l;
269-
for (int i = 0; i < cols; i++) {
270-
T m1 = row[i];
271-
T m2 = column[i];
272-
273-
Long result2 = m1.longValue() * m2.longValue();
274-
result = result+result2;
275-
}
276-
output.set(r, c, (T)result);
277-
} else if (test instanceof Double) {
278-
Double result = 0d;
279-
for (int i = 0; i < cols; i++) {
280-
T m1 = row[i];
281-
T m2 = column[i];
282-
283-
Double result2 = m1.doubleValue() * m2.doubleValue();
284-
result = result+result2;
285-
}
286-
output.set(r, c, (T)result);
287-
} else if (test instanceof Float) {
288-
Float result = 0f;
289-
for (int i = 0; i < cols; i++) {
290-
T m1 = row[i];
291-
T m2 = column[i];
292-
293-
Float result2 = m1.floatValue() * m2.floatValue();
294-
result = result+result2;
295-
}
296-
output.set(r, c, (T)result);
297-
} else {
298-
// Integer
299-
Integer result = 0;
300-
for (int i = 0; i < cols; i++) {
301-
T m1 = row[i];
302-
T m2 = column[i];
303-
304-
Integer result2 = m1.intValue() * m2.intValue();
305-
result = result+result2;
306-
}
307-
output.set(r, c, (T)result);
308-
}
248+
output.set(r, c, (T) Operations.rowMultiplication(test, row, column, cols));
309249
}
310250
}
311251
return output;
@@ -318,6 +258,7 @@ public void copy(Matrix<T> m) {
318258
}
319259
}
320260
}
261+
321262

322263
/**
323264
* {@inheritDoc}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
11
package com.jwetherell.algorithms.mathematics;
22

3-
public class Operations{
3+
import java.math.BigDecimal;
4+
import java.math.BigInteger;
45

6+
public class Operations {
7+
8+
/* TODO: How to handle number overflow? */
9+
public static Number rowMultiplication(Number test, Number[] row, Number[] column, int cols){
10+
if (test instanceof BigDecimal) {
11+
BigDecimal result = BigDecimal.ZERO;
12+
for (int i = 0; i < cols; i++) {
13+
Number m1 = row[i];
14+
Number m2 = column[i];
15+
BigDecimal result2 = ((BigDecimal)m1).multiply(((BigDecimal)m2));
16+
result = result.add(result2);
17+
}
18+
return result;
19+
} else if (test instanceof BigInteger) {
20+
BigInteger result = BigInteger.ZERO;
21+
for (int i = 0; i < cols; i++) {
22+
Number m1 = row[i];
23+
Number m2 = column[i];
24+
BigInteger result2 = ((BigInteger)m1).multiply(((BigInteger)m2));
25+
result = result.add(result2);
26+
}
27+
return result;
28+
} else if (test instanceof Long) {
29+
Long result = 0l;
30+
for (int i = 0; i < cols; i++) {
31+
Number m1 = row[i];
32+
Number m2 = column[i];
33+
Long result2 = m1.longValue() * m2.longValue();
34+
result = result+result2;
35+
}
36+
return result;
37+
} else if (test instanceof Double) {
38+
Double result = 0d;
39+
for (int i = 0; i < cols; i++) {
40+
Number m1 = row[i];
41+
Number m2 = column[i];
42+
Double result2 = m1.doubleValue() * m2.doubleValue();
43+
result = result+result2;
44+
}
45+
return result;
46+
} else if (test instanceof Float) {
47+
Float result = 0f;
48+
for (int i = 0; i < cols; i++) {
49+
Number m1 = row[i];
50+
Number m2 = column[i];
51+
Float result2 = m1.floatValue() * m2.floatValue();
52+
result = result+result2;
53+
}
54+
return result;
55+
} else {
56+
// Integer
57+
Integer result = 0;
58+
for (int i = 0; i < cols; i++) {
59+
Number m1 = row[i];
60+
Number m2 = column[i];
61+
Integer result2 = m1.intValue() * m2.intValue();
62+
result = result+result2;
63+
}
64+
return result;
65+
}
66+
}
67+
568
}

0 commit comments

Comments
 (0)