mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Refactor files to be in correctly nested packages (#6120)
This commit is contained in:
103
src/main/java/com/thealgorithms/matrix/InverseOfMatrix.java
Normal file
103
src/main/java/com/thealgorithms/matrix/InverseOfMatrix.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
/**
|
||||
* This class provides methods to compute the inverse of a square matrix
|
||||
* using Gaussian elimination. For more details, refer to:
|
||||
* https://en.wikipedia.org/wiki/Invertible_matrix
|
||||
*/
|
||||
public final class InverseOfMatrix {
|
||||
private InverseOfMatrix() {
|
||||
}
|
||||
|
||||
public static double[][] invert(double[][] a) {
|
||||
int n = a.length;
|
||||
double[][] x = new double[n][n];
|
||||
double[][] b = new double[n][n];
|
||||
int[] index = new int[n];
|
||||
|
||||
// Initialize the identity matrix
|
||||
for (int i = 0; i < n; ++i) {
|
||||
b[i][i] = 1;
|
||||
}
|
||||
|
||||
// Perform Gaussian elimination
|
||||
gaussian(a, index);
|
||||
|
||||
// Update matrix b with the ratios stored during elimination
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
for (int j = i + 1; j < n; ++j) {
|
||||
for (int k = 0; k < n; ++k) {
|
||||
b[index[j]][k] -= a[index[j]][i] * b[index[i]][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Perform backward substitution to find the inverse
|
||||
for (int i = 0; i < n; ++i) {
|
||||
x[n - 1][i] = b[index[n - 1]][i] / a[index[n - 1]][n - 1];
|
||||
for (int j = n - 2; j >= 0; --j) {
|
||||
x[j][i] = b[index[j]][i];
|
||||
for (int k = j + 1; k < n; ++k) {
|
||||
x[j][i] -= a[index[j]][k] * x[k][i];
|
||||
}
|
||||
x[j][i] /= a[index[j]][j];
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
/**
|
||||
* Method to carry out the partial-pivoting Gaussian
|
||||
* elimination. Here index[] stores pivoting order.
|
||||
**/
|
||||
private static void gaussian(double[][] a, int[] index) {
|
||||
int n = index.length;
|
||||
double[] c = new double[n];
|
||||
|
||||
// Initialize the index array
|
||||
for (int i = 0; i < n; ++i) {
|
||||
index[i] = i;
|
||||
}
|
||||
|
||||
// Find the rescaling factors for each row
|
||||
for (int i = 0; i < n; ++i) {
|
||||
double c1 = 0;
|
||||
for (int j = 0; j < n; ++j) {
|
||||
double c0 = Math.abs(a[i][j]);
|
||||
if (c0 > c1) {
|
||||
c1 = c0;
|
||||
}
|
||||
}
|
||||
c[i] = c1;
|
||||
}
|
||||
|
||||
// Perform pivoting
|
||||
for (int j = 0; j < n - 1; ++j) {
|
||||
double pi1 = 0;
|
||||
int k = j;
|
||||
for (int i = j; i < n; ++i) {
|
||||
double pi0 = Math.abs(a[index[i]][j]) / c[index[i]];
|
||||
if (pi0 > pi1) {
|
||||
pi1 = pi0;
|
||||
k = i;
|
||||
}
|
||||
}
|
||||
|
||||
// Swap rows
|
||||
int temp = index[j];
|
||||
index[j] = index[k];
|
||||
index[k] = temp;
|
||||
|
||||
for (int i = j + 1; i < n; ++i) {
|
||||
double pj = a[index[i]][j] / a[index[j]][j];
|
||||
|
||||
// Record pivoting ratios below the diagonal
|
||||
a[index[i]][j] = pj;
|
||||
|
||||
// Modify other elements accordingly
|
||||
for (int l = j + 1; l < n; ++l) {
|
||||
a[index[i]][l] -= pj * a[index[j]][l];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
46
src/main/java/com/thealgorithms/matrix/MatrixTranspose.java
Normal file
46
src/main/java/com/thealgorithms/matrix/MatrixTranspose.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* <h1>Find the Transpose of Matrix!</h1>
|
||||
*
|
||||
* Simply take input from the user and print the matrix before the transpose and
|
||||
* after the transpose.
|
||||
*
|
||||
* <p>
|
||||
* <b>Note:</b> Giving proper comments in your program makes it more user
|
||||
* friendly and it is assumed as a high quality code.
|
||||
*
|
||||
* @author Rajat-Jain29
|
||||
* @version 11.0.9
|
||||
* @since 2014-03-31
|
||||
*/
|
||||
public final class MatrixTranspose {
|
||||
private MatrixTranspose() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the transpose of the given matrix.
|
||||
*
|
||||
* @param matrix The matrix to be transposed
|
||||
* @throws IllegalArgumentException if the matrix is empty
|
||||
* @throws NullPointerException if the matrix is null
|
||||
* @return The transposed matrix
|
||||
*/
|
||||
public static int[][] transpose(int[][] matrix) {
|
||||
if (matrix == null || matrix.length == 0) {
|
||||
throw new IllegalArgumentException("Matrix is empty");
|
||||
}
|
||||
|
||||
int rows = matrix.length;
|
||||
int cols = matrix[0].length;
|
||||
int[][] transposedMatrix = new int[cols][rows];
|
||||
for (int i = 0; i < cols; i++) {
|
||||
for (int j = 0; j < rows; j++) {
|
||||
transposedMatrix[i][j] = matrix[j][i];
|
||||
}
|
||||
}
|
||||
return transposedMatrix;
|
||||
}
|
||||
}
|
||||
32
src/main/java/com/thealgorithms/matrix/MedianOfMatrix.java
Normal file
32
src/main/java/com/thealgorithms/matrix/MedianOfMatrix.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Median of Matrix (https://medium.com/@vaibhav.yadav8101/median-in-a-row-wise-sorted-matrix-901737f3e116)
|
||||
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
|
||||
*/
|
||||
|
||||
public final class MedianOfMatrix {
|
||||
private MedianOfMatrix() {
|
||||
}
|
||||
|
||||
public static int median(Iterable<List<Integer>> matrix) {
|
||||
// Flatten the matrix into a 1D list
|
||||
List<Integer> linear = new ArrayList<>();
|
||||
for (List<Integer> row : matrix) {
|
||||
linear.addAll(row);
|
||||
}
|
||||
|
||||
// Sort the 1D list
|
||||
Collections.sort(linear);
|
||||
|
||||
// Calculate the middle index
|
||||
int mid = (0 + linear.size() - 1) / 2;
|
||||
|
||||
// Return the median
|
||||
return linear.get(mid);
|
||||
}
|
||||
}
|
||||
57
src/main/java/com/thealgorithms/matrix/MirrorOfMatrix.java
Normal file
57
src/main/java/com/thealgorithms/matrix/MirrorOfMatrix.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
// Problem Statement
|
||||
/*
|
||||
We have given an array of m x n (where m is the number of rows and n is the number of columns).
|
||||
Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
|
||||
|
||||
The Original matrix is: | The Mirror matrix is:
|
||||
1 2 3 | 3 2 1
|
||||
4 5 6 | 6 5 4
|
||||
7 8 9 | 9 8 7
|
||||
|
||||
@author - Aman (https://github.com/Aman28801)
|
||||
*/
|
||||
|
||||
public final class MirrorOfMatrix {
|
||||
private MirrorOfMatrix() {
|
||||
}
|
||||
|
||||
public static int[][] mirrorMatrix(final int[][] originalMatrix) {
|
||||
if (originalMatrix == null) {
|
||||
// Handle invalid input
|
||||
return null;
|
||||
}
|
||||
if (originalMatrix.length == 0) {
|
||||
return new int[0][0];
|
||||
}
|
||||
|
||||
checkInput(originalMatrix);
|
||||
|
||||
int numRows = originalMatrix.length;
|
||||
int numCols = originalMatrix[0].length;
|
||||
|
||||
int[][] mirroredMatrix = new int[numRows][numCols];
|
||||
|
||||
for (int i = 0; i < numRows; i++) {
|
||||
mirroredMatrix[i] = reverseRow(originalMatrix[i]);
|
||||
}
|
||||
return mirroredMatrix;
|
||||
}
|
||||
private static int[] reverseRow(final int[] inRow) {
|
||||
int[] res = new int[inRow.length];
|
||||
for (int i = 0; i < inRow.length; ++i) {
|
||||
res[i] = inRow[inRow.length - 1 - i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private static void checkInput(final int[][] matrix) {
|
||||
// Check if all rows have the same number of columns
|
||||
for (int i = 1; i < matrix.length; i++) {
|
||||
if (matrix[i].length != matrix[0].length) {
|
||||
throw new IllegalArgumentException("The input is not a matrix.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PrintAMatrixInSpiralOrder {
|
||||
/**
|
||||
* Search a key in row and column wise sorted matrix
|
||||
*
|
||||
* @param matrix matrix to be searched
|
||||
* @param row number of rows matrix has
|
||||
* @param col number of columns matrix has
|
||||
* @author Sadiul Hakim : https://github.com/sadiul-hakim
|
||||
*/
|
||||
|
||||
public List<Integer> print(int[][] matrix, int row, int col) {
|
||||
|
||||
// r traverses matrix row wise from first
|
||||
int r = 0;
|
||||
// c traverses matrix column wise from first
|
||||
int c = 0;
|
||||
int i;
|
||||
|
||||
List<Integer> result = new ArrayList<>();
|
||||
|
||||
while (r < row && c < col) {
|
||||
// print first row of matrix
|
||||
for (i = c; i < col; i++) {
|
||||
result.add(matrix[r][i]);
|
||||
}
|
||||
|
||||
// increase r by one because first row printed
|
||||
r++;
|
||||
|
||||
// print last column
|
||||
for (i = r; i < row; i++) {
|
||||
result.add(matrix[i][col - 1]);
|
||||
}
|
||||
|
||||
// decrease col by one because last column has been printed
|
||||
col--;
|
||||
|
||||
// print rows from last except printed elements
|
||||
if (r < row) {
|
||||
for (i = col - 1; i >= c; i--) {
|
||||
result.add(matrix[row - 1][i]);
|
||||
}
|
||||
|
||||
row--;
|
||||
}
|
||||
|
||||
// print columns from first except printed elements
|
||||
if (c < col) {
|
||||
for (i = row - 1; i >= r; i--) {
|
||||
result.add(matrix[i][c]);
|
||||
}
|
||||
c++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
import java.util.Scanner;
|
||||
/**
|
||||
* Given a matrix of size n x n We have to rotate this matrix by 90 Degree Here
|
||||
* is the algorithm for this problem .
|
||||
*/
|
||||
final class RotateMatrixBy90Degrees {
|
||||
private RotateMatrixBy90Degrees() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int t = sc.nextInt();
|
||||
|
||||
while (t-- > 0) {
|
||||
int n = sc.nextInt();
|
||||
int[][] arr = new int[n][n];
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = sc.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
Rotate.rotate(arr);
|
||||
printMatrix(arr);
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
|
||||
static void printMatrix(int[][] arr) {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
for (int j = 0; j < arr[0].length; j++) {
|
||||
System.out.print(arr[i][j] + " ");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class containing the algo to roate matrix by 90 degree
|
||||
*/
|
||||
final class Rotate {
|
||||
private Rotate() {
|
||||
}
|
||||
|
||||
static void rotate(int[][] a) {
|
||||
int n = a.length;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (i > j) {
|
||||
int temp = a[i][j];
|
||||
a[i][j] = a[j][i];
|
||||
a[j][i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
int i = 0;
|
||||
int k = n - 1;
|
||||
while (i < k) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
int temp = a[i][j];
|
||||
a[i][j] = a[k][j];
|
||||
a[k][j] = temp;
|
||||
}
|
||||
|
||||
i++;
|
||||
k--;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.thealgorithms.matrix.matrixexponentiation;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* @author Anirudh Buvanesh (https://github.com/anirudhb11) For more information
|
||||
* see https://www.geeksforgeeks.org/matrix-exponentiation/
|
||||
*
|
||||
*/
|
||||
public final class Fibonacci {
|
||||
private Fibonacci() {
|
||||
}
|
||||
|
||||
// Exponentiation matrix for Fibonacci sequence
|
||||
private static final int[][] FIB_MATRIX = {{1, 1}, {1, 0}};
|
||||
private static final int[][] IDENTITY_MATRIX = {{1, 0}, {0, 1}};
|
||||
// First 2 fibonacci numbers
|
||||
private static final int[][] BASE_FIB_NUMBERS = {{1}, {0}};
|
||||
|
||||
/**
|
||||
* Performs multiplication of 2 matrices
|
||||
*
|
||||
* @param matrix1
|
||||
* @param matrix2
|
||||
* @return The product of matrix1 and matrix2
|
||||
*/
|
||||
private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
|
||||
// Check if matrices passed can be multiplied
|
||||
int rowsInMatrix1 = matrix1.length;
|
||||
int columnsInMatrix1 = matrix1[0].length;
|
||||
|
||||
int rowsInMatrix2 = matrix2.length;
|
||||
int columnsInMatrix2 = matrix2[0].length;
|
||||
|
||||
assert columnsInMatrix1 == rowsInMatrix2;
|
||||
int[][] product = new int[rowsInMatrix1][columnsInMatrix2];
|
||||
for (int rowIndex = 0; rowIndex < rowsInMatrix1; rowIndex++) {
|
||||
for (int colIndex = 0; colIndex < columnsInMatrix2; colIndex++) {
|
||||
int matrixEntry = 0;
|
||||
for (int intermediateIndex = 0; intermediateIndex < columnsInMatrix1; intermediateIndex++) {
|
||||
matrixEntry += matrix1[rowIndex][intermediateIndex] * matrix2[intermediateIndex][colIndex];
|
||||
}
|
||||
product[rowIndex][colIndex] = matrixEntry;
|
||||
}
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the fibonacci number using matrix exponentiaition technique
|
||||
*
|
||||
* @param n The input n for which we have to determine the fibonacci number
|
||||
* Outputs the nth * fibonacci number
|
||||
* @return a 2 X 1 array as { {F_n+1}, {F_n} }
|
||||
*/
|
||||
public static int[][] fib(int n) {
|
||||
if (n == 0) {
|
||||
return IDENTITY_MATRIX;
|
||||
} else {
|
||||
int[][] cachedResult = fib(n / 2);
|
||||
int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult);
|
||||
if (n % 2 == 0) {
|
||||
return matrixExpResult;
|
||||
} else {
|
||||
return matrixMultiplication(FIB_MATRIX, matrixExpResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ]
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n = sc.nextInt();
|
||||
int[][] result = matrixMultiplication(fib(n), BASE_FIB_NUMBERS);
|
||||
System.out.println("Fib(" + n + ") = " + result[1][0]);
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user