Skip to content

Commit e2c259b

Browse files
committed
Merge branch 'master' of git://github.com/laucer/java-algorithms-implementation into laucer-master
2 parents 053654a + 5cd1858 commit e2c259b

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

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

+56-1
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,66 @@ 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){
120+
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){
127+
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+
}
146+
}
147+
}
148+
149+
Matrix<T> identityMatrix = new Matrix<T>(this.rows, this.cols, array);
150+
151+
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+
}
170+
}
171+
return identityMatrix;
172+
}
173+
118174
public Matrix<T> add(Matrix<T> input) {
119175
Matrix<T> output = new Matrix<T>(this.rows, this.cols);
120176
if ((this.cols != input.cols) || (this.rows != input.rows))
121177
return output;
122-
123178
for (int r = 0; r < output.rows; r++) {
124179
for (int c = 0; c < output.cols; c++) {
125180
for (int i = 0; i < cols; i++) {

test/com/jwetherell/algorithms/data_structures/test/MatrixTests.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.jwetherell.algorithms.data_structures.test;
22

33
import static org.junit.Assert.assertTrue;
4-
4+
import static org.junit.Assert.fail;
5+
import static org.junit.Assert.assertArrayEquals;
56
import org.junit.Test;
67

78
import com.jwetherell.algorithms.data_structures.Matrix;
@@ -86,4 +87,29 @@ public void testMatrix() {
8687
Matrix<Integer> matrix9 = matrix7.multiply(matrix8);
8788
assertTrue("Matrix multiplication error. matrix9="+matrix9+" result4"+result4, matrix9.equals(result4));
8889
}
90+
91+
@Test
92+
public void testIdentityMethod1() {
93+
Matrix<Integer> matrix = new Matrix<Integer>(2, 2);
94+
matrix.set(0, 0, 0);
95+
matrix.set(0, 1, 0);
96+
matrix.set(1, 0, 0);
97+
matrix.set(1, 1, 0);
98+
99+
Matrix<Integer> expectedResult = new Matrix<Integer>(2, 2);
100+
expectedResult.set(0, 0, 1);
101+
expectedResult.set(0, 1, 0);
102+
expectedResult.set(1, 0, 0);
103+
expectedResult.set(1, 1, 1);
104+
105+
try{
106+
matrix = matrix.identity();
107+
}
108+
catch(Exception ex){
109+
fail();
110+
}
111+
112+
assertArrayEquals(expectedResult.getRow(0), matrix.getRow(0));
113+
assertArrayEquals(expectedResult.getRow(1), matrix.getRow(1));
114+
}
89115
}

0 commit comments

Comments
 (0)