4
4
import java .math .BigInteger ;
5
5
import java .util .Comparator ;
6
6
7
+ import com .jwetherell .algorithms .mathematics .Operations ;
8
+
7
9
/**
8
10
* Matrx. This Matrix implementation is designed to be more efficient
9
11
* in cache. A matrix is a rectangular array of numbers, symbols, or expressions.
@@ -243,69 +245,7 @@ public Matrix<T> multiply(Matrix<T> input) {
243
245
T [] row = getRow (r );
244
246
T [] column = input .getColumn (c );
245
247
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 ));
309
249
}
310
250
}
311
251
return output ;
@@ -318,6 +258,7 @@ public void copy(Matrix<T> m) {
318
258
}
319
259
}
320
260
}
261
+
321
262
322
263
/**
323
264
* {@inheritDoc}
0 commit comments