*/
package matrix;
/**
*
* Adeniran Adetunji
*/
public class Matrix {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
MatrixMath calMatrix = new MatrixMath();
double[][]matrix_A = calMatrix.createMatrix();
double[][]matrix_B = calMatrix.createMatrix();
calMatrix.enterMatrixElement(matrix_A);
calMatrix.enterMatrixElement(matrix_B);
double [][]matrix_C= calMatrix.addMatrix(matrix_A, matrix_B);
calMatrix.displayMatrix(matrix_A);
System.out.println(" ");
calMatrix.displayMatrix(matrix_B);
System.out.println(" ");
calMatrix.displayMatrix(matrix_C);
}
Output
run:
Enter Number of row(s)2
Enter Number of column(s)2
Enter Number of row(s)2
Enter Number of column(s)2
Enter element in row: 1 column :1==>3
Enter element in row: 1 column :2==>14
Enter element in row: 2 column :1==>2
Enter element in row: 2 column :2==>3
Enter element in row: 1 column :1==>56
Enter element in row: 1 column :2==>2
Enter element in row: 2 column :1==>3
Enter element in row: 2 column :2==>45
3.0 14.0
2.0 3.0
56.0 2.0
3.0 45.0
59.0 16.0
5.0 48.0
BUILD SUCCESSFUL (total time: 30 seconds)
Menu
Friday, 20 November 2015
MatrixMaths Class (Java Concept)
package matrix;
/**
*
* Adeniran Adetunji
*/
public class MatrixMath extends MatrixC {
double [][] addMatrix(double[][]augendMatrix, double[][]addendMatrix)
{
int x = augendMatrix.length;
int y = augendMatrix[0].length;
double [][]totalMatrix = new double[x][y];
if((augendMatrix.length==addendMatrix.length)
&&(augendMatrix[0].length==addendMatrix[0].length))
{
for(int i=0; i<augendMatrix.length; i++)
{
for (int k=0; k<augendMatrix[i].length; k++)
{
totalMatrix[i][k]=getElement(augendMatrix, i, k )+
getElement(addendMatrix, i, k);
}
}
}
else
{
System.out.println("Matrix a not of the same dimension");
System.out.println("Matrix Cannot be added");
}
return totalMatrix;
}
}
/**
*
* Adeniran Adetunji
*/
public class MatrixMath extends MatrixC {
double [][] addMatrix(double[][]augendMatrix, double[][]addendMatrix)
{
int x = augendMatrix.length;
int y = augendMatrix[0].length;
double [][]totalMatrix = new double[x][y];
if((augendMatrix.length==addendMatrix.length)
&&(augendMatrix[0].length==addendMatrix[0].length))
{
for(int i=0; i<augendMatrix.length; i++)
{
for (int k=0; k<augendMatrix[i].length; k++)
{
totalMatrix[i][k]=getElement(augendMatrix, i, k )+
getElement(addendMatrix, i, k);
}
}
}
else
{
System.out.println("Matrix a not of the same dimension");
System.out.println("Matrix Cannot be added");
}
return totalMatrix;
}
}
Matrix Class (Java Concept)
package matrix;
import java.util.Scanner;
/**
*
* Adeniran Adetunji
*/
public class MatrixC {
double [][]matrix;
//Create Matrix of Row = x and Column=y
MatrixC ()
{
matrix = new double[0][0];
}
double [][] createMatrix()
{
int x;
int y;
Scanner setMatrix = new Scanner (System.in);
System.out.print("Enter Number of row(s)");
x = setMatrix.nextInt();
System.out.print("Enter Number of column(s)");
y = setMatrix.nextInt();
matrix = new double[x][y];
initializeMatrix(matrix);
return matrix;
}
double[][]createMatrix(double [][]matrix_1)
{
matrix = matrix_1;
return matrix;
}
double[][] createColumnVector()
{
final int x= 1;
int y;
Scanner columnNo = new Scanner (System.in);
System.out.print("Enter Number of Column(s): ");
y = columnNo.nextInt();
matrix=new double [x][y];
initializeMatrix(matrix);
return matrix;
}
double[][]createColumnVector(double[]cMatrix)
{
int y = cMatrix.length;
matrix = new double [y][1];
for (int i=0; i<matrix.length; i++)
{
for (int k=0; k<matrix[i].length; k++)
{
matrix[i][k]=cMatrix[i];
}
}
return matrix;
}
double [][] createRowVector()
{
final int y= 1;
int x;
Scanner columnNo = new Scanner (System.in);
System.out.print("Enter Number of Row(s): ");
x = columnNo.nextInt();
matrix=new double [x][y];
initializeMatrix(matrix);
return matrix;
}
double [][]createRowVector(double[]rMatrix)
{
int y = rMatrix.length;
matrix = new double [1][y];
for (int i=0; i<matrix.length; i++)
{
for (int k=0; k<matrix[i].length; k++)
{
matrix[i][k]=rMatrix[k];
}
}
return matrix;
}
void initializeMatrix(double [][]matrix)
{
for (int i=0; i<matrix.length; i++)
{
for(int k=0; k<matrix[i].length; k++)
{
matrix[i][k]=0.0;
}
}
}
void displayMatrix(double[][]matrix1)
{
for(int i=0; i<matrix1.length; i++)
{
for(int k=0; k<matrix1[i].length; k++)
{
System.out.printf("%-10.1f", matrix1[i][k]);
}
System.out.println("");
}
}
//Initialize matrix of row i and column K with each element initialy equal to zero
void enterMatrixElement(double[][]matrix)
{
for(int i=0; i<matrix.length; i++)
{
for(int k = 0; k<matrix[i].length; k++)
{
Scanner input_matrix_element = new Scanner (System.in);
System.out.print("Enter element in "+ "row:"+" "+ (i+1)+" "+ "column :"+(k+1));
System.out.print("==>");
matrix[i][k]= input_matrix_element.nextInt();
}
}
}
boolean equalMatrix(double [][]matrix_1, double[][]matrix_2)
{
boolean test = false;
if((matrix_1.length==matrix_2.length)
&&(matrix_1[0].length==matrix_2[0].length))
{
for (int i = 0; i<matrix_1.length; i++)
{
for (int k = 0; k<matrix_1[i].length; i++)
{
if(matrix_1[i][k]==matrix_2[i][k])
{
test = true;
}
}
}
}
else
{
test = false;
}
return test;
}
void clearMatrix (double[][]matrix)
{
initializeMatrix(matrix);
}
double [][] cloneMatrix(double[][]matrix)
{
return matrix;
}
double getElement(double[][]matrix, int x, int y)
{
double element = matrix[x][y];
return element;
}
void setElement( double[][]matrix, int x, int y, double z)
{
matrix[x][y]=z;
}
double [][]getColumn(double[][]matrix, int col_index)
{
int num = matrix.length;
double [][] matrixR = new double[num][1];
for (int i=0; i<matrix.length; i++)
{
matrixR[i][0]= matrix[i][col_index];
}
return matrixR;
}
boolean IsVector (double[][]matrix_c)
{
boolean result = false;
if((matrix_c.length==1)||(matrix_c[0].length==1))
{
result = true;
}
return result;
}
boolean checkVectorEqualLength(double [][]matrix_A, double[][] matrix_B)
{
boolean result = false;
if((matrix_A.length==matrix_B.length)&&(matrix_A[0].length==matrix_B[0].length))
{
result=true;
}
else if ((matrix_A.length==matrix_B[0].length)&&(matrix_A[0].length==matrix_B.length))
{
result = true;
}
else
{
result=false;
}
return result;
}
boolean equalDimension(double[][]matrix_A, double[][]matrix_B)
{
boolean check = false;
if((matrix_A.length==matrix_B.length)&&(matrix_A[0].length==matrix_B[0].length))
{
check = true;
}
return check;
}
double dotProduct(double[][]matrix_A, double[][]matrix_B)
{
boolean check;
boolean check2;
double result=0;
check = (IsVector(matrix_A)&&IsVector(matrix_B));
check2 = ((check)&&(checkVectorEqualLength(matrix_A, matrix_B)));
if(check2)
{
if((matrix_A.length==matrix_B.length)&&(matrix_A[0].length==matrix_B[0].length))
{
for(int i=0; i<matrix_A.length; i++)
{
for(int k=0; k<matrix_A[i].length; k++)
{
result= result+(matrix_A[i][k]*matrix_B[i][k]);
}
}
}
if((matrix_A.length==matrix_B[0].length)&&(matrix_A[0].length==matrix_B.length))
{
if(matrix_A.length==1)
{
for (int i=0; i<matrix_A.length; i++)
{
for (int k=0; k<matrix_B.length; k++)
{
result = result+(matrix_A[i][k]*matrix_B[k][i]);
}
}
}
if(matrix_B.length==1)
for(int i=0; i<matrix_B.length; i++)
{
for(int k=0; k<matrix_A.length; k++)
{
result = result+(matrix_B[i][k]*matrix_A[k][i]);
}
}
}
}
else
{
System.out.println("Matrices are not compartible for dot product");
}
return result;
}
double [][] addMatrix (double[][] matrix_A, double [][]matrix_B)
{
double [][]result;
result = new double[matrix_A.length][matrix_A[0].length];
initializeMatrix(result);
if(equalDimension(matrix_A, matrix_B))
{
result = new double[matrix_A.length][matrix_A[0].length];
initializeMatrix(result);
for(int i =0; i<matrix_A.length; i++)
{
for(int k=0; k<matrix_A[i].length; k++)
{
result[i][k]=matrix_A[i][k]+matrix_B[i][k];
}
}
}
else
{
System.out.println("The matrices do not have the same dimesion");
}
return result;
}
}
import java.util.Scanner;
/**
*
* Adeniran Adetunji
*/
public class MatrixC {
double [][]matrix;
//Create Matrix of Row = x and Column=y
MatrixC ()
{
matrix = new double[0][0];
}
double [][] createMatrix()
{
int x;
int y;
Scanner setMatrix = new Scanner (System.in);
System.out.print("Enter Number of row(s)");
x = setMatrix.nextInt();
System.out.print("Enter Number of column(s)");
y = setMatrix.nextInt();
matrix = new double[x][y];
initializeMatrix(matrix);
return matrix;
}
double[][]createMatrix(double [][]matrix_1)
{
matrix = matrix_1;
return matrix;
}
double[][] createColumnVector()
{
final int x= 1;
int y;
Scanner columnNo = new Scanner (System.in);
System.out.print("Enter Number of Column(s): ");
y = columnNo.nextInt();
matrix=new double [x][y];
initializeMatrix(matrix);
return matrix;
}
double[][]createColumnVector(double[]cMatrix)
{
int y = cMatrix.length;
matrix = new double [y][1];
for (int i=0; i<matrix.length; i++)
{
for (int k=0; k<matrix[i].length; k++)
{
matrix[i][k]=cMatrix[i];
}
}
return matrix;
}
double [][] createRowVector()
{
final int y= 1;
int x;
Scanner columnNo = new Scanner (System.in);
System.out.print("Enter Number of Row(s): ");
x = columnNo.nextInt();
matrix=new double [x][y];
initializeMatrix(matrix);
return matrix;
}
double [][]createRowVector(double[]rMatrix)
{
int y = rMatrix.length;
matrix = new double [1][y];
for (int i=0; i<matrix.length; i++)
{
for (int k=0; k<matrix[i].length; k++)
{
matrix[i][k]=rMatrix[k];
}
}
return matrix;
}
void initializeMatrix(double [][]matrix)
{
for (int i=0; i<matrix.length; i++)
{
for(int k=0; k<matrix[i].length; k++)
{
matrix[i][k]=0.0;
}
}
}
void displayMatrix(double[][]matrix1)
{
for(int i=0; i<matrix1.length; i++)
{
for(int k=0; k<matrix1[i].length; k++)
{
System.out.printf("%-10.1f", matrix1[i][k]);
}
System.out.println("");
}
}
//Initialize matrix of row i and column K with each element initialy equal to zero
void enterMatrixElement(double[][]matrix)
{
for(int i=0; i<matrix.length; i++)
{
for(int k = 0; k<matrix[i].length; k++)
{
Scanner input_matrix_element = new Scanner (System.in);
System.out.print("Enter element in "+ "row:"+" "+ (i+1)+" "+ "column :"+(k+1));
System.out.print("==>");
matrix[i][k]= input_matrix_element.nextInt();
}
}
}
boolean equalMatrix(double [][]matrix_1, double[][]matrix_2)
{
boolean test = false;
if((matrix_1.length==matrix_2.length)
&&(matrix_1[0].length==matrix_2[0].length))
{
for (int i = 0; i<matrix_1.length; i++)
{
for (int k = 0; k<matrix_1[i].length; i++)
{
if(matrix_1[i][k]==matrix_2[i][k])
{
test = true;
}
}
}
}
else
{
test = false;
}
return test;
}
void clearMatrix (double[][]matrix)
{
initializeMatrix(matrix);
}
double [][] cloneMatrix(double[][]matrix)
{
return matrix;
}
double getElement(double[][]matrix, int x, int y)
{
double element = matrix[x][y];
return element;
}
void setElement( double[][]matrix, int x, int y, double z)
{
matrix[x][y]=z;
}
double [][]getColumn(double[][]matrix, int col_index)
{
int num = matrix.length;
double [][] matrixR = new double[num][1];
for (int i=0; i<matrix.length; i++)
{
matrixR[i][0]= matrix[i][col_index];
}
return matrixR;
}
boolean IsVector (double[][]matrix_c)
{
boolean result = false;
if((matrix_c.length==1)||(matrix_c[0].length==1))
{
result = true;
}
return result;
}
boolean checkVectorEqualLength(double [][]matrix_A, double[][] matrix_B)
{
boolean result = false;
if((matrix_A.length==matrix_B.length)&&(matrix_A[0].length==matrix_B[0].length))
{
result=true;
}
else if ((matrix_A.length==matrix_B[0].length)&&(matrix_A[0].length==matrix_B.length))
{
result = true;
}
else
{
result=false;
}
return result;
}
boolean equalDimension(double[][]matrix_A, double[][]matrix_B)
{
boolean check = false;
if((matrix_A.length==matrix_B.length)&&(matrix_A[0].length==matrix_B[0].length))
{
check = true;
}
return check;
}
double dotProduct(double[][]matrix_A, double[][]matrix_B)
{
boolean check;
boolean check2;
double result=0;
check = (IsVector(matrix_A)&&IsVector(matrix_B));
check2 = ((check)&&(checkVectorEqualLength(matrix_A, matrix_B)));
if(check2)
{
if((matrix_A.length==matrix_B.length)&&(matrix_A[0].length==matrix_B[0].length))
{
for(int i=0; i<matrix_A.length; i++)
{
for(int k=0; k<matrix_A[i].length; k++)
{
result= result+(matrix_A[i][k]*matrix_B[i][k]);
}
}
}
if((matrix_A.length==matrix_B[0].length)&&(matrix_A[0].length==matrix_B.length))
{
if(matrix_A.length==1)
{
for (int i=0; i<matrix_A.length; i++)
{
for (int k=0; k<matrix_B.length; k++)
{
result = result+(matrix_A[i][k]*matrix_B[k][i]);
}
}
}
if(matrix_B.length==1)
for(int i=0; i<matrix_B.length; i++)
{
for(int k=0; k<matrix_A.length; k++)
{
result = result+(matrix_B[i][k]*matrix_A[k][i]);
}
}
}
}
else
{
System.out.println("Matrices are not compartible for dot product");
}
return result;
}
double [][] addMatrix (double[][] matrix_A, double [][]matrix_B)
{
double [][]result;
result = new double[matrix_A.length][matrix_A[0].length];
initializeMatrix(result);
if(equalDimension(matrix_A, matrix_B))
{
result = new double[matrix_A.length][matrix_A[0].length];
initializeMatrix(result);
for(int i =0; i<matrix_A.length; i++)
{
for(int k=0; k<matrix_A[i].length; k++)
{
result[i][k]=matrix_A[i][k]+matrix_B[i][k];
}
}
}
else
{
System.out.println("The matrices do not have the same dimesion");
}
return result;
}
}
Subscribe to:
Posts (Atom)