Pregunta

I am trying to determine the inverse of a matrix using the adjoint method i.e. (First calculate the cofactor of the matrix, then transpose this matrix and finally, multiply it by (1/determinant) for the inverse of the value of the determinant) I have methods that determine the transpose and determinant, however i am struggling with the cofactor matrix -

Link showing how the inverse is determined using the adjoint method by hand http://www.mathwords.com/i/inverse_of_a_matrix.htm Link showing how the cofactor is calculated by hand http://www.mathwords.com/c/cofactor_matrix.htm>

I have a method that calculates the determinant and the transpose fine, but I cant get the cofactor method to give me the output I require

A sample output is => 24 5 -4
24 5 -4
24 5 -4 but the second & third row should be different, any suggestions? Thanks! Here is the method that I am using => The checkIfSquare and assigningSign methods are working fine also

    public static int[][] cofactor(int[][] matrix) {
    int[][] cofactorMatrix = new int[matrix.length][matrix.length];

    for (int j = 0; j < matrix.length; j++) {
        if (checkIfSquare(matrix)) {
            for (int location = 0; location < matrix.length; location++) {
                int reducedMatrix[][] = new int[matrix.length - 1][matrix.length - 1];

                for (int rows = 1; rows < matrix.length; rows++) {
                    for (int cols = 0; cols < matrix.length; cols++) {
                        if (cols > location) {
                            reducedMatrix[rows - 1][cols - 1] = matrix[rows][cols];
                        } else if (cols < location) {
                            reducedMatrix[rows - 1][cols] = matrix[rows][cols];
                        }
                    }
                }

                int sign = assigningSign(location);
                cofactorMatrix[j][location] = determinantCalc(reducedMatrix) * sign;
            }
        }
    }
    return cofactorMatrix;
}
¿Fue útil?

Solución

When you calculate reducedMatrix you always skip over the first row. This works for the first row of the cofactor matrix, but for the rows below you need to skip over row j. This is why your output consists of the first row repeated.

By the way, this is a horrible way to calculate the inverse of a matrix. I assume this is an academic exercise, not meant to be used in serious programs.

Otros consejos

There is no need to re-invent the wheel. Jama is a well written package. Maven repository

    <dependency>
        <groupId>Jama</groupId>
        <artifactId>Jama</artifactId>
        <version>1.0.2</version>
    </dependency>

    <repository>    
        <id>ebi-repo</id>    
        <name>The EBI internal repository</name>    
        <url>http://www.ebi.ac.uk/~maven/m2repo</url>    
        <releases>      
            <enabled>true</enabled>    
        </releases>    
        <snapshots>      
            <enabled>false</enabled>    
        </snapshots>  
    </repository>        

Alternatively, the jar file is available at http://math.nist.gov/javanumerics/jama/#Package

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top