88 * Matrx. This Matrix implementation is designed to be more efficient
99 * in cache. A matrix is a rectangular array of numbers, symbols, or expressions.
1010 * <p>
11- * http ://en.wikipedia.org/wiki/Matrix_(mathematics)
12- *
11+ * @see <a href="https ://en.wikipedia.org/wiki/Matrix_(mathematics)">Matrix (Wikipedia)</a>
12+ * <br>
1313 * @author Justin Wetherell <[email protected] > 1414 */
1515@ SuppressWarnings ("unchecked" )
@@ -28,12 +28,12 @@ public int compare(T o1, T o2) {
2828 /* TODO: What if Java adds new numeric type? */
2929 int result = 0 ;
3030 if (o1 instanceof BigDecimal || o2 instanceof BigDecimal ) {
31- BigDecimal c1 = (BigDecimal ) o1 ;
32- BigDecimal c2 = (BigDecimal ) o2 ;
31+ BigDecimal c1 = (BigDecimal )o1 ;
32+ BigDecimal c2 = (BigDecimal )o2 ;
3333 result = c1 .compareTo (c2 );
3434 } else if (o1 instanceof BigInteger || o2 instanceof BigInteger ) {
35- BigInteger c1 = (BigInteger ) o1 ;
36- BigInteger c2 = (BigInteger ) o2 ;
35+ BigInteger c1 = (BigInteger )o1 ;
36+ BigInteger c2 = (BigInteger )o2 ;
3737 result = c1 .compareTo (c2 );
3838 } else if (o1 instanceof Long || o2 instanceof Long ) {
3939 Long c1 = o1 .longValue ();
@@ -72,17 +72,17 @@ public Matrix(int rows, int cols) {
7272 * Matrix with 'rows' number of rows and 'cols' number of columns, populates
7373 * the double index matrix.
7474 *
75- * @param rows Number of rows in Matrix.
76- * @param cols Number of columns in Matrix.
75+ * @param rows Number of rows in Matrix.
76+ * @param cols Number of columns in Matrix.
7777 * @param matrix 2D matrix used to populate Matrix.
7878 */
7979 public Matrix (int rows , int cols , T [][] matrix ) {
8080 this .rows = rows ;
8181 this .cols = cols ;
8282 this .matrix = (T []) new Number [rows * cols ];
83- for (int r = 0 ; r < rows ; r ++)
84- for (int c = 0 ; c < cols ; c ++)
85- this .matrix [getIndex (r , c )] = matrix [r ][c ];
83+ for (int r = 0 ; r < rows ; r ++)
84+ for (int c = 0 ; c < cols ; c ++)
85+ this .matrix [getIndex (r ,c )] = matrix [r ][c ];
8686 }
8787
8888 private int getIndex (int row , int col ) {
@@ -115,42 +115,42 @@ public void set(int row, int col, T value) {
115115 matrix [getIndex (row , col )] = value ;
116116 }
117117
118- public Matrix <T > identity () throws Exception {
119- if (this .rows != this .cols )
118+ public Matrix <T > identity () throws Exception {
119+ if (this .rows != this .cols )
120120 throw new Exception ("Matrix should be a square" );
121121
122122 final T element = this .get (0 , 0 );
123123 final T zero ;
124124 final T one ;
125125 if (element instanceof BigDecimal ) {
126- zero = (T ) BigDecimal .ZERO ;
127- one = (T ) BigDecimal .ONE ;
128- } else if (element instanceof BigInteger ) {
129- zero = (T ) BigInteger .ZERO ;
130- one = (T ) BigInteger .ONE ;
131- } else if (element instanceof Long ) {
132- zero = (T ) new Long (0 );
133- one = (T ) new Long (1 );
134- } else if (element instanceof Double ) {
135- zero = (T ) new Double (0 );
136- one = (T ) new Double (1 );
137- } else if (element instanceof Float ) {
138- zero = (T ) new Float (0 );
139- one = (T ) new Float (1 );
126+ zero = (T )BigDecimal .ZERO ;
127+ one = (T )BigDecimal .ONE ;
128+ } else if (element instanceof BigInteger ){
129+ zero = (T )BigInteger .ZERO ;
130+ one = (T )BigInteger .ONE ;
131+ } else if (element instanceof Long ){
132+ zero = (T )new Long (0 );
133+ one = (T )new Long (1 );
134+ } else if (element instanceof Double ){
135+ zero = (T )new Double (0 );
136+ one = (T )new Double (1 );
137+ } else if (element instanceof Float ){
138+ zero = (T )new Float (0 );
139+ one = (T )new Float (1 );
140140 } else {
141- zero = (T ) new Integer (0 );
142- one = (T ) new Integer (1 );
141+ zero = (T )new Integer (0 );
142+ one = (T )new Integer (1 );
143143 }
144144
145- final T array [][] = (T [][]) new Number [this .rows ][this .cols ];
146- for (int i = 0 ; i < this .rows ; ++i ) {
147- for (int j = 0 ; j < this .cols ; ++j ) {
145+ final T array [][] = (T [][])new Number [this .rows ][this .cols ];
146+ for (int i = 0 ; i < this .rows ; ++i ) {
147+ for (int j = 0 ; j < this .cols ; ++j ){
148148 array [i ][j ] = zero ;
149149 }
150150 }
151151
152152 final Matrix <T > identityMatrix = new Matrix <T >(this .rows , this .cols , array );
153- for (int i = 0 ; i < this .rows ; ++i ) {
153+ for (int i = 0 ; i < this .rows ;++i ){
154154 identityMatrix .set (i , i , one );
155155 }
156156 return identityMatrix ;
@@ -168,24 +168,24 @@ public Matrix<T> add(Matrix<T> input) {
168168 T result ;
169169 /* TODO: This is ugly and how to handle number overflow? */
170170 if (m1 instanceof BigDecimal || m2 instanceof BigDecimal ) {
171- BigDecimal result2 = ((BigDecimal ) m1 ).add ((BigDecimal ) m2 );
172- result = (T ) result2 ;
171+ BigDecimal result2 = ((BigDecimal )m1 ).add ((BigDecimal )m2 );
172+ result = (T )result2 ;
173173 } else if (m1 instanceof BigInteger || m2 instanceof BigInteger ) {
174- BigInteger result2 = ((BigInteger ) m1 ).add ((BigInteger ) m2 );
175- result = (T ) result2 ;
174+ BigInteger result2 = ((BigInteger )m1 ).add ((BigInteger )m2 );
175+ result = (T )result2 ;
176176 } else if (m1 instanceof Long || m2 instanceof Long ) {
177177 Long result2 = (m1 .longValue () + m2 .longValue ());
178- result = (T ) result2 ;
178+ result = (T )result2 ;
179179 } else if (m1 instanceof Double || m2 instanceof Double ) {
180180 Double result2 = (m1 .doubleValue () + m2 .doubleValue ());
181- result = (T ) result2 ;
181+ result = (T )result2 ;
182182 } else if (m1 instanceof Float || m2 instanceof Float ) {
183183 Float result2 = (m1 .floatValue () + m2 .floatValue ());
184- result = (T ) result2 ;
184+ result = (T )result2 ;
185185 } else {
186186 // Integer
187187 Integer result2 = (m1 .intValue () + m2 .intValue ());
188- result = (T ) result2 ;
188+ result = (T )result2 ;
189189 }
190190 output .set (r , c , result );
191191 }
@@ -207,24 +207,24 @@ public Matrix<T> subtract(Matrix<T> input) {
207207 T result ;
208208 /* TODO: This is ugly and how to handle number overflow? */
209209 if (m1 instanceof BigDecimal || m2 instanceof BigDecimal ) {
210- BigDecimal result2 = ((BigDecimal ) m1 ).subtract ((BigDecimal ) m2 );
211- result = (T ) result2 ;
210+ BigDecimal result2 = ((BigDecimal )m1 ).subtract ((BigDecimal )m2 );
211+ result = (T )result2 ;
212212 } else if (m1 instanceof BigInteger || m2 instanceof BigInteger ) {
213- BigInteger result2 = ((BigInteger ) m1 ).subtract ((BigInteger ) m2 );
214- result = (T ) result2 ;
213+ BigInteger result2 = ((BigInteger )m1 ).subtract ((BigInteger )m2 );
214+ result = (T )result2 ;
215215 } else if (m1 instanceof Long || m2 instanceof Long ) {
216216 Long result2 = (m1 .longValue () - m2 .longValue ());
217- result = (T ) result2 ;
217+ result = (T )result2 ;
218218 } else if (m1 instanceof Double || m2 instanceof Double ) {
219219 Double result2 = (m1 .doubleValue () - m2 .doubleValue ());
220- result = (T ) result2 ;
220+ result = (T )result2 ;
221221 } else if (m1 instanceof Float || m2 instanceof Float ) {
222222 Float result2 = (m1 .floatValue () - m2 .floatValue ());
223- result = (T ) result2 ;
223+ result = (T )result2 ;
224224 } else {
225225 // Integer
226226 Integer result2 = (m1 .intValue () - m2 .intValue ());
227- result = (T ) result2 ;
227+ result = (T )result2 ;
228228 }
229229 output .set (r , c , result );
230230 }
@@ -250,50 +250,50 @@ public Matrix<T> multiply(Matrix<T> input) {
250250 T m1 = row [i ];
251251 T m2 = column [i ];
252252
253- BigDecimal result2 = ((BigDecimal ) m1 ).multiply (((BigDecimal ) m2 ));
253+ BigDecimal result2 = ((BigDecimal )m1 ).multiply (((BigDecimal )m2 ));
254254 result = result .add (result2 );
255255 }
256- output .set (r , c , (T ) result );
256+ output .set (r , c , (T )result );
257257 } else if (test instanceof BigInteger ) {
258258 BigInteger result = BigInteger .ZERO ;
259259 for (int i = 0 ; i < cols ; i ++) {
260260 T m1 = row [i ];
261261 T m2 = column [i ];
262262
263- BigInteger result2 = ((BigInteger ) m1 ).multiply (((BigInteger ) m2 ));
263+ BigInteger result2 = ((BigInteger )m1 ).multiply (((BigInteger )m2 ));
264264 result = result .add (result2 );
265265 }
266- output .set (r , c , (T ) result );
266+ output .set (r , c , (T )result );
267267 } else if (test instanceof Long ) {
268268 Long result = 0l ;
269269 for (int i = 0 ; i < cols ; i ++) {
270270 T m1 = row [i ];
271271 T m2 = column [i ];
272272
273273 Long result2 = m1 .longValue () * m2 .longValue ();
274- result = result + result2 ;
274+ result = result + result2 ;
275275 }
276- output .set (r , c , (T ) result );
276+ output .set (r , c , (T )result );
277277 } else if (test instanceof Double ) {
278278 Double result = 0d ;
279279 for (int i = 0 ; i < cols ; i ++) {
280280 T m1 = row [i ];
281281 T m2 = column [i ];
282282
283283 Double result2 = m1 .doubleValue () * m2 .doubleValue ();
284- result = result + result2 ;
284+ result = result + result2 ;
285285 }
286- output .set (r , c , (T ) result );
286+ output .set (r , c , (T )result );
287287 } else if (test instanceof Float ) {
288288 Float result = 0f ;
289289 for (int i = 0 ; i < cols ; i ++) {
290290 T m1 = row [i ];
291291 T m2 = column [i ];
292292
293293 Float result2 = m1 .floatValue () * m2 .floatValue ();
294- result = result + result2 ;
294+ result = result + result2 ;
295295 }
296- output .set (r , c , (T ) result );
296+ output .set (r , c , (T )result );
297297 } else {
298298 // Integer
299299 Integer result = 0 ;
@@ -302,9 +302,9 @@ public Matrix<T> multiply(Matrix<T> input) {
302302 T m2 = column [i ];
303303
304304 Integer result2 = m1 .intValue () * m2 .intValue ();
305- result = result + result2 ;
305+ result = result + result2 ;
306306 }
307- output .set (r , c , (T ) result );
307+ output .set (r , c , (T )result );
308308 }
309309 }
310310 }
@@ -345,11 +345,11 @@ public boolean equals(Object obj) {
345345 return false ;
346346 if (this .cols != m .cols )
347347 return false ;
348- for (int i = 0 ; i < matrix .length ; i ++) {
348+ for (int i = 0 ; i < matrix .length ; i ++) {
349349 T t1 = matrix [i ];
350350 T t2 = m .matrix [i ];
351351 int result = comparator .compare (t1 , t2 );
352- if (result != 0 )
352+ if (result != 0 )
353353 return false ;
354354 }
355355 return true ;
@@ -379,4 +379,4 @@ public int getRows() {
379379 public int getCols () {
380380 return cols ;
381381 }
382- }
382+ }
0 commit comments