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:
@@ -1,103 +0,0 @@
|
||||
package com.thealgorithms.misc;
|
||||
|
||||
/**
|
||||
* 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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.thealgorithms.misc;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* <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;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.thealgorithms.misc;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.thealgorithms.misc;
|
||||
|
||||
// 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user