About the three operations of matrices in the SSA algorithm: transpose, inversion, and multiplication

1. Matrix transposition

 // Matrix transpose (two-dimensional matrix)
    public static double[][] transposeTwo(double[][] matrix) {<!-- -->
        int rows = matrix.length;
        int cols = matrix[0].length;
        double[][] transposedMatrix = new double[cols][rows];

        for (int i = 0; i < rows; i + + ) {<!-- -->
            for (int j = 0; j < cols; j + + ) {<!-- -->
                transposedMatrix[j][i] = matrix[i][j];
            }
        }

        return transposedMatrix;
    }

Matrix transposition test:

package test;

public class transposeOne {<!-- -->

    //Matrix transpose (two-dimensional matrix)
    public static double[][] transposeTwo(double[][] matrix) {<!-- -->
        int rows = matrix.length;
        int cols = matrix[0].length;
        double[][] transposedMatrix = new double[cols][rows];

        for (int i = 0; i < rows; i + + ) {<!-- -->
            for (int j = 0; j < cols; j + + ) {<!-- -->
                transposedMatrix[j][i] = matrix[i][j];
            }
        }

        return transposedMatrix;
    }

    public static void printArray(double[][] array) {<!-- -->
        for (int i = 0; i < array.length; i + + ) {<!-- -->
            for (int j = 0; j < array[i].length; j + + ) {<!-- -->
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {<!-- -->
        //Define a 3x3 matrix
        double[][] matrixData1= {<!-- -->
                {<!-- -->1, 2, 3},
                {<!-- -->0, 1, 4},
                {<!-- -->5, 6, 0}
        };

        double[][] f=transposeTwo(matrixData1);
        printArray(f);

    }

}

2. Matrix inversion

 // Matrix inversion (using Gauss-Jordan elimination method)
    public static double[][] matrixInverse(double[][] matrix) {<!-- -->
        int n = matrix.length;
        double[][] result = new double[n][n];
        double[][] temp = new double[n][n];

        for (int i = 0; i < n; i + + ) {<!-- -->
            for (int j = 0; j < n; j + + ) {<!-- -->
                temp[i][j] = matrix[i][j];
            }
        }

        for (int i = 0; i < n; i + + ) {<!-- -->
            result[i][i] = 1;
        }

        for (int i = 0; i < n; i + + ) {<!-- -->
            double diagonalElement = temp[i][i];
            if (diagonalElement == 0) {<!-- -->
                throw new IllegalArgumentException("Matrix is not invertible");
            }

            for (int j = 0; j < n; j + + ) {<!-- -->
                temp[i][j] /= diagonalElement;
                result[i][j] /= diagonalElement;
            }

            for (int k = 0; k < n; k + + ) {<!-- -->
                if (k != i) {<!-- -->
                    double tempValue = temp[k][i];
                    for (int j = 0; j < n; j + + ) {<!-- -->
                        temp[k][j] -= tempValue * temp[i][j];
                        result[k][j] -= tempValue * result[i][j];
                    }
                }
            }
        }

        return result;
    }

Matrix inversion test

package test;

/**
 * Find the inverse matrix of a two-dimensional matrix
 */
public class test1 {<!-- -->

    public static double[][] invert(double[][] matrix) {<!-- -->
        int n = matrix.length;
        double[][] result = new double[n][n];
        double[][] temp = new double[n][n];

        for (int i = 0; i < n; i + + ) {<!-- -->
            for (int j = 0; j < n; j + + ) {<!-- -->
                temp[i][j] = matrix[i][j];
            }
        }

        for (int i = 0; i < n; i + + ) {<!-- -->
            result[i][i] = 1;
        }

        for (int i = 0; i < n; i + + ) {<!-- -->
            double diagonalElement = temp[i][i];
            if (diagonalElement == 0) {<!-- -->
                throw new IllegalArgumentException("Matrix is not invertible");
            }

            for (int j = 0; j < n; j + + ) {<!-- -->
                temp[i][j] /= diagonalElement;
                result[i][j] /= diagonalElement;
            }

            for (int k = 0; k < n; k + + ) {<!-- -->
                if (k != i) {<!-- -->
                    double tempValue = temp[k][i];
                    for (int j = 0; j < n; j + + ) {<!-- -->
                        temp[k][j] -= tempValue * temp[i][j];
                        result[k][j] -= tempValue * result[i][j];
                    }
                }
            }
        }

        return result;
    }

    public static void printArray(double[][] array) {<!-- -->
        for (int i = 0; i < array.length; i + + ) {<!-- -->
            for (int j = 0; j < array[i].length; j + + ) {<!-- -->
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {<!-- -->
        //Define a 3x3 matrix
        double[][] matrixData = {<!-- -->
                {<!-- -->1, 2, 3},
                {<!-- -->0, 1, 4},
                {<!-- -->5, 6, 0}
        };

        // Find the inverse of a matrix
        double[][] result = invert(matrixData);
        printArray(result);

    }
    
}


3. Matrix multiplication

 //Matrix multiplication
    public static double[][] matrixMultiply(double[][] matrix1, double[][] matrix2) {<!-- -->
        int rows1 = matrix1.length;
        int cols1 = matrix1[0].length;
        int rows2 = matrix2.length;
        int cols2 = matrix2[0].length;

        if (cols1 != rows2) {<!-- -->
            throw new IllegalArgumentException("The number of columns of matrix 1 must be equal to the number of rows of matrix 2");
        }

        double[][] result = new double[rows1][cols2];

        for (int i = 0; i < rows1; i + + ) {<!-- -->
            for (int j = 0; j < cols2; j + + ) {<!-- -->
                for (int k = 0; k < cols1; k + + ) {<!-- -->
                    result[i][j] + = matrix1[i][k] * matrix2[k][j];
                }
            }
        }

        return result;
    }

Matrix multiplication test:

package test;

public class test_matrixMultiply {<!-- -->
    //Matrix multiplication
    public static double[][] matrixMultiply(double[][] matrix1, double[][] matrix2) {<!-- -->
        int rows1 = matrix1.length;
        int cols1 = matrix1[0].length;
        
        int rows2 = matrix2.length;
        int cols2 = matrix2[0].length;

        if (cols1 != rows2) {<!-- -->
            throw new IllegalArgumentException("The number of columns of matrix 1 must be equal to the number of rows of matrix 2");
        }

        double[][] result = new double[rows1][cols2];

        for (int i = 0; i < rows1; i + + ) {<!-- -->
            for (int j = 0; j < cols2; j + + ) {<!-- -->
                for (int k = 0; k < cols1; k + + ) {<!-- -->
                    result[i][j] + = matrix1[i][k] * matrix2[k][j];
                }
            }
        }

        return result;
    }

    public static void printArray(double[][] array) {<!-- -->
        for (int i = 0; i < array.length; i + + ) {<!-- -->
            for (int j = 0; j < array[i].length; j + + ) {<!-- -->
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {<!-- -->
        //Define a 3x3 matrix
        double[][] matrixData1= {<!-- -->
                {<!-- -->1, 2, 3},
                {<!-- -->0, 1, 4},
                {<!-- -->5, 6, 0}
        };

        double[][] matrixData2 = {<!-- -->
                {<!-- -->1, 2, 3},
                {<!-- -->0, 1, 4},
                {<!-- -->5, 6, 0}
        };

        // Find the inverse of the matrix
        double[][] result = matrixMultiply(matrixData1,matrixData2);
        printArray(result);
    }
    
}