Skip to content

Commit 7773b96

Browse files
committed
Bit of code clean-up in the Matrix class
1 parent e2c259b commit 7773b96

File tree

1 file changed

+31
-46
lines changed
  • src/com/jwetherell/algorithms/data_structures

1 file changed

+31
-46
lines changed

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

+31-46
Original file line numberDiff line numberDiff line change
@@ -116,57 +116,42 @@ public void set(int row, int col, T value) {
116116
}
117117

118118
public Matrix<T> identity() throws Exception{
119-
if(this.rows != this.cols){
119+
if(this.rows != this.cols)
120120
throw new Exception("Matrix should be a square");
121-
}
122-
123-
T element = this.get(0, 0);
124-
125-
T array[][] = (T[][])new Number[this.rows][this.cols];
126-
for(int i = 0; i < this.rows; ++i){
121+
122+
final T element = this.get(0, 0);
123+
final T zero;
124+
final T one;
125+
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);
140+
} else {
141+
zero = (T)new Integer(0);
142+
one = (T)new Integer(1);
143+
}
144+
145+
final T array[][] = (T[][])new Number[this.rows][this.cols];
146+
for(int i = 0; i < this.rows; ++i) {
127147
for(int j = 0 ; j < this.cols; ++j){
128-
if(element instanceof BigDecimal){
129-
array[i][j] = (T)BigDecimal.ZERO;
130-
}
131-
else if(element instanceof BigInteger){
132-
array[i][j] = (T)BigInteger.ZERO;
133-
}
134-
else if(element instanceof Long){
135-
array[i][j] = (T)new Long(0);
136-
}
137-
else if(element instanceof Double){
138-
array[i][j] = (T)new Double(0);
139-
}
140-
else if(element instanceof Float){
141-
array[i][j]= (T)new Float(0);
142-
}
143-
else{
144-
array[i][j] = (T)new Integer(0);
145-
}
148+
array[i][j] = zero;
146149
}
147150
}
148-
149-
Matrix<T> identityMatrix = new Matrix<T>(this.rows, this.cols, array);
150-
151+
152+
final Matrix<T> identityMatrix = new Matrix<T>(this.rows, this.cols, array);
151153
for(int i = 0; i < this.rows;++i){
152-
if(element instanceof BigDecimal){
153-
identityMatrix.set(i, i, (T)BigDecimal.ONE);
154-
}
155-
else if(element instanceof BigInteger){
156-
identityMatrix.set(i,i,(T)BigInteger.ONE);
157-
}
158-
else if(element instanceof Long){
159-
identityMatrix.set(i, i, (T)new Long(1));
160-
}
161-
else if(element instanceof Double){
162-
identityMatrix.set(i, i, (T)new Double(1));
163-
}
164-
else if(element instanceof Float){
165-
identityMatrix.set(i, i, (T)new Float(1));
166-
}
167-
else{
168-
identityMatrix.set(i, i, (T)new Integer(1));
169-
}
154+
identityMatrix.set(i, i, one);
170155
}
171156
return identityMatrix;
172157
}

0 commit comments

Comments
 (0)