Question: Recall, operations over sparse matrices require methods that skip over the zero-elements. The enclosed class file provides a design where th e O(1) non-zero elements
Recall, operations over sparse matrices require methods that skip over the zero-elements. The enclosed class file provides a design where th e O(1) non-zero elements in each row of the matrix is stored in an unordered linked list.
Add the missing code for the methods in the class that allow the Main routine to operate correctly.(in Java)
public class SparseMatrix { public static final int MAX = 1000; public SparseMatrix(int size) { n=size; for (int i=1; i<=n; i++) rowStart[i] = null; } public int getSize() { return n; } public double getVal(int i, int j) { boolean found = false; MatrixElement current = rowStart[i]; while (current != null && !found) { if (current.col == j) found = true; else current = current.next; } if (found) return current.value; else return 0; } public int assign(int i, int j, double val) { if(i>n || j>n) return -1; else { boolean found = false; MatrixElement current = rowStart[i]; while (current != null && !found) { if (current.col == j) found = true; else current = current.next; } if (found) { current.value = val; return 0; } else { MatrixElement newElement = new MatrixElement(i,j,val); newElement.next = rowStart[i]; rowStart[i] = newElement; return 0; } } } public int increaseBy(int i, int j, double val) { // ADD CODE HERE } public SparseMatrix add(SparseMatrix other) { // ADD CODE HERE } public SparseMatrix mult(SparseMatrix other) { if (other.getSize() != n) return null; else { SparseMatrix result = new SparseMatrix(n); for(int i=1; i<=n; i++) { MatrixElement e1 = this.rowStart[i]; while (e1!=null) { MatrixElement e2 = other.rowStart[e1.col]; while (e2 != null) { result.increaseBy(i,e2.col, this.getVal(i,e1.col)*other.getVal(e2.row,e2.col)); e2 = e2.next; } e1 = e1.next; } } return result; } } public SparseMatrix copy() { SparseMatrix result = new SparseMatrix(n); for (int i=1; i<=n; i++) { MatrixElement e = rowStart[i]; while (e != null) { result.assign(i,e.col,e.value); e=e.next; } } return result; } public SparseMatrix transpose() { // ADD CODE HERE } public SparseMatrix negative() { // ADD CODE HERE } private int n; private MatrixElement[] rowStart = new MatrixElement[MAX]; } ================================
public class MatrixMain { public static void main(String[] args) { SparseMatrix M1 = new SparseMatrix(4); SparseMatrix M2 = new SparseMatrix(4); M1.assign(1,2,12); M1.assign(3,2,32); M1.assign(2,2,22); M1.assign(1,1,11); M1.assign(4,4,44); M1.assign(3,1,31); M1.assign(2,3,23); M1.assign(4,2,42); M2.assign(1,2,12); M2.assign(1,3,13); M2.assign(1,4,14); M2.assign(2,2,22); M2.assign(2,4,24); M2.assign(3,1,31); M2.assign(3,3,33); M2.assign(4,4,44); SparseMatrix M3 = null; for (int i=1; i<=4; i++) { System.out.println(); for (int j=1; j<=4; j++) System.out.print(M1.getVal(i, j) + " "); } System.out.println(); for (int i=1; i<=4; i++) { System.out.println(); for (int j=1; j<=4; j++) System.out.print(M2.getVal(i, j) + " "); } System.out.println(); M3 = M1.add(M2); for (int i=1; i<=4; i++) { System.out.println(); for (int j=1; j<=4; j++) System.out.print(M3.getVal(i, j) + " "); } System.out.println(); M3 = M1.mult(M2); for (int i=1; i<=4; i++) { System.out.println(); for (int j=1; j<=4; j++) System.out.print(M3.getVal(i, j) + " "); } System.out.println(); M3 = M1.transpose(); for (int i=1; i<=4; i++) { System.out.println(); for (int j=1; j<=4; j++) System.out.print(M3.getVal(i, j) + " "); } System.out.println(); M3 = M1.add(M2.negative()); for (int i=1; i<=4; i++) { System.out.println(); for (int j=1; j<=4; j++) System.out.print(M3.getVal(i, j) + " "); } } } ============================================================= public class MatrixElement { public MatrixElement(int r, int c, double val) { row = r; col = c; value = val; next = null; } public int row; public int col; public double value; public MatrixElement next; } ================
Here is the output
11.0 12.0 0.0 0.0 0.0 22.0 23.0 0.0 31.0 32.0 0.0 0.0 0.0 42.0 0.0 44.0
0.0 12.0 13.0 14.0 0.0 22.0 0.0 24.0 31.0 0.0 33.0 0.0 0.0 0.0 0.0 44.0
11.0 24.0 13.0 14.0 0.0 44.0 23.0 24.0 62.0 32.0 33.0 0.0 0.0 42.0 0.0 88.0
0.0 396.0 143.0 442.0 713.0 484.0 759.0 528.0 0.0 1076.0 403.0 1202.0 0.0 924.0 0.0 2944.0
11.0 0.0 31.0 0.0 12.0 22.0 32.0 42.0 0.0 23.0 0.0 0.0 0.0 0.0 0.0 44.0
11.0 0.0 -13.0 -14.0 0.0 0.0 23.0 -24.0 0.0 32.0 -33.0 0.0 0.0 42.0 0.0 0.0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
