You are to write a class called Matrix that implements a constructor, a method t
ID: 3665404 • Letter: Y
Question
You are to write a class called Matrix that implements a constructor, a method to returns an array with the dimensions of the stored matrix, a method that returns a reference to the internal array (Not an element-by-element copy of the array), a method that performs matrix multiplication, and one that performs scalar addition. For simplicity of this writeup, the code for the constructor is already provided for you.
Constructor
Provided. Do NOT change. You will also not need to add any instance variables to complete the assignment
return a one-dimensional array where index 0 contains the row dimension of the internal array mat and index 1 contains the column dimension of the internal array mat
return a reference to the internal array mat. Do not return an element-by-element copy.
If rhs is of compatible dimension, return a new Matrix object that contains the result of multiplying mat x rhs where multiplication is standard matrix multiplication. If mat is N x M, then rhs must M x Q (where Q is any positive integer). The result matrix in this case has dimension N x Q
Error cases:
if rhs is null, return a null Matrix object
if rhs is not of compatible dimension, return a null Matrix object
return a new Matrix object that is the result of mat + (scalar)*rhs. rhs is for "right hand side". For matrix addition, this is element-by-element addition. That is, the (i,j) element of the result of adding A + c*B is computed as A[i][j]+ c*B[i][j].
Error cases:
if rhs is null, return a reference to the Matrix object of the left-hand side of the addition
if rhs is not of compatible dimension, return a reference to the Matrix object of the left-hand side of the addition
Notes
Why do you think the assignment asks getMatrix() to return a reference to double[][] mat instead of an element-by-element copy?
You can use System.out.println() in your code to help you debug. You can print out the variables passed to your methods, for example.
Read the specifications carefully, When asked to create a new object, do exactly that. When asked to return a reference to the original, return the proper reference.
Explanation / Answer
Please find the required solution:
public class Matrix {
double[][] matrix;
public Matrix(double[][] matrix) {
this.matrix = matrix;
}
/* Returns dimensions as internal array */
int[] getDim() {
if (matrix == null)
return null;
int[] dim = new int[matrix.length];
for (int i = 0; i < matrix.length; i++) {
dim[i] = matrix[i].length;
}
return dim;
}
/* Returns all elements as 2-dimensional array */
public double[][] getMatrix() {
return matrix;
}
/* determine if the matrix is sparse */
public boolean isSparse(double[][] matrix) {
for (int i = 1; i < matrix.length; i++) {
if (matrix[i - 1].length != matrix[i].length) {
return true;
}
}
return true;
}
public Matrix multiply(Matrix rhs) {
if (rhs == null || rhs.matrix == null)
return null;
// incompatible dimension
if (isSparse(matrix) || isSparse(rhs.getMatrix()))
return null;
int m = matrix.length;
int n = matrix[0].length;
int p = rhs.getMatrix().length;
int q = rhs.getMatrix()[0].length;
// incompatible dimension
if (n != p) {
return null;
}
// perform multiplication
double sum = 0;
double[][] result = new double[m][q];
for (int c = 0; c < m; c++) {
for (int d = 0; d < q; d++) {
for (int k = 0; k < p; k++) {
sum = sum + matrix[c][k] * rhs.getMatrix()[k][d];
}
result[c][d] = sum;
sum = 0;
}
}
return new Matrix(result);
}
public Matrix add(Matrix rhs) {
int m, n, c, d;
m = matrix.length;
n = matrix[0].length;
double sum[][] = new double[m][n];
System.out.println("Sum of entered matrices:-");
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++)
sum[c][d] = matrix[c][d] + rhs.getMatrix()[c][d];
}
return new Matrix(sum);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.