J
J
Joulence2020-11-24 17:43:28
Java
Joulence, 2020-11-24 17:43:28

How to find the determinant of a matrix (two-dimensional array) in Java?

During the writing of the laboratory work, I hit a dead end. There is a two-dimensional array, ala matrix and it is necessary to find its determinant. I know how to find the determinant itself, but I can't figure out how to implement it. By condition, third-party libraries cannot be used.

public class ThirdTask {
  public static void main(String[] args) {
    int[][] arrayMatrix = {{1,3,5,17,18,11,15,9,23,22}, {2,48,15,19,11,23,22,7,9,1},
        {1,2,3,10,22,11,33,8,18,13}, {2,48,15,19,11,23,22,7,9,1},
        {1,3,5,17,18,11,15,9,23,22}, {1,2,3,10,22,11,33,8,18,13},
        {2,48,15,19,11,23,22,7,9,1}, {3,1,19,27,5,11,20,17,12,8} };
    printArray(arrayMatrix);
  }
  
  public static void printArray(int[][] arrayMatrix) {
    for(int[] value: arrayMatrix) {
      for (int x = 0; x < value.length; x++) {
        System.out.print(value[x] + "\t");
        if (x == value.length - 1) {
          System.out.println();
        }
      }
      
      
    }
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2020-11-24
@galaxy

I join the previous speakers, but I want to warn you that the recursive algorithm for expanding in minors has complexity O (n!), And 10! = 3628800. In general, it can slow down if it is important :)
From the point of view of speed, it is better to bring the matrix to a triangular form using the Gaussian method, the determinant in this case is equal to the product of the diagonal elements. Complexity - O(n 3 ).

A
AVKor, 2020-11-24
@AVKor

The formula for expanding the determinant in a row (column) plus recursion.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question