Skip to content

Instantly share code, notes, and snippets.

@palaashatri
Last active June 12, 2023 05:58
Show Gist options
  • Save palaashatri/e8bdec8280e4e894dda04e8995e87556 to your computer and use it in GitHub Desktop.
Save palaashatri/e8bdec8280e4e894dda04e8995e87556 to your computer and use it in GitHub Desktop.
[Java] Operation on Matrices
import java.util.Scanner;
public class App {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws Exception {
// program for matrix operations
System.out.println("Enter the operation to be performed : ");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Determinant");
System.out.println("5. Transpose");
System.out.println("6. Inverse");
int choice = sc.nextInt();
switch (choice) {
case 1:
int[][] matrix1 = createMatrix();
int[][] matrix2 = createMatrix();
printMatrix(true, add(matrix1, matrix2));
break;
case 2:
int[][] matrix3 = createMatrix();
int[][] matrix4 = createMatrix();
printMatrix(true, subtract(matrix3, matrix4));
break;
case 3:
int[][] matrix5 = createMatrix();
int[][] matrix6 = createMatrix();
printMatrix(true, multiply(matrix5, matrix6));
break;
case 4:
System.out.println("Enter the matrix :");
int[][] matrix7 = createMatrix();
System.out.println("Determinant of the matrix is : " + determinant(matrix7));
break;
case 5:
System.out.println("Enter the matrix :");
int[][] matrix8 = createMatrix();
printMatrix(true, transpose(matrix8));
break;
case 6:
System.out.println("Enter the matrix :");
int[][] matrix9 = createMatrix();
printMatrix(true, inverse(matrix9));
break;
default:
System.out.println("Invalid choice");
System.exit(0);
}
}
/**
* Create a matrix
* space complexity : O(n^2)
* time complexity : O(n^2)
*
* @return matrix
*/
private static int[][] createMatrix() {
System.out.println("Enter the number of rows the matrix :");
int rows = sc.nextInt();
System.out.println("Enter the number of columns the matrix :");
int columns = sc.nextInt();
int[][] matrix = new int[rows][columns];
System.out.println("Enter the elements of the matrix :");
for (int row = 0; row < rows; row++) {
for (int column = 0; column < columns; column++) {
matrix[row][column] = sc.nextInt();
}
}
printMatrix(false, matrix);
return matrix;
}
/**
* Print the matrix
* space complexity : O(1)
* time complexity : O(n^2)
*
* @param isResult
* @param matrix
*/
private static void printMatrix(Boolean isResult, int[][] matrix) {
if (isResult) {
System.out.println("##### Result #####");
}
System.out.println("The matrix is : ");
for (int row = 0; row < matrix.length; row++) {
System.out.print("| ");
for (int column = 0; column < matrix[0].length; column++) {
System.out.print(matrix[row][column] + " ");
}
System.out.println("|");
}
}
/**
* Check if the order of the matrices are same
* space complexity : O(1)
* time complexity : O(1)
*
* @param matrix1
* @param matrix2
*/
private static void checkOrder(int[][] matrix1, int[][] matrix2) {
// check if the order of the matrices are same
if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length) {
System.out.println("The order of the matrices are not same");
System.exit(0);
}
}
/**
* Add two matrices
* space complexity : O(n^2)
* time complexity : O(n^2)
*
* @param matrix1
* @param matrix2
* @return sum of the two matrices
*/
private static int[][] add(int[][] matrix1, int[][] matrix2) {
checkOrder(matrix1, matrix2);
int[][] result = new int[matrix1.length][matrix1[0].length];
for (int row = 0; row < matrix1.length; row++) {
for (int column = 0; column < matrix1[0].length; column++) {
result[row][column] = matrix1[row][column] + matrix2[row][column];
}
}
return result;
}
/**
* Subtract two matrices
* space complexity : O(n^2)
* time complexity : O(n^2)
*
* @param matrix1
* @param matrix2
* @return difference of the two matrices
*/
private static int[][] subtract(int[][] matrix1, int[][] matrix2) {
checkOrder(matrix1, matrix2);
int[][] result = new int[matrix1.length][matrix1[0].length];
for (int row = 0; row < matrix1.length; row++) {
for (int column = 0; column < matrix1[0].length; column++) {
result[row][column] = matrix1[row][column] - matrix2[row][column];
}
}
return result;
}
/**
* Multiply two matrices
* space complexity : O(n^2)
* time complexity : O(n^3)
*
* @param matrix1
* @param matrix2
* @return product of the two matrices
*/
private static int[][] multiply(int[][] matrix1, int[][] matrix2) {
// matrix multiplication
if (matrix1[0].length != matrix2.length) {
System.out.println("The order of the matrices are not compatible for multiplication");
System.exit(0);
}
int[][] result = new int[matrix1.length][matrix2[0].length];
for (int row = 0; row < matrix1.length; row++) {
for (int column = 0; column < matrix2[0].length; column++) {
for (int k = 0; k < matrix1[0].length; k++) {
result[row][column] += matrix1[row][k] * matrix2[k][column];
}
}
}
return result;
}
/**
* Find the transpose of the matrix
* space complexity : O(n^2)
* time complexity : O(n^2)
*
* @param matrix
* @return transpose of the matrix
*/
private static int[][] transpose(int[][] matrix) {
// interchange rows with columns
int[][] result = new int[matrix[0].length][matrix.length];
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[0].length; column++) {
result[column][row] = matrix[row][column];
}
}
return result;
}
/**
* Find the determinant of the matrix
* space complexity : O(n^2)
* time complexity : O(n!)
*
* @param matrix
* @return determinant of the matrix
*/
private static int determinant(int[][] matrix) {
// find determinant of matrix
int determinant = 0;
if (matrix.length != matrix[0].length) {
System.out.println("The matrix is not a square matrix");
System.exit(0);
}
if (matrix.length == 1) {
determinant = (matrix[0][0]);
}
if (matrix.length == 2) {
determinant = ((matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]));
}
if (matrix.length > 2) {
for (int column = 0; column < matrix.length; column++) {
int[][] temp = new int[matrix.length - 1][matrix.length - 1];
for (int row = 1; row < matrix.length; row++) {
int k = 0;
for (int j = 0; j < matrix.length; j++) {
if (j == column) {
continue;
}
temp[row - 1][k] = matrix[row][j];
k++;
}
}
determinant += (Math.pow(-1, column) * matrix[0][column] * determinant(temp));
}
}
return determinant;
}
/**
* Find the inverse of the matrix
* space complexity : O(n^2)
* time complexity : O(n^4)
*
* @param matrix
* @return inverse of the matrix
*/
private static int[][] inverse(int[][] matrix) {
/**
* How to find inverse of a matrix?
* 1. Find the determinant of the matrix
* 2. Find the transpose of the matrix
* 3. Find the adjoint of the matrix
* 4. Find the inverse of the matrix
*/
// find determinant of matrix
int determinant = determinant(matrix);
if (determinant == 0) {
System.out.println("Inverse of the matrix does not exist");
System.exit(0);
}
// find transpose of matrix
int[][] transpose = transpose(matrix);
// find adjoint of matrix
int[][] adjoint = new int[transpose.length][transpose[0].length];
for (int row = 0; row < transpose.length; row++) {
for (int column = 0; column < transpose[0].length; column++) {
int[][] temp = new int[transpose.length - 1][transpose[0].length - 1];
int k = 0;
for (int i = 0; i < transpose.length; i++) {
if (i == row) {
continue;
}
int l = 0;
for (int j = 0; j < transpose[0].length; j++) {
if (j == column) {
continue;
}
temp[k][l] = transpose[i][j];
l++;
}
k++;
}
adjoint[row][column] = (int) (Math.pow(-1, row + column) * determinant(temp));
}
}
// find inverse of matrix
int[][] inverse = new int[adjoint.length][adjoint[0].length];
for (int row = 0; row < adjoint.length; row++) {
for (int column = 0; column < adjoint[0].length; column++) {
inverse[row][column] = adjoint[row][column] / determinant;
}
}
return inverse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment