In java Write a complete class named MatMath that will provide the following int
ID: 3825663 • Letter: I
Question
In java
Write a complete class named MatMath that will provide the following integer matrix operations: MatMath transpose(a) - returns the transpose of a where a is a matrix. (A^T)_i, j = A_j, I MatMath identity(n) - returns an identity matrix of size n, n > 1. MatMath.vectorProduct(a, x)-returns the vector product of matrix a and vector x, vector = 1D array. A m times n matrix, x n-vector, y m-vector y_i = A_i1 x_1 + ... + A_in x_n I = 1, ..., m MatMath diagonal(a)-returns the diagonal elements of an n times n matrix a as a vector. MatMath trace(a)-returns the sum of the diagonal elements of an n times n matrix. MatMath element(a)-returns all the elements column-wise of an n times m matrix a as a vector. MatMath print (a)-prints the matrix a with one row per line, nicely formatted. MatMath.print(v)- prints the vector v in one row, nicely formatted. MatMath equals(a, b)-compare matrix a with matrix b. Write a main method that will test out all the above operations.Explanation / Answer
ANSWER - i write this program in java according to the question requirements .
package my_chegg_package;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.Vector;
public class MatMath {
public static void main(String[] args) {
int m,n,c,d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int matrix1[][] = new int[m][n];
System.out.println("Enter the elements of matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
matrix1[c][d] = in.nextInt();
int transpose[][] = new int[n][m];
transpose =MatMath.transpose(matrix1,m,n);
System.out.println("Transpose of entered matrix:-");
for ( c = 0 ; c < n ; c++ )
{
for ( d = 0 ; d < m ; d++ )
System.out.print(transpose[c][d]+" ");
System.out.print(" ");
}
/* here is logic to get sum of the diagonal elements of a matrix*/
int sum=MatMath.trace(matrix1,m,n);
System.out.println("sum of the diagonal elements of a matrix= "+sum+"");
/* here the logic to get diagonal elements of a matrix as a vector */
Vector<Integer> v= MatMath.diagonal(matrix1, m, n);
Enumeration<Integer> vEnum = v.elements();
System.out.println(" Elements in vector:");
while(vEnum.hasMoreElements())
{
System.out.print(vEnum.nextElement() + " ");
}
System.out.println(" "); /* simple to print in new line */
/* here the logic to print the matrix with one row per line */
MatMath.print(matrix1,m, n);
/* here the logic start for identity metrix */
System.out.println("now to create an indentity matrix :- ");
System.out.println("Enter size of matrix : ");
int size=in.nextInt();
int matrix2[][]=Identity(size);
for (int i=0 ; i < matrix2.length ; i++)
{System.out.println();
for (int j=0 ; j < matrix2[i].length ; j++)
{
System.out.print(matrix2[i][j]+" ");
}
}
}
public static int[][] transpose(int[][] matrix,int m,int n)
{
int c,d;
int transpose[][] = new int[n][m];
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];
}
return transpose;
}
public static int[][] Identity(int size) {
int[][] matrix = new int[size][size];
for(int i = 0; i < size; i++)
for(int j = 0; j < size; j++)
matrix[i][j] = (i == j) ? 1 : 0;
return matrix;
}
public static int trace(int[][] matrix,int m,int n){
int sum = 0;
for(int x = 0; x <m; x++){
for(int y = 0; y < n; y++){
if(x == y){
sum = sum + matrix[x][y] ;
}
}
}
return sum;
}
public static Vector<Integer> diagonal(int[][] matrix,int m,int n){
int d=0;
Vector<Integer> v = new Vector<Integer>();
for(int x = 0; x <m; x++){
for(int y = 0; y < n; y++){
if(x == y){
d= matrix[x][y] ;
v.addElement(new Integer(d));
}
}
}
return v;
}
public static void print(int[][] matrix,int m,int n)
{
int c,d;
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
System.out.println(matrix[c][d] + "" );
}
System.out.println(" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.