55import java .util .Comparator ;
66
77/**
8- * Matrx. This Matrix implementation is designed to be more efficient
8+ * 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>
1111 * @see <a href="https://en.wikipedia.org/wiki/Matrix_(mathematics)">Matrix (Wikipedia)</a>
@@ -29,27 +29,27 @@ public int compare(T o1, T o2) {
2929 int result = 0 ;
3030 if (o1 instanceof BigDecimal || o2 instanceof BigDecimal ) {
3131 BigDecimal c1 = (BigDecimal )o1 ;
32- BigDecimal c2 = (BigDecimal )o2 ;
32+ BigDecimal c2 = (BigDecimal )o2 ;
3333 result = c1 .compareTo (c2 );
3434 } else if (o1 instanceof BigInteger || o2 instanceof BigInteger ) {
3535 BigInteger c1 = (BigInteger )o1 ;
36- BigInteger c2 = (BigInteger )o2 ;
36+ BigInteger c2 = (BigInteger )o2 ;
3737 result = c1 .compareTo (c2 );
3838 } else if (o1 instanceof Long || o2 instanceof Long ) {
3939 Long c1 = o1 .longValue ();
40- Long c2 = o2 .longValue ();
40+ Long c2 = o2 .longValue ();
4141 result = c1 .compareTo (c2 );
4242 } else if (o1 instanceof Double || o2 instanceof Double ) {
4343 Double c1 = o1 .doubleValue ();
44- Double c2 = o2 .doubleValue ();
45- result = c1 .compareTo (c2 );
44+ Double c2 = o2 .doubleValue ();
45+ result = c1 .compareTo (c2 );
4646 } else if (o1 instanceof Float || o2 instanceof Float ) {
4747 Float c1 = o1 .floatValue ();
48- Float c2 = o2 .floatValue ();
49- result = c1 .compareTo (c2 );
48+ Float c2 = o2 .floatValue ();
49+ result = c1 .compareTo (c2 );
5050 } else {
5151 Integer c1 = o1 .intValue ();
52- Integer c2 = o2 .intValue ();
52+ Integer c2 = o2 .intValue ();
5353 result = c1 .compareTo (c2 );
5454 }
5555 return result ;
@@ -58,7 +58,7 @@ public int compare(T o1, T o2) {
5858
5959 /**
6060 * Matrix with 'rows' number of rows and 'cols' number of columns.
61- *
61+ *
6262 * @param rows Number of rows in Matrix.
6363 * @param cols Number of columns in Matrix.
6464 */
@@ -71,7 +71,7 @@ public Matrix(int rows, int cols) {
7171 /**
7272 * Matrix with 'rows' number of rows and 'cols' number of columns, populates
7373 * the double index matrix.
74- *
74+ *
7575 * @param rows Number of rows in Matrix.
7676 * @param cols Number of columns in Matrix.
7777 * @param matrix 2D matrix used to populate Matrix.
@@ -116,15 +116,15 @@ public void set(int row, int col, T value) {
116116 }
117117
118118 public Matrix <T > identity () throws Exception {
119- if (this .rows != this .cols )
120- throw new Exception ("Matrix should be a square" );
119+ if (this .rows != this .cols )
120+ 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 ;
125- if (element instanceof BigDecimal ) {
126- zero = (T )BigDecimal .ZERO ;
127- one = (T )BigDecimal .ONE ;
125+ if (element instanceof BigDecimal ) {
126+ zero = (T )BigDecimal .ZERO ;
127+ one = (T )BigDecimal .ONE ;
128128 } else if (element instanceof BigInteger ){
129129 zero = (T )BigInteger .ZERO ;
130130 one = (T )BigInteger .ONE ;
@@ -142,20 +142,20 @@ public Matrix<T> identity() throws Exception{
142142 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 ){
148- array [i ][j ] = zero ;
149- }
150- }
151-
152- final Matrix <T > identityMatrix = new Matrix <T >(this .rows , this .cols , array );
153- for (int i = 0 ; i < this .rows ;++i ){
154- identityMatrix .set (i , i , one );
155- }
156- return identityMatrix ;
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 ){
148+ array [i ][j ] = zero ;
149+ }
150+ }
151+
152+ final Matrix <T > identityMatrix = new Matrix <T >(this .rows , this .cols , array );
153+ for (int i = 0 ; i < this .rows ;++i ){
154+ identityMatrix .set (i , i , one );
155+ }
156+ return identityMatrix ;
157157 }
158-
158+
159159 public Matrix <T > add (Matrix <T > input ) {
160160 Matrix <T > output = new Matrix <T >(this .rows , this .cols );
161161 if ((this .cols != input .cols ) || (this .rows != input .rows ))
@@ -249,7 +249,7 @@ public Matrix<T> multiply(Matrix<T> input) {
249249 for (int i = 0 ; i < cols ; i ++) {
250250 T m1 = row [i ];
251251 T m2 = column [i ];
252-
252+
253253 BigDecimal result2 = ((BigDecimal )m1 ).multiply (((BigDecimal )m2 ));
254254 result = result .add (result2 );
255255 }
@@ -259,7 +259,7 @@ public Matrix<T> multiply(Matrix<T> input) {
259259 for (int i = 0 ; i < cols ; i ++) {
260260 T m1 = row [i ];
261261 T m2 = column [i ];
262-
262+
263263 BigInteger result2 = ((BigInteger )m1 ).multiply (((BigInteger )m2 ));
264264 result = result .add (result2 );
265265 }
@@ -269,7 +269,7 @@ public Matrix<T> multiply(Matrix<T> input) {
269269 for (int i = 0 ; i < cols ; i ++) {
270270 T m1 = row [i ];
271271 T m2 = column [i ];
272-
272+
273273 Long result2 = m1 .longValue () * m2 .longValue ();
274274 result = result +result2 ;
275275 }
@@ -279,7 +279,7 @@ public Matrix<T> multiply(Matrix<T> input) {
279279 for (int i = 0 ; i < cols ; i ++) {
280280 T m1 = row [i ];
281281 T m2 = column [i ];
282-
282+
283283 Double result2 = m1 .doubleValue () * m2 .doubleValue ();
284284 result = result +result2 ;
285285 }
@@ -289,7 +289,7 @@ public Matrix<T> multiply(Matrix<T> input) {
289289 for (int i = 0 ; i < cols ; i ++) {
290290 T m1 = row [i ];
291291 T m2 = column [i ];
292-
292+
293293 Float result2 = m1 .floatValue () * m2 .floatValue ();
294294 result = result +result2 ;
295295 }
@@ -300,7 +300,7 @@ public Matrix<T> multiply(Matrix<T> input) {
300300 for (int i = 0 ; i < cols ; i ++) {
301301 T m1 = row [i ];
302302 T m2 = column [i ];
303-
303+
304304 Integer result2 = m1 .intValue () * m2 .intValue ();
305305 result = result +result2 ;
306306 }
@@ -348,7 +348,7 @@ public boolean equals(Object obj) {
348348 for (int i =0 ; i <matrix .length ; i ++) {
349349 T t1 = matrix [i ];
350350 T t2 = m .matrix [i ];
351- int result = comparator .compare (t1 , t2 );
351+ int result = comparator .compare (t1 , t2 );
352352 if (result !=0 )
353353 return false ;
354354 }
@@ -371,4 +371,12 @@ public String toString() {
371371 }
372372 return builder .toString ();
373373 }
374- }
374+
375+ public int getRows () {
376+ return rows ;
377+ }
378+
379+ public int getCols () {
380+ return cols ;
381+ }
382+ }
0 commit comments