James Lockhart
James Lockhart

Reputation: 141

Finding the cofactor and determinant of a 3x3 matrix

In the code listed below I am able to correctly find the sum, multiplication, and transpose of a two matrices. I am unsure how to find the cofactor and determinant going along the same type of set up I have for the other matrices. Any help would be appreciated. Thanks!!

public class MatrixMult {

public MatrixMult(int first[][], int second[][], int m, int n, int p, int q) {
    doMatrixMultiply(first, second, m, n, p, q);
}

public void doMatrixMultiply(int first[][], int second[][], int m, int n,
        int p, int q) {

    if (n != p)
        System.out
                .println("Matrices with entered orders can't be multiplied with each other.");
    else {
        int multiply[][] = new int[m][q];
        int addition[][] = new int[m][q];
        int transpose[][] = new int[m][q];
        int transpose2[][] = new int[m][q];
        int cofactor[][] = new int[m][q];

        int mult = 0;
        int sum = 0;
        int tran = 0;
        int co = 0;

        for (int c = 0; c < m; c++) {
            for (int d = 0; d < q; d++) {
                for (int k = 0; k < p; k++) {
                    mult = mult + first[c][k] * second[k][d];
                }

                multiply[c][d] = mult;
                mult = 0;
            }
        }

        System.out.println("Product of entered matrices:-");

        for (int c = 0; c < m; c++) {
            for (int d = 0; d < q; d++)
                System.out.print(multiply[c][d] + "\t");

            System.out.print("\n");
        }

        for (int c = 0; c < m; c++) {
            for (int d = 0; d < q; d++) {
                for (int k = 0; k < p; k++) {
                    sum = first[c][d] + second[c][d];
                }

                addition[c][d] = sum;
                sum = 0;
            }
        }

        System.out.println("Sum of entered matrices:-");

        for (int c = 0; c < m; c++) {
            for (int d = 0; d < q; d++)
                System.out.print(addition[c][d] + "\t");

            System.out.print("\n");
        }

        int c;
        int d;
        for (c = 0; c < m; c++) {
            for (d = 0; d < q; d++)
                transpose[d][c] = first[c][d];
        }
        for (c = 0; c < m; c++) {
            for (d = 0; d < q; d++)
                transpose2[d][c] = second[c][d];
        }

        System.out.println("Transpose of first entered matrix:-");

        for (c = 0; c < n; c++) {
            for (d = 0; d < m; d++)
                System.out.print(transpose[c][d] + "\t");

            System.out.print("\n");
        }

        System.out.println("Transpose of second entered matrix:-");

        for (c = 0; c < n; c++) {
            for (d = 0; d < m; d++)
                System.out.print(transpose2[c][d] + "\t");

            System.out.print("\n");
        }
    }
}
}

Upvotes: 1

Views: 3995

Answers (2)

Mathias Kogler
Mathias Kogler

Reputation: 140

Following is the implemenation of determinant using your structure for the matrix represantation using the code from the link Determining Cofactor Matrix in Java:

public int determinant(int[][] result, int rows, int cols) {
    if (rows == 2)
        return result[0][0] * result[1][1] - result[0][1] * result[1][0];

    int determinant1 = 0, determinant2 = 0;
    for (int i = 0; i < rows; i++) {
        int temp = 1, temp2 = 1;
        for (int j = 0; j < cols; j++) {
            temp *= result[(i + j) % cols][j];
            temp2 *= result[(i + j) % cols][rows - 1 - j];
        }

        determinant1 += temp;
        determinant2 += temp2;
    }

    return determinant1 - determinant2;
}

and for calculating the cofactor also using the code from the provided link:

public int[][] cofactor(int[][] matrix, int rows, int cols) {
    int[][] result = new int[rows][cols];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[i][j] = (int) (Math.pow(-1, i + j) * determinant(
                    removeRowCol(matrix, rows, cols, i, j), rows - 1,
                    cols - 1));
        }
    }

    return result;
}

public int[][] removeRowCol(int[][] matrix, int rows, int cols,
        int row, int col) {
    int[][] result = new int[rows - 1][cols - 1];

    int k = 0, l = 0;
    for (int i = 0; i < rows; i++) {
        if (i == row)
            continue;
        for (int j = 0; j < cols; j++) {
            if (j == col)
                continue;
            result[l][k] = matrix[i][j];

            k = (k + 1) % (rows - 1);
            if (k == 0)
                l++;
        }
    }

    return result;
}

Upvotes: 1

the
the

Reputation: 361

A simple Google search will find you a lot of examples. E.g. http://mrbool.com/how-to-use-java-for-performing-matrix-operations/26800

Upvotes: 0

Related Questions